Skip to content

Commit

Permalink
Merge pull request #3 from mdhayath64/steeringwheel2sbus
Browse files Browse the repository at this point in the history
clean code
  • Loading branch information
rotorman authored Dec 7, 2022
2 parents 73e8be9 + cdf6263 commit c3dc337
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 35 deletions.
6 changes: 3 additions & 3 deletions FTDI_SBUS/C#SteeringWheelDemo/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Window x:Name="wMainWindow" x:Class="SteeringWheelDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Steering wheel to S.BUS Demo" Height="375" Width="560" Background="#FFF0F0F0" ResizeMode="NoResize">
Title="Steering wheel to S.BUS Demo" Height="420" Width="560" Background="#FFF0F0F0" ResizeMode="NoResize">
<Grid x:Name="TitleBarMainWindowGrid">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="*"/>
<RowDefinition Height="120"/>
<RowDefinition Height="160"/>
</Grid.RowDefinitions>
<Rectangle Grid.Row="0" x:Name="orangeRectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="Black" Fill="#FFF6881E" />
<Grid x:Name="TitleBarGrid" Grid.Row="0">
Expand Down Expand Up @@ -36,7 +36,7 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Content="Error Window" FontWeight="Bold" Foreground="Red"/>
<ListBox Grid.Row="1" x:Name="OutputList" >
<ListBox Grid.Row="1" x:Name="OutputList" Foreground="Red">
</ListBox>
</Grid>

Expand Down
90 changes: 58 additions & 32 deletions FTDI_SBUS/C#SteeringWheelDemo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public partial class MainWindow : Window
private const UInt16 DEFAULTPOSITION = 1024; // 1.5 ms
private const UInt16 MINPOSITION = 144; // ca. 1 ms
private const UInt16 MAXPOSITION = 1904; // ca. 2 ms
private const UInt16 CENTERPOSITION = 1024;
private const UInt16 WHEELDIFFERENCE = (MAXPOSITION - MINPOSITION) / 2;
private const UInt16 PADELDIFFERENCE = MAXPOSITION - MINPOSITION;
private const int SBUS_BAUDRATE = 100000;
private const int SBUS_DATABITS = 8;
private const StopBits SBUS_STOPBITS = StopBits.Two;

private const UInt16 FILEHEADER = 0xaabb;
private const UInt16 FILEFOOTER = 0xccdd;

private int ErrCount = 1;

// Variable declarations
private SerialPort spUSB = new SerialPort(); // Communication over (virtual) serial port
Expand All @@ -41,7 +52,8 @@ public MainWindow()
{
InitializeComponent();
btConnectDisconnect.Content = sCONNECT; // Initialize the button content
spUSB.DataReceived += new SerialDataReceivedEventHandler(spUSB_DataReceived); // Register new event for receiving serial data
// Remove below commant if you want to receive the data and write it into the binary file.
//spUSB.DataReceived += new SerialDataReceivedEventHandler(spUSB_DataReceived); // Register new event for receiving serial data
AddDevices();
}

Expand All @@ -55,8 +67,8 @@ private void btConnectDisconnect_Click(object sender, RoutedEventArgs e)

// Set (virtual) serial port parameters
// S.BUS is 100.000 baud 8E2
spUSB.BaudRate = 100000;
spUSB.DataBits = 8; // default is 8
spUSB.BaudRate = SBUS_BAUDRATE;
spUSB.DataBits = SBUS_DATABITS; // default is 8
// spUSB.DiscardNull = false; // default is false
// spUSB.DtrEnable = false; // default is false
// spUSB.Handshake = Handshake.None; // default is None
Expand All @@ -67,7 +79,7 @@ private void btConnectDisconnect_Click(object sender, RoutedEventArgs e)
// spUSB.ReadTimeout = -1; // -1 = default = InfiniteTimeout
// spUSB.ReceivedBytesThreshold = 1; // Fire receive event when this amount of data is available, Default = 1
spUSB.RtsEnable = true;
spUSB.StopBits = StopBits.Two; // One is default
spUSB.StopBits = SBUS_STOPBITS; // One is default
//spUSB.WriteBufferSize = 2048; // Default 2048
//spUSB.WriteTimeout = -1; // -1 = default = InfiniteTimeout

