00001 using System;
00002 using System.Collections;
00003 using System.Collections.Generic;
00004 using System.Diagnostics;
00005 using System.Runtime.InteropServices;
00006 using System.Threading;
00007 using System.Linq;
00008 using System.Text;
00009 using RS232Port;
00010
00011 namespace PlatformController {
00012 public class PlatformStrategy : SerialStrategy {
00013
00014 public event updateStatusEventHandler updateResetEvent;
00015
00016 PlatformData _data;
00017 public PlatformData Data {
00018 get { return _data; }
00019 set { _data = value; }
00020 }
00021
00022 int _manualThrottle = 0;
00023 public int ManualThrottle {
00024 get { return _manualThrottle; }
00025 set { _manualThrottle = value; }
00026 }
00027
00028 int _manualSteering = 0;
00029 public int ManualSteering {
00030 get { return _manualSteering; }
00031 set { _manualSteering = value; }
00032 }
00033
00034 public PlatformStrategy(string platformControllerPortName
00035 , double magneticDeclination) {
00036 serialPortName = platformControllerPortName;
00037 _data = new PlatformData(magneticDeclination);
00038 initializePort();
00039 resetPlatformController();
00040 }
00041
00042 public void initializePort() {
00043 if (_rs232 == null) {
00044 _rs232 = new RS232MainForm();
00045 _rs232.updateErrorEvent += updateError;
00046 _rs232.receiveDataEvent += receiveData;
00047 _rs232.setSilentModeEvent += updateSilentMode;
00048 _rs232.Show();
00049 _rs232.Hide();
00050
00051 _rs232.Port.ReceivedBytesThreshold = 1;
00052 _rs232.DebugMode = RS232MainForm.debugTerminalMode.silent;
00053 _rs232.Port.BaudRate = 19200;
00054 _rs232.Text = "Platform Controller Serial Port";
00055 _rs232.Port.PortName = serialPortName;
00056 _rs232.updatePortSettings();
00057
00058 _rs232.RecieveDelayMs = 65;
00059
00060 _status = openPort();
00061 }
00062 }
00063
00064 public void resetPlatformController() {
00065 if (_rs232.Port.IsOpen) {
00066 _rs232.Port.DtrEnable = true;
00067 Thread.Sleep(100);
00068 _rs232.Port.DtrEnable = false;
00069 }
00070 }
00071
00072 public void writeToPlatformController(PlatformCommand command, byte argument) {
00073 _rs232.transmit(command.command.ToString() + argument.ToString());
00074 }
00075
00076 private void receiveData() {
00077 if (_rs232.SerialData.Length > 0) {
00078 parsSerialData(_rs232.SerialData);
00079 updateUi();
00080 updateData();
00081 _watchdogTimer.Reset();
00082 _watchdogTimer.Start();
00083 }
00084 }
00085
00086
00087
00088 public void parsSerialData(string buffer) {
00089 try {
00090 _data.setBuffer(buffer);
00091 string s = "";
00092 char c = '0';
00093
00094 buffer = buffer.Remove(buffer.LastIndexOf(";") + 1
00095 , buffer.Length - (buffer.LastIndexOf(";") + 1));
00096 while (buffer.Contains(":") && buffer.Contains(";")) {
00097 s = buffer.Substring(buffer.IndexOf(":") - 1, buffer.IndexOf(";"));
00098 c = s[0];
00099 s = s.Remove(0, 2);
00100 switch (c) {
00101 case '0':
00102
00103 if (s == "plat_init") {
00104 redLedOff();
00105 _data.setStatus(s);
00106 updateStatus();
00107 }
00108 _rs232.Port.DiscardInBuffer();
00109 break;
00110 case 'a':
00111
00112 if (int.TryParse(s, out _outInt))
00113 _data.SystemBatteryRaw = _outInt;
00114 break;
00115 case 'b':
00116
00117 if (int.TryParse(s, out _outInt))
00118 _data.MicroBatteryRaw = _outInt;
00119 break;
00120 case 'c':
00121
00122 if (int.TryParse(s, out _outInt))
00123 _data.InfraredLeftRaw = _outInt;
00124 break;
00125 case 'd':
00126
00127 if (int.TryParse(s, out _outInt))
00128 _data.InfraredRightRaw = _outInt;
00129 break;
00130 case 'e':
00131
00132 if (int.TryParse(s, out _outInt))
00133 _data.SonarRangefinderLeft = _outInt;
00134 break;
00135 case 'f':
00136
00137 if (int.TryParse(s, out _outInt))
00138 _data.SonarRangefinderCenter = _outInt;
00139 break;
00140 case 'g':
00141
00142 if (int.TryParse(s, out _outInt))
00143 _data.SonarRangefinderRight = _outInt;
00144 break;
00145 case 'h':
00146
00147 _data.BumperLeft = (s == "1");
00148 break;
00149 case 'i':
00150
00151 _data.BumperRight = (s == "1");
00152 break;
00153 case 'j':
00154
00155 _data.ResetSwitch = (s == "0");
00156 if (updateResetEvent != null && _data.ResetSwitch == true)
00157 updateResetEvent.Invoke();
00158 break;
00159 case 'k':
00160
00161 _data.RadioControlModeSwitch = (s != "0");
00162 break;
00163 case 'l':
00164
00165 _data.RedLedValue = (s == "0");
00166 break;
00167 case 'm':
00168
00169 _data.GreenLedValue = (s == "0");
00170 break;
00171 case 'n':
00172
00173 if (int.TryParse(s, out _outInt))
00174 _data.SteeringCalculatedValue = _outInt;
00175 break;
00176 case 'o':
00177
00178 if (int.TryParse(s, out _outInt))
00179 _data.ThrottleCalculatedValue = _outInt;
00180 break;
00181 case 'p':
00182
00183 _data.AutoAvoiding = (s == "1");
00184 break;
00185 case 'q':
00186
00187 if (int.TryParse(s, out _outInt)) {
00188 if (_outInt <= 35990 && _outInt >= 0)
00189 _data.CompassHeading = _outInt;
00190 }
00191 break;
00192 default:
00193 break;
00194 }
00195 buffer = buffer.Substring(buffer.IndexOf(";") + 1
00196 , buffer.Length - (buffer.IndexOf(";") + 1));
00197 }
00198 } catch (Exception e) {
00199 updateError(e);
00200 _error = e.Message + "\r\n" + e.StackTrace;
00201 }
00202 }
00203
00204 public void redLedOn() {
00205 sendSerialCommand('l', 1);
00206 }
00207
00208 public void redLedOff() {
00209 sendSerialCommand('l', 0);
00210 }
00211
00212 public void greenLedOn() {
00213 sendSerialCommand('m', 1);
00214 }
00215
00216 public void greenLedOff() {
00217 sendSerialCommand('m', 0);
00218 }
00219
00220
00221 }
00222 }