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.Text;
00008 using System.Windows.Forms;
00009 using GpsModule;
00010 using NavigationController;
00011 using PlatformController;
00012 using VisionSystem;
00013 using UtmConvert;
00014 using CompassDial;
00015
00016 namespace system_controller {
00017 public partial class SystemControllerMainForm : Form {
00018
00019 public delegate void updateStatusEventHandler();
00020 public event updateStatusEventHandler updateStatusEvent;
00021
00022 Bitmap backupMap;
00023 SystemData _data;
00024 RaceStrategy _strategy;
00025 PointPickerForm _waypointChooser;
00026 WaypointInserter _waypointInserter;
00027 CompassDialForm compass;
00028 bool _silent = true;
00029
00030 public bool Silent {
00031 get { return _silent; }
00032 set {
00033 _silent = value;
00034 if (_silent) {
00035 _data.Navigation.updateDataEvent -= updateNavigationUi;
00036 _data.Platform.updateDataEvent -= updatePlatformUi;
00037 _data.Gps.updateDataEvent -= updateGpsUi;
00038 _strategy.updateUiEvent -= updateStrategyUi;
00039 } else {
00040 _data.Navigation.updateDataEvent += updateNavigationUi;
00041 _data.Platform.updateDataEvent += updatePlatformUi;
00042 _data.Gps.updateDataEvent += updateGpsUi;
00043 _strategy.updateUiEvent += updateStrategyUi;
00044 }
00045 }
00046 }
00047
00048 public SystemControllerMainForm() {
00049 InitializeComponent();
00050 updateStatusEvent += updateDebugTextBox;
00051 newRace();
00052 if (_data.RaceMode)
00053 _data.Platform.updateResetEvent += newRace;
00054 _data.ErrorLogger.updateStatusEvent += updateDebugTextBox;
00055 }
00056
00057 private void silentCheckBox_CheckedChanged(object sender, EventArgs e) {
00058 if (_data.RaceMode) {
00059 _data.Platform.setSilentMode(silentCheckBox.Checked);
00060 _data.Navigation.setSilentMode(silentCheckBox.Checked);
00061 _data.Gps.setSilentMode(silentCheckBox.Checked);
00062 Silent = silentCheckBox.Checked;
00063 }
00064 }
00065
00066
00067 private void updateNavigationUi() {
00068 xAccTextBox.Text = Math.Round(_data.Navigation.Data.XAccGs, 2).ToString();
00069 yAccTextBox.Text = Math.Round(_data.Navigation.Data.YAccGs,2).ToString();
00070 zAccTextBox.Text = Math.Round(_data.Navigation.Data.ZAccGs,2).ToString();
00071 encoderCountsTextBox.Text = _data.Navigation.Data.EncoderCounts.ToString();
00072 steeringTextBox.Text = _data.Navigation.Data.Steering.ToString();
00073 throttleTextBox.Text = _data.Navigation.Data.Throttle.ToString();
00074 }
00075
00076
00077 private void updatePlatformUi() {
00078 systemVoltageTextBox.Text = Math.Round(_data.Platform.Data.SystemBatteryVolts, 2).ToString();
00079 platformVoltageTextBox.Text = Math.Round(_data.Platform.Data.MicroBatteryVolts, 2).ToString();
00080 leftIrTextBox.Text = Math.Round(_data.Platform.Data.InfraredLeftCm, 2).ToString();
00081 rightIrTextBox.Text = Math.Round(_data.Platform.Data.InfraredRightCm, 2).ToString();
00082 leftBumperTextBox.Text = _data.Platform.Data.BumperLeft.ToString();
00083 rightBumperTextBox.Text = _data.Platform.Data.BumperRight.ToString();
00084 leftSonarTextBox.Text = Math.Round(_data.Platform.Data.SonarRangefinderLeftCm, 2).ToString();
00085 centerSonarTextBox.Text = Math.Round(_data.Platform.Data.SonarRangefinderCenterCm, 2).ToString();
00086 rightSonarTextBox.Text = Math.Round(_data.Platform.Data.SonarRangefinderRightCm, 2).ToString();
00087 compassHeadingTextBox.Text = Math.Round(_data.Platform.Data.CompassHeadingDouble, 2).ToString();
00088 if (compass != null && compass.Visible)
00089 compass.drawCompassDial(Math.Round(_data.Platform.Data.CompassHeadingDouble, 2));
00090 }
00091
00092
00093 private void updateGpsUi() {
00094 gpsFixTextBox.Text = _data.Gps.Gga.Data.QualityIndicator.ToString();
00095 if (_data.Gps.Gga.Data.QualityIndicator > 0) {
00096 sateliteCountTextBox.Text = _data.Gps.Gga.Data.SatelitesInView.ToString();
00097 latTextBox.Text = _data.Gps.Gga.Data.Latitude;
00098 lonTextBox.Text = _data.Gps.Gga.Data.Longitude;
00099 speedOfverGroundTextBox.Text = _data.Gps.Rmc.Data.SpeedOverGround.ToString();
00100 gpsNorthingTextBox.Text = Math.Round(_strategy.CurrentGpsLocation.Point.Y, 2).ToString();
00101 gpsEastingTextBox.Text = Math.Round(_strategy.CurrentGpsLocation.Point.X, 2).ToString();
00102 if (_data.UtmPoint2.Handle != "") {
00103 Bitmap b = new Bitmap(backupMap);
00104 _data.drawCurrentPosition(ref b, _strategy.CurrentGpsLocation, Color.Red);
00105 _data.drawCurrentPosition(ref b, _strategy.CurrentVectorLocation, Color.Blue);
00106 _data.drawCurrentPosition(ref b, _strategy.CurrentLocation, Color.Green);
00107 mapPictureBox.Image = b;
00108 mapPictureBox.Invalidate();
00109 }
00110 }
00111 }
00112
00113
00114 private void updateStrategyUi() {
00115 waypointDistanceTextBox.Text = Math.Round(_strategy.WaypointDistance, 2).ToString();
00116 thetaTextBox.Text = Math.Round(_strategy.Theta, 2).ToString();
00117 crossTrackErrorTextBox.Text = Math.Round(_strategy.CrossTrackError, 2).ToString();
00118 turnDirectionTextBox.Text = _strategy.TurnDirection;
00119 offsetCompassTextBox.Text = Math.Round(_strategy.OffsetCompassHeading, 2).ToString();
00120 totalDistanceTextBox.Text = Math.Round(_data.Navigation.Data.Meters, 2).ToString();
00121 eastingTextBox.Text = Math.Round(_strategy.CurrentVectorLocation.Point.X, 2).ToString();
00122 northingTextBox.Text = Math.Round(_strategy.CurrentVectorLocation.Point.Y, 2).ToString();
00123 }
00124
00125 private void exitToolStripMenuItem1_Click(object sender, EventArgs e) {
00126 if (_data.RaceMode) {
00127 _data.Navigation.setThrottleSetpoint(0);
00128 _data.Platform.greenLedOff();
00129 _data.Navigation.setDeadMan(1);
00130 }
00131 _data.safeShutdown();
00132 this.Close();
00133 }
00134
00135 private void updateDebugTextBox() {
00136 if (this.InvokeRequired) this.Invoke(updateStatusEvent);
00137 else debugTextBox.AppendText(_strategy.EventLog.DataString + "\r\n");
00138 }
00139
00140 private void platformToolStripMenuItem1_Click(object sender, EventArgs e) {
00141 _data.showPlatform();
00142 }
00143
00144 private void navigationToolStripMenuItem1_Click(object sender, EventArgs e) {
00145 _data.showNavigation();
00146 }
00147
00148 private void visionToolStripMenuItem1_Click(object sender, EventArgs e) {
00149 _data.showVision();
00150 }
00151
00152 private void gPSToolStripMenuItem2_Click(object sender, EventArgs e) {
00153 _data.showGps();
00154 }
00155
00156 private void uTMConvertToolStripMenuItem1_Click(object sender, EventArgs e) {
00157 _data.showUtm();
00158 }
00159
00160 private void mapPictureBox_MouseDoubleClick(object sender, MouseEventArgs e) {
00161 if (_data.UtmPoint2.Handle != "") {
00162 double mouseX = (double)e.X * ((double)(mapPictureBox.Image.Width)
00163 / (double)(mapPictureBox.Width));
00164 double mouseY = (double)e.Y * ((double)(mapPictureBox.Image.Height)
00165 / (double)(mapPictureBox.Height));
00166 double x = (double)(_data.Click1.X) / (double)(_data.ScaleX)
00167 + (mouseX - (double)_data.Click1.X) / (double)(_data.ScaleDeltaX);
00168 double y = (double)(_data.Click1.Y) / (double)(_data.ScaleY)
00169 + (mouseY - (double)_data.Click1.Y) / (double)(_data.ScaleDeltaY);
00170 UtmPoint uPt = new UtmPoint();
00171 uPt.Point.X = x;
00172 uPt.Point.Y = y;
00173 uPt.Zone = _data.Parser.UtmPointSetList[0].Points[0].Zone;
00174 _waypointInserter = new WaypointInserter(uPt
00175 , _data.Parser.UtmPointSetList[0].Points.Count);
00176 _waypointInserter.waypointInsertEvent += onWaypointInsert;
00177 _waypointInserter.Show();
00178 }
00179 }
00180
00181 private void onWaypointSelect() {
00182 if (_data.UtmPoint1.Handle == "")
00183 _data.UtmPoint1 = _waypointChooser.getSelectedPoint();
00184 else
00185 _data.UtmPoint2 = _waypointChooser.getSelectedPoint();
00186 if (_data.UtmPoint1.Handle == _data.UtmPoint2.Handle)
00187 onWaypointSelectCancel();
00188 if (_data.UtmPoint2.Handle != "") {
00189 Bitmap b = new Bitmap(mapPictureBox.Image);
00190 _data.drawRedLines(ref b);
00191 mapPictureBox.Image = b;
00192 backupMap = new Bitmap(mapPictureBox.Image);
00193 mapPictureBox.Invalidate();
00194 }
00195 }
00196
00197 private void onWaypointSelectCancel() {
00198 _data.Parser.Parse("ddd_dd");
00199 _data.Click1 = new System.Drawing.Point();
00200 _data.Click2 = new System.Drawing.Point();
00201 _data.UtmPoint1 = new UtmPoint();
00202 _data.UtmPoint2 = new UtmPoint();
00203 mapPictureBox.Image = new Bitmap(_data.MapFileName);
00204 }
00205
00206 private void mapPictureBox_MouseMove(object sender, MouseEventArgs e) {
00207 if (_data.UtmPoint2.Handle != "") {
00208 int mouseX = (int)((float)e.X * ((float)(mapPictureBox.Image.Width)
00209 / (float)(mapPictureBox.Width)));
00210 int mouseY = (int)((float)e.Y * ((float)(mapPictureBox.Image.Height)
00211 / (float)(mapPictureBox.Height)));
00212 int x = (int)((float)(_data.Click1.X) / _data.ScaleX + (float)(mouseX
00213 - _data.Click1.X) / _data.ScaleDeltaX);
00214 int y = (int)((float)(_data.Click1.Y) / _data.ScaleY + (float)(mouseY
00215 - _data.Click1.Y) / _data.ScaleDeltaY);
00216 if (SystemControllerMainForm.ActiveForm != null)
00217 toolTip1.Show(x.ToString() + ", " + y.ToString()
00218 , SystemControllerMainForm.ActiveForm
00219 , e.X + mapGroupBox.Location.X, e.Y + mapGroupBox.Location.Y);
00220 }
00221 }
00222
00223 private void mapPictureBox_MouseClick(object sender, MouseEventArgs e) {
00224 if (_data.UtmPoint2.Handle == "") {
00225 Bitmap b = new Bitmap(mapPictureBox.Image);
00226 _data.drawClickedWaypoint(ref b, (int)(e.X * ((double)(b.Width)
00227 / (double)(mapPictureBox.Width)))
00228 , (int)(e.Y * ((double)(b.Height) / (double)(mapPictureBox.Height))));
00229 mapPictureBox.Image = b;
00230 mapPictureBox.Invalidate();
00231 _waypointChooser = new PointPickerForm(_data.Parser.UtmPointSetList);
00232 _waypointChooser.waypointSelectEvent += onWaypointSelect;
00233 _waypointChooser.waypointSelectCancelEvent += onWaypointSelectCancel;
00234 _waypointChooser.Show();
00235 }
00236 }
00237
00238 private void onWaypointInsert() {
00239 _data.Parser.UtmPointSetList[0].Points.Insert(
00240 _waypointInserter.getInsertedWaypointIndex()
00241 , _waypointInserter.getInsertedWaypoint());
00242 Bitmap b = new Bitmap(_data.MapFileName);
00243 _data.drawRedLines(ref b);
00244 mapPictureBox.Image = b;
00245 mapPictureBox.Invalidate();
00246 }
00247
00248 private void newToolStripMenuItem_Click(object sender, EventArgs e) {
00249
00250 newRace();
00251 debugTextBox.Clear();
00252 }
00253
00254 public void newRace() {
00255 try {
00256 if (_data == null) _data = new SystemData();
00257 if (_data.RaceMode) {
00258 if (_strategy != null) _strategy.shutdownRace();
00259 _strategy = new RaceStrategy(ref _data
00260 , double.Parse(InitializationParameters
00261 .IniFileParameters["steering_kp"])
00262 , double.Parse(InitializationParameters
00263 .IniFileParameters["cone_search_distance"])
00264 , double.Parse(InitializationParameters
00265 .IniFileParameters["cone_distance_limit"])
00266 , double.Parse(InitializationParameters
00267 .IniFileParameters["dummy_distance_limit"])
00268 , int.Parse(InitializationParameters
00269 .IniFileParameters["minimum_speed"])
00270 , int.Parse(InitializationParameters
00271 .IniFileParameters["maximum_speed"]));
00272 _data.Navigation.setEncoderCounts(0);
00273 if (!_silent) _strategy.updateUiEvent += updateStrategyUi;
00274 }
00275 mapPictureBox.Image = new Bitmap(_data.MapFileName);
00276 _strategy.EventLog.updateStatusEvent += updateDebugTextBox;
00277 } catch (Exception e) {
00278 _data.logError(e);
00279 }
00280 }
00281
00282 private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
00283 _data.saveMap();
00284 }
00285
00286 private void lookForConeButton_Click(object sender, EventArgs e) {
00287 _strategy.prepareToLookForCone();
00288 }
00289
00290 private void compassDialToolStripMenuItem_Click(object sender, EventArgs e) {
00291 compass = new CompassDialForm();
00292 compass.Show();
00293 }
00294 }
00295 }