Expand All @@ -87,7 +99,7 @@ private void btConnectDisconnect_Click(object sender, RoutedEventArgs e)
catch (Exception ex)
{
// In case of error, show message to user
MessageBox.Show("Could not open the communication port!" + "\n" + "Error: " + ex.Message);
OutputList.Items.Add(ErrCount++.ToString() + "Could not open the communication port!" + "\n" + "Error: " + ex.Message);
}
}
else
Expand All @@ -106,7 +118,7 @@ private void btConnectDisconnect_Click(object sender, RoutedEventArgs e)
catch (Exception ex)
{
// In case of error, show message to user
MessageBox.Show("Could not close the communication port!" + "\n" + "Error: " + ex.Message);
OutputList.Items.Add(ErrCount++.ToString() + "Could not close the communication port!" + "\n" + "Error: " + ex.Message);
}
slider_ch1.Value = DEFAULTPOSITION;
slider_ch2.Value = MINPOSITION;
Expand All @@ -123,30 +135,34 @@ private void spUSB_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int iDataLength = spUSB.BytesToRead; // The amount of bytes, serial port has received
byte[] baSerialPortDataIn = new byte[iDataLength]; // Create an internal array to store the data
if (iDataLength > 0) // Makes sense to continue only, if there really is new data available
try
{
spUSB.Read(baSerialPortDataIn, 0, iDataLength); // Copy the data from serial port to internal array

ushort[] fileHdr = new ushort[2];
fileHdr[0] = 0xaabb;
fileHdr[1] = 0xccdd;
using (var stream = new FileStream("test.bin", FileMode.Append, FileAccess.Write, FileShare.None))
using (var writer = new BinaryWriter(stream))
if (iDataLength > 0) // Makes sense to continue only, if there really is new data available
{
writer.Write(fileHdr[0]);
writer.Write(baSerialPortDataIn);
writer.Write(fileHdr[1]);
}
spUSB.Read(baSerialPortDataIn, 0, iDataLength); // Copy the data from serial port to internal array

// Discard the data
// Write the received data into binary file.
using (var stream = new FileStream("SerialReceivedData.bin", FileMode.Append, FileAccess.Write, FileShare.None))
using (var writer = new BinaryWriter(stream))
{
writer.Write(FILEHEADER);
writer.Write(baSerialPortDataIn);
writer.Write(FILEFOOTER);
}
}
}
catch (Exception ex)
{
// In case of error, show message to user
OutputList.Items.Add(ErrCount++.ToString() + "Could not receive the data from the serial port of could not write data into file!" + "\n" + "Error: " + ex.Message);
}
}

public static byte[] SbusWriteChannels(ushort[] ch_)
{
byte[] buf_ = new byte[25];
buf_[0] = bHEADER;
buf_[1] = (byte)((ch_[0] & 0x07FF));
buf_[1] = (byte)(ch_[0] & 0x07FF);
buf_[2] = (byte)((ch_[0] & 0x07FF) >> 8 | (ch_[1] & 0x07FF) << 3);
buf_[3] = (byte)((ch_[1] & 0x07FF) >> 5 | (ch_[2] & 0x07FF) << 6);
buf_[4] = (byte)((ch_[2] & 0x07FF) >> 2);
Expand All @@ -157,7 +173,7 @@ public static byte[] SbusWriteChannels(ushort[] ch_)
buf_[9] = (byte)((ch_[5] & 0x07FF) >> 9 | (ch_[6] & 0x07FF) << 2);
buf_[10] = (byte)((ch_[6] & 0x07FF) >> 6 | (ch_[7] & 0x07FF) << 5);
buf_[11] = (byte)((ch_[7] & 0x07FF) >> 3);
buf_[12] = (byte)((ch_[8] & 0x07FF));
buf_[12] = (byte)(ch_[8] & 0x07FF);
buf_[13] = (byte)((ch_[8] & 0x07FF) >> 8 | (ch_[9] & 0x07FF) << 3);
buf_[14] = (byte)((ch_[9] & 0x07FF) >> 5 | (ch_[10] & 0x07FF) << 6);
buf_[15] = (byte)((ch_[10] & 0x07FF) >> 2);
Expand Down Expand Up @@ -222,7 +238,7 @@ private void dtSBUSTimer_Tick(object sender, EventArgs e)
catch (Exception ex)
{
// In case of error, show message to user
MessageBox.Show("Could not write data out the communication port!" + "\n" + "Error: " + ex.Message);
OutputList.Items.Add(ErrCount++.ToString() + ". " + "Could not write data out the communication port!" + "\n" + "Error: " + ex.Message);
}
}

