-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
652 lines (588 loc) · 20 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
* Project: viTestApp
* Company: Vera Ikona, http://vera-ikona.com
* Author: Robert Milne
* Created: June 2011
*
* Notes:
*/
#region Inclusions
/******************************************************************************/
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using viServoMaster;
#endregion
namespace viTestApp
{
public partial class MainForm : Form
{
#region Constants
/******************************************************************************/
// Invalid servo network address
public const byte NULL_NODE = 255;
#endregion
#region Public Enumerations
/******************************************************************************/
public enum LogMsgType
{
Attention,
Outgoing,
Normal,
Warning,
Error
};
#endregion
#region Private Variables
/******************************************************************************/
// Comport string
private string _port = string.Empty;
// XML file directory
private string XMLDir = string.Empty;
private SortedDictionary<byte, Servo> _servo_ht = null;
//private Servos _servos_obj = null;
// FuzzyRules object to handle dynamic creation of comboboxes for setting fuzzy rules
private FuzzyRules _fuzzy_rules_obj = null;
// FuzzyMemFuncs object to handle dynamic creation of textboxes for setting fuzzy membership functions
private FuzzyMemFuncs _fuzzy_mem_funcs_obj = null;
// Gateway
private Gateway _gateway;
// Group command object
private Group _group;
// Current servo node
private Servo _active_servo;
// Various colors for logging info
private Color[] LogMsgTypeColor = { Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red };
// ControlList used for Slave ComboBox DataSource
private List<slaveSelector> _slave_selector;
private bool _update_flag = false;
Version _app_ver;
Version _dll_ver;
#endregion
#region Constructors
/******************************************************************************/
public MainForm()
{
_app_ver = Assembly.GetExecutingAssembly().GetName().Version;
_dll_ver = Master.Version;
InitializeComponent();
// Hide settings menus and controls until servo network is detected
tabControl1.Hide();
_servo_ht = new SortedDictionary<byte, Servo>();
_active_servo = null;
// Drill down to locate containers where programatically constructed elements located (yuck!)
GroupBox _rules_container = null;
// Unfocus label required to remove focus from rules comboboxes after selection - hide under another control (yuck!)
Label _unfocus_lbl = null;
foreach(Control c_0 in this.Controls)
{
if(c_0.Name == "tabControl1")
{
TabControl tab_ctl_0 = (TabControl)c_0;
foreach(Control c_1 in tab_ctl_0.Controls)
{
if(c_1.Name == "tabPageSetup")
{
TabPage tab_page_0 = (TabPage)c_1;
foreach(Control c_2 in tab_page_0.Controls)
{
if(c_2.Name == "tabServoSettings")
{
TabControl tab_ctl_1 = (TabControl)c_2;
foreach(Control c_3 in tab_ctl_1.Controls)
{
if(c_3.Name == "tabPageSetupFuzzy")
{
TabPage tab_page_1 = (TabPage)c_3;
foreach(Control c_4 in tab_page_1.Controls)
{
if(c_4.Name == "gbSetFuzzyRules")
{
GroupBox gbx = (GroupBox)c_4;
_rules_container = gbx;
foreach(Control c_5 in gbx.Controls)
{
if(c_5.Name == "lblUnfocus")
{
_unfocus_lbl = (Label)c_5;
}
}
/*foreach(Control c_5 in gbx.Controls)
{
if(c_5.Name == "pnlFuzzyRules")
{
_rules_container = (Panel)c_5;
}
if(c_5.Name == "lblUnfocus")
{
_unfocus_lbl = (Label)c_5;
}
}*/
}
if(c_4.Name == "gbSetFuzzyMF")
{
_fuzzy_mem_funcs_obj = new FuzzyMemFuncs((GroupBox)c_4);
}
}
if(_rules_container != null && _unfocus_lbl != null)
{
_fuzzy_rules_obj = new FuzzyRules(_rules_container, _unfocus_lbl);
}
else
{
_fuzzy_rules_obj = null;
MsgBox.Show("Fuzzy Rules Panel not found");
}
if(_fuzzy_mem_funcs_obj == null)
{
MsgBox.Show("Fuzzy Mem Func groupbox not found");
}
}
if(c_3.Name == "tabPageSetupControl")
{
// Set top level groupboxes in "Servo Control" tab to have bold text
TabPage tab_page_2 = (TabPage)c_3;
foreach(Control c_5 in tab_page_2.Controls)
{
if(c_5 is GroupBox)
{
c_5.Font = new Font(c_5.Font, FontStyle.Bold);
foreach(Control c_5_ctl in c_5.Controls)
{
c_5_ctl.Font = new Font(c_5.Font, FontStyle.Regular);
}
}
}
}
}
}
}
}
}
}
}
// Populate comboboxes
InitializeControls();
// Disable symmetric control points.
cbxMemFuncCtlPts.Checked = false;
CtlPtsActive(false);
}
#endregion
#region Private Methods
/******************************************************************************/
private bool GetGateway()
{
_gateway = new Gateway(Master.Gateway);
// Instantiate gateway after successful detection of Master.GatewayNode inside StartForm
if(_gateway.ServoGateway == null)
{
MsgBox.Show("No Gateway detected");
return false;
}
else
{
//this.lblGatewayVer.Text = _gateway.Node.Version.Version.ToString();
this.lblGatewayVer.Text = _gateway.ServoGateway.Description;
SetComboBox(cboCANBitrate, (int)_gateway.ServoGateway.Bitrate);
}
return true;
}
/// <summary>
/// GetServoNodes - Build slave hashtables from viServoMaster library
/// </summary>
[RefreshProperties(RefreshProperties.Repaint)]
private bool GetServoNodes()
{
// First slave selector is "no slave" selection
_slave_selector = new List<slaveSelector>();
Slave _null_slave = null;
_slave_selector.Add(new slaveSelector(_null_slave));
if(Master.SlaveHashtable != null)
{
// Node zero always exists if any non-zero nodes present
_group = Master.GroupNode;
Dictionary<byte, Slave>.ValueCollection valueColl = Master.SlaveHashtable.Values;
foreach(Slave slave in valueColl)
{
// Build new Servo object if not already existent
if(!_servo_ht.ContainsKey(slave.Id) && slave.IsReady)
{
Servo servo = new Servo(slave);
_servo_ht.Add(slave.Id, servo);
_slave_selector.Add(new slaveSelector(slave));
}
}
}
else
{
MsgBox.Show(this, String.Format("{0} does not connect to a slave network\n", _port), "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
// Populate Slave Node combobox
cboSlaveNode.DataSource = _slave_selector;
cboSlaveNode.DisplayMember = "Description";
cboSlaveNode.ValueMember = "Id";
if(_slave_selector.Count == 2)
{
// Single servo node - Update controls with selection
cboSlaveNode.SelectedIndex = 1;
}
return true;
}
private void InitializeControls()
{
//NB: String arrays used to populate combobox selections MUST match the control enum
// Get Servo Rate Enums (first three correspond to 'NO_INIT')
ClrComboBox(cboSetServoRate);
for(int i = 3; i < Strings.ServoRateStrings.Length; i++)
{
cboSetServoRate.Items.Add(Strings.ServoRateStrings[i]);
}
cboSetServoRate.Text = "";
// Get Encoder Divider
ClrComboBox(cboSetEncDiv);
for(int i = 0; i < Strings.EncoderDividerStrings.Length; i++)
{
cboSetEncDiv.Items.Add(Strings.EncoderDividerStrings[i]);
}
cboSetEncDiv.Text = "";
// Get H-Bridge Enums (first corresponds to 'NO_INIT')
ClrComboBox(cboSetHBridge);
for(int i = 0; i < Strings.HBridgeStrings.Length; i++)
{
cboSetHBridge.Items.Add(Strings.HBridgeStrings[i]);
}
cboSetHBridge.Text = "";
// Get H-Bridge PWM Rate Enums (first corresponds to 'NO_INIT')
ClrComboBox(cboSetPWMRate);
for(int i = 0; i < Strings.PWMRateStrings.Length; i++)
{
cboSetPWMRate.Items.Add(Strings.PWMRateStrings[i]);
}
cboSetPWMRate.Text = "";
ClrComboBox(cboCANBitrate);
for(int i = 0; i < Strings.CANBitrateStrings.Length; i++)
{
cboCANBitrate.Items.Add(Strings.CANBitrateStrings[i]);
}
cboCANBitrate.Text = "";
}
/// <summary>
/// Log - Log data to the terminal window.
/// </summary>
private void Log(LogMsgType msgtype, string msg)
{
rtbMessageLog.Invoke(new EventHandler(delegate
{
rtbMessageLog.SelectedText = string.Empty;
rtbMessageLog.SelectionFont = new Font(rtbMessageLog.SelectionFont, FontStyle.Bold);
rtbMessageLog.SelectionColor = LogMsgTypeColor[(int)msgtype];
rtbMessageLog.AppendText(msg);
rtbMessageLog.ScrollToCaret();
}));
}
// Delegates that enable asynchronous calls to UI controls
delegate void SetTextCallback(Control ctl, string text);
delegate void ClrTextCallback(TextBoxBase ctl);
delegate void SetCheckboxCallback(CheckBox ctl, bool val);
delegate void SetRadioButtonCallback(RadioButton ctl, bool val);
delegate void SetListBoxCallback(ListBox ctl, int idx);
delegate void ClrListBoxCallback(ListBox ctl);
delegate void SetComboBoxCallback(ComboBox ctl, int idx);
delegate void ClrComboBoxCallback(ComboBox ctl);
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.
private void SetText(Control ctl, string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if(ctl.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { ctl, text });
}
else
{
ctl.Text = text;
}
}
private void ClrText(TextBoxBase ctl)
{
if(ctl.InvokeRequired)
{
ClrTextCallback d = new ClrTextCallback(ClrText);
this.Invoke(d, new object[] { ctl });
}
else
{
ctl.Clear();
}
}
private void SetCheckbox(CheckBox ctl, bool val)
{
if(ctl.InvokeRequired)
{
SetCheckboxCallback d = new SetCheckboxCallback(SetCheckbox);
this.Invoke(d, new object[] { ctl, val });
}
else
{
ctl.Checked = val;
}
}
private void SetRadioButton(RadioButton ctl, bool val)
{
if(ctl.InvokeRequired)
{
SetRadioButtonCallback d = new SetRadioButtonCallback(SetRadioButton);
this.Invoke(d, new object[] { ctl, val });
}
else
{
ctl.Checked = val;
}
}
private void SetListBox(ListBox ctl, int idx)
{
if(ctl.InvokeRequired)
{
SetListBoxCallback d = new SetListBoxCallback(SetListBox);
this.Invoke(d, new object[] { ctl, idx });
}
else
{
ctl.SelectedIndex = idx;
}
}
private void ClrListBox(ListBox ctl)
{
if(ctl.InvokeRequired)
{
ClrListBoxCallback d = new ClrListBoxCallback(ClrListBox);
this.Invoke(d, new object[] { ctl });
}
else
{
ctl.ClearSelected();
}
}
private void SetComboBox(ComboBox ctl, int idx)
{
if(ctl.InvokeRequired)
{
SetComboBoxCallback d = new SetComboBoxCallback(SetComboBox);
this.Invoke(d, new object[] { ctl, idx });
}
else
{
ctl.SelectedIndex = idx;
}
}
private void ClrComboBox(ComboBox ctl)
{
// Note: cannot be used for ComboBox with a bound list control (DataSource property)
if(ctl.InvokeRequired)
{
ClrComboBoxCallback d = new ClrComboBoxCallback(ClrComboBox);
this.Invoke(d, new object[] { ctl });
}
else
{
ctl.Items.Clear();
}
}
#endregion
#region Event Handlers
/******************************************************************************/
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Dispose Node, Slave and FTDI objects
Master.Dispose();
}
/// <summary>
/// openMenuItem_Click - 'Open Servo Network' menu selection under 'File'
/// </summary>
private void openMenuItem_Click(object sender, EventArgs e)
{
// app & dll versions must match
if(_app_ver != _dll_ver)
{
MsgBox.Show(this, "Application and dll versions do not match!\nSee \"About\" menu item");
}
else
{
// Build slave network (hashtable) in start form if
// selected comport opens and detects servo network
if(_port == string.Empty)
{
// Port Dialog to select Serial Port
StartForm startForm = new StartForm();
if(startForm.ShowDialog(Form.ActiveForm) == DialogResult.OK)
{
_port = startForm.Port;
if(GetGateway())
{
tabControl1.Show();
tabControl1.DeselectTab(tabPageSetup);
tabControl1.SelectedTab = tabPageGateWay;
tabPageSetup.Hide();
cboSlaveNode.Hide();
lblEnumNodes.Hide();
tabPageLog.Hide();
tabPageGateWay.BringToFront();
// programmatically click gateway 'get status' button (enables bitrate cbo)
this.InvokeOnClick(btnGatewayGetStatus, EventArgs.Empty);
// Send ENUM command to servo network to discover nodes
if(GetServoNodes())
{
// Show settings selection menu item
Log(LogMsgType.Normal, _port + " opened\n");
tabPageSetup.Show();
tabServoSettings.Show();
cboSlaveNode.Show();
lblEnumNodes.Show();
tabPageLog.Show();
tabControl1.Show();
tabControl1.SelectedTab = tabPageSetup;
openMenuItem.Enabled = false;
}
}
else
{
CloseComPort(ref _port);
}
}
}
else
{
Master.ReEnumerateNodes();
// Send ENUM command to servo network to discover nodes
if(GetServoNodes())
{
// Show settings selection menu item
Log(LogMsgType.Normal, _port + " opened\n");
tabPageSetup.Show();
tabServoSettings.Show();
cboSlaveNode.Show();
lblEnumNodes.Show();
tabPageLog.Show();
tabControl1.Show();
tabControl1.SelectedTab = tabPageSetup;
openMenuItem.Enabled = false;
}
}
}
}
/// <summary>
/// aboutToolStripMenuItem_Click - 'About' menu selection under 'File'
/// </summary>
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MsgBox.Show(this, String.Format("Created by Rob Milne\nVera Ikona Ltd\nhttp://vera-ikona.com\nviTestApp Ver: {0}\nviServoMaster.dll Ver: {1}\nCopyright © 2011", Assembly.GetExecutingAssembly().GetName().Version.ToString(), _dll_ver.ToString()), "About viTestApp", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void CloseComPort(ref string port)
{
try
{
string err_str;
Master.OpenCloseComPort(port, out err_str, true);
}
catch(Exception ex)
{
MsgBox.Show(this, String.Format("Master.OpenCloseComPort({0}) err:\n {1}\n", port, ex.ToString()), "Comport Close Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
port = string.Empty;
tabControl1.Hide();
}
/// <summary>
/// btnMessageLogClear_Click - Clear log messages from the message window
/// </summary>
private void btnMessageLogClear_Click(object sender, EventArgs e)
{
rtbMessageLog.Clear();
}
private void cboSlaveNode_SelectedIndexChanged(object sender, EventArgs e)
{
// first "null" selection returns obj instead of value ?!?
if(cboSlaveNode.SelectedValue is slaveSelector)
{
_active_servo = null;
}
else
{
if((byte)cboSlaveNode.SelectedValue != NULL_NODE &&
_servo_ht != null &&
_servo_ht.ContainsKey((byte)cboSlaveNode.SelectedValue))
{
_active_servo = _servo_ht[(byte)cboSlaveNode.SelectedValue];
// Prevent calling this if combobox selections not initialized
this.InvokeOnClick(btnServoCtlLoad, EventArgs.Empty);
this.InvokeOnClick(btnStatusRead, EventArgs.Empty);
this.InvokeOnClick(btnMotionCtlLoad, EventArgs.Empty);
this.InvokeOnClick(btnLimitCtlLoad, EventArgs.Empty);
// Automatically populate fuzzy vars
this.InvokeOnClick(btnGetMF, EventArgs.Empty);
this.InvokeOnClick(btnGetRules, EventArgs.Empty);
}
else
{
_active_servo = null;
}
}
}
private void cboSlaveNode_DataSourceChanged(Object sender, EventArgs e)
{
ComboBox ctlLIST = (ComboBox)sender;
if(ctlLIST.DataSource == null)
ctlLIST.Items.Clear();
}
#endregion
#region Private Class
/******************************************************************************/
/// <summary>
/// slaveSelector - nested private class
/// Used in slave combobox list to display node/version and select by nodeId rather than combobox index
/// </summary>
private class slaveSelector : ISlave
{
private byte _id;
private string _desc;
public byte Id {
set { _id = value; }
get { return _id; }
}
public string Description {
set { _desc = value; }
get { return (_desc); }
}
public slaveSelector(ISlave slave)
{
if(slave == null)
{
// There should only be one of these per combobox (top selection)
// Id is invalid node Id
_id = NULL_NODE;
_desc = " ";
}
else
{
_id = slave.Id;
_desc = slave.Description;
}
}
}
#endregion
}
}