00001 using System;
00002 using System.Collections.Generic;
00003 using System.Threading;
00004 using System.Linq;
00005 using System.Text;
00006 using RS232Port;
00007
00008 namespace NavigationController {
00009 public class NavigationStrategy : SerialStrategy {
00010
00011 public event updateStatusEventHandler updateResetEvent;
00012
00013 NavigationData _data;
00014 public NavigationData Data {
00015 get { return _data; }
00016 }
00017
00018 public NavigationStrategy(string portName, double countsPerMeter) {
00019 _data = new NavigationData(countsPerMeter);
00020 serialPortName = portName;
00021 initializePort();
00022 _watchdogTimer.Start();
00023 }
00024
00025 private void initializePort() {
00026 _rs232 = new RS232MainForm();
00027 _rs232.updateErrorEvent += updateError;
00028 _rs232.receiveDataEvent += receiveData;
00029 _rs232.setSilentModeEvent += updateSilentMode;
00030 _rs232.Show();
00031 _rs232.Hide();
00032 _rs232.Port.ReceivedBytesThreshold = 1;
00033 _rs232.DebugMode = RS232MainForm.debugTerminalMode.silent;
00034 _rs232.Port.BaudRate = 19200;
00035 _rs232.Text = "Navigation Controller Serial Port";
00036 _rs232.Port.PortName = serialPortName;
00037 _rs232.updatePortSettings();
00038
00039 _rs232.RecieveDelayMs = 60;
00040
00041 _status = openPort();
00042
00043 resetNavigationController();
00044 }
00045
00046 public void resetNavigationController() {
00047 if (_rs232.Port.IsOpen) {
00048 _rs232.Port.DtrEnable = true;
00049 Thread.Sleep(100);
00050 _rs232.Port.DtrEnable = false;
00051 }
00052 if(updateResetEvent != null)
00053 updateResetEvent.Invoke();
00054 }
00055
00056 public void writeToNavigationController(NavigationCommand command) {
00057 _rs232.transmit(command.command.ToString());
00058 }
00059
00060 private void receiveData() {
00061 parsSerialData(_rs232.SerialData);
00062 updateUi();
00063 updateData();
00064 _watchdogTimer.Reset();
00065 _watchdogTimer.Start();
00066 }
00067
00068
00069
00070 public void parsSerialData(string buffer) {
00071 try {
00072 _data.NavigationBuffer = buffer;
00073 string s = "";
00074 char c = '0';
00075
00076 buffer = buffer.Remove(buffer.LastIndexOf(";") + 1
00077 , buffer.Length - (buffer.LastIndexOf(";") + 1));
00078 while (buffer.Length > 0) {
00079 s = buffer.Substring(buffer.IndexOf(":") - 1, buffer.IndexOf(";"));
00080 c = s[0];
00081 s = s.Remove(0, 2);
00082 switch (c) {
00083 case '0':
00084
00085 if (s == "nav_init") {
00086 _data.setStatus(s);
00087 updateStatus();
00088 }
00089 _rs232.Port.DiscardInBuffer();
00090 break;
00091 case 'a':
00092
00093 if (int.TryParse(s, out _outInt))
00094 _data.XAcc = _outInt;
00095 break;
00096 case 'b':
00097
00098 if (int.TryParse(s, out _outInt))
00099 _data.YAcc = _outInt;
00100 break;
00101 case 'c':
00102
00103 if (int.TryParse(s, out _outInt))
00104 _data.ZAcc = _outInt;
00105 break;
00106 case 'd':
00107 if (long.TryParse(s, out _outLong)) {
00108
00109 _data.DeltaEncoderCounts = (short)
00110 (_outLong - _data.EncoderCounts);
00111
00112 _data.EncoderCounts = _outLong;
00113 }
00114 break;
00115 case 'e':
00116
00117 if (int.TryParse(s, out _outInt))
00118 _data.SteeringPulseDuration = _outInt;
00119 break;
00120 case 'f':
00121
00122 if (int.TryParse(s, out _outInt))
00123 _data.ThrottlePulseDuration = _outInt;
00124 break;
00125
00126
00127 case 'n':
00128
00129 if (int.TryParse(s, out _outInt))
00130 _data.RunMode = _outInt;
00131 break;
00132 case 'o':
00133
00134 _data.DeadMan = s == "1";
00135 break;
00136 default:
00137 break;
00138 }
00139 buffer = buffer.Substring(buffer.IndexOf(";") + 1
00140 , buffer.Length - (buffer.IndexOf(";") + 1));
00141 }
00142 } catch (Exception e) {
00143 updateError(e);
00144 _error = e.Message + "\r\n" + e.StackTrace;
00145 }
00146 }
00147
00148 public void setSteeringSetpoint(int steering) {
00149 if (steering > 127) steering = 127;
00150 else if (steering < -127) steering = -127;
00151 sendSerialCommand('l', (byte)steering);
00152 _data.SteeringSetpoint = steering;
00153 }
00154
00155 public void setThrottleSetpoint(int throttle) {
00156 sendSerialCommand('m', (byte)throttle);
00157 _data.ThrottleSetpoint = throttle;
00158 }
00159
00160 public void setEncoderCounts(int counts) {
00161
00162 }
00163
00164 public void clearEncoderCounts() {
00165 sendSerialCommand('k', 0);
00166 }
00167
00168 public void setRunMode(int mode) {
00169 sendSerialCommand('n', (byte)mode);
00170 }
00171
00172 public void setDeadMan(int flag) {
00173 sendSerialCommand('o', (byte)flag);
00174 }
00175 }
00176 }