Guest User

Particle Code

a guest
Aug 5th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. // This #include statement was automatically added by the Particle IDE.
  2. #include "gp20u7_particle/gp20u7_particle.h"
  3.  
  4. #include "math.h"
  5.  
  6.  
  7. // Pin numbers for vibrating discs
  8. int vib1 = D7;
  9. int vib2 = D6;
  10. int vib3 = D5;
  11.    
  12. GP20U7 gps = GP20U7(Serial1);
  13.  
  14. // Set up a Geolocation variable to track your current location
  15. Geolocation currentLocation;
  16.  
  17. // Initialize variable to store coordinates
  18. String pos;
  19.  
  20. // Variables used to calculate the distance between two sets of coordinates
  21. int comma;      // Index of the comma in the received string
  22. String slat;    // Latitude of client as a string
  23. String slng;
  24. float flat;     // Latitude of the client as a float
  25. float flng;
  26. float flatr;    // Latitude of client in radians
  27. float flngr;
  28. double gpslat;  // Latitude of the GPS
  29. double gpslng;  
  30. float gpslatr;  // Latitude of GPS in radians
  31. float gpslngr;
  32. float latSin;   // Store value to be used in calculation
  33. float lonSin;
  34. float distKM;   // Distance values
  35. float distMI;
  36. float distFT;
  37.  
  38. void setup() {
  39.   // Start the GPS module
  40.   gps.begin();
  41.  
  42.   // Setup virtual com port for output
  43.   Serial.begin(9600);
  44.    
  45.    
  46.    // Set pins to output for the vibrating discs
  47.    pinMode(vib1, OUTPUT);
  48.    pinMode(vib2, OUTPUT);
  49.    pinMode(vib3, OUTPUT);
  50.  
  51.    
  52.    // Set vibration to low, so the vibrating discs don't vibrate at startup
  53.    digitalWrite(vib1, LOW);
  54.    digitalWrite(vib2, LOW);
  55.    digitalWrite(vib3, LOW);
  56.    
  57.    // Set variable pos that web page can access to store the GPS position
  58.    Particle.variable("pos", pos);
  59.    
  60.    // Set up buzz function for web page to access
  61.    Particle.function("buzz", buzz);
  62.    
  63.    // Set up dist function for web page to access
  64.    Particle.function("dist", dist);
  65. }
  66.  
  67. void loop() {
  68.     // If the GPS is transmitting...
  69.     if(gps.read()){
  70.         currentLocation = gps.getGeolocation();
  71.         gpslat = (currentLocation.latitude);    // Store the latitude
  72.         gpslng = (currentLocation.longitude);   // Store the longitude
  73.         String lati = String(gpslat);           // Convert latitude and longitude to strings
  74.         String lngi = String(gpslng);
  75.         // Combine coordinates into one string that can be accessed by the web page
  76.         pos = lati + "," + lngi;  
  77.         // Print to serial to check format
  78.         Serial.print("GPS Position: ");
  79.         Serial.println(pos);
  80.     }
  81. }
  82.  
  83. int dist(String coords) {
  84.         // Calculates the distance between two sets of coordinates
  85.         int comma = coords.indexOf(",");         // Finds the index of the comma separating the latitude and longitude in the string
  86.         slat = coords.substring(0,comma);        // Latitude as a string
  87.         slng = coords.substring((comma + 1),(coords.length()));  // Longitude as a string
  88.         flat = slat.toFloat();          // Convert string to float, in degrees
  89.         flng = slng.toFloat();
  90.         flatr = toRadians(flat);        // Convert client coordinates to radians
  91.         flngr = toRadians(flng);
  92.         gpslatr = toRadians(gpslat);    // Convert GPS coordinates to radians
  93.         gpslngr = toRadians(gpslng);
  94.         // Begin distance calculations using the Haversine formula
  95.         latSin = sin((flatr - gpslatr)/2);  
  96.         lonSin = sin((flngr - gpslngr)/2);
  97.         distKM = 2 * asin(sqrt((latSin*latSin) + cos(flatr) * cos(gpslatr) * (lonSin * lonSin)));
  98.         distKM = distKM * 6371;         // Multiply by the average radius of earth
  99.         distMI = distKM * 0.621371192;  // Convert from kilometers to miles
  100.         distFT = distMI * 5280;         // Convert from miles to feet
  101.         // Print information to the serial monitor to check calculations
  102.         Serial.print("String received: ");
  103.         Serial.println(coords);
  104.         Serial.println(coords.length());
  105.         Serial.print("GPS LAT: ");
  106.         Serial.println(gpslat);
  107.         Serial.print("GPS LON: ");
  108.         Serial.println(gpslng);
  109.         Serial.print("Current LAT: ");
  110.         Serial.println(flat);
  111.         Serial.print("Current LON: ");
  112.         Serial.println(flng);
  113.         Serial.print("DISTANCE IN KM: ");
  114.         Serial.println(distKM);
  115.         Serial.print("DISTANCE IN MI: ");
  116.         Serial.println(distMI);
  117.         Serial.print("DISTANCE IN FT: ");
  118.         Serial.println(distFT);
  119.         Serial.println();
  120.         // Check the distance between the coordinates
  121.         if(distFT>50) {     // Sets the range to 50 feet
  122.             buzz("buzz");   // Vibrate the motors if the GPS and the client aren't within the range
  123.             return 1;       // Return 1 if the GPS and client are not within the set range
  124.         } else {
  125.             return 0;       // Return 0 if the GPS and client are within the set range
  126.         }
  127. }
  128.  
  129. int buzz(String string) {
  130.     // Vibrates the motors when the command 'buzz' is recieved
  131.     if (string == "buzz") {
  132.         digitalWrite(vib1,HIGH);    // Turn on the motors
  133.         digitalWrite(vib2,HIGH);
  134.         digitalWrite(vib3,HIGH);
  135.         delay(1000);                // Wait one second
  136.         digitalWrite(vib1,LOW);     // Turn off the motors
  137.         digitalWrite(vib2,LOW);
  138.         digitalWrite(vib3,LOW);
  139.         Serial.println("buzz");     // Print 'buzz' to make sure the command went through
  140.         return 1;
  141.     }
  142.     else {                          // If a different string was received...
  143.         Serial.println("NOT BUZZ"); // Can add new strings to perform different functions
  144.         return 0;
  145.     }
  146. }
  147.    
  148. float toRadians(float deg) {
  149.     // Convert degrees to radians
  150.     return (3.141592653589793238462643383279502884197169399375105820974944592307816406286/180 ) * deg; // There is no preset value for pi
  151. }
Add Comment
Please, Sign In to add comment