peony3000

Gadgeteer SatNav Simple

Apr 18th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Microsoft.SPOT;
  4. using Microsoft.SPOT.Presentation;
  5. using Microsoft.SPOT.Presentation.Controls;
  6. using Microsoft.SPOT.Presentation.Media;
  7. using Microsoft.SPOT.Touch;
  8.  
  9. using Gadgeteer.Networking;
  10. using GT = Gadgeteer;
  11. using GTM = Gadgeteer.Modules;
  12. using Gadgeteer.Modules.Seeed;
  13. using Gadgeteer.Modules.GHIElectronics;
  14. using GHIElectronics.NETMF.System;
  15.  
  16. namespace Gadgeteer_Pointer_01
  17. {
  18.     public partial class Program
  19.     {
  20.        
  21.         GT.Timer timer = new GT.Timer(2000, GT.Timer.BehaviorType.RunContinuously);
  22.         bool gpsPositionHasBeenReceived = false;
  23.         bool compassDirectionHasBeenRecieved = false;
  24.         GTM.Seeed.Compass.SensorData compassSensorData;
  25.         GT.Color displayColor = GT.Color.White;
  26.         double targetDirection;
  27.         double compassAngle;
  28.         // risd
  29.         //double targetLatitude = 41.823259499999992;
  30.         //double targetLongitude = -71.406410000000008;
  31.         // harvard 42.366788,-71.124008
  32.         //double targetLatitude = 42.366788;
  33.         //double targetLongitude = -71.124008;
  34.         // microsoft boston 42.361794,-71.08135
  35.         double targetLatitude = 42.361794;
  36.         double targetLongitude = -71.08135;
  37.         //
  38.         uint xPos;
  39.         uint yPos;
  40.  
  41.         void ProgramStarted()
  42.         {
  43.             //gps.InvalidPositionReceived += new GTM.Seeed.GPS.InvalidPositionReceivedHandler(gps_InvalidPositionReceived);
  44.             gps.PositionReceived += new GTM.Seeed.GPS.PositionReceivedHandler(gps_PositionReceived);
  45.             //gps.NMEASentenceReceived += new GPS.NMEASentenceReceivedHandler(gps_NMEASentenceReceived);
  46.  
  47.             // setup a compass reading robot event watcher
  48.             compass.MeasurementComplete += new GTM.Seeed.Compass.MeasurementCompleteEventHandler(compass_MeasurementComplete);
  49.             //compass.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 0, 500);
  50.             //compass.StartContinuousMeasurements();
  51.            
  52.             oledDisplay.SimpleGraphics.AutoRedraw = false;
  53.  
  54.             // create a timer robot
  55.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  56.             timer.Start();
  57.  
  58.             // tell everyone that you have started
  59.             Debug.Print("Program Started");
  60.         }
  61.  
  62.         void timer_Tick(GT.Timer timer)
  63.         {
  64.             //Debug.Print("GPS last position " + gps.LastPosition + " age " + gps.LastValidPositionAge);
  65.             compass.RequestMeasurement();
  66.  
  67.                
  68.             if (gpsPositionHasBeenReceived && compassDirectionHasBeenRecieved)
  69.             {
  70.                 displayColor = GT.Color.Gray;
  71.                 compassAngle = ((compassSensorData.Angle / 360d) * (2d * MathEx.PI));
  72.                 //radians
  73.                 // should be abs?
  74.                 targetDirection = (MathEx.Atan2(targetLatitude - gps.LastPosition.Latitude, targetLongitude - gps.LastPosition.Longitude));
  75.                 //targetDirection -= (MathEx.PI * 1.5);
  76.                 xPos = 64u + (uint)(MathEx.Cos(compassAngle - targetDirection + MathEx.PI) * 60d);
  77.                 yPos = 64u + (uint)(MathEx.Sin(compassAngle - targetDirection + MathEx.PI) * 60d);
  78.                
  79.  
  80.                 oledDisplay.SimpleGraphics.Clear();
  81.                 printOledDisplay(0, "GPS Last Position:");
  82.                 printOledDisplay(1, "Lat  :" + gps.LastPosition.Latitude.ToString().Substring(0, 5));
  83.                 printOledDisplay(2, "Long :" + gps.LastPosition.Longitude.ToString().Substring(0, 5));
  84.                 printOledDisplay(3, "Compass Reading");
  85.                 printOledDisplay(4, compassAngle.ToString().Substring(0, 5));
  86.                 printOledDisplay(5, "Target Direction");
  87.                 printOledDisplay(6, targetDirection.ToString().Substring(0, 5));
  88.                 drawCompass();
  89.                 clearOledDisplay();
  90.             }
  91.             else if (compassDirectionHasBeenRecieved)
  92.             {
  93.                 compassAngle = ((compassSensorData.Angle / 360d) * (2d * MathEx.PI));
  94.                
  95.                 oledDisplay.SimpleGraphics.Clear();
  96.                 printOledDisplay(0, "GPS Last Position:");
  97.                 printOledDisplay(1, "UNKNOWN");
  98.                 printOledDisplay(2, "Connecting...");
  99.                 printOledDisplay(3, "Compass Reading");
  100.                 // what if not a long double???
  101.                 printOledDisplay(4, compassAngle.ToString().Substring(0, 5));
  102.                 printOledDisplay(5, "Target Direction");
  103.                 printOledDisplay(6, "UNKNOWN");
  104.                 clearOledDisplay();
  105.             }
  106.             else
  107.             {
  108.                 oledDisplay.SimpleGraphics.Clear();
  109.                 printOledDisplay(0, "Waiting...");
  110.                
  111.                 clearOledDisplay();
  112.             }
  113.         }
  114.  
  115.         void clearOledDisplay()
  116.         {
  117.             //oledDisplay.SimpleGraphics.Clear();
  118.             oledDisplay.SimpleGraphics.Redraw();
  119.         }
  120.  
  121.         void drawCompass()
  122.         {
  123.             oledDisplay.SimpleGraphics.DisplayRectangle(GT.Color.Red, 0, GT.Color.Red, 64, 64, 5, 5);
  124.             //oledDisplay.SimpleGraphics.DisplayRectangle(GT.Color.Red, 0, GT.Color.Red, ((xPos - 64) / 2) + 64, ((yPos - 64) / 2) + 64, 5, 5);
  125.             oledDisplay.SimpleGraphics.DisplayRectangle(GT.Color.Red, 0, GT.Color.Red, xPos, yPos, 5, 5);
  126.         }
  127.  
  128.         void printOledDisplay(uint lineNumber, string displayMessage)
  129.         {
  130.             uint xPos = 0;
  131.             oledDisplay.SimpleGraphics.DisplayText(displayMessage, Resources.GetFont(Resources.FontResources.NinaB), displayColor, xPos, lineNumber * 12);
  132.            
  133.         }
  134.  
  135.         void compass_MeasurementComplete(GTM.Seeed.Compass sender, GTM.Seeed.Compass.SensorData sensorData)
  136.         {
  137.             compassSensorData = sensorData;
  138.             compassDirectionHasBeenRecieved = true;
  139.             //Debug.Print(sensorData.ToString());
  140.         }
  141.  
  142.         void gps_InvalidPositionReceived(GTM.Seeed.GPS sender)
  143.         {
  144.             //Debug.Print("Invalid position");
  145.         }
  146.  
  147.         void gps_PositionReceived(GTM.Seeed.GPS sender, GTM.Seeed.GPS.Position position)
  148.         {
  149.             gpsPositionHasBeenReceived = true;
  150.             //Debug.Print(position);
  151.         }
  152.  
  153.         void gps_NMEASentenceReceived(GPS sender, string nmeaSentence)
  154.         {
  155.             if (!gpsPositionHasBeenReceived)
  156.             {
  157.                 //Debug.Print("NMEA Sentence: " + nmeaSentence);
  158.             }
  159.         }
  160.  
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment