Pages: 1 [2] 3   Go Down
Author Topic: calculate relative direction to destination  (Read 2389 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your advice.

John you are right its shows me continuously and some times a left when i continuously rotate my car
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Advice
-- make a new project and start stripping until you only do GPS readings.
-- make a project that only does the compass reading.

Do you have a link to the NMEA library used? Think that is a crucial

yes i have the Link to the NMEA library

i have the GPS reading and the compass reading separately
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do you have a link to the NMEA library used? Think that is a crucial

yes i have the Link to the NMEA library

Well, that's nice. I think maybe the idea was that you posted the link.

As usual, you appear to be ignoring the advice being given. Send debugging information to the LCD. Put a delay in to give you time to read it, and make notes. That way you can figure out what is going on, or if you can't, you can present something that might allow others to figure it out.

If you have a laptop or netbook, sit it in the car, and use it to collect debugging data from Serial.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


here is the Link for the Library i used
http://www.maartenlamers.com/nmea/

second can you tell me the piece of code where should i edit the code and show it on the LCD
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
second can you tell me the piece of code where should i edit the code and show it on the LCD
Anywhere you have a "Serial" debug print, and probably a few more where you currently don't.

is there a particular reason you didn't use the "course_to" method?
« Last Edit: March 26, 2012, 08:04:00 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

we first were using the course to technique but it  gave us the fluctuated heading towards destination why dont know?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8662
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is there a particular reason you didn't use the "course_to" method?
The GPS-determined heading fluctuated too much at low speeds because it depends on motion to determine heading.  The compass measures absolute heading against an external reference (Earth's magnetic field).
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how about using modifying Calc bearing code i got it from my friend i think there is something wrong with this code
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <math.h>
#include <nmea.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal myLCD(42, 40, 38, 36, 34, 32);
float lat;
float lon;
NMEA myGPS(GPRMC);
float dest_latitude = 25.37104;//the destination Latitude
float dest_longitude = 68.35567;//the destination Longitude
int HMC6352SlaveAddress = 0x42;
int HMC6352ReadAddress = 0x41;
const int potPin = A8;          //Analog pin A8 for reading Potientiometer that drives the Servo steering
const int leftMotor = 27; //Pin connected to the Relay pin for moving the steering to left
const int rightMotor = 26;//Pin connected to the Relay pin for moving the steering to right
int     motorA1=22;       //Pin connected to the Relay pin for moving the car forward
int     motorA2=23;        //Pin connected to the Relay pin for moving the car backward
//below code describes the Steering ranges for left right and center
char Dir;
bool isLeft()          {
  return (analogRead(potPin)/4) <65;
}
bool isLeftOfCenter()  {
  return (analogRead(potPin)/4) < 108;
}
bool isRightOfCenter() {
  return (analogRead(potPin)/4) > 118;
}
bool isRight()         {
  return (analogRead(potPin)/4) > 128;
}
int beeperpin=44;//the following pin is connected to the buzzer pin
void setup() {
  Serial1.begin(4800);//Start the serial communication of GPS from the Mega Board
  //PIN DESCRIPTION
  pinMode(leftMotor,OUTPUT);
  pinMode(rightMotor,OUTPUT);
  pinMode(motorA1,OUTPUT);
  pinMode(motorA2,OUTPUT);
  myLCD.begin(16,2);//starting the LCD
  myLCD.print("GPS BASED");//Printing on LCD
  delay(3000);
  myLCD.clear();//Clearing the LCD
  myLCD.home();
  Wire.begin();
  HMC6352SlaveAddress = HMC6352SlaveAddress >> 1;
  pinMode(beeperpin,OUTPUT);
  pinMode(13,OUTPUT);
}
void loop(){
  if (Serial1.available() > 0 ) {
    // read incoming character from GPS
    if (myGPS.decode(Serial1.read())) {
      // check if the character completes a valid GPS sentence
      // check if GPS positioning was unactive
      myLCD.setCursor(15,1);
      myLCD.print('V');
      // check if GPS positioning was active
      if (myGPS.gprmc_status() == 'A') {
        myLCD.setCursor(15,1);
        myLCD.print('A');
        lat=myGPS.gprmc_latitude();//Current latitude
        lon = myGPS.gprmc_longitude();//Current longitude
        int d=(round(myGPS.gprmc_distance_to(dest_latitude, dest_longitude, MTR)));
        //calculate the distance by rounding the GPS data from the current latitude,longitude to the desired Longitude
        myLCD.setCursor(0,0);
        myLCD.print('D:');
        myLCD.setCursor(3, 0);
        myLCD.print(d);//Print the Distnace on LCD
        myLCD.setCursor(7, 0);
        myLCD.print('m');
        //if the distance is less than 6 than it should stop the car
        if (d < 6){
          myLCD.clear();
          myLCD.home();
          myLCD.setCursor(0,0);
          myLCD.print("Destinat");//Print destination
          digitalWrite(13, HIGH);//Put the LED high
          digitalWrite(beeperpin, HIGH);//On the Buzzer pin
          carbreak();//apply car breaks
        }
        //if the distnace is not greater than 6 then move towards the waypoint
        if (d > 6){       
          myLCD.clear();
          myLCD.home();
          myLCD.setCursor(0,0);
          myLCD.print('D:');
          myLCD.setCursor(3, 0);
          myLCD.print(d);//Print the Distnace on LCD
          myLCD.setCursor(7, 0);
          myLCD.print('m');
          digitalWrite(beeperpin, LOW);
          digitalWrite(13, LOW);
          //average the down the direction from current latitude,longitude to destination latitude,longitude
          float mydir= 0;
          for (int x = 0; x<20; x=x+1){
            mydir=mydir + myGPS.gprmc_course_to(dest_latitude, dest_longitude);
          }
          mydir=mydir/20;
          if (mydir < 0) {//thats what i not understand
            mydir += 360; //thats what i not understand
          }
          if (mydir > 180) { //thats what i not understand
            mydir -= 360; //thats what i not understand
          }
          float currentdir = compassvalue();//its the compass value
          float finaldir =currentdir - mydir;//subtract the compass value form the GPS beairng
          myLCD.setCursor(2,1);
          if ( abs (finaldir) < 20)//if the final direction is +20 or -20 it should go straight
          {
            myLCD.print ('Go Straight');
            motorForward();//car Forward
            steering('C');//Steering Centered 
            while(finaldir < 0)//if the Final direction is less than zero than goto LEFT
            {
              myLCD.print ("L");
              motorForward();
              steering('L');
            }
            while(finaldir > 0)//if the Final direction is greater than zero than goto RIGHT
            {
              myLCD.print ("R");
              motorForward();
              steering('R');
            }
            myLCD.print(abs(finaldir), DEC);//print the Direction
            myLCD.print(223, BYTE);  // print °-character


          }
        }

      }
    }
  }
}
int compassvalue(){
  //"Get Data. Compensate and Calculate New Heading"
  Wire.beginTransmission(HMC6352SlaveAddress);
  Wire.send(HMC6352ReadAddress);              // The "Get Data" command
  Wire.endTransmission();

  //time delays required by HMC6352 upon receipt of the command
  //Get Data. Compensate and Calculate New Heading : 6ms
  delay(6);

  Wire.requestFrom(HMC6352SlaveAddress, 2); //get the two data bytes, MSB and LSB

  //"The heading output data will be the value in tenths of degrees
  //from zero to 3599 and provided in binary format over the two bytes."
  byte MSB = Wire.receive();
  byte LSB = Wire.receive();

  float headingSum = (MSB << 8) + LSB; //(MSB / LSB sum)
  float headingInt = headingSum / 10;

  //Serial.print(headingInt);
  //Serial.println(" degrees");

  return headingInt;
}
//Motor commands Description
void motorStop() {
  digitalWrite(leftMotor, LOW);
  digitalWrite(rightMotor, LOW);
}
void motorLeft() {
  digitalWrite(leftMotor, HIGH);
}
void motorRight() {
  digitalWrite(rightMotor, HIGH);
}

//back wheels
void motorForward()
{
  digitalWrite(motorA1,HIGH);
  digitalWrite(motorA2,LOW);
}

void motorBackward()
{
  digitalWrite(motorA1,LOW);
  digitalWrite(motorA2,HIGH);
}


void carbreak()
{

  digitalWrite(motorA1,LOW);
  digitalWrite(motorA2,LOW);
}
void steering(char Dir)
{
  //if (Serial.available() == 0)
  //return;
  //Dir=toupper(Serial.read());
  switch (Dir)
  {
  case 'L':
    //Serial.println("New command: L");
    if (isLeft())
    {
    }//Serial.println("Already Left");
    else
    {
      //Serial.println("Heading Left");
      motorLeft();
      while (!isLeft()) /* JUST WAITING */;
      motorStop();
      //Serial.println("Is now Left");
    }
    break;

  case 'C':
    //Serial.println("New command: C");
    if (isRightOfCenter())
    {
      //Serial.println("Heading Left toward Center");
      motorLeft();
      while (isRightOfCenter()) /* JUST WAITING */;
      motorStop();
      //Serial.println("Is now Centered");
    }
    else
      if (isLeftOfCenter())
      {
        //Serial.println("Heading Right toward Center");
        motorRight();
        while (isLeftOfCenter()) /* JUST WAITING */;
        motorStop();
        //Serial.println("Is now Centered");
      }
      else
      {
      }
    //Serial.println("Already Centered");
    break;
  case 'R':
    //Serial.println("New command: R");
    if (isRight())
    {
    }//Serial.println("Already Right");
    else
    {
      //Serial.println("Heading Right");
      motorRight();
      while (!isRight()) /* JUST WAITING */;
      motorStop();
      //Serial.println("Is now Right");
    }
    break;
  }  // end of switch on direction
}





should i try this one
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
should i try this one
Try it by all means, and when you have done, if you still have problems, post the debug output and your observations.

If you don't have problems, pat yourself on the back, and post in the "Exhibitions" section.

This:
Code:
         //average the down the direction from current latitude,longitude to destination latitude,longitude
          float mydir= 0;
          for (int x = 0; x<20; x=x+1){
            mydir=mydir + myGPS.gprmc_course_to(dest_latitude, dest_longitude);
          }
          mydir=mydir/20;
seems a bit of a waste of time, if I'm understanding how the library works with the current NMEA sentence. A running average over several sentences would be more sensible.

Code:
           myLCD.print ('Go Straight');
Can you see what you did wrong there?

Code:
            while(finaldir < 0) {
Code:
      while(finaldir > 0) {
Uh-oh!
« Last Edit: March 27, 2012, 02:12:18 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the above code did not worked for me so i modified it
Code:
#include <math.h>
#include <nmea.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal myLCD(42, 40, 38, 36, 34, 32);
float lat;
float lon;
NMEA myGPS(GPRMC);
float dest_latitude = 25.37104;//the destination Latitude
float dest_longitude = 68.35567;//the destination Longitude
int HMC6352SlaveAddress = 0x42;
int HMC6352ReadAddress = 0x41;
const int potPin = A8;          //Analog pin A8 for reading Potientiometer that drives the Servo steering
const int leftMotor = 27; //Pin connected to the Relay pin for moving the steering to left
const int rightMotor = 26;//Pin connected to the Relay pin for moving the steering to right
int     motorA1=22;       //Pin connected to the Relay pin for moving the car forward
int     motorA2=23;        //Pin connected to the Relay pin for moving the car backward
//below code describes the Steering ranges for left right and center
char Dir;
bool isLeft()          {
  return (analogRead(potPin)/4) <65;
}
bool isLeftOfCenter()  {
  return (analogRead(potPin)/4) < 108;
}
bool isRightOfCenter() {
  return (analogRead(potPin)/4) > 118;
}
bool isRight()         {
  return (analogRead(potPin)/4) > 128;
}
int beeperpin=44;//the following pin is connected to the buzzer pin
void setup() {
  Serial.begin(9600);
  Serial1.begin(4800);//Start the serial communication of GPS from the Mega Board
  //PIN DESCRIPTION
  pinMode(leftMotor,OUTPUT);
  pinMode(rightMotor,OUTPUT);
  pinMode(motorA1,OUTPUT);
  pinMode(motorA2,OUTPUT);
  myLCD.begin(16,2);//starting the LCD
  Serial.print("GPS BASED");
  myLCD.print("GPS BASED");//Printing on LCD
  delay(1000);
  myLCD.clear();//Clearing the LCD
  myLCD.home();
  Wire.begin();
  HMC6352SlaveAddress = HMC6352SlaveAddress >> 1;
  pinMode(beeperpin,OUTPUT);
  pinMode(13,OUTPUT);
}
void loop(){
  if (Serial1.available() > 0 ) {
    // read incoming character from GPS
    if (myGPS.decode(Serial1.read())) {
      // check if the character completes a valid GPS sentence
      // check if GPS positioning was unactive
      Serial.println("V");
      myLCD.setCursor(15,1);
      myLCD.print("V");
      // check if GPS positioning was active
      if (myGPS.gprmc_status() == 'A') {
        Serial.println("A");
        myLCD.setCursor(15,1);
        myLCD.print("A");
        lat=myGPS.gprmc_latitude();//Current latitude
        lon = myGPS.gprmc_longitude();//Current longitude
        int d=(round(myGPS.gprmc_distance_to(dest_latitude, dest_longitude, MTR)));
        //calculate the distance by rounding the GPS data from the current latitude,longitude to the desired Longitude
        Serial.println("D:");
        Serial.print(d);
        Serial.print("m");
        Serial.print("\n");
        myLCD.setCursor(0,0);
        myLCD.print("D:");
        myLCD.setCursor(3, 0);
        myLCD.print(d);//Print the Distnace on LCD
        myLCD.setCursor(7, 0);
        myLCD.print("m");
        //if the distance is less than 6 than it should stop the car
        if (d < 6){
          myLCD.clear();
          myLCD.home();
          Serial.println("Destination");
          Serial.println("CarbreaK");
          myLCD.setCursor(0,0);
          myLCD.print("Destinat");//Print destination
          digitalWrite(13, HIGH);//Put the LED high
          digitalWrite(beeperpin, HIGH);//On the Buzzer pin
          carbreak();//apply car breaks
        }
        //if the distnace is not greater than 6 then move towards the waypoint
        if (d > 6){        
          Serial.println("D:");
          Serial.print(d);
          Serial.print("m");
          Serial.print("\n");
          myLCD.clear();
          myLCD.home();
          myLCD.setCursor(0,0);
          myLCD.print("D:");
          myLCD.setCursor(3, 0);
          myLCD.print(d);//Print the Distnace on LCD
          myLCD.setCursor(7, 0);
          myLCD.print("m");
          digitalWrite(beeperpin, LOW);
          digitalWrite(13, LOW);
          //average the down the direction from current latitude,longitude to destination latitude,longitude
          float mydir= 0;
          for (int x = 0; x<20; x=x+1){
            mydir=mydir + myGPS.gprmc_course_to(dest_latitude, dest_longitude);
          }
          mydir=mydir/20;
          if (mydir < 0) {//thats what i not understand
            mydir += 360; //thats what i not understand
          }
          if (mydir > 180) { //thats what i not understand
            mydir -= 360; //thats what i not understand
          }
          Serial.println("GPS degree:");
          Serial.print(mydir);
          Serial.print("deg");
          Serial.print("\n");
          float currentdir = compassvalue();//its the compass value
          Serial.println("Compass Degree");
          Serial.print(currentdir);
          Serial.print("deg");
          Serial.print("\n");
          float finaldir =currentdir - mydir;//subtract the compass value form the GPS beairng
          Serial.println(finaldir);
          Serial.print("\n");
          Serial.println("Straight");

          myLCD.setCursor(2,1);
          if ( finaldir < 20 && finaldir > 340 )//if the final direction is +20 or -20 it should go straight
          {
            Serial.println("Straight");
            myLCD.print ("Go Straight");
            //motorForward();//car Forward
            //steering('C');//Steering Centered
          }  
          if(finaldir >180 && finaldir < 339)
          {
            Serial.print("Straight");
            Serial.println("Left");

            myLCD.print ("L");
            //motorForward();
            //steering('L');
          }
          if(finaldir > 20 && finaldir < 179)
          {
            Serial.print("Straight");
            Serial.println("Right");

            myLCD.print ("R");
            //motorForward();
            //steering('R');
          }
          myLCD.print(abs(finaldir), DEC);//print the Direction
          myLCD.print(223, BYTE);  // print °-character
        }
      }
    }
  }
}
int compassvalue(){
  //"Get Data. Compensate and Calculate New Heading"
  Wire.beginTransmission(HMC6352SlaveAddress);
  Wire.send(HMC6352ReadAddress);              // The "Get Data" command
  Wire.endTransmission();
  //time delays required by HMC6352 upon receipt of the command
  //Get Data. Compensate and Calculate New Heading : 6ms
  delay(6);
  Wire.requestFrom(HMC6352SlaveAddress, 2); //get the two data bytes, MSB and LSB
  //"The heading output data will be the value in tenths of degrees
  //from zero to 3599 and provided in binary format over the two bytes."
  byte MSB = Wire.receive();
  byte LSB = Wire.receive();
  float headingSum = (MSB << 8) + LSB; //(MSB / LSB sum)
  float headingInt = headingSum / 10;
  //Serial.print(headingInt);
  //Serial.println(" degrees");
  return headingInt;
}
//Motor commands Description
void motorStop() {
  digitalWrite(leftMotor, LOW);
  digitalWrite(rightMotor, LOW);
}
void motorLeft() {
  digitalWrite(leftMotor, HIGH);
}
void motorRight() {
  digitalWrite(rightMotor, HIGH);
}

//back wheels
void motorForward()
{
  digitalWrite(motorA1,HIGH);
  digitalWrite(motorA2,LOW);
}

void motorBackward()
{
  digitalWrite(motorA1,LOW);
  digitalWrite(motorA2,HIGH);
}


void carbreak()
{

  digitalWrite(motorA1,LOW);
  digitalWrite(motorA2,LOW);
}
void steering(char Dir)
{
  //if (Serial.available() == 0)
  //return;
  //Dir=toupper(Serial.read());
  switch (Dir)
  {
  case 'L':
    //Serial.println("New command: L");
    if (isLeft())
    {
    }//Serial.println("Already Left");
    else
    {
      //Serial.println("Heading Left");
      motorLeft();
      while (!isLeft()) /* JUST WAITING */;
      motorStop();
      //Serial.println("Is now Left");
    }
    break;

  case 'C':
    //Serial.println("New command: C");
    if (isRightOfCenter())
    {
      //Serial.println("Heading Left toward Center");
      motorLeft();
      while (isRightOfCenter()) /* JUST WAITING */;
      motorStop();
      //Serial.println("Is now Centered");
    }
    else
      if (isLeftOfCenter())
      {
        //Serial.println("Heading Right toward Center");
        motorRight();
        while (isLeftOfCenter()) /* JUST WAITING */;
        motorStop();
        //Serial.println("Is now Centered");
      }
      else
      {
      }
    //Serial.println("Already Centered");
    break;
  case 'R':
    //Serial.println("New command: R");
    if (isRight())
    {
    }//Serial.println("Already Right");
    else
    {
      //Serial.println("Heading Right");
      motorRight();
      while (!isRight()) /* JUST WAITING */;
      motorStop();
      //Serial.println("Is now Right");
    }
    break;
  }  // end of switch on direction
}

and the output is here



* output.txt (8.89 KB - downloaded 14 times.)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the point of this:
Code:
          float mydir= 0;
          for (int x = 0; x<20; x=x+1){
            mydir=mydir + myGPS.gprmc_course_to(dest_latitude, dest_longitude);
          }
          mydir=mydir/20;
?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

its used for averaging the 20 readings from the GPS

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25666
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought you might say that.

Think of a number, but don't tell me what it is.
Double it.
Multiply that number by five.
Double that number.
Divide the number you have now by twenty.

You have the number you started with.
Amazing, or what?

I'm available for weddings and bar mitzvahs, my rate-sheet is online.

I think I pointed this out earlier.
« Last Edit: April 13, 2012, 07:35:31 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok then but it still does not work good from my side
Quote
Think of a number, but don't tell me what it is.
Add it to itself twenty times.
Divide the number you have now by twenty.

You have the number you started with.
Amazing, or what?

I'm available for weddings and bar mitzvahs, my rate-sheet is online.

I think I pointed this out earlier.
Logged

Pages: 1 [2] 3   Go Up
Jump to: