keesp
Published © Apache-2.0

Day 2: Adding the GPS Unit

Day 2 of the Aquabots Autnomous Vessels Project: Adding GPS.

EasyFull instructions provided47
Day 2: Adding the GPS Unit

Things used in this project

Hardware components

Seeed Grove-GPS
×1
Seeed Grove Blue Wrapper Pack 1-2
×1
Adafruit GPS Antenna
optional, but highly recommended for better performance
×1
Adafruit SMA to uFL/u.FL/IPX/IPEX RF Adapter Cable
×1
Base Shield V2
Seeed Base Shield V2
×1

Software apps and online services

Aquabots Client

Story

Read more

Code

TinyGPS.c

C/C++
Implementation of the TinyGPS module
TinyGPS::TinyGPS() {};

//Repeatedly feed it characters from your
void TinyGPS::setup( ) {
  Serial1.begin(9600);
  Serial.println(F("GPS INITIALISED"));
  latitude = -1;
  longitude = -1;
  bearing = 0;
  speed = 0;
  complete = false;
}

double TinyGPS::getBearing() {
  return bearing;
}

double TinyGPS::getLatitude() {
  return latitude;
}

double TinyGPS::getLongitude() {
  return longitude;
}

bool TinyGPS::wait() {
  return complete;
}

double TinyGPS::getBearing( double latFrom,  double lonFrom, double latTo, double lonTo ) {
  return gps.courseTo( latFrom, lonFrom, latTo, lonTo );
}

double TinyGPS::getDistance( double latFrom, double lonFrom, double latTo, double lonTo ) {
  return gps.distanceBetween( latFrom, lonFrom, latTo, lonTo );
}

void TinyGPS::loop() {
  Serial.println(F("CHECKING GPS"));
  bool updated = false;
  unsigned long current = millis();

  while (Serial1.available() && ( millis() < ( current + TIME_OUT  ))) {
    char chr = Serial1.read();
    gps.encode( chr );
    updated = gps.location.isUpdated();
    if ( updated ) {

      latitude = gps.location.lat();
      longitude = gps.location.lng();
      bearing = gps.course.deg();
      speed = gps.speed.mps();
      Serial.print(F("\n\nGPS Location Updated: "));
      Serial.print( latitude, 6 ); Serial.print("E ");
      Serial.print( longitude, 6 ), Serial.println("N\n\n "); // bearing, speed );
      break;
    }
  }
}

Vessels

C/C++
This contains the modifications to the main programme, so that the vessel will not only register itself, but also relay its position
#include <ArduinoJson.h>

#include "WebClient.h"
#include "Registration.h"
#include "TinyGPS.h"

#define VESSEL F("AquaBoat")
#define PASSPHRASE F("AquaPassphrase")
#define LATITUDE 51.2
#define LONGITUDE 4.2
#define TIME_OUT 300 //msec

SoftwareSerial Serial1(2, 3); // RX, TX
WebClient webClient;
Registration registration;
TinyGPS tinyGPS;

void setup() {
  Serial.begin(9600);
  Serial.print(F("Setup Vessel: ")); Serial.println( VESSEL );
  webClient.setup();
  registration.setup();
  tinyGPS.setup();
}

void loop() {
  tinyGPS.loop();
  long vesselId =  registration.registerVessel( VESSEL, PASSPHRASE, tinyGPS.getLatitude(), tinyGPS.getLongitude() );
  if ( vesselId >= 0 )
    Serial.print(F("VESSEL: ")); Serial.println( vesselId );
  delay(1000);
}

TinGPS.h

C Header File
The header files for the TinyGPS module
#ifndef TinyGPS_h
#define TinyGPS_h

#define TINY_GPS "TinyGPS"
#define TINY_GPS_ID "gps.tiny"

#include <SoftwareSerial.h>
#include <TinyGPS++.h>

class TinyGPS {

  private:
    bool complete; //general purpose flag for reading nmea sentences
    String sentence;
    double bearing;
    double speed;
    double latitude;
    double longitude;
    TinyGPSPlus gps;
 
  public: TinyGPS(void);
    void setup();
    double getBearing();
    double getLatitude();
    double getLongitude();
    double getBearing( double latFrom, double lonFrom, double latTo, double lonTo );
    double getDistance( double latFrom, double lonFrom, double latTo, double lonTo );
    bool wait();//wait for processing of the nmea sentence
    void loop();
};

#endif

Credits

keesp

keesp

0 projects • 1 follower

Comments

Add projectSign up / Login