Expand Down Expand Up @@ -272,14 +288,15 @@ private void DisplayRadio()
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
OutputList.Items.Add(ErrCount++.ToString() + ". " + ex.Message);
}
}

private void DisplayWheel()
{
try
{
checkbWheel.IsEnabled = false;
controllers = RawGameController.RawGameControllers;
if (controllers.Any())
{
Expand All @@ -291,12 +308,13 @@ private void DisplayWheel()

if (cbWheel.Items.Count > 0)
{
checkbWheel.IsEnabled = true;
cbWheel.Text = cbWheel.Items[0].ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
OutputList.Items.Add(ErrCount++.ToString() + ". " + ex.Message);
}
}

Expand Down Expand Up @@ -328,16 +346,24 @@ private void checkbWheel_Unchecked(object sender, RoutedEventArgs e)

private void dtWheelTimer_Tick(object sender, EventArgs e)
{
RacingWheel racingWheel = RacingWheel.FromGameController(RawGameController.RawGameControllers[cbWheel.SelectedIndex]);
if (racingWheel != null)
try
{
RacingWheelReading wheel = racingWheel.GetCurrentReading();
RacingWheel racingWheel = RacingWheel.FromGameController(RawGameController.RawGameControllers[cbWheel.SelectedIndex]);
if (racingWheel != null)
{
RacingWheelReading wheel = racingWheel.GetCurrentReading();

// Assuming the steering is between -1 and +1, then the following converts it to 144..1904 for S.BUS
slider_ch1.Value = (UInt16)(DEFAULTPOSITION + ((MAXPOSITION - MINPOSITION)/2) * wheel.Wheel);
// Assuming brake and throttle are between 0 and +1, then the following converts it to 144..1904 for S.BUS
slider_ch2.Value = (UInt16)(MINPOSITION + (MAXPOSITION - MINPOSITION) * wheel.Brake);
slider_ch3.Value = (UInt16)(MINPOSITION + (MAXPOSITION - MINPOSITION) * wheel.Throttle);
// Assuming input wheel value is between -1 and +1, then the following converts it to 144..1904 for S.BUS
slider_ch1.Value = (UInt16)(CENTERPOSITION + WHEELDIFFERENCE * wheel.Wheel);
// Assuming input break and throttle value is between 0 and +1, then the following converts it to 144..1904 for S.BUS
slider_ch2.Value = (UInt16)(MINPOSITION + PADELDIFFERENCE * wheel.Brake);
slider_ch3.Value = (UInt16)(MINPOSITION + PADELDIFFERENCE * wheel.Throttle);
}
}
catch (Exception ex)
{
dtWheelTimer.Stop();
OutputList.Items.Add(ErrCount++.ToString() + ". " + "There is a problem with connecting with steering wheel because of the following reason - " + ex.Message);
}
}
}
Expand Down
Binary file removed FTDI_SBUS/C#SteeringWheelDemo/SteeringWheelDemo.exe
Binary file not shown.

0 comments on commit c3dc337

Please sign in to comment.