Advertisement
Guest User

erek / Autonomous Lander (Stubbed Out)

a guest
Oct 14th, 2015
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.55 KB | None | 0 0
  1. // Teensy 3.2 32-bit Micro-controller-based Project
  2. // by Erik (2015)
  3. // Test code for Adafruit GPS modules using MTK3329/MTK3339 driver / BNO055 Bosch / Servo Motor Shield
  4. //
  5. // This code shows how to listen to the GPS module in an interrupt
  6. // which allows the program to have more 'freedom' - just parse
  7. // when a new NMEA sentence is available! Then access data when
  8. // desired.
  9. //
  10. // Tested and works great with the Adafruit Ultimate GPS module
  11. // using MTK33x9 chipset
  12.  
  13. #include <Servo.h>
  14. #include <PID_v1.h>
  15. #include <Adafruit_GPS.h>  /*  Modified in Adafruit_GPS.cpp :  "isDigit()" -> "isdigit()" and "isAlpha()" -> "isalpha()" cases to work with Teensy compiling.  Erik  */
  16. #include <Wire.h>
  17. #include <Adafruit_Sensor.h>
  18. #include <Adafruit_BNO055.h>
  19. #include <utility/imumaths.h>
  20. #include <stdlib.h>
  21.  
  22. /* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
  23.    which provides a common 'type' for sensor data and some helper functions.
  24.  
  25.    To use this driver you will also need to download the Adafruit_Sensor
  26.    library and include it in your libraries folder.
  27.  
  28.    You should also assign a unique ID to this sensor for use with
  29.    the Adafruit Sensor API so that you can identify this particular
  30.    sensor in any data logs, etc.  To assign a unique ID, simply
  31.    provide an appropriate value in the constructor below (12345
  32.    is used by default in this example).
  33.  
  34.    Connections
  35.    ===========
  36.    Connect SCL to analog 5
  37.    Connect SDA to analog 4
  38.    Connect VDD to 3-5V DC
  39.    Connect GROUND to common ground
  40.  
  41.    History
  42.    =======
  43.    2015/MAR/03  - First release (KTOWN)
  44.    2015/AUG/27  - Added calibration and system status helpers
  45. */
  46.  
  47. /* Set the delay between fresh samples */
  48. #define BNO055_SAMPLERATE_DELAY_MS (100)
  49.  
  50. Adafruit_BNO055 bno = Adafruit_BNO055(55);
  51.  
  52.  
  53. Servo myservo;  // create servo object to control a servo
  54.                 // twelve servo objects can be created on most boards
  55.  
  56. int pos = 0;    // variable to store the servo position
  57.  
  58.  
  59. /**************************************************************************/
  60. /*
  61.     Displays some basic information on this sensor from the unified
  62.     sensor API sensor_t type (see Adafruit_Sensor for more information)
  63. */
  64. /**************************************************************************/
  65. void displaySensorDetails(void)
  66. {
  67.   sensor_t sensor;
  68.   bno.getSensor(&sensor);
  69.   Serial.println("------------------------------------");
  70.   Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  71.   Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  72.   Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  73.   Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" xxx");
  74.   Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" xxx");
  75.   Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" xxx");
  76.   Serial.println("------------------------------------");
  77.   Serial.println("");
  78.   delay(500);
  79. }
  80.  
  81. /**************************************************************************/
  82. /*
  83.     Display some basic info about the sensor status
  84. */
  85. /**************************************************************************/
  86. void displaySensorStatus(void)
  87. {
  88.   /* Get the system status values (mostly for debugging purposes) */
  89.   uint8_t system_status, self_test_results, system_error;
  90.   system_status = self_test_results = system_error = 0;
  91.   bno.getSystemStatus(&system_status, &self_test_results, &system_error);
  92.  
  93.   /* Display the results in the Serial Monitor */
  94.   Serial.println("");
  95.   Serial.print("System Status: 0x");
  96.   Serial.println(system_status, HEX);
  97.   Serial.print("Self Test:     0x");
  98.   Serial.println(self_test_results, HEX);
  99.   Serial.print("System Error:  0x");
  100.   Serial.println(system_error, HEX);
  101.   Serial.println("");
  102.   delay(500);
  103. }
  104.  
  105. /**************************************************************************/
  106. /*
  107.     Display sensor calibration status
  108. */
  109. /**************************************************************************/
  110. void displayCalStatus(void)
  111. {
  112.   /* Get the four calibration values (0..3) */
  113.   /* Any sensor data reporting 0 should be ignored, */
  114.   /* 3 means 'fully calibrated" */
  115.   uint8_t system, gyro, accel, mag;
  116.   system = gyro = accel = mag = 0;
  117.   bno.getCalibration(&system, &gyro, &accel, &mag);
  118.  
  119.   /* The data should be ignored until the system calibration is > 0 */
  120.   Serial.print("\t");
  121.   if (!system)
  122.   {
  123.     Serial.print("! ");
  124.   }
  125.  
  126.   /* Display the individual values */
  127.   Serial.print("Sys:");
  128.   Serial.print(system, DEC);
  129.   Serial.print(" G:");
  130.   Serial.print(gyro, DEC);
  131.   Serial.print(" A:");
  132.   Serial.print(accel, DEC);
  133.   Serial.print(" M:");
  134.   Serial.print(mag, DEC);
  135. }
  136. // If you're using a GPS module:
  137. // Connect the GPS Power pin to 5V
  138. // Connect the GPS Ground pin to ground
  139. // If using software serial (sketch example default):
  140. //   Connect the GPS TX (transmit) pin to Digital 3
  141. //   Connect the GPS RX (receive) pin to Digital 2
  142. // If using hardware serial (e.g. Arduino Mega):
  143. //   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
  144. //   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3
  145.  
  146. // If you're using the Adafruit GPS shield, change
  147. // SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);
  148. // and make sure the switch is set to SoftSerial
  149.  
  150. // If using software serial, keep this line enabled
  151. // (you can change the pin numbers to match your wiring):
  152. //SoftwareSerial mySerial(3, 2);
  153.  
  154. // If using hardware serial (e.g. Arduino Mega), comment out the
  155. // above SoftwareSerial line, and enable this line instead
  156. // (you can change the Serial number to match your wiring):
  157.  
  158. HardwareSerial mySerial = Serial1;
  159.  
  160.  
  161. Adafruit_GPS GPS(&mySerial);
  162.  
  163.  
  164. // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
  165. // Set to 'true' if you want to debug and listen to the raw GPS sentences.
  166. #define GPSECHO  true
  167.  
  168. // this keeps track of whether we're using the interrupt
  169. // off by default!
  170. //boolean usingInterrupt = false;
  171. //void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
  172.  
  173. void setup()  
  174. {
  175.  
  176.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  177.      
  178.   // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  179.   // also spit it out
  180.   Serial.begin(115200);
  181.   Serial.println("Adafruit GPS library basic test!");
  182.  
  183.   // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  184.   GPS.begin(9600);
  185.  
  186.   // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  187.   GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  188.   // uncomment this line to turn on only the "minimum recommended" data
  189.   //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  190.   // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  191.   // the parser doesn't care about other sentences at this time
  192.  
  193.   // Set the update rate
  194.   GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  195.   // For the parsing code to work nicely and have time to sort thru the data, and
  196.   // print it out we don't suggest using anything higher than 1 Hz
  197.  
  198.   // Request updates on antenna status, comment out to keep quiet
  199.   GPS.sendCommand(PGCMD_ANTENNA);
  200.  
  201.   // the nice thing about this code is you can have a timer0 interrupt go off
  202.   // every 1 millisecond, and read data from the GPS for you. that makes the
  203.   // loop code a heck of a lot easier!
  204. //  useInterrupt(true);
  205.  
  206.   delay(1000);
  207.   // Ask for firmware version
  208.   mySerial.println(PMTK_Q_RELEASE);
  209.  
  210.  
  211.   /* Initialise the sensor */
  212.   if(!bno.begin())
  213.   {
  214.     /* There was a problem detecting the BNO055 ... check your connections */
  215.     Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
  216.     while(1);
  217.   }
  218.  
  219.   delay(1000);
  220.  
  221.   /* Display some basic information on this sensor */
  222.   displaySensorDetails();
  223.  
  224.   /* Optional: Display current status */
  225.   displaySensorStatus();
  226.  
  227.   bno.setExtCrystalUse(true);
  228. }
  229. /*
  230.  
  231. // Interrupt is called once a millisecond, looks for any new GPS data, and stores it
  232. SIGNAL(TIMER0_COMPA_vect) {
  233.   char c = GPS.read();
  234.   // if you want to debug, this is a good time to do it!
  235. #ifdef UDR0
  236.   if (GPSECHO)
  237.     if (c) UDR0 = c;  
  238.     // writing direct to UDR0 is much much faster than Serial.print
  239.     // but only one character can be written at a time.
  240. #endif
  241. }
  242.  
  243. void useInterrupt(boolean v) {
  244.   if (v) {
  245.     // Timer0 is already used for millis() - we'll just interrupt somewhere
  246.     // in the middle and call the "Compare A" function above
  247.     OCR0A = 0xAF;
  248.     TIMSK0 |= _BV(OCIE0A);
  249.     usingInterrupt = true;
  250.   } else {
  251.     // do not call the interrupt function COMPA anymore
  252.     TIMSK0 &= ~_BV(OCIE0A);
  253.     usingInterrupt = false;
  254.   }
  255. }
  256. */
  257. uint32_t timer = millis();
  258. void loop()                     // run over and over again
  259. {
  260.   // in case you are not using the interrupt above, you'll
  261.   // need to 'hand query' the GPS, not suggested :(
  262. //  if (! usingInterrupt) {
  263.     // read data from the GPS in the 'main loop'
  264.     char c = GPS.read();
  265.     // if you want to debug, this is a good time to do it!
  266.     if (GPSECHO)
  267.       if (c) Serial.print(c);
  268.   //}
  269.  
  270.   // if a sentence is received, we can check the checksum, parse it...
  271.   if (GPS.newNMEAreceived()) {
  272.     // a tricky thing here is if we print the NMEA sentence, or data
  273.     // we end up not listening and catching other sentences!
  274.     // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
  275.     //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false
  276.  
  277.     if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
  278.       return;  // we can fail to parse a sentence in which case we should just wait for another
  279.   }
  280.  
  281.   // if millis() or timer wraps around, we'll just reset it
  282.   if (timer > millis())  timer = millis();
  283.  
  284.   // approximately every 2 seconds or so, print out the current stats
  285.   if (millis() - timer > 2000) {
  286.     timer = millis(); // reset the timer
  287.    
  288.     Serial.print("\nTime: ");
  289.     Serial.print(GPS.hour, DEC); Serial.print(':');
  290.     Serial.print(GPS.minute, DEC); Serial.print(':');
  291.     Serial.print(GPS.seconds, DEC); Serial.print('.');
  292.     Serial.println(GPS.milliseconds);
  293.     Serial.print("Date: ");
  294.     Serial.print(GPS.day, DEC); Serial.print('/');
  295.     Serial.print(GPS.month, DEC); Serial.print("/20");
  296.     Serial.println(GPS.year, DEC);
  297.     Serial.print("Fix: "); Serial.print((int)GPS.fix);
  298.     Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
  299.     if (GPS.fix) {
  300.       Serial.print("Location: ");
  301.       Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
  302.       Serial.print(", ");
  303.       Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
  304.       Serial.print("Location (in degrees, works with Google Maps): ");
  305.       Serial.print(GPS.latitudeDegrees, 4);
  306.       Serial.print(", ");
  307.       Serial.println(GPS.longitudeDegrees, 4);
  308.      
  309.       Serial.print("Speed (knots): "); Serial.println(GPS.speed);
  310.       Serial.print("Angle: "); Serial.println(GPS.angle);
  311.       Serial.print("Altitude: "); Serial.println(GPS.altitude);
  312.       Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
  313.     }
  314.   }
  315.  
  316.   /* Get a new sensor event */
  317.   sensors_event_t event;
  318.   bno.getEvent(&event);
  319.  
  320.   /* Display the floating point data */
  321.   Serial.print("X: ");
  322.   Serial.print(event.orientation.x, 4);
  323.   Serial.print("\tY: ");
  324.   Serial.print(event.orientation.y, 4);
  325.   Serial.print("\tZ: ");
  326.   Serial.print(event.orientation.z, 4);
  327.  
  328.   /* Optional: Display calibration status */
  329.   displayCalStatus();
  330.  
  331.   /* Optional: Display sensor status (debug only) */
  332.   //displaySensorStatus();
  333.  
  334.   /* New line for the next sample */
  335.   Serial.println("");
  336.  
  337.   /* Wait the specified delay before requesting nex data */
  338.   delay(BNO055_SAMPLERATE_DELAY_MS);
  339.  
  340.  
  341.   /*  Servo Motion
  342.    *  
  343.    *   for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
  344.   {                                  // in steps of 1 degree
  345.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  346.     delay(15);                       // waits 15ms for the servo to reach the position
  347.   }
  348.   for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees
  349.   {                                
  350.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  351.     delay(15);                       // waits 15ms for the servo to reach the position
  352.   }
  353.  
  354.   */
  355.    
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement