00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using GpsModule;
00006 using NavigationController;
00007 using PlatformController;
00008 using VisionSystem;
00009 using UtmConvert;
00010 using System.Drawing;
00011
00012 namespace system_controller {
00013 class SystemData {
00014
00015 string _waypointsFileName = "";
00016 public string WaypointsFileName {
00017 get { return _waypointsFileName; }
00018 }
00019
00020 string _mapFileName = "";
00021 public string MapFileName {
00022 get { return _mapFileName; }
00023 }
00024
00025 PlatformStrategy _platform;
00026 public PlatformStrategy Platform {
00027 get { return _platform; }
00028
00029 }
00030
00031 NavigationStrategy _navigation;
00032 public NavigationStrategy Navigation {
00033 get { return _navigation; }
00034
00035 }
00036 VisionSystemMainForm _vision;
00037
00038 public VisionSystemMainForm Vision {
00039 get { return _vision; }
00040
00041 }
00042
00043 GpsStrategy _gps;
00044 public GpsStrategy Gps {
00045 get { return _gps; }
00046
00047 }
00048
00049 UtmConvertMainForm _utm;
00050 public UtmConvertMainForm Utm {
00051 get { return _utm; }
00052
00053 }
00054
00055 ConvertLatLonUtm _convertUtm;
00056 public ConvertLatLonUtm ConvertUtm {
00057 get { return _convertUtm; }
00058 }
00059
00060 System.Drawing.Point _click1, _click2;
00061
00062 public System.Drawing.Point Click2 {
00063 get { return _click2; }
00064 set { _click2 = value; }
00065 }
00066
00067 public System.Drawing.Point Click1 {
00068 get { return _click1; }
00069 set { _click1 = value; }
00070 }
00071 UtmPoint _utmPoint1, _utmPoint2;
00072
00073 public UtmPoint UtmPoint1 {
00074 get { return _utmPoint1; }
00075 set { _utmPoint1 = value; }
00076 }
00077
00078 public UtmPoint UtmPoint2 {
00079 get { return _utmPoint2; }
00080 set { _utmPoint2 = value; }
00081 }
00082
00083 string _point1 = "", _point2 = "";
00084 public string Point1 {
00085 get { return _point1; }
00086 set { _point1 = value; }
00087 }
00088
00089 public string Point2 {
00090 get { return _point2; }
00091 set { _point2 = value; }
00092 }
00093
00094 XmlParser _parser;
00095
00096 public XmlParser Parser {
00097 get { return _parser; }
00098 set { _parser = value; }
00099 }
00100
00101 float _scaleDeltaX;
00102 public float ScaleDeltaX {
00103 get { return _scaleDeltaX; }
00104 }
00105
00106 float _scaleDeltaY;
00107 public float ScaleDeltaY {
00108 get { return _scaleDeltaY; }
00109 }
00110
00111 float _scaleX;
00112 public float ScaleX {
00113 get { return _scaleX; }
00114 }
00115
00116 float _scaleY;
00117 public float ScaleY {
00118 get { return _scaleY; }
00119 }
00120
00121 bool _raceMode = false;
00122 public bool RaceMode {
00123 get { return _raceMode; }
00124 }
00125 ErrorLogger _errorLogger;
00126 public ErrorLogger ErrorLogger {
00127 get { return _errorLogger; }
00128 set { _errorLogger = value; }
00129 }
00130
00131 public SystemData() {
00132
00133
00134 InitializationParameters.readIniParameters();
00135 _errorLogger = new ErrorLogger();
00136 _errorLogger.openFile();
00137 InitializationParameters.updateErrorEvent += logError;
00138 InitializationParameters.readIniParameters();
00139 _convertUtm = new ConvertLatLonUtm();
00140 _mapFileName = InitializationParameters
00141 .IniFileParameters["map"];
00142 _waypointsFileName = InitializationParameters
00143 .IniFileParameters["waypoints"];
00144 _raceMode = InitializationParameters
00145 .IniFileParameters["race_mode"] == "yes";
00146 _parser = new XmlParser();
00147
00148 _parser.updateErrorEvent += logError;
00149 _parser.readFile(_waypointsFileName);
00150 if (_waypointsFileName.EndsWith(".gpx"))
00151 _parser.parseGPX();
00152 else _parser.Parse("ddd_dd");
00153 _click1 = new System.Drawing.Point();
00154 _click2 = new System.Drawing.Point();
00155 _utmPoint1 = new UtmPoint();
00156 _utmPoint2 = new UtmPoint();
00157
00158
00159 }
00160
00161 public void logError(Exception error) {
00162 _errorLogger.write(DateTime.Now.ToString() + ":\r\n"
00163 + "Message: " + error.Message + "\r\n"
00164 + "Stack Trace: " + error.StackTrace + "\r\n"
00165 + "Source: " + error.Source + "\r\n");
00166 }
00167
00168 public void saveMap() {
00169 Printer p = new Printer();
00170 p.writeUtmXmlFile(_parser.UtmPointSetList);
00171 }
00172
00173 public bool waypointMatch(UtmPoint p) {
00174 if (_point2 == "")
00175 return _point1 == p.Handle;
00176 else return _point2 == p.Handle;
00177 }
00178
00179
00180 const int LINE_WIDTH = 4;
00181 const int CIRCLE_DIAMETER = 16;
00182
00183 public void drawRedLines(ref Bitmap map) {
00184 float deltaClickX = (float)_click2.X - (float)_click1.X;
00185 float deltaClickY = (float)_click2.Y - (float)_click1.Y;
00186 float deltaUtmX = (float)(_utmPoint2.Point.X - _utmPoint1.Point.X);
00187 float deltaUtmY = (float)(_utmPoint2.Point.Y - _utmPoint1.Point.Y);
00188 _scaleDeltaX = deltaClickX / deltaUtmX;
00189 _scaleDeltaY = deltaClickY / deltaUtmY;
00190 _scaleX = (float)_click1.X / (float)_utmPoint1.Point.X;
00191 _scaleY = (float)_click1.Y / (float)_utmPoint1.Point.Y;
00192 Graphics g = Graphics.FromImage(map);
00193 Pen pen = new Pen(Color.Red, 4);
00194 SolidBrush brush = new SolidBrush(Color.Orange);
00195 SolidBrush gBrush = new SolidBrush(Color.Gray);
00196 float nextX = 0, nextY = 0;
00197 for (int i = 0; i < _parser.UtmPointSetList[0].Points.Count; ++i) {
00198 float deltaX = (float)(_parser.UtmPointSetList[0].Points[i].Point.X)
00199 - (float)(_utmPoint1.Point.X);
00200 float deltaY = (float)(_parser.UtmPointSetList[0].Points[i].Point.Y)
00201 - (float)(_utmPoint1.Point.Y);
00202 float x1 = (float)(_parser.UtmPointSetList[0].Points[i].Point.X);
00203 float y1 = (float)(_parser.UtmPointSetList[0].Points[i].Point.Y);
00204 if (_parser.UtmPointSetList[0].Points[i].Note == "dummy")
00205 g.FillEllipse(gBrush, x1 * _scaleX + deltaX * _scaleDeltaX
00206 - 8, y1 * _scaleY + deltaY * _scaleDeltaY - 8, 16, 16);
00207 else
00208 g.FillEllipse(brush, x1 * _scaleX + deltaX * _scaleDeltaX
00209 - 8, y1 * _scaleY + deltaY * _scaleDeltaY - 8, 16, 16);
00210 g.DrawLine(pen, x1 * _scaleX + nextX, y1 * _scaleY + nextY
00211 , x1 * _scaleX + deltaX * _scaleDeltaX
00212 , y1 * _scaleY + deltaY * _scaleDeltaY);
00213 nextX = _scaleX + deltaX * _scaleDeltaX;
00214 nextY = _scaleY + deltaY * _scaleDeltaY;
00215 }
00216 }
00217
00218 public void drawCurrentPosition(ref Bitmap map, UtmPoint point, Color color) {
00219 float deltaClickX = (float)_click2.X - (float)_click1.X;
00220 float deltaClickY = (float)_click2.Y - (float)_click1.Y;
00221 float deltaUtmX = (float)(_utmPoint2.Point.X - _utmPoint1.Point.X);
00222 float deltaUtmY = (float)(_utmPoint2.Point.Y - _utmPoint1.Point.Y);
00223 _scaleDeltaX = deltaClickX / deltaUtmX;
00224 _scaleDeltaY = deltaClickY / deltaUtmY;
00225 _scaleX = (float)_click1.X / (float)_utmPoint1.Point.X;
00226 _scaleY = (float)_click1.Y / (float)_utmPoint1.Point.Y;
00227 Graphics g = Graphics.FromImage(map);
00228 SolidBrush brush = new SolidBrush(color);
00229 float x1 = (float)point.Point.X;
00230 float y1 = (float)point.Point.Y;
00231 float deltaX = (float)point.Point.X - (float)(_utmPoint1.Point.X);
00232 float deltaY = (float)point.Point.Y - (float)(_utmPoint1.Point.Y);
00233 x1 = x1 * _scaleX + deltaX * _scaleDeltaX - 6;
00234 y1 = y1 * _scaleY + deltaY * _scaleDeltaY - 6;
00235 g.FillEllipse(brush, x1, y1, 12, 12);
00236
00237
00238
00239 }
00240
00241 public void drawClickedWaypoint(ref Bitmap map, int x, int y) {
00242 if (_click1.IsEmpty) { _click1.X = x; _click1.Y = y; }
00243 else { _click2.X = x; _click2.Y = y; }
00244 Graphics g = Graphics.FromImage(map);
00245 SolidBrush brush = new SolidBrush(Color.Green);
00246 g.FillEllipse(brush, x - 8, y - 8, 16, 16);
00247 }
00248
00249 ~SystemData() {
00250 safeShutdown();
00251 }
00252
00253 public void showPlatform() {
00254 initPlatform();
00255 PlatformControllerMainForm p = new PlatformControllerMainForm(ref _platform);
00256 p.Show();
00257 }
00258
00259 public void initPlatform() {
00260 try {
00261 if (_platform == null) {
00262 _platform = new PlatformStrategy(
00263 InitializationParameters
00264 .IniFileParameters
00265 ["platform_controller_port_name"]
00266 , double.Parse(InitializationParameters
00267 .IniFileParameters["magnetic_declination"]));
00268 _platform.updateErrorEvent += logError;
00269 }
00270 } catch (Exception e) {
00271 logError(e);
00272 }
00273 }
00274
00275 public void showNavigation() {
00276 initNavigation();
00277 NavigationControllerMainForm p = new NavigationControllerMainForm(ref _navigation);
00278 p.Show();
00279 }
00280
00281 public void initNavigation() {
00282 try {
00283 if (_navigation == null) {
00284 _navigation = new NavigationStrategy(
00285 InitializationParameters.IniFileParameters
00286 ["navigation_controller_port_name"]
00287 , double.Parse(InitializationParameters
00288 .IniFileParameters["counts_per_meter"]));
00289 _navigation.updateErrorEvent += logError;
00290 }
00291 } catch (Exception e) {
00292 logError(e);
00293 }
00294 }
00295
00296 public void showVision() {
00297 if (_vision != null && !_vision.IsDisposed) {
00298 _vision.Show();
00299 } else {
00300 _vision = new VisionSystemMainForm(
00301 int.Parse(InitializationParameters
00302 .IniFileParameters["red"]),
00303 int.Parse(InitializationParameters
00304 .IniFileParameters["green"]),
00305 int.Parse(InitializationParameters
00306 .IniFileParameters["blue"]));
00307 _vision.updateErrorEvent += logError;
00308 _vision.Show();
00309 }
00310 }
00311
00312 public void initVision() {
00313 if (_vision == null) {
00314 _vision = new VisionSystemMainForm(
00315 int.Parse(InitializationParameters
00316 .IniFileParameters["red"]),
00317 int.Parse(InitializationParameters
00318 .IniFileParameters["green"]),
00319 int.Parse(InitializationParameters
00320 .IniFileParameters["blue"]));
00321 _vision.updateErrorEvent += logError;
00322 }
00323 }
00324
00325 public void initGps() {
00326 try {
00327 if (_gps == null) {
00328 _gps = new GpsStrategy(
00329 InitializationParameters
00330 .IniFileParameters["gps_module_port_name"]);
00331 _gps.updateErrorEvent += logError;
00332 }
00333 } catch (Exception e) {
00334 logError(e);
00335 }
00336 }
00337
00338 public void showGps() {
00339 initGps();
00340 GpsModuleMainForm g = new GpsModuleMainForm(ref _gps);
00341 g.Show();
00342 }
00343
00344 public void showUtm() {
00345 if (_utm != null && !_utm.IsDisposed) {
00346 _utm.Show();
00347 } else {
00348 _utm = new UtmConvertMainForm();
00349 _utm.Show();
00350 }
00351 }
00352
00353 public void initUtm() {
00354 if (_utm == null)
00355 _utm = new UtmConvertMainForm();
00356 }
00357
00358 public void safeShutdown() {
00359
00360
00361
00362
00363
00364 if (_gps != null) {
00365 _gps.setSilentMode(true);
00366 _gps.closePort();
00367 }
00368 if (_platform != null) {
00369 _platform.setSilentMode(true);
00370 _platform.closePort();
00371 }
00372 if (_navigation != null) {
00373 _navigation.setSilentMode(true);
00374 _navigation.closePort();
00375 }
00376 }
00377 }
00378 }