Hello All -
I am trying to use some of the functions already written in the TinyGPS.h to find my heading, i need some help on how to call that function. I would like to know if i have to define "currentCourse" as a string or a float? Right now i have it as float.
Here's the function i am trying to call from the library:
const char TinyGPS::cardinal (float course)
{
static const char directions[] = {"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"};
int direction = (int)((course + 11.25f) / 22.5f);
return directions[direction % 16];
}
and from my code below, see function:void processGPS()
Also i'm getting an error for my function saying "expected primary-expression before '==' token" --
I have a long code which is attached and posted below. Your help and guidance are very much appreciated as I am fairly new to Arduino. If there's anything else that you find in my code, that you can enhance or modify to make it better, please do let me know. Thanks!
/* By Dieudonné Kaswa
*
* The goal of this project is to similate a self-driving car.
*
* */
/*
* HC-SR04 Ping distance sensor:
* VCC to Arduino 5V
* GND to Arduino GND
* Trig to Arduino: Right - pin13, ctr - pin11, left - pin9
* Echo to Arduino: Right - pin 12, ctr - pin10, left - pin8
*/
/*
* UBLOX - NEO6M GPS RECEIVER
* VCC to Arduino 5V
* GND to Arduino GND
* Transmit signal on pin 4 Arduino
* Receive signal on pin 3 Arduino
*/
// GPS Library and Software Serial
#include <SoftwareSerial.h>
#include <TinyGPS.h>
// Setting up Serial Port and GPS Module
SoftwareSerial gpsSerial(4,3); //rx,tx
TinyGPS gps; // creates gps object
// Digital Compass library & Variables
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <waypointClass.h>
// Pin assignments
#define trigLeft 9 // Trigger Left Sensor
#define trigCtr 11 // Trigger Center Sensor
#define trigRight 13 // Trigger Right Sensor
#define echoLeft 8 // Echo Left Sensor
#define echoCtr 10 // Echo Center Sensor
#define echoRight 12 // Echo Right Sensor
#define front1 A0 // Motor 1, out 1
#define front2 A1 // Motor 1, out 2
#define rear1 A2 // Motor 2, out 3
#define rear2 A3 // Motor 2, out 4
#define loadEN A4 // Enable sig for Solenoid on A5
#define loadDrop A5 // Solenoid, out 4
#define greenLED 7 // Green LED
#define redLED 2 // Red LED
#define enA 6 // Enable sig for mot1 PWM, black wire
#define enB 5 // Enable sig for mot2 PWM, brown wire
#define gpsTX 4 // Transmit signal PIN, purple wire GPS --> Yellow wire Arduino
#define gpsRX 3 // Receive signal PIN, blue wire
#define headingTol 10 // tolerance +/- (in degrees) within which we don't attempt to turn to intercept targetHeading
/* Waypoints */
#define WAYPOINT_DIST_TOLERANCE 1 // tolerance in meters to waypoint; once within this tolerance, will advance to the next waypoint
#define NUMBER_WAYPOINTS 3 // enter the number of way points here (will run from 0 to (n-1))
// Values initialization
/* Motor Speed Variables */
int slowSP = 190; // Rear Motor slow spin speed
int maxrSP = 210; // Rear Motor Max spin speed
int maxfSP = 250; // Front Motor Max turn angle
/* Ping Sensor Variables */
long duration; // Duration used to calculate distance for Ping Sensor
int distance, distLft, distRgt; // Distance for center Ping Sensor
/* Compass Variables */
int targetHeading; // Where to face to reach destination
int currentHeading; // Where we are currently facing
int headingError; // signed (+/-) degrees difference between targetHeading and currentHeading
/* GPS Variables */
float currentCourse;
String latitude;
String longitude;
float currentLat,
currentLong,
targetLat,
targetLong;
int distanceToTarget, // current distance to target (current waypoint)
originalDistanceToTarget; // distance to original waypoing when we started navigating to it
int waypointNumber = -1; // current waypoint number; will run from 0 to (NUMBER_WAYPOINTS -1); start at -1 and gets
// initialized during setup()
waypointClass waypointList[NUMBER_WAYPOINTS] = {
waypointClass(30.508496, -97.832872), // Paul's douse
waypointClass(30.508419, -97.832757), // Our house
waypointClass(30.508296, -97.832618), // Ron's house
};
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);
// Code Begins
void setup()
{
Serial.begin(115200); // connect serial
gpsSerial.begin(9600); // connect gps sensor
//Compass setup
Serial.println("Magnetometer Test"); Serial.println("");
// Initialise the magnetometer sensor
if(!mag.begin())
{
// There was a problem detecting the LSM303 ... check your connections
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
}
}
void loop() // Main Program Loop
{
GPSloop();
calcDesiredTurn(); // calculate how we would optimatally turn, without regard to obstacles
departLED();
movefwd();
brake();
dropLoad();
delay (2000);
destLED();
delay (2000);
retunHome();
}
void GPSloop()
{
while(gpsSerial.available()) // check for gps data
{
if(gps.encode(gpsSerial.read())) // encode gps data
{
gps.f_get_position(&lat,&lon); // get latitude and longitude
// gps.cardinal(course); // To be tested
// display position in serial window
Serial.print("Latitude:");
Serial.print(lat,6);
Serial.print(";");
Serial.print("Longitude:");
Serial.println(lon,6);
processGPS(); // Process Data Gathered from the GPS
}
}
Serial.println(lat);
Serial.println(lon);
// navigate
currentHeading = readCompass(); // get our current heading
}
// Called after new GPS data is received; updates our position and course/distance to waypoint
void processGPS()
{
currentLat = convertDegMinToDecDeg(lat);
currentLong = convertDegMinToDecDeg(lon);
if (gps.cardinal(currentCourse)) == 'S') // make them signed
Serial.println(gps.cardinal(currentCourse));
currentLat = -currentLat;
if (gps.cardinal(currentCourse)) == 'W') // make them signed
Serial.println(gps.cardinal(currentCourse));
currentLong = -currentLong;
// update the course and distance to waypoint based on our new position
//distanceToWaypoint();
gps.distance_between(currentLat, currentLong, targetLat, targetLong);
//courseToWaypoint();
gps.course_to(currentLat, currentLong, targetLat, targetLong);
} // End of processGPS(void)
int readCompass(void)
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
float Pi = 3.14159;
// Calculate the angle of the vector y,x
float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
// Normalize to 0-360
if (heading < 0)
{
heading = 360 + heading;
}
//Serial.print("Compass Heading: ");
//Serial.println(heading);
//delay(500);
} // end of readCompass()
// Which way to turn the car and face the destination?
void calcDesiredTurn(void)
{
// calculate where we need to turn to head to destination
headingError = targetHeading - currentHeading; // signed (+/-) diff btw targetHeading and currentHeading
// adjust for compass wrap
if (headingError < -180)
headingError += 360;
if (headingError > 180)
headingError -= 360;
// calculate which way to turn to intercept the targetHeading
if (abs(headingError) <= headingTol) // if within tolerance of +/- 10
Serial.println("No turns");
else if (headingError < 0)
turnLeft();
else if (headingError > 0)
turnRight();
else
Serial.println("No turns");
} // end of calcDesiredTurn()
// Had to chop some of the code to make it fit here within the limits
waypointClass.h (436 Bytes)