| 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
FormSettings.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 FormSettings : 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 FormSettings 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 FormSettings(SerialPort formMainCOMPort, UARTSend_Delegate SendMethod)
72 {
73 serialPort_formMain = formMainCOMPort;
74 UARTSendMethod = SendMethod;
75
76 //Register handler
77 FormMain.COMStatusChanged += delegate()
78 {
79 if (this.InvokeRequired)
80 {
81 this.Invoke((MethodInvoker)delegate
82 {
83 FormSettings_Activated(this, null);
84 });
85 }
86 else
87 {
88 FormSettings_Activated(this, null);
89 }
90 };
91
92 InitializeComponent();
93 }
94
101 public static FormSettings GetInstance(SerialPort formMainCOMPort, UARTSend_Delegate sendMethod)
102 {
103 //Prevent possible thread race
104 lock (singeltonLock)
105 {
106 if (instance == null || instance.IsDisposed) //Create new singleton instance
107 instance = new FormSettings(formMainCOMPort, sendMethod);
108 }
109
110 return instance;
111 }
112
113 private void buttonSaveSRFAddress_Click(object sender, EventArgs e)
114 {
115 UInt16 tempFront, tempRear;
116 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
117
118 tempFront = UInt16.Parse(textBoxSRF08_Front.Text, System.Globalization.NumberStyles.HexNumber);
119 tempRear = UInt16.Parse(textBoxSRF08_Rear.Text, System.Globalization.NumberStyles.HexNumber);
120
121 //Confirmation required
122 if (MessageBox.Show("Set SRF08 addresses to 0x" + tempFront.ToString("X2") + " (front) and 0x" + tempRear.ToString("X2") + " (rear)?",
123 "Confirmation required!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.Yes)
124 {
125 tsslInfo.Text = "Addresses not set.";
126
127 return;
128 }
129
130 if (tempFront < 0xE0 || tempFront > 0xE8 || tempRear < 0xE0 || tempRear > 0xE8) //Address out of range
131 {
132 MessageBox.Show("Only addresses 0xE0 - 0xE8 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
133 }
134 else
135 {
136 outputBuffer[0] = 0xE9;
137 outputBuffer[1] = tempFront;
138 outputBuffer[2] = tempRear;
139 UARTSendMethod(outputBuffer);
140 }
141
142 tsslInfo.Text = "Front SRF08 address set to: 0x" + tempFront.ToString("X2") + " , rear SRF08 address set to: 0x" + tempRear.ToString("X2");
143 }
144
145 private void button1_Click_1(object sender, EventArgs e)
146 {
147 UInt16 tempRight, tempLeft;
148 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
149
150 tempRight = Convert.ToUInt16(textBoxIPMS_Right.Text);
151 tempLeft = Convert.ToUInt16(textBoxIMPS_Left.Text);
152
153 if (tempRight < 0 || tempRight > 510 || tempLeft < 0 || tempLeft > 510) //Address out of range
154 {
155 MessageBox.Show("Only values 0 - 510 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
156 }
157 else
158 {
159 //Transmit data (divided by 2 before transmission to keep 8-bit resolution)
160 outputBuffer[0] = 0xE3;
161 outputBuffer[1] = (byte)(tempRight / 2);
162 outputBuffer[2] = (byte)(tempLeft / 2);
163 UARTSendMethod(outputBuffer);
164 }
165 }
166
167 private void button2_Click(object sender, EventArgs e)
168 {
169 UInt16 temp;
170 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
171
172 temp = Convert.ToUInt16(textBoxValueToSet.Text);
173
174 if (temp < 0 || temp > 10) //out of range
175 {
176 MessageBox.Show("Only values 0 - 10 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
177 }
178 else
179 {
180 outputBuffer[0] = 0xE5;
181 outputBuffer[1] = 0x00;
182 outputBuffer[2] = temp;
183 UARTSendMethod(outputBuffer);
184 }
185 }
186
187 private void button3_Click(object sender, EventArgs e)
188 {
189 UInt16 temp;
190 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
191
192 temp = Convert.ToUInt16(textBoxValueToSet.Text);
193
194 if (temp < 0 || temp > 255) //out of range
195 {
196 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
197 }
198 else
199 {
200 outputBuffer[0] = 0xE6;
201 outputBuffer[1] = 0x00;
202 outputBuffer[2] = temp;
203 UARTSendMethod(outputBuffer);
204 }
205 }
206
207 private void button4_Click(object sender, EventArgs e)
208 {
209 UInt16 temp;
210 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
211
212 temp = Convert.ToUInt16(textBoxValueToSet.Text);
213
214 if (temp < 0 || temp > 255) //out of range
215 {
216 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
217 }
218 else
219 {
220 outputBuffer[0] = 0xE7;
221 outputBuffer[1] = 0x00;
222 outputBuffer[2] = temp;
223 UARTSendMethod(outputBuffer);
224 }
225 }
226
227 private void button5_Click(object sender, EventArgs e)
228 {
229 UInt16 temp;
230 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
231
232 temp = Convert.ToUInt16(textBoxValueToSet.Text);
233
234 if (temp < 0 || temp > 255) //out of range
235 {
236 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
237 }
238 else
239 {
240 outputBuffer[0] = 0xE8;
241 outputBuffer[1] = 0x00;
242 outputBuffer[2] = temp;
243 UARTSendMethod(outputBuffer);
244 }
245 }
246
247 private void button6_Click(object sender, EventArgs e)
248 {
249 UInt16 temp;
250 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
251
252 temp = Convert.ToUInt16(textBoxValueToSet.Text);
253
254 if (temp < 0 || temp > 255) //out of range
255 {
256 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
257 }
258 else
259 {
260 outputBuffer[0] = 0xEA;
261 outputBuffer[1] = 0x00;
262 outputBuffer[2] = temp;
263 UARTSendMethod(outputBuffer);
264 }
265 }
266
267 private void button7_Click(object sender, EventArgs e)
268 {
269 UInt16 temp;
270 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
271
272 temp = Convert.ToUInt16(textBoxValueToSet.Text);
273
274 if (temp < 0 || temp > 255) //out of range
275 {
276 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
277 }
278 else
279 {
280 outputBuffer[0] = 0xEB;
281 outputBuffer[1] = 0x00;
282 outputBuffer[2] = temp;
283 UARTSendMethod(outputBuffer);
284 }
285 }
286
287 private void button9_Click(object sender, EventArgs e)
288 {
289 UInt16 temp;
290 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
291
292 temp = Convert.ToUInt16(textBoxValueToSet.Text);
293
294 if (temp < 0 || temp > 255) //out of range
295 {
296 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
297 }
298 else
299 {
300 outputBuffer[0] = 0xEC;
301 outputBuffer[1] = 0x00;
302 outputBuffer[2] = temp;
303 UARTSendMethod(outputBuffer);
304 }
305 }
306
307 private void button10_Click(object sender, EventArgs e)
308 {
309 UInt16 temp;
310 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
311
312 temp = Convert.ToUInt16(textBoxValueToSet.Text);
313
314 if (temp < 0 || temp > 255) //out of range
315 {
316 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
317 }
318 else
319 {
320 outputBuffer[0] = 0xED;
321 outputBuffer[1] = 0x00;
322 outputBuffer[2] = temp;
323 UARTSendMethod(outputBuffer);
324 }
325 }
326
327 private void button14_Click(object sender, EventArgs e)
328 {
329 UInt16 temp;
330 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
331
332 temp = Convert.ToUInt16(textBoxValueToSet.Text);
333
334 if (temp < 0 || temp > 255) //out of range
335 {
336 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
337 }
338 else
339 {
340 outputBuffer[0] = 0xE2;
341 outputBuffer[1] = 0x00;
342 outputBuffer[2] = temp;
343 UARTSendMethod(outputBuffer);
344 }
345 }
346
347 private void button13_Click(object sender, EventArgs e)
348 {
349 UInt16 temp;
350 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
351
352 temp = Convert.ToUInt16(textBoxValueToSet.Text);
353
354 if (temp < 0 || temp > 65535) //out of range
355 {
356 MessageBox.Show("Only values 0 - 65535 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
357 }
358 else
359 {
360 outputBuffer[0] = 0xE1;
361 outputBuffer[1] = (uint)temp >> 8;
362 outputBuffer[2] = (uint)temp % 256;
363 UARTSendMethod(outputBuffer);
364 }
365 }
366
367 private void button12_Click(object sender, EventArgs e)
368 {
369 UInt16 temp;
370 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
371
372 temp = Convert.ToUInt16(textBoxValueToSet.Text);
373
374 if (temp < 0 || temp > 65535) //out of range
375 {
376 MessageBox.Show("Only values 0 - 65535 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
377 }
378 else
379 {
380 outputBuffer[0] = 0xE0;
381 outputBuffer[1] = (uint)temp >> 8;
382 outputBuffer[2] = (uint)temp % 256;
383 UARTSendMethod(outputBuffer);
384 }
385 }
386
387 private void button15_Click(object sender, EventArgs e)
388 {
389 UInt16 temp;
390 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
391
392 temp = Convert.ToUInt16(textBoxValueToSet.Text);
393
394 if (temp < 0 || temp > 255) //out of range
395 {
396 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
397 }
398 else
399 {
400 outputBuffer[0] = 0xEE;
401 outputBuffer[1] = 0x00;
402 outputBuffer[2] = temp;
403 UARTSendMethod(outputBuffer);
404 }
405 }
406
407 private void button16_Click(object sender, EventArgs e)
408 {
409 Int16 temp;
410 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
411
412 temp = Convert.ToInt16(textBoxValueToSet.Text);
413
414 if (temp < -128 || temp > 127) //out of range
415 {
416 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
417 }
418 else
419 {
420 outputBuffer[0] = 0xEF;
421 outputBuffer[1] = 0x00;
422 outputBuffer[2] = (byte)(temp + 128);
423 UARTSendMethod(outputBuffer);
424 }
425 }
426
427 private void buttonSTOP_Click(object sender, EventArgs e)
428 {
429 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
430
431 for (int i = 0; i < STOP_REQUEST_REPEATS; i++) //Send multiple times, just for sure
432 {
433 outputBuffer[0] = 0xC4; //Send stop (128 = zero moment)
434 outputBuffer[1] = 128;
435 outputBuffer[2] = 128;
436 UARTSendMethod(outputBuffer);
437 }
438
439 tsslInfo.Text = "STOP signal has been sent.";
440 }
441
442 private void FormSettings_Activated(object sender, EventArgs e)
443 {
444 //Com port status info & controls enable
445 if (serialPort_formMain.IsOpen)
446 {
447 tssLabelComPortState.Text = "COM Port Status: OPEN";
448
449 //Enable buttons
450 foreach (Control controlItem in this.Controls)
451 controlItem.Enabled = true;
452 }
453 else
454 {
455 tssLabelComPortState.Text = "COM Port Status: CLOSED";
456 tsslInfo.Text = "Port closed, controls are disabled!";
457
458 //Disable buttons (leave status strip enabled)
459 foreach (Control controlItem in this.Controls)
460 if (controlItem.GetType() != typeof(StatusStrip))
461 controlItem.Enabled = false;
462 }
463 }
464
465 private void buttonGamma_Click(object sender, EventArgs e)
466 {
467 UInt16 temp;
468 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
469
470 temp = Convert.ToUInt16(textBoxValueToSet.Text);
471
472 if (temp < 0 || temp > 255) //out of range
473 {
474 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
475 }
476 else
477 {
478 outputBuffer[0] = 0xD2;
479 outputBuffer[1] = 0x00;
480 outputBuffer[2] = temp;
481 UARTSendMethod(outputBuffer);
482 }
483 }
484
485 private void buttonEpsilon_Click(object sender, EventArgs e)
486 {
487 UInt16 temp;
488 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
489
490 temp = Convert.ToUInt16(textBoxValueToSet.Text);
491
492 if (temp < 0 || temp > 255) //out of range
493 {
494 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
495 }
496 else
497 {
498 outputBuffer[0] = 0xD3;
499 outputBuffer[1] = 0x00;
500 outputBuffer[2] = temp;
501 UARTSendMethod(outputBuffer);
502 }
503 }
504
505 private void buttonAngleGain_Click(object sender, EventArgs e)
506 {
507 UInt16 temp;
508 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
509
510 temp = Convert.ToUInt16(textBoxValueToSet.Text);
511
512 if (temp < 0 || temp > 255) //out of range
513 {
514 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
515 }
516 else
517 {
518 outputBuffer[0] = 0xD4;
519 outputBuffer[1] = 0x00;
520 outputBuffer[2] = temp;
521 UARTSendMethod(outputBuffer);
522 }
523 }
524
525 private void buttonPositionGain_Click(object sender, EventArgs e)
526 {
527 UInt16 temp;
528 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
529
530 temp = Convert.ToUInt16(textBoxValueToSet.Text);
531
532 if (temp < 0 || temp > 255) //out of range
533 {
534 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
535 }
536 else
537 {
538 outputBuffer[0] = 0xD5;
539 outputBuffer[1] = 0x00;
540 outputBuffer[2] = temp;
541 UARTSendMethod(outputBuffer);
542 }
543 }
544
545 private void buttonRegSpeed_Click(object sender, EventArgs e)
546 {
547 UInt16 temp;
548 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
549
550 temp = Convert.ToUInt16(textBoxValueToSet.Text);
551
552 if (temp < 0 || temp > 255) //out of range
553 {
554 MessageBox.Show("Only values 0 - 255 allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
555 }
556 else
557 {
558 outputBuffer[0] = 0xD6;
559 outputBuffer[1] = 0x00;
560 outputBuffer[2] = temp;
561 UARTSendMethod(outputBuffer);
562 }
563 }
564
565 private void buttonSaveSRF08SafetyAddr_Click(object sender, EventArgs e)
566 {
567 UInt16 tempFront, tempRear;
568 uint[] outputBuffer = new uint[RX_DATA_LENGTH - 1];
569
570 tempFront = UInt16.Parse(textBoxSRF08_Front.Text, System.Globalization.NumberStyles.HexNumber);
571 tempRear = UInt16.Parse(textBoxSRF08_Rear.Text, System.Globalization.NumberStyles.HexNumber);
572
573 //Confirmation required
574 if (MessageBox.Show("Set SRF08 safety addresses to 0x" + tempFront.ToString("X2") + " (safety 1) and 0x" + tempRear.ToString("X2") + " (safety 2)?",
575 "Confirmation required!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.Yes)
576 {
577 tsslInfo.Text = "Addresses not set.";
578
579 return;
580 }
581
582 if (tempFront < 0x00 || tempFront > 0xFF || tempRear < 0x00 || tempRear > 0xFF) //Address out of range
583 {
584 MessageBox.Show("Only addresses 0x00 - 0xFF allowed!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
585 }
586 else
587 {
588 outputBuffer[0] = 0xE4;
589 outputBuffer[1] = tempFront;
590 outputBuffer[2] = tempRear;
591 UARTSendMethod(outputBuffer);
592 }
593
594 tsslInfo.Text = "Safety 1 SRF08 address set to: 0x" + tempFront.ToString("X2") + " , safety 2 SRF08 address set to: 0x" + tempRear.ToString("X2");
595 }
596 }
597 }