Raspberry Pi and the NEO 6M GPS

RASPBERRY PI & THE NEO 6M GPS

Original post here.

Intro: Raspberry Pi & the Neo 6M GPS

Previously I built a project where I connected a Neo-6M to an Arduino, but this time around, I wanted to show how to use a GPS with the Raspberry PI. Now there are several USB solutions, and apps that work with them, but I wanted to show how to use a $20 GPS module with a a serial UART, and Python code to decode the NMEA strings. Then you can write your own GPS interface, or combine the data with Google Maps.

Parts Needed:

Raspberry PI (any version)

Neo-6M GPS

Female to Female Jumpers

Step 1: Electrical Connection

Picture of Electrical Connection

The first step is to connect the GPS module to the Raspberry PI. There are only 4 wires (F to F), so it’s a simple connection.

Neo-6M RPI

VCC to Pin 1, which is 3.3v

TX to Pin 10, which is RX (GPIO15)

RX to Pin 8, Which is TX (GPIO14)

Gnd to Pin 6, which is Gnd

Step 2: Turn Off the Serial Console

By default, the Raspberry Pi uses the UART as a serial console. We need to turn off that functionality so that we can use the UART for our own application.

Open a terminal session on the Raspberry Pi.

The first thing we will do is backup the file cmdline.txt before we edit it.

sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt and press Enter.

The we need to edit cmdlint.txt and remove the serial interface.

Type in sudo nano /boot/cmdline.txt and press Enter.

Delete console=ttyAMA0,115200 and save the file by pressing Ctrl X, Y, and Enter.

Now type in sudo nano /etc/inittab and press enter.

Find ttyAMA0 by pressing Ctrl W and typing ttyAMA0 on the search line.

When it finds that line, press home, insert a # symbol to comment out that line, and Ctrl X, Y, Enter to save.

Type sudo reboot and press Enter to restart the Pi.

Step 3: Testing the GPS

Picture of Testing the GPS

Before we start writing our own code, let’s test the GPS by using some off the shelf programs.

Open a terminal session and type sudo apt-get install gpsd gpsd-clients and press Enter.

After that installs, let’s start the serial port:

Type stty -F /dev/ttyAMA0 9600 and press Enter.

Now start GPSD:

Type sudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock and press Enter.

Now display by typing cgps -s and press Enter.

Racing Pygame Sample

First, pygame must be installed.
In CMD: pip install pygame

import pygame


pygame.init()


display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')

def car(x,y):
    gameDisplay.blit(carImg, (x,y))

x =  (display_width * 0.45)
y = (display_height * 0.8)

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    gameDisplay.fill(white)
    car(x,y)

        
    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()

Source

Pygame Chest Open Sample

First, pygame must be installed.
In CMD: pip install pygame


import pygame

pygame.init()
pygame.display.set_caption('Crash!')
window = pygame.display.set_mode((300, 300))
running = True

# Draw Once
Rectplace = pygame.draw.rect(window, (255, 0, 0),(100, 100, 100, 100))
pygame.display.update()
# Main Loop
while running:
    # Mouse position and button clicking.
    pos = pygame.mouse.get_pos()
    pressed1, pressed2, pressed3 = pygame.mouse.get_pressed()
    # Check if the rect collided with the mouse pos
    # and if the left mouse button was pressed.
    if Rectplace.collidepoint(pos) and pressed1:
        print("You have opened a chest!")
    # Quit pygame.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

Source

Current Bogie Code *Change Port Definitions*

