using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using RS232;
using System.Threading;
namespace Styrning
{
public class FormMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer TimerKeyboard;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Label LabelLeft;
private System.Windows.Forms.Label LabelRight;
private System.Windows.Forms.Label LabelBack;
private System.Windows.Forms.Label LabelForward;
private System.Windows.Forms.Timer TimerSteering;
private System.Windows.Forms.GroupBox groupBox6;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TrackBar TBAcceleration;
private System.Windows.Forms.TrackBar TBDeceleration;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Label LabelTurning;
private System.Windows.Forms.Label LabelDirection;
private System.Windows.Forms.Label LabelThrottle;
private System.Windows.Forms.ProgressBar PBThrottle;
private System.Windows.Forms.GroupBox groupBox5;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label LabelBinaryData;
private System.Windows.Forms.CheckBox CheckLockAcc;
private System.Windows.Forms.Label LabelPacketRate;
private System.Windows.Forms.CheckBox CheckLights;
private System.Windows.Forms.ProgressBar PBDataRate;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label LabelStatus;
private System.Windows.Forms.ComboBox ComboPort;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.ComboBox ComboBaud;
private System.Windows.Forms.Button ButtonToggleEnable;
private System.Windows.Forms.Label LabelBaseLabel;
private System.ComponentModel.IContainer components;
// Hidden windows generated code
#region Hidden generated code
public FormMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
rs232 = new Rs232();
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.TimerKeyboard = new System.Windows.Forms.Timer(this.components);
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.LabelLeft = new System.Windows.Forms.Label();
this.LabelRight = new System.Windows.Forms.Label();
this.LabelBack = new System.Windows.Forms.Label();
this.LabelForward = new System.Windows.Forms.Label();
this.TimerSteering = new System.Windows.Forms.Timer(this.components);
this.groupBox6 = new System.Windows.Forms.GroupBox();
this.CheckLockAcc = new System.Windows.Forms.CheckBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.TBAcceleration = new System.Windows.Forms.TrackBar();
this.TBDeceleration = new System.Windows.Forms.TrackBar();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.CheckLights = new System.Windows.Forms.CheckBox();
this.LabelTurning = new System.Windows.Forms.Label();
this.LabelDirection = new System.Windows.Forms.Label();
this.LabelThrottle = new System.Windows.Forms.Label();
this.PBThrottle = new System.Windows.Forms.ProgressBar();
this.groupBox5 = new System.Windows.Forms.GroupBox();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.PBDataRate = new System.Windows.Forms.ProgressBar();
this.LabelPacketRate = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.LabelBinaryData = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.ButtonToggleEnable = new System.Windows.Forms.Button();
this.ComboBaud = new System.Windows.Forms.ComboBox();
this.label8 = new System.Windows.Forms.Label();
this.LabelBaseLabel = new System.Windows.Forms.Label();
this.ComboPort = new System.Windows.Forms.ComboBox();
this.LabelStatus = new System.Windows.Forms.Label();
this.groupBox3.SuspendLayout();
this.groupBox6.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.TBAcceleration)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.TBDeceleration)).BeginInit();
this.groupBox4.SuspendLayout();
this.groupBox5.SuspendLayout();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// TimerKeyboard
//
this.TimerKeyboard.Enabled = true;
this.TimerKeyboard.Interval = 50;
this.TimerKeyboard.Tick += new System.EventHandler(this.TimerKeyboard_Tick);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.LabelLeft);
this.groupBox3.Controls.Add(this.LabelRight);
this.groupBox3.Controls.Add(this.LabelBack);
this.groupBox3.Controls.Add(this.LabelForward);
this.groupBox3.Location = new System.Drawing.Point(16, 336);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(168, 64);
this.groupBox3.TabIndex = 9;
this.groupBox3.TabStop = false;
//
// LabelLeft
//
this.LabelLeft.Location = new System.Drawing.Point(16, 40);
this.LabelLeft.Name = "LabelLeft";
this.LabelLeft.Size = new System.Drawing.Size(40, 16);
this.LabelLeft.TabIndex = 7;
this.LabelLeft.Text = "Left";
//
// LabelRight
//
this.LabelRight.Location = new System.Drawing.Point(112, 40);
this.LabelRight.Name = "LabelRight";
this.LabelRight.Size = new System.Drawing.Size(40, 16);
this.LabelRight.TabIndex = 6;
this.LabelRight.Text = "Right";
//
// LabelBack
//
this.LabelBack.Location = new System.Drawing.Point(64, 40);
this.LabelBack.Name = "LabelBack";
this.LabelBack.Size = new System.Drawing.Size(40, 16);
this.LabelBack.TabIndex = 5;
this.LabelBack.Text = "Back";
//
// LabelForward
//
this.LabelForward.Location = new System.Drawing.Point(56, 16);
this.LabelForward.Name = "LabelForward";
this.LabelForward.Size = new System.Drawing.Size(56, 16);
this.LabelForward.TabIndex = 4;
this.LabelForward.Text = "Forward";
//
// TimerSteering
//
this.TimerSteering.Enabled = true;
this.TimerSteering.Tick += new System.EventHandler(this.TimerSteering_Tick);
//
// groupBox6
//
this.groupBox6.Controls.Add(this.CheckLockAcc);
this.groupBox6.Controls.Add(this.label3);
this.groupBox6.Controls.Add(this.label2);
this.groupBox6.Controls.Add(this.TBAcceleration);
this.groupBox6.Controls.Add(this.TBDeceleration);
this.groupBox6.Location = new System.Drawing.Point(16, 144);
this.groupBox6.Name = "groupBox6";
this.groupBox6.Size = new System.Drawing.Size(168, 184);
this.groupBox6.TabIndex = 19;
this.groupBox6.TabStop = false;
this.groupBox6.Text = "Steering settings";
//
// CheckLockAcc
//
this.CheckLockAcc.Checked = true;
this.CheckLockAcc.CheckState = System.Windows.Forms.CheckState.Checked;
this.CheckLockAcc.Location = new System.Drawing.Point(40, 24);
this.CheckLockAcc.Name = "CheckLockAcc";
this.CheckLockAcc.Size = new System.Drawing.Size(96, 16);
this.CheckLockAcc.TabIndex = 27;
this.CheckLockAcc.Text = "Lock controls";
//
// label3
//
this.label3.Location = new System.Drawing.Point(88, 48);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 16);
this.label3.TabIndex = 26;
this.label3.Text = "Acceleration";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 25;
this.label2.Text = "Deceleration";
//
// TBAcceleration
//
this.TBAcceleration.Location = new System.Drawing.Point(104, 72);
this.TBAcceleration.Maximum = 20;
this.TBAcceleration.Minimum = 1;
this.TBAcceleration.Name = "TBAcceleration";
this.TBAcceleration.Orientation = System.Windows.Forms.Orientation.Vertical;
this.TBAcceleration.Size = new System.Drawing.Size(42, 104);
this.TBAcceleration.TabIndex = 24;
this.TBAcceleration.TickFrequency = 2;
this.TBAcceleration.Value = 1;
//
// TBDeceleration
//
this.TBDeceleration.Location = new System.Drawing.Point(24, 72);
this.TBDeceleration.Maximum = 20;
this.TBDeceleration.Name = "TBDeceleration";
this.TBDeceleration.Orientation = System.Windows.Forms.Orientation.Vertical;
this.TBDeceleration.Size = new System.Drawing.Size(42, 104);
this.TBDeceleration.TabIndex = 23;
this.TBDeceleration.TickFrequency = 2;
//
// groupBox4
//
this.groupBox4.Controls.Add(this.CheckLights);
this.groupBox4.Controls.Add(this.LabelTurning);
this.groupBox4.Controls.Add(this.LabelDirection);
this.groupBox4.Controls.Add(this.LabelThrottle);
this.groupBox4.Controls.Add(this.PBThrottle);
this.groupBox4.Location = new System.Drawing.Point(192, 144);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(280, 128);
this.groupBox4.TabIndex = 20;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "Steering output";
//
// CheckLights
//
this.CheckLights.Location = new System.Drawing.Point(72, 104);
this.CheckLights.Name = "CheckLights";
this.CheckLights.Size = new System.Drawing.Size(112, 16);
this.CheckLights.TabIndex = 25;
this.CheckLights.Text = "Car Lights active";
//
// LabelTurning
//
this.LabelTurning.Location = new System.Drawing.Point(56, 80);
this.LabelTurning.Name = "LabelTurning";
this.LabelTurning.Size = new System.Drawing.Size(160, 16);
this.LabelTurning.TabIndex = 22;
this.LabelTurning.Text = "Turning: No";
//
// LabelDirection
//
this.LabelDirection.Location = new System.Drawing.Point(56, 64);
this.LabelDirection.Name = "LabelDirection";
this.LabelDirection.Size = new System.Drawing.Size(160, 16);
this.LabelDirection.TabIndex = 21;
this.LabelDirection.Text = "Direction: None";
//
// LabelThrottle
//
this.LabelThrottle.Location = new System.Drawing.Point(56, 24);
this.LabelThrottle.Name = "LabelThrottle";
this.LabelThrottle.Size = new System.Drawing.Size(160, 16);
this.LabelThrottle.TabIndex = 20;
this.LabelThrottle.Text = "Throttle: 0%";
//
// PBThrottle
//
this.PBThrottle.Location = new System.Drawing.Point(56, 40);
this.PBThrottle.Name = "PBThrottle";
this.PBThrottle.Size = new System.Drawing.Size(160, 16);
this.PBThrottle.TabIndex = 19;
//
// groupBox5
//
this.groupBox5.Controls.Add(this.label6);
this.groupBox5.Controls.Add(this.label5);
this.groupBox5.Controls.Add(this.label4);
this.groupBox5.Controls.Add(this.PBDataRate);
this.groupBox5.Controls.Add(this.LabelPacketRate);
this.groupBox5.Controls.Add(this.label1);
this.groupBox5.Controls.Add(this.LabelBinaryData);
this.groupBox5.Location = new System.Drawing.Point(192, 280);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(280, 120);
this.groupBox5.TabIndex = 22;
this.groupBox5.TabStop = false;
this.groupBox5.Text = "Data flow";
//
// label6
//
this.label6.Location = new System.Drawing.Point(232, 24);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(40, 16);
this.label6.TabIndex = 29;
this.label6.Text = "200%";
//
// label5
//
this.label5.Location = new System.Drawing.Point(120, 24);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(40, 16);
this.label5.TabIndex = 28;
this.label5.Text = "100%";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 24);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(24, 16);
this.label4.TabIndex = 27;
this.label4.Text = "0%";
//
// PBDataRate
//
this.PBDataRate.Location = new System.Drawing.Point(16, 40);
this.PBDataRate.Name = "PBDataRate";
this.PBDataRate.Size = new System.Drawing.Size(248, 16);
this.PBDataRate.TabIndex = 26;
//
// LabelPacketRate
//
this.LabelPacketRate.Location = new System.Drawing.Point(48, 56);
this.LabelPacketRate.Name = "LabelPacketRate";
this.LabelPacketRate.Size = new System.Drawing.Size(144, 16);
this.LabelPacketRate.TabIndex = 25;
this.LabelPacketRate.Text = "Data rate: 0 packets / s";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 80);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 16);
this.label1.TabIndex = 23;
this.label1.Text = "Binary control data:";
//
// LabelBinaryData
//
this.LabelBinaryData.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.LabelBinaryData.Location = new System.Drawing.Point(8, 96);
this.LabelBinaryData.Name = "LabelBinaryData";
this.LabelBinaryData.Size = new System.Drawing.Size(264, 16);
this.LabelBinaryData.TabIndex = 22;
this.LabelBinaryData.Text = "Wait...";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.ButtonToggleEnable);
this.groupBox1.Controls.Add(this.ComboBaud);
this.groupBox1.Controls.Add(this.label8);
this.groupBox1.Controls.Add(this.LabelBaseLabel);
this.groupBox1.Controls.Add(this.ComboPort);
this.groupBox1.Controls.Add(this.LabelStatus);
this.groupBox1.Location = new System.Drawing.Point(16, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(456, 128);
this.groupBox1.TabIndex = 23;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Serial port communication";
//
// ButtonToggleEnable
//
this.ButtonToggleEnable.Location = new System.Drawing.Point(352, 96);
this.ButtonToggleEnable.Name = "ButtonToggleEnable";
this.ButtonToggleEnable.Size = new System.Drawing.Size(96, 24);
this.ButtonToggleEnable.TabIndex = 14;
this.ButtonToggleEnable.Text = "Enable port";
this.ButtonToggleEnable.Click += new System.EventHandler(this.ButtonToggleEnable_Click);
//
// ComboBaud
//
this.ComboBaud.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ComboBaud.Items.AddRange(new object[] {
"600",
"1200",
"2400",
"3600",
"4800",
"6000",
"7200",
"8400",
"9600",
"14400",
"19200",
"28800",
"76800"});
this.ComboBaud.Location = new System.Drawing.Point(208, 64);
this.ComboBaud.Name = "ComboBaud";
this.ComboBaud.Size = new System.Drawing.Size(88, 21);
this.ComboBaud.TabIndex = 13;
//
// label8
//
this.label8.Location = new System.Drawing.Point(144, 64);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(64, 16);
this.label8.TabIndex = 12;
this.label8.Text = "Baud rate";
//
// LabelBaseLabel
//
this.LabelBaseLabel.Location = new System.Drawing.Point(144, 32);
this.LabelBaseLabel.Name = "LabelBaseLabel";
this.LabelBaseLabel.Size = new System.Drawing.Size(56, 16);
this.LabelBaseLabel.TabIndex = 11;
this.LabelBaseLabel.Text = "COM port";
//
// ComboPort
//
this.ComboPort.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ComboPort.Items.AddRange(new object[] {
"COM1",
"COM2"});
this.ComboPort.Location = new System.Drawing.Point(208, 32);
this.ComboPort.Name = "ComboPort";
this.ComboPort.Size = new System.Drawing.Size(88, 21);
this.ComboPort.TabIndex = 10;
//
// LabelStatus
//
this.LabelStatus.Location = new System.Drawing.Point(144, 96);
this.LabelStatus.Name = "LabelStatus";
this.LabelStatus.Size = new System.Drawing.Size(96, 16);
this.LabelStatus.TabIndex = 9;
this.LabelStatus.Text = "Port active: No";
//
// FormMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(482, 423);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox5);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox6);
this.Controls.Add(this.groupBox3);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "FormMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Styrning till radiostyrd bil";
this.Closing += new System.ComponentModel.CancelEventHandler(this.FormMain_Closing);
this.Load += new System.EventHandler(this.FormMain_Load);
this.groupBox3.ResumeLayout(false);
this.groupBox6.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.TBAcceleration)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.TBDeceleration)).EndInit();
this.groupBox4.ResumeLayout(false);
this.groupBox5.ResumeLayout(false);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
// Application startup point
[STAThread]
static void Main()
{
Application.Run(new FormMain());
}
#endregion
#endregion
// DLL imports
[System.Runtime.InteropServices.DllImport("user32.dll")]
extern static short GetKeyState(int vKey);
// Private variables
bool twoDelays = false;
string sendData = "0";
Settings settings;
byte[] packet = new byte[4];
private Rs232 rs232;
private Thread thrPacketSenderThread;
private int timerPacketTics = 0;
private int packetsSent = 0;
private double throttle = 0;
private int directionCar = 0;
private int throttleStep = 0;
private bool activateLights = false;
private bool activateBrakeLights = false;
private bool keyBackPressed = false;
private bool keyLeftPressed = false;
private bool keyRightPressed = false;
private bool keyForwardPressed = false;
private string binaryControlData = "";
private double THROTTLE_SENSITIVITY;
private double DECELERATION_FACTOR;
// Private constants
//private string BINARY_SIGNATURE = "01101100"; // 108
private void FormMain_Load(object sender, System.EventArgs e)
{
settings = (Settings)LoadSaveData.LoadData("settings.dat");
if (settings == null) settings = new Settings();
ComboPort.SelectedIndex = settings.portIndex;
ComboBaud.SelectedIndex = settings.baudIndex;
// Set tracbar values
TBAcceleration.Value = (int)settings.throttleFactor;
TBDeceleration.Value = (int)settings.decelerationFactor;
CheckLights.Checked = settings.lightsActive;
}
//private int packetsSent2 = 0;
private void PacketSenderThread()
{
while(true)
{
bool sendDataFlag = false;
lock (sendData)
{
if (sendData == "1")
sendDataFlag = true;
else
sendDataFlag = false;
}
if (sendDataFlag && rs232.IsOpen)
{
rs232.Write(packet);
packetsSent++;
//packetsSent2++;
}
}
}
# region Open port code
// Setup driver and open
private void OpenPort()
{
// RS232 driver settings
rs232.DataBit = 8;
rs232.Timeout = 1500;
rs232.StopBit = Rs232.DataStopBit.StopBit_2;
rs232.Parity = Rs232.DataParity.Parity_None;
rs232.BaudRate = Convert.ToInt16(ComboBaud.Text);
rs232.Port = Convert.ToInt16(ComboPort.Text.Substring(3, 1));
try
{
// Open serial port
rs232.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region Closing
private void FormMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
lock (sendData)
{
sendData = "0";
}
if (thrPacketSenderThread != null) thrPacketSenderThread.Abort();
Thread.Sleep(500);
settings.baudIndex = ComboBaud.SelectedIndex;
settings.decelerationFactor = TBDeceleration.Value;
settings.throttleFactor = TBAcceleration.Value;
settings.lightsActive = CheckLights.Checked;
settings.portIndex = ComboPort.SelectedIndex;
LoadSaveData.SaveData("settings.dat", settings);
try
{
rs232.DisableEvents();
if (rs232.IsOpen) rs232.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
private void TimerKeyboard_Tick(object sender, System.EventArgs e)
{
//this.Text = packetsSent2.ToString();
//if (packetsSent2 >= 500)
//{
// TimerKeyboard.Enabled = false;
// ButtonToggleEnable_Click(null, EventArgs.Empty);
//}
string data = "";
string keyForward, keyBack, keyLeft, keyRight;
// Key state handler
#region Key state code
Font fontBold = new Font(LabelBaseLabel.Font, FontStyle.Bold);
Font fontRegular = new Font(LabelBaseLabel.Font, FontStyle.Regular);
//for (int i = 0; i <= 512; i++)
//{
// keyForward = Convert.ToString(GetKeyState(i), 16).ToUpper();
// if (keyForward.Substring(0,1) == "F") this.Text = i.ToString();
// Application.DoEvents();
//}
// Get key states
keyForward = Convert.ToString(GetKeyState(0x26), 16).ToUpper();
keyBack = Convert.ToString(GetKeyState(0x28), 16).ToUpper();
keyLeft = Convert.ToString(GetKeyState(0x25), 16).ToUpper();
keyRight = Convert.ToString(GetKeyState(0x27), 16).ToUpper();
// Forward key
if (keyForward.Substring(0,1) == "F")
{
LabelForward.ForeColor = Color.Red;
LabelForward.Font = fontBold;
keyForwardPressed = true;
}
else
{
LabelForward.ForeColor = Color.Gray;
LabelForward.Font = fontRegular;
keyForwardPressed = false;
}
// Back key
if (keyBack.Substring(0,1) == "F")
{
LabelBack.ForeColor = Color.Red;
LabelBack.Font = fontBold;
keyBackPressed = true;
}
else
{
LabelBack.ForeColor = Color.Gray;
LabelBack.Font = fontRegular;
keyBackPressed = false;
}
// Left key
if (keyLeft.Substring(0,1) == "F")
{
LabelLeft.ForeColor = Color.Red;
LabelLeft.Font = fontBold;
keyLeftPressed = true;
}
else
{
LabelLeft.ForeColor = Color.Gray;
LabelLeft.Font = fontRegular;
keyLeftPressed = false;
}
// Right key
if (keyRight.Substring(0,1) == "F")
{
LabelRight.ForeColor = Color.Red;
LabelRight.Font = fontBold;
keyRightPressed = true;
}
else
{
LabelRight.ForeColor = Color.Gray;
LabelRight.Font = fontRegular;
keyRightPressed = false;
}
#endregion
// Lock or open controls
if (CheckLockAcc.Checked)
{
TBDeceleration.Enabled = false;
TBAcceleration.Enabled = false;
}
else
{
TBDeceleration.Enabled = true;
TBAcceleration.Enabled = true;
}
// Set acceleration and deceleration values from trackbar settings
THROTTLE_SENSITIVITY = TBAcceleration.Value;
DECELERATION_FACTOR = (double)TBDeceleration.Value / 2;
// Activate car lights?
activateLights = CheckLights.Checked;
// Show binary control data
//LabelBinaryData.Text = binaryControlData;
// Packet counter
timerPacketTics++;
if (timerPacketTics * this.TimerKeyboard.Interval >= 500)
{
LabelPacketRate.Text = "Data rate: " + Math.Round(packetsSent * (double)2, 1).ToString() +
" packets / s";
int dataRate = (int)Math.Round(packetsSent * (double)2, 1);
if (dataRate <= PBDataRate.Maximum) PBDataRate.Value = (int)Math.Round(packetsSent * (double)2, 1);
packetsSent = 0;
timerPacketTics = 0;
}
// Checksum generator
data = binaryControlData;
// Convert binary data to byte array
if (data.Length != 0)
{
/*
packet = new byte[3];
packet[0] = Convert.ToByte(BINARY_SIGNATURE, 2);
packet[1] = Convert.ToByte(data.Substring(0, 8), 2);
packet[2] = Convert.ToByte(data.Substring(8, 8), 2);
*/
lock (sendData)
{
if (twoDelays)
{
packet = new byte[6];
//packet[0] = Convert.ToByte(BINARY_SIGNATURE, 2);
packet[0] = Convert.ToByte(data, 2);
packet[1] = Convert.ToByte(CalcCRC(data), 2);
packet[2] = Convert.ToByte("00000000", 2);
packet[3] = Convert.ToByte("00000000", 2);
packet[4] = Convert.ToByte("00000000", 2);
packet[5] = Convert.ToByte("00000000", 2);
twoDelays = false;
}
else
{
//string x = data.Substring(0, 6) + "0" + (data.Substring(7,1));
//data = x;
packet = new byte[5];
//packet[0] = Convert.ToByte(BINARY_SIGNATURE, 2);
packet[0] = Convert.ToByte(data, 2);
packet[1] = Convert.ToByte(CalcCRC(data), 2);
packet[2] = Convert.ToByte("00000000", 2);
packet[3] = Convert.ToByte("00000000", 2);
packet[4] = Convert.ToByte("00000000", 2);
twoDelays = true;
}
}
// Show binary control data
//LabelBinaryData.Text = BINARY_SIGNATURE + data + " [" +
//packet[0].ToString() + ":" + packet[1].ToString() + ":" + packet[2].ToString() + "]";
LabelBinaryData.Text = data + " [" + packet[0].ToString() + ":" + packet[1].ToString() + "]";
}
}
string CalcCRC(string data)
{
return NumberOfOnesInBinary(BitwiseInvert(data));
}
string BitwiseInvert(string data)
{
string result = "";
for (int i = 0; i < data.Length; i++)
{
if (data[i] == '0')
result = result + "1";
else
result = result + "0";
}
return result;
}
string NumberOfOnesInBinary(string data)
{
int count = 0;
for (int i = 0; i < data.Length; i++)
{
if (data[i] == '1') count++;
}
return Convert.ToString(count, 2);
}
private void TimerSteering_Tick(object sender, System.EventArgs e)
{
activateBrakeLights = false;
if (keyForwardPressed)
{
if (throttle < 0 && DECELERATION_FACTOR > 0) throttle = 0;
if (throttle < 0 && DECELERATION_FACTOR == 0)
{
throttle += THROTTLE_SENSITIVITY;
}
else
{
if (throttle < 100) throttle += THROTTLE_SENSITIVITY; // Increase throttle, max = 100
}
}
if (keyBackPressed)
{
if (throttle > 0 && DECELERATION_FACTOR > 0) throttle = 0;
if (throttle > 0 && DECELERATION_FACTOR == 0)
{
throttle -= THROTTLE_SENSITIVITY;
}
else
{
if (throttle > -66) throttle -= THROTTLE_SENSITIVITY; // Decrease throttle, min = -66
}
}
if (!keyForwardPressed && !keyBackPressed)
{
if (throttle > 0) throttle -= DECELERATION_FACTOR;
if (throttle < 0) throttle += DECELERATION_FACTOR;
if (Math.Abs(throttle) < THROTTLE_SENSITIVITY * 2) throttle = 0;
if (throttle != 0 && DECELERATION_FACTOR > 0) activateBrakeLights = true;
}
if (throttle > 100) throttle = 100;
if (throttle < -66) throttle = -66;
// Car direction
if (throttle < 0) directionCar = -1;
if (throttle == 0) directionCar = 0;
if (throttle > 0) directionCar = 1;
// Update binary control data
UpdateBinaryControlData();
#region Visual status update
// Update visual status
Font fontBold = new Font(LabelBaseLabel.Font, FontStyle.Bold);
Font fontRegular = new Font(LabelBaseLabel.Font, FontStyle.Regular);
if (throttle < 0) LabelDirection.Font = fontBold;
if (throttle == 0) LabelDirection.Font = fontRegular;
if (throttle > 0) LabelDirection.Font = fontBold;
if (throttle < 0) LabelDirection.Text = "Direction: Back";
if (throttle == 0) LabelDirection.Text = "Direction: None";
if (throttle > 0) LabelDirection.Text = "Direction: Forward";
LabelThrottle.Text = "Throttle: " + Math.Round(Math.Abs(throttle), 0) + " %";
if (throttle != 0) LabelThrottle.Font = fontBold;
if (throttle == 0) LabelThrottle.Font = fontRegular;
if (throttle <= -66) PBThrottle.Value = 66;
if (throttle <= -33 && throttle > -66) PBThrottle.Value = 33;
if (throttle <= 32 && throttle > -32) PBThrottle.Value = 0;
if (throttle <= 66 && throttle > 33) PBThrottle.Value = 33;
if (throttle <= 99 && throttle > 66) PBThrottle.Value = 66;
if (throttle > 99) PBThrottle.Value = 100;
if (keyLeftPressed) LabelTurning.Font = fontBold;
if (keyRightPressed) LabelTurning.Font = fontBold;
if (!keyLeftPressed && !keyRightPressed) LabelTurning.Font = fontRegular;
if (keyLeftPressed) LabelTurning.Text = "Turning: Left";
if (keyRightPressed) LabelTurning.Text = "Turning: Right";
if (!keyLeftPressed && !keyRightPressed) LabelTurning.Text = "Turning: None";
if (rs232.IsOpen)
{
ButtonToggleEnable.Text = "Disable port";
LabelStatus.Font = fontBold;
LabelStatus.Text = "Port active: Yes";
ComboPort.Enabled = false;
ComboBaud.Enabled = false;
}
else
{
ButtonToggleEnable.Text = "Enable port";
LabelStatus.Font = fontRegular;
LabelStatus.Text = "Port active: No";
ComboPort.Enabled = true;
ComboBaud.Enabled = true;
}
#endregion
// Update throttle step
if (PBThrottle.Value == 0) throttleStep = 0;
if (PBThrottle.Value == 33 && directionCar < 0) throttleStep = -1;
if (PBThrottle.Value == 33 && directionCar > 0) throttleStep = 1;
if (PBThrottle.Value == 66 && directionCar < 0) throttleStep = -2;
if (PBThrottle.Value == 66 && directionCar > 0) throttleStep = 2;
if (PBThrottle.Value == 100 && directionCar > 0) throttleStep = 3;
}
private void UpdateBinaryControlData()
{
// Update binary control data
binaryControlData = "";
// ----------------------------------------------------
// No forward/backward direction of the car
// ----------------------------------------------------
if (directionCar == 0)
{
// If turn key is pressed, rotate motors in different angles
if (keyLeftPressed) binaryControlData = "111011";
if (keyRightPressed) binaryControlData = "011111";
// No key pressed
if (!keyLeftPressed && !keyRightPressed) binaryControlData = "000000"; // 0% both motors
}
// ----------------------------------------------------
// Backward direction of the car
// ----------------------------------------------------
if (directionCar < 0)
{
// No turning
if (!keyLeftPressed && !keyRightPressed)
{
// Throttle
if (throttleStep == 0) binaryControlData = "000000"; // 0% both motors
if (throttleStep == -1) binaryControlData = "001001"; // 33% both motors
if (throttleStep == -2) binaryControlData = "010010"; // 66% both motors
}
// Turning left
if (keyLeftPressed)
{
// Throttle
if (throttleStep == 0) binaryControlData = "000000"; // 0% both motors
if (throttleStep == -1) binaryControlData = "001000"; // 33% / 0%
if (throttleStep == -2) binaryControlData = "010001"; // 66% / 33%
}
// Turning right
if (keyRightPressed)
{
// Throttle
if (throttleStep == 0) binaryControlData = "000000"; // 0% both motors
if (throttleStep == -1) binaryControlData = "000001"; // 0% / 33%
if (throttleStep == -2) binaryControlData = "001010"; // 33% / 66%
}
}
// ----------------------------------------------------
// Forward direction of the car
// ----------------------------------------------------
if (directionCar > 0)
{
// No turning
if (!keyLeftPressed && !keyRightPressed)
{
// Throttle
if (throttleStep == 0) binaryControlData = "000000"; // 0% both motors
if (throttleStep == 1) binaryControlData = "101101"; // 33% both motors
if (throttleStep == 2) binaryControlData = "110110"; // 66% both motors
if (throttleStep == 3) binaryControlData = "111111"; // 100% both motors
}
// Turning left
if (keyLeftPressed)
{
// Throttle
if (throttleStep == 0) binaryControlData = "000000"; // 0% both motors // Throttle = 0%
if (throttleStep == 1) binaryControlData = "100101"; // 0% / 33% // Throttle = 33%
if (throttleStep == 2) binaryControlData = "101110"; // 33% / 66% // Throttle = 66%
if (throttleStep == 3) binaryControlData = "110111"; // 66% / 100% // Throttle = 100%
}
// Turning right
if (keyRightPressed)
{
// Throttle
if (throttleStep == 0) binaryControlData = "000000"; // 0% both motors
if (throttleStep == 1) binaryControlData = "101100"; // 33% / 0%
if (throttleStep == 2) binaryControlData = "110101"; // 66% / 33%
if (throttleStep == 3) binaryControlData = "111110"; // 100% / 66%
}
}
// Lights active?
if (activateLights) binaryControlData = binaryControlData + "1";
if (!activateLights) binaryControlData = binaryControlData + "0";
// Brake lights
if (activateBrakeLights) binaryControlData = binaryControlData + "1";
if (!activateBrakeLights) binaryControlData = binaryControlData + "0";
// The binary data must be 8 bits long. Fill 0:s
while (binaryControlData.Length < 8)
{
binaryControlData = "0" + binaryControlData;
}
}
private void ButtonToggleEnable_Click(object sender, System.EventArgs e)
{
//packetsSent2 = 0;
//TimerKeyboard.Enabled = true;
if (ButtonToggleEnable.Text == "Enable port")
{
OpenPort();
Thread.Sleep(200);
lock (sendData)
{
sendData = "1";
}
thrPacketSenderThread = new Thread(new ThreadStart(PacketSenderThread));
thrPacketSenderThread.Priority = ThreadPriority.BelowNormal;
thrPacketSenderThread.Start();
}
else
{
lock (sendData)
{
sendData = "0";
}
thrPacketSenderThread.Abort();
Thread.Sleep(500);
rs232.Close();
}
}
}
[Serializable()]
public class Settings
{
public double throttleFactor = 5;
public double decelerationFactor = 10;
public bool lightsActive = false;
public int portIndex = 0;
public int baudIndex = 8;
}
}