peony3000

Gadgeteer_Compass_GPS_Pointer_02

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