| Wheelchair Control Diploma Thesis, Author: Ondrej Vozda, FEEC BUT, 2013

- Main Page
- Packages
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
FormManualControl.cs
1 ﻿using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.IO.Ports;
10
11 namespace WheelchairControl
12 {
16 public partial class FormManualControl : Form
17 {
18 #region Delegates
19
24 public delegate void UARTSend_Delegate(uint[] data);
25
26 #endregion
27
28 #region Constants
29
33 private const Byte STOP_REQUEST_REPEATS = 3;
34
38 private const Byte RX_DATA_LENGTH = FormMain.RX_DATA_LENGTH;
39
40 #endregion
41
42 #region Private variables
43
47 private static FormManualControl instance = null;
48
52 private static Object singeltonLock = new Object();
53
57 private SerialPort serialPort_formMain;
58
62 private UARTSend_Delegate UARTSendMethod;
63
64 #endregion
65
71 private FormManualControl(SerialPort formMainCOMPort, UARTSend_Delegate sendMethod)
72 {
73 serialPort_formMain = formMainCOMPort;
74 UARTSendMethod = sendMethod;
75
76 //Register handler
77 FormMain.COMStatusChanged += delegate() {
78 if (this.InvokeRequired)
79 {
80 this.Invoke((MethodInvoker)delegate
81 {
82 FormManualControl_Activated(this, null);
83 });
84 }
85 else
86 {
87 FormManualControl_Activated(this, null);
88 }
89 };
90
91 InitializeComponent();
92 }
93
100 public static FormManualControl GetInstance(SerialPort formMainCOMPort, UARTSend_Delegate sendMethod)
101 {
102 //Prevent possible thread race
103 lock (singeltonLock)
104 {
105 if (instance == null || instance.IsDisposed) //Create new singleton instance
106 instance = new FormManualControl(formMainCOMPort, sendMethod);
107 }
108
109 return instance;
110 }
111
112 private void hScrollBar1_ValueChanged(object sender, EventArgs e)
113 {
114 labelRightMotorSpeed.Text = hScrollBarRight.Value.ToString();
115 }
116
117 private void hScrollBar2_ValueChanged(object sender, EventArgs e)
118 {
119 labelLeftMotorSpeed.Text = hScrollBarLeft.Value.ToString();
120 }
121
122 private void buttonResetSpeeds_Click(object sender, EventArgs e)
123 {
124 hScrollBarRight.Value = 128;
125 hScrollBarLeft.Value = 128;
126 hScrollBarSpCmR.Value = 0;
127 hScrollBarSpCmL.Value = 0;
128
129 tsslInfo.Text = "Trackbars' values have been reset.";
130 }
131
132 private void buttonMomRight_Click(object sender, EventArgs e)
133 {
134 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
135
136 outputBuffer[0] = 0xC0;
137 outputBuffer[1] = 0x00;
138 outputBuffer[2] = Convert.ToUInt16(labelRightMotorSpeed.Text);
139 UARTSendMethod(outputBuffer);
140
141 tsslInfo.Text = "Moment of the right wheel has been set to " + outputBuffer[2].ToString() + " impps.";
142 }
143
144 private void buttonMomLeft_Click(object sender, EventArgs e)
145 {
146 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
147
148 outputBuffer[0] = 0xC1;
149 outputBuffer[1] = 0x00;
150 outputBuffer[2] = Convert.ToUInt16(labelLeftMotorSpeed.Text);
151 UARTSendMethod(outputBuffer);
152
153 tsslInfo.Text = "Moment of the left wheel has been set to " + outputBuffer[2].ToString() + " impps.";
154 }
155
156 private void buttonSpeedRight_Click(object sender, EventArgs e)
157 {
158 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
159
160 int temp = Convert.ToInt32(labelSpeedIncmpsR.Text) + 500;
161
162 outputBuffer[0] = 0xC2;
163 outputBuffer[1] = (byte)(temp >> 8);
164 outputBuffer[2] = (byte)(temp % 256);
165 UARTSendMethod(outputBuffer);
166
167 tsslInfo.Text = "Speed of the right wheel has been set to " + labelSpeedIncmpsR.Text + " cmps.";
168 }
169
170 private void buttonSpeedLeft_Click(object sender, EventArgs e)
171 {
172 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
173
174 int temp = Convert.ToInt32(labelSpeedIncmpsL.Text) + 500;
175
176 outputBuffer[0] = 0xC3;
177 outputBuffer[1] = (byte)(temp >> 8);
178 outputBuffer[2] = (byte)(temp % 256);
179 UARTSendMethod(outputBuffer);
180
181 tsslInfo.Text = "Speed of the left wheel has been set to " + labelSpeedIncmpsR.Text + " cmps.";
182 }
183
184 private void buttonMomBoth_Click(object sender, EventArgs e)
185 {
186 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
187
188 outputBuffer[0] = 0xC4;
189 outputBuffer[1] = Convert.ToUInt16(labelRightMotorSpeed.Text);
190 outputBuffer[2] = Convert.ToUInt16(labelLeftMotorSpeed.Text);
191 UARTSendMethod(outputBuffer);
192
193 tsslInfo.Text = "Moment of both wheels has been set to: Left: " + outputBuffer[2].ToString() +
194 " impps, Right: " + outputBuffer[1].ToString() + " impps.";
195 }
196
197 private void buttonSpeedBoth_Click(object sender, EventArgs e)
198 {
199 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
200
201 outputBuffer[0] = 0xC5;
202 outputBuffer[1] = Convert.ToUInt16(labelRightMotorSpeed.Text);
203 outputBuffer[2] = Convert.ToUInt16(labelLeftMotorSpeed.Text);
204 UARTSendMethod(outputBuffer);
205
206 tsslInfo.Text = "Speed of both wheels has been set to: Left: " + outputBuffer[2].ToString() +
207 " impps, Right: " + outputBuffer[1].ToString() + " impps.";
208 }
209
210 private void hScrollBarSpCmR_ValueChanged(object sender, EventArgs e)
211 {
212 labelSpeedIncmpsR.Text = hScrollBarSpCmR.Value.ToString();
213 }
214
215 private void hScrollBarSpCmL_ValueChanged(object sender, EventArgs e)
216 {
217 labelSpeedIncmpsL.Text = hScrollBarSpCmL.Value.ToString();
218 }
219
220 private void buttonSpeedIncmpsBoth_Click(object sender, EventArgs e)
221 {
222 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
223
224 //Send speed of right wheel
225 Int16 temp = (Int16)(Convert.ToInt16(labelSpeedIncmpsR.Text) + 500);
226
227 outputBuffer[0] = 0xC2;
228 outputBuffer[1] = (uint)temp >> 8;
229 outputBuffer[2] = (uint)temp % 256;
230 UARTSendMethod(outputBuffer);
231
232 //Send speed of left wheel
233 temp = (Int16)(Convert.ToInt16(labelSpeedIncmpsL.Text) + 500);
234
235 outputBuffer[0] = 0xC3;
236 outputBuffer[1] = (uint)temp >> 8;
237 outputBuffer[2] = (uint)temp % 256;
238 UARTSendMethod(outputBuffer);
239
240 tsslInfo.Text = "Speed of both wheels has been set to: Left: " + labelSpeedIncmpsL.Text +
241 " cmps, Right: " + labelSpeedIncmpsR.Text + " cmps.";
242 }
243
244 private void buttonSTOP_Click(object sender, EventArgs e)
245 {
246 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
247
248 for (int i = 0; i < STOP_REQUEST_REPEATS; i++) //Send multiple times, just for sure
249 {
250 outputBuffer[0] = 0xC4; //Send stop (128 = zero moment)
251 outputBuffer[1] = 128;
252 outputBuffer[2] = 128;
253 UARTSendMethod(outputBuffer);
254 }
255
256 tsslInfo.Text = "STOP signal has been sent.";
257 }
258
259 private void FormManualControl_Activated(object sender, EventArgs e)
260 {
261 //Com port status info & controls enable
262 if (serialPort_formMain.IsOpen)
263 {
264 tssLabelComPortState.Text = "COM Port Status: OPEN";
265
266 //Enable buttons
267 foreach (Control controlItem in this.Controls)
268 controlItem.Enabled = true;
269 }
270 else
271 {
272 tssLabelComPortState.Text = "COM Port Status: CLOSED";
273 tsslInfo.Text = "Port closed, controls are disabled!";
274
275 //Disable buttons (leave status strip enabled)
276 foreach (Control controlItem in this.Controls)
277 if (controlItem.GetType() != typeof(StatusStrip))
278 controlItem.Enabled = false;
279 }
280 }
281 }
282 }