#define left1 32
#define left2 26
#define right2 30
#define right1 28
#define eLeft A8
#define eRight A9
//********************************************************************************************************************************************************************************************
#include <LiquidCrystal.h>
int lcd_key     = 0;
int adc_key_in  = 0;
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//********************************************************************************************************************************************************************************************
String data="";
int mark = 0;
boolean Mark_Start=false;
boolean valid=false;
String GGAUTCtime,GGAlatitude,GGAlongitude,GPStatus,SatelliteNum,HDOPfactor,Height,
PositionValid,RMCUTCtime,RMClatitude,RMClongitude,Speed,Direction,Date,Declination,Mode;
//********************************************************************************************************************************************************************************************
int longitude, latitude;
//********************************************************************************************************************************************************************************************
void setup() {
  pinMode(right1, OUTPUT);
  pinMode(right2, OUTPUT);
  pinMode(left1, OUTPUT);
  pinMode(left2, OUTPUT);
  pinMode(eLeft, OUTPUT);
  pinMode(eRight, OUTPUT);  
  digitalWrite(eLeft, HIGH);
  digitalWrite(eRight, HIGH);
//********************************************************************************************************************************************************************************************
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println(0);
  delay(1000);
  pinMode(21, OUTPUT);
  digitalWrite (21, HIGH);
  pinMode(20, OUTPUT);
  digitalWrite (20, LOW);
  lcd.begin(16, 2);              // start the library
  lcd.setCursor(0,0);
  lcd.print("Initializing GPS"); // print a simple message
}
bool clipped = 0;
//********************************************************************************************************************************************************************************************
void loop() {
  gpsBoi();
  clipped = 0;
}
//********************************************************************************************************************************************************************************************
void turnLeft(int speed){
  analogWrite(left2, speed);
  digitalWrite(left1, LOW);
  analogWrite(right1, speed);
  digitalWrite(right2, LOW);
}
//******************************
void turnRight(int speed){
  analogWrite(left1, speed);
  digitalWrite(left2, LOW);
  analogWrite(right2, speed);
  digitalWrite(right1, LOW);
}
//******************************
void moveForwards(int speed){
analogWrite(right1, speed);
digitalWrite(right2, LOW);
analogWrite(left1, speed);
digitalWrite(left2, LOW);
}
//******************************
void moveBackwards(int speed){
analogWrite(right2, speed);
digitalWrite(right1, LOW);
analogWrite(left2, speed);
digitalWrite(left1, LOW);
}
//******************************
void turn90right(){
  turnRight(150);
  delay(985);
}
void turn90left(){
  turnLeft(150);
  delay(985);
}
//******************************
void halt(){
digitalWrite(right2, LOW);
digitalWrite(right1, LOW);
digitalWrite(left2, LOW);
digitalWrite(left1, LOW);
}
//******************************
void gpsBoi(){
    while (Serial1.available()> 0){
    if(Mark_Start){
      data=reader();
      Serial.println(data);
      if(data.equals("GPGGA")){
        //Serial.println(1);
        GGAUTCtime=reader();
        GGAlatitude=reader();
        GGAlatitude+=reader();
        GGAlongitude=reader();
        GGAlongitude+=reader();
        GPStatus=reader();
        SatelliteNum=reader();
        HDOPfactor=reader();
        Height=reader();
        Mark_Start=false;
        valid=true;
        data="";
 
      }
      else if(data.equals("GPGSA")){
        Serial.println(2);
        Mark_Start=false;
        data="";
      }
      else if(data.equals("GPGSV")){
        Serial.println(3);
        Mark_Start=false;
        data="";
      }
      else if(data.equals("GPRMC")){
        //Serial.println(4);
        RMCUTCtime=reader();
        PositionValid=reader();
        RMClatitude=reader();
        RMClatitude+=reader();
        RMClongitude=reader();
        RMClongitude+=reader();
        Speed=reader();
        Direction=reader();
        Date=reader();
        Declination=reader();
        Declination+=reader();
        Mode=reader();
        valid=true;
        Mark_Start=false;
        data="";
      }
      else if(data.equals("GPVTG")){
        Serial.println(5);
        Mark_Start=false;
        data="";
      }
      else{
        Serial.println(6);
        Mark_Start=false;
        data="";
      }
    }
    if(valid){
      if(PositionValid=="A"){
        Serial.println("Position Valid");
        lcd.clear();
      }
      else{
        Serial.println("Your position is not valid.");
      }
      //lcd.clear();
      clearCoord();
      Serial.print("Date:");
      Serial.println(Date);
      Serial.print("UTCtime:");
      Serial.print(RMCUTCtime);
      Serial.print("   ");
      Serial.println(GGAUTCtime);
      Serial.print("Latitude:");
      Serial.print(RMClatitude);
      lcd.setCursor(0,0);
      lcd.print(latitude);
      Serial.print("   ");
      Serial.println(GGAlatitude);
      Serial.print("Longitude:");
      Serial.print(RMClongitude);
      lcd.setCursor(0,1);
      lcd.print(longitude);
      Serial.println("LONGITUDE THE REAL ONE");
      Serial.println(longitude);
      Serial.println("LATITUDE THE REAL ONE");
      Serial.println(latitude);
      Serial.print("   ");
      Serial.println(GGAlongitude);
      Serial.print("GPStatus:");
      Serial.println(GPStatus);
      Serial.print("SatelliteNum:");
      Serial.println(SatelliteNum);
      Serial.print("HDOPfactor:");
      Serial.println(HDOPfactor);
      Serial.print("Height:");
      Serial.println(Height);
      Serial.print("Speed:");
      Serial.println(Speed);
      Serial.print("Direction:");
      Serial.println(Direction);
      Serial.print("Declination:");
      Serial.println(Declination);
      Serial.print("Mode:");
      Serial.println(Mode);     
      valid=false;
    }
    if(Serial1.find("$")){
      Serial.println("capture");
      Mark_Start=true;
    }
  }
}

String reader(){
  String value="";
  int temp;
startover:
  while (Serial1.available() > 0){
    delay(2);
    temp=Serial1.read();
    if((temp==',')||(temp=='*')){
      if(value.length()){
        //Serial.println("meaningful message");
        return value;
      }
      else {
        //Serial.println("empty");
        return "";
      }     
    }
    else if(temp=='$'){
      //Serial.println("failure");
      Mark_Start=false;
    }
    else{
      //Serial.println("add");
      value+=char(temp);
    }
  }
  while (!(Serial1.available()>0)){
  }
  goto startover;
}
//******************************
void clearCoord(){
  if(clipped == 0){
    RMClatitude.remove(RMClatitude.length()-1);
    RMClongitude.remove(RMClongitude.length()-1);
    RMClatitude.remove(0,5);
    RMClongitude.remove(0,6);
    latitude = RMClatitude.toInt();
    longitude = RMClongitude.toInt();
  }
  clipped = 1;
  }