00001 using System;
00002 using System.Collections.Generic;
00003 using System.ComponentModel;
00004 using System.Data;
00005 using System.Drawing;
00006 using System.Linq;
00007 using System.Threading;
00008 using System.Text;
00009 using System.Windows.Forms;
00010 using RS232Port;
00011
00012 namespace NavigationController {
00013 public partial class NavigationControllerMainForm : Form {
00014
00015 NavigationStrategy _strategy;
00016
00017 bool _silent = true;
00018 public bool Silent {
00019 get { return _silent; }
00020 set {
00021 _silent = value;
00022 if (_silent) {
00023 _strategy.updateUiEvent -= updateUi;
00024 _strategy.setSilentModeEvent -= setSilentMode;
00025 } else {
00026 _strategy.updateUiEvent += updateUi;
00027 _strategy.setSilentModeEvent += setSilentMode;
00028 }
00029 }
00030 }
00031
00032 public NavigationControllerMainForm() {
00033 InitializeComponent();
00034 _strategy = new NavigationStrategy("COM2", 2185.39);
00035 initializeObject();
00036 }
00037
00038 public NavigationControllerMainForm(ref NavigationStrategy strategy) {
00039 InitializeComponent();
00040 _strategy = strategy;
00041 initializeObject();
00042 onInitialize();
00043 }
00044
00045 private void initializeObject() {
00046 _strategy.updateStatusEvent += onInitialize;
00047 toolStripStatusLabel2.Text = _strategy.Status;
00048 }
00049
00050
00051 private void setSilentMode() {
00052 if (Silent != _strategy.Silent)
00053 Silent = _strategy.Silent;
00054 }
00055
00056 private void onInitialize() {
00057 if (!_silent) {
00058 toolStripStatusLabel1.Text = _strategy.Data.Status;
00059 if (_strategy.Data.Status == "nav_init")
00060 toolStripStatusLabel1.BackColor = Color.Green;
00061 else
00062 toolStripStatusLabel1.BackColor = Color.Red;
00063 }
00064 }
00065
00066 private void launchMakeItCToolStripMenuItem_Click(object sender, EventArgs e) {
00067 System.Diagnostics.Process makeItC = new System.Diagnostics.Process();
00068 makeItC.StartInfo.FileName = "C:\\Program Files\\Coridium\\bin\\MakeItC.exe";
00069 makeItC.StartInfo.WorkingDirectory = "C:\\Program Files\\Coridium\\bin\\";
00070 makeItC.Start();
00071 System.Diagnostics.Process npp = new System.Diagnostics.Process();
00072 npp.StartInfo.FileName = "C:\\Program Files\\Notepad++\\notepad++.exe";
00073 npp.Start();
00074 }
00075
00076 private void coridiumHideButton_Click(object sender, EventArgs e) {
00077 this.Hide();
00078 }
00079
00080
00081 private void openCom5Button_Click(object sender, EventArgs e) {
00082 toolStripStatusLabel2.Text = _strategy.openPort();
00083 toolStripStatusLabel1.Text = _strategy.Data.Status;
00084 if (_strategy.Data.Status == "nav_init")
00085 toolStripStatusLabel1.BackColor = Color.Green;
00086 }
00087
00088 private void closeCom5Button_Click(object sender, EventArgs e) {
00089 toolStripStatusLabel2.Text = _strategy.closePort();
00090 toolStripStatusLabel1.BackColor = Color.Red;
00091 toolStripStatusLabel1.Text = "not initialized";
00092 }
00093
00094 private void resetCoridiumcautionToolStripMenuItem_Click(object sender, EventArgs e) {
00095 _strategy.resetNavigationController();
00096 }
00097
00098
00099 private void navigationControllerSerialPortToolStripMenuItem_Click(object sender, EventArgs e) {
00100 _strategy.showSerialPort();
00101 }
00102
00103 private void silentCheckBox_CheckedChanged(object sender, EventArgs e) {
00104 _strategy.setSilentMode(silentCheckBox.Checked);
00105 Silent = silentCheckBox.Checked;
00106 onInitialize();
00107 }
00108
00109 private void updateUi() {
00110 debugTextBox.Text = _strategy.Data.NavigationBuffer;
00111 xAccTextBox.Text = Math.Round(_strategy.Data.XAccGs, 2).ToString();
00112 yAccTextBox.Text = Math.Round(_strategy.Data.YAccGs, 2).ToString();
00113 zAccTextBox.Text = Math.Round(_strategy.Data.ZAccGs, 2).ToString();
00114 encoderCountsTextBox.Text = _strategy.Data.EncoderCounts.ToString();
00115 countRateTextBox.Text = _strategy.Data.DeltaEncoderCounts.ToString();
00116 steeringOutputTextBox.Text = _strategy.Data.Steering.ToString();
00117 throttleOutputTextBox.Text = _strategy.Data.Throttle.ToString();
00118 dataUpdateRateTextBox.Text = _strategy.WatchdogTimer.ElapsedMilliseconds.ToString();
00119 if (_strategy.Data.RunMode == 0) runMode0RadioButton.Checked = true;
00120 else if (_strategy.Data.RunMode == 1) runMode1RadioButton.Checked = true;
00121 else runMode2RadioButton.Checked = true;
00122 deadManCheckBox.Checked = _strategy.Data.DeadMan;
00123 }
00124
00125 private void clearDebugWindowButton_Click(object sender, EventArgs e) {
00126 debugTextBox.Clear();
00127 }
00128
00129 private void ledOnLedButton_Click(object sender, EventArgs e) {
00130 _strategy.writeToNavigationController(NavigationCommands.cmdArray
00131 [(int)NavigationCommands.commands.systemControllerUp]);
00132 }
00133
00134 private void flashLedButton_Click(object sender, EventArgs e) {
00135 _strategy.writeToNavigationController(NavigationCommands.cmdArray
00136 [(int)NavigationCommands.commands.errorFlashLed]);
00137 }
00138
00139 private void turnOffLedButton_Click(object sender, EventArgs e) {
00140 _strategy.writeToNavigationController(NavigationCommands.cmdArray
00141 [(int)NavigationCommands.commands.resetEncoderLedOff]);
00142 }
00143
00144 private void sendCommandEventRaised(object sender, EventArgs e) {
00145 int steeringOut = 0, throttleOut = 0;
00146 int.TryParse(steeringSetpointTextBox.Text, out steeringOut);
00147 int.TryParse(throttleSetpointTextBox.Text, out throttleOut);
00148 Control cntrl = (Control)sender;
00149 if (cntrl.Tag.ToString() == "steering minus") {
00150 steeringOut--;
00151 _strategy.setSteeringSetpoint(steeringOut);
00152 } else if (cntrl.Tag.ToString() == "steering plus") {
00153 steeringOut++;
00154 _strategy.setSteeringSetpoint(steeringOut);
00155 } else if (cntrl.Tag.ToString() == "steering set")
00156 _strategy.setSteeringSetpoint(steeringOut);
00157 else if (cntrl.Tag.ToString() == "steering setpoint")
00158 _strategy.setSteeringSetpoint(steeringOut);
00159 else if (cntrl.Tag.ToString() == "throttle minus") {
00160 throttleOut--;
00161 _strategy.setThrottleSetpoint(throttleOut);
00162 } else if (cntrl.Tag.ToString() == "throttle plus") {
00163 throttleOut++;
00164 _strategy.setThrottleSetpoint(throttleOut);
00165 } else if (cntrl.Tag.ToString() == "throttle set")
00166 _strategy.setThrottleSetpoint(throttleOut);
00167 else if (cntrl.Tag.ToString() == "throttle setpoint")
00168 _strategy.setThrottleSetpoint(throttleOut);
00169 else if (cntrl.Tag.ToString() == "mode 0")
00170 _strategy.setRunMode(0);
00171 else if (cntrl.Tag.ToString() == "mode 1")
00172 _strategy.setRunMode(1);
00173 else if (cntrl.Tag.ToString() == "mode 2")
00174 _strategy.setRunMode(2);
00175 else if (cntrl.Tag.ToString() == "dead man") {
00176 if(deadManCheckBox.Checked) _strategy.setDeadMan(1);
00177 else _strategy.setDeadMan(0);
00178 }
00179 steeringSetpointTextBox.Text = ((sbyte)steeringOut).ToString();
00180 throttleSetpointTextBox.Text = ((sbyte)throttleOut).ToString();
00181 }
00182 }
00183 }