2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Files at this revision

API Documentation at this revision

Comitter:
shimniok
Date:
Thu Dec 13 17:35:29 2018 +0000
Parent:
15:35c40765f7c3
Child:
17:5c2af0904e87
Commit message:
Initial gps receive implementation

Changed in this revision

GPS.h Show annotated file Show diff for this revision Revisions of this file
SimpleShell.lib Show annotated file Show diff for this revision Revisions of this file
Ublox6.cpp Show annotated file Show diff for this revision Revisions of this file
Ublox6.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPS.h	Thu Dec 13 17:35:29 2018 +0000
@@ -0,0 +1,102 @@
+// For SiRF III
+
+#ifndef __GPS_H
+#define __GPS_H
+
+/**
+ * GPS "interface" class
+ */
+
+#include "mbed.h"
+
+class GPS {
+public:
+    /**
+     * create a new instance of GPS
+     */
+    GPS(void) {}
+    GPS(PinName tx, PinName rx) {}
+
+    /**
+     * delete instance of GPS
+     */
+    virtual ~GPS(void) {}
+
+    /**
+     * Initalize everything necessary for the GPS to collect the required data
+     */
+    virtual void init(void) = 0;
+
+    /**
+     * Return serial object
+     */
+    virtual Serial *getSerial(void) = 0;
+
+    /**
+     * Set baud rate
+     */
+    virtual void setBaud(int baud) = 0;
+
+    /**
+     * Disable serial data collection
+     */
+    virtual void disable(void) = 0;
+     
+    /**
+     * Enable serial data collection
+     */
+    virtual void enable(void) = 0;
+
+    /**
+     * Enable verbose messages for debugging
+     */
+    virtual void enableVerbose(void) = 0;
+    
+    /**
+     * Disable verbose messages for debugging
+     */
+    virtual void disableVerbose(void) = 0;
+   
+    /**
+     * get latitude
+     */
+    virtual double latitude(void) = 0;
+         
+    /**
+     * get longitude
+     */
+    virtual double longitude(void) = 0;
+     
+    /**
+     * Get Horizontal Dilution of Precision
+     * @return float horizontal dilution of precision
+     */
+    virtual float hdop(void) = 0;
+    
+    /**
+     * get count of active satellites
+     */
+    virtual int sat_count(void) = 0;
+    
+    /**
+     * get speed in m/s
+     */
+    virtual float speed_mps(void) = 0;
+    
+    /**
+     * get heading in degrees
+     */
+    virtual float heading_deg(void) = 0;
+    
+    /**
+     * determine if data is available to be used
+     */
+    virtual bool available(void) = 0;
+    
+    /**
+     * reset the data available flag
+     */
+    virtual void reset_available(void) = 0;
+};
+
+#endif
--- a/SimpleShell.lib	Thu Dec 13 00:30:59 2018 +0000
+++ b/SimpleShell.lib	Thu Dec 13 17:35:29 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/shimniok/code/SimpleShell/#b58450c94d32
+https://os.mbed.com/users/shimniok/code/SimpleShell/#ecf3fc049bca
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ublox6.cpp	Thu Dec 13 17:35:29 2018 +0000
@@ -0,0 +1,188 @@
+#include "mbed.h"
+#include "Ublox6.h"
+
+#define MAX_LENGTH 512
+
+#define DOP_BIT     0x01
+#define POSLLH_BIT  0x02
+#define SOL_BIT     0x04
+#define VELNED_BIT  0x08
+#define SYNC1       0xB5
+#define SYNC2       0x62
+#define POSLLH_MSG  0x02
+#define SBAS_MSG    0x32
+#define VELNED_MSG  0x12
+#define STATUS_MSG  0x03
+#define SOL_MSG     0x06
+#define DOP_MSG     0x04
+#define DGPS_MSG    0x31
+#define SVINFO_MSG  0x30
+#define CFG         0x06
+#define NAV         0x01
+#define MSG         0x01
+
+#define LONG(X)    *(int32_t *)(&data[X])
+#define ULONG(X)   *(uint32_t *)(&data[X])
+#define INT(X)     *(int16_t *)(&data[X])
+#define UINT(X)    *(uint16_t *)(&data[X])
+
+// TODO 2: abstract LED/status to some kind of pub/sub thing
+
+void Ublox6::parse(unsigned char cc)
+{
+    //unsigned char cc = buf[out++];
+    //out &= (MAX_LENGTH-1);
+    static unsigned char ck1, ck2, state, code, id, idx, length, chk1, chk2;
+    static bool checkOk;
+    static unsigned char data[MAX_LENGTH];
+
+    switch (state) {
+        case 0:    // wait for sync 1 (0xB5)
+            ck1 = ck2 = 0;
+            checkOk = false;
+            if (cc == SYNC1)
+                state++;
+            break;
+        case 1:    // wait for sync 2 (0x62)
+            if (cc == SYNC2)
+                state++;
+            else
+                state = 0;
+            break;
+        case 2:    // wait for class code
+            code = cc;
+            ck1 += cc;
+            ck2 += ck1;
+            state++;
+            break;
+        case 3:    // wait for Id
+            id = cc;
+            ck1 += cc;
+            ck2 += ck1;
+            state++;
+            break;
+        case 4:    // wait for length uint8_t 1
+            length = cc;
+            ck1 += cc;
+            ck2 += ck1;
+            state++;
+            break;
+        case 5:    // wait for length uint8_t 2
+            length |= (unsigned int) cc << 8;
+            ck1 += cc;
+            ck2 += ck1;
+            idx = 0;
+            state++;
+            if (length > MAX_LENGTH)
+                state= 0;
+            break;
+        case 6:    // wait for <length> payload uint8_ts
+            data[idx++] = cc;
+            ck1 += cc;
+            ck2 += ck1;
+            if (idx >= length) {
+                state++;
+            }
+            break;
+        case 7:    // wait for checksum 1
+            chk1 = cc;
+            state++;
+            break;
+        case 8: {  // wait for checksum 2
+            chk2 = cc;
+            checkOk = ck1 == chk1  &&  ck2 == chk2;
+            if (!checkOk) {
+                // do something...?
+            } else {
+                switch (code) {
+                    case 0x01:      // NAV-
+                        switch (id) {
+                            case POSLLH_MSG:  // NAV-POSLLH
+                                _longitude = ((float)LONG(4))/10000000.0;
+                                _latitude = ((float)LONG(8))/10000000.0;
+                                // vAcc = ULONG(24); // mm
+                                // hAcc = ULONG(20); // mm
+                                _available |= POSLLH_BIT;
+                                break;
+                            case DOP_MSG:  // NAV-DOP
+                                //gDOP = ((float) UINT(4))/100.0;
+                                //tDOP = ((float) UINT(8))/100.0;
+                                //vDOP = ((float) UINT(10))/100.0;
+                                _hdop = ((float) UINT(12))/100.0;
+                                _available |= DOP_BIT;
+                                break;
+                            case SOL_MSG:  // NAV-SOL
+                                //week = UINT(8);
+                                //pDOP = ((float) UINT(44))/ 100.0;
+                                //pAcc = ULONG(24);
+                                _sat_count = data[47];
+                                _available |= SOL_BIT;
+                                break;
+                            case VELNED_MSG:  // NAV-VELNED
+                                _speed_mps = ULONG(20)/100.0;
+                                //sAcc = ULONG(28)/100.0;
+                                _course_deg = ((float) LONG(24))/100000.0;
+                                //cAcc = ((float) LONG(32))/100000.0;
+                                _available |= VELNED_BIT;                                
+                                break;
+                            default:
+                                break;
+                        }
+                        break;
+                    case 0x05:      // ACK-
+                        switch (id) {
+                            case 0x00:  // ACK-NAK
+                                break;
+                            case 0x01:  // ACK-ACK
+                                break;
+                        }
+                        break;
+                }
+            }
+            state = 0;
+            break;
+        }
+        default:
+            break;
+    }
+}
+
+bool Ublox6::available(void)
+{
+    return (_available & (DOP_BIT|POSLLH_BIT|SOL_BIT|VELNED_BIT));
+}
+
+void Ublox6::reset_available(void)
+{
+    _available = 0;
+}
+
+double Ublox6::latitude(void)
+{
+    return _latitude;
+}
+
+double Ublox6::longitude(void)
+{
+    return _longitude;
+}
+
+float Ublox6::speed_mps(void)
+{
+    return _speed_mps;
+}
+
+float Ublox6::heading_deg(void)
+{
+    return _course_deg;
+}
+
+float Ublox6::hdop(void)
+{
+    return _hdop;
+}
+
+int Ublox6::sat_count(void)
+{
+    return _sat_count;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ublox6.h	Thu Dec 13 17:35:29 2018 +0000
@@ -0,0 +1,75 @@
+#ifndef __UBLOX6_H
+#define __UBLOX6_H
+
+/** uBlox GPS UBX Protocol Reader
+ * Parses uBlox GPS binary protocol
+ * 
+ * @author Wayne Holder; Ported to mbed by Michael Shimniok
+ */
+#include "mbed.h"
+
+class Ublox6 {
+public:
+    // TODO 3 convert this to time units
+    static const int lag=40;        // number of updater steps by which gps output lags reality
+
+    /**
+     * UBX protocol parser (Wayne Holder)
+     * @param cc is the character to parse
+     * @note stores parsed gps data in member variables and sets _available
+     * to true to indicate gps data is waiting.
+     */    
+    void parse(unsigned char cc);
+
+    /**
+     * get latitude
+     */    
+    double latitude(void);   
+    
+    /**
+     * get longitude
+     */
+    double longitude(void);
+
+    /**
+     * Get Horizontal Dilution of Precision
+     * @return float horizontal dilution of precision
+     */
+    float hdop(void);
+
+    /**
+     * get count of active satellites
+     */
+    int sat_count(void);
+
+    /**
+     * get speed in m/s
+     */    
+    float speed_mps(void);
+    
+    /**
+     * get heading in degrees
+     */
+    float heading_deg(void);
+    
+    /**
+     * determine if data is available to be used
+     */
+    bool available(void);
+    
+    /**
+     * reset the data available flag
+     */
+    void reset_available(void);
+
+private:
+    int _available;     // is data available?
+    float _latitude;        // temp storage, latitude
+    float _longitude;       // temp storage, longitude
+    float _hdop;            // horiz dilution of precision
+    float _course_deg;      // course in degrees
+    float _speed_mps;       // speed in m/s
+    int _sat_count;         // satellite count
+};
+
+#endif
--- a/main.cpp	Thu Dec 13 00:30:59 2018 +0000
+++ b/main.cpp	Thu Dec 13 17:35:29 2018 +0000
@@ -6,12 +6,14 @@
 #include "mbed.h"
 #include <stdio.h>
 #include <errno.h>
+#include "pinouts.h"
 #include "stats_report.h"
 #include "SDBlockDevice.h"
 #include "FATFileSystem.h"
 #include "SimpleShell.h"
 #include "Config.h"
 #include "Updater.h"
+#include "Ublox6.h"
 
 Serial pc(USBTX, USBRX);
 //SDBlockDevice bd(p5, p6, p7, p8); // MOSI, MISO, CLK, CS
@@ -21,6 +23,19 @@
 SimpleShell sh;
 
 DigitalOut led1(LED1);
+DigitalOut led3(LED3);
+
+void do_gps() {
+    Ublox6 ublox;
+    RawSerial s(UART1TX, UART1RX, 38400);
+    while (1) {
+        int c = s.getc();
+        //int c = 'a';
+        ublox.parse(c);
+        led3 = !led3;
+    }
+}    
+
 
 /******** SHELL COMMANDS ********/
 
@@ -101,6 +116,10 @@
     Thread updaterThread;
     updaterThread.set_priority(osPriorityRealtime);
     updaterThread.start(callback(u, &Updater::start));
+
+    printf("Starting gps...\n");
+    Thread gpsThread;
+    gpsThread.start(&do_gps);
     
 /*
     FILE *fp;