The following code works fine when alone :
while (gpsSerial.available() > 0)
{
gps.encode(gpsSerial.read());
if (gps.location.isUpdated())
{
Position_latitude_initiale = gps.location.lat();
Serial.print("Position_latitude_initiale1000 = ");
Serial.println(Position_latitude_initiale1000.0);
Position_longitude_initiale = gps.location.lng();
Serial.print("Position_longitude_initiale*1000 = ");
Serial.println(Position_longitude_initiale*1000.0);
displayInfo(); // Appel de la fonction displayInfo
}
else if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
}
break;
But: if (gps.location.isUpdated())
always False if the same loop is include in the following code :
#include <Wire.h>
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#define HMC5803L_Address 0x1E //HMC5803L module's I2C
#define X 3 //Register address for the X data
#define Y 7 //Register address for the Y data
// valeurs requises pour calibrer nos mesures (spécifique à votre module)(2022/08/12)
const int xmin = -223;
const int xmax = 446;
const int ymin = -1075;
const int ymax = -374;
int RXPin = 8; // Choose 2 Arduino pins to use for Software Serial
int TXPin = 9; // Attention, inverser les Rx et TX.
int GPSBaud = 9600;
// Pour pointer vers le nord géographique plutôt que vers
// le nord magnétique, on additionne la déclinaison, qui dépend
// de l'endroit où vous êtes:
// http://www.magnetic-declination.com/ (Anthy = 0.026)
float declinaison = 0.026; // en radians
float Position_latitude_initiale = 0.0;
float Position_longitude_initiale = 0.0;
int pwm_a = 3; //PWM control for motor outputs 1 and 2
int pwm_b = 9; //PWM control for motor outputs 3 and 4
int dir_a = 2; //direction control for motor outputs 1 and 2
int dir_b = 8; //direction control for motor outputs 3 and 4
int xbrut, ybrut, xcalibree, ycalibree;
int newX;
int newX_calibree;
int count = 0;
int count_1 = 0;
//float orientationNord = 0.2;
TinyGPSPlus gps; // Create a TinyGPSPlus object
SoftwareSerial gpsSerial(RXPin, TXPin); // Create a software serial port called "gpsSerial"
//-----------------------Fonctions---------------------------------------
void displayInfo()
{
if (gps.location.isValid())
{
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 10); // Précision de 10 chiffres aprés la virgule, à affiner
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 10);
}
else
{
Serial.println("Location: Not Available");
}
Serial.println();
delay(1000);
}
void Init_HMC5803L(void)
{
Wire.beginTransmission(HMC5803L_Address); // Set the module to 8x averaging and 15Hz measurement rate
Wire.write(0x00);
Wire.write(0x70);
Wire.write(0x01); // set a gain of 5
Wire.write(0xA0);
Wire.endTransmission();
}
int HMC5803L_Read(byte Axis) // this function will read once from one of the 2 axis (x and y) data registers
// and return the 16 bits signed result
{
int Result;
Wire.beginTransmission(HMC5803L_Address); // Set the module to 8x averaging and 15Hz measurement rate
Wire.write(0x02);
Wire.write(0x01);
Wire.endTransmission();
delay(6);
Wire.beginTransmission(HMC5803L_Address); // Move the register pointer to one of the axis data register
Wire.write(Axis);
Wire.endTransmission();
Wire.requestFrom(HMC5803L_Address,2); //Read the data register ( there are two 8 bits register for each axis )
Result = Wire.read() << 8; // selection upper digits (shift left 8)
Result |= Wire.read();
return Result;
}
void setup()
{
Serial.begin(9600); // Start the Arduino hardware serial port at 9600 bauds
delay(100);
Wire.begin();
gpsSerial.begin(GPSBaud); // Start the software serial port at the GPS's default baud
Serial.print("Application started.......awaitng GPS data.....");
Serial.println(" ");
delay(4000); // wait for GPS to initialize (précision 10 chiffres décimaux)
Init_HMC5803L(); // Initialisation du module HMC5803L
pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
}
void loop()
{
/while (gpsSerial.available() > 0)
{
gps.encode(gpsSerial.read());
if (gps.location.isUpdated())
{
Position_latitude_initiale = gps.location.lat();
Serial.print("Position_latitude_initiale1000 = ");
Serial.println(Position_latitude_initiale*1000.0);
Position_longitude_initiale = gps.location.lng();
Serial.print("Position_longitude_initiale*1000 = ");
Serial.println(Position_longitude_initiale*1000.0);
displayInfo(); // Appel de la fonction displayInfo
}
else if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
}
break;
} //end while
*/
xbrut = HMC5803L_Read(X); //Read sensor X axis data
Serial.print("xbrut = ");
Serial.print(xbrut);
Serial.print(" ");
delay(1000);
ybrut = HMC5803L_Read(Y); //Read sensor Y axis data
Serial.print("ybrut = ");
Serial.println(ybrut);
delay(1000);
xcalibree = map(xbrut, xmin, xmax, -1000, 1000);
Serial.print("xcalibree = ");
Serial.print(xcalibree);
Serial.print(" ");
delay(1000);
ycalibree = map(ybrut, ymin, ymax, -1000, 1000);
Serial.print("ycalibree = ");
Serial.print(ycalibree);
Serial.print(" ");
delay(1000);
// Calcul de l'angle entre le nord magnétique et l'axe x du capteur (???)
float orientationNord = atan2(ycalibree, xcalibree);
// Pour pointer vers le nord géographique plutôt que vers
// le nord magnétique, on additionne la déclinaison
//orientationNord += declinaison;
Serial.print(" OrientationNord = ");
Serial.println(orientationNord);
delay(1000);
newX = HMC5803L_Read(X);
if (newX > 0)
{
while (newX > 5 | newX < -5) // angle plage +/- 5°
{
newX = HMC5803L_Read(X); //lecture cyclique de la position pour ajustemant
digitalWrite(dir_a, HIGH);
digitalWrite(dir_b, LOW);
if (newX > 40 | newX < -40) //Angle par rapport au Nord IMPORTANT = temps de rotation plus long
//A voir avec propulseurs les réactions seront différentes
{
analogWrite(pwm_a, 100);
analogWrite(pwm_b, 100);
delay(100);
}
else
{
analogWrite(pwm_a, 100); //Angle par rapport au Nord FAIBLE = temps de rotation plus petit
analogWrite(pwm_b, 100);
delay(50);
analogWrite(pwm_a, 0); // Brake both moteurs
analogWrite(pwm_b, 0);
delay(50);
Serial.print(" HEADING = ");
Serial.print(HMC5803L_Read(X));
Serial.print(" ");
Serial.print(" OrientationNord = ");
Serial.println(orientationNord);
}
break;
}
}
else
{
while (newX > 5 | newX < -5) // angle plage +/- 3°
{
newX = HMC5803L_Read(X); // lecture cyclique de la position pour ajustement
digitalWrite(dir_a, LOW);
digitalWrite(dir_b, HIGH);
if (newX > 40 | newX < -40) //Angle par rapport au Nord IMPORTANT = temps de rotation plus long
{
analogWrite(pwm_a, 100);
analogWrite(pwm_b, 100);
delay(100);
}
else
{
analogWrite(pwm_a, 100); //Angle par rapport au Nord FAIBLE = temps de rotation plus petit
analogWrite(pwm_b, 100);
delay(50);
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 0);
delay(50);
}
Serial.print(" HEADING (degrées) = ");
Serial.print(HMC5803L_Read(X));
Serial.print(" ");
Serial.print(" HEADING (atan2, x, y) = ");
Serial.println(orientationNord);
Serial.println(" ");
break;
}
}
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 0);
delay(500);
while (gpsSerial.available() > 0)
{
gps.encode(gpsSerial.read());
while (gpsSerial.available() > 0)
{
gps.encode(gpsSerial.read());
while (gpsSerial.available() > 0)
{
gps.encode(gpsSerial.read());
if (gps.location.isUpdated())
{
Position_latitude_initiale = gps.location.lat();
Serial.print("Position_latitude_initiale1000 = ");
Serial.println(Position_latitude_initiale1000.0);
Position_longitude_initiale = gps.location.lng();
Serial.print("Position_longitude_initiale*1000 = ");
Serial.println(Position_longitude_initiale*1000.0);
displayInfo(); // Appel de la fonction displayInfo
}
else if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
}
break;
{
Position_latitude_initiale = gps.location.lat();
Serial.print("Position_latitude_initiale*1000 = ");
Serial.println(Position_latitude_initiale*1000.0);
Position_longitude_initiale = gps.location.lng();
Serial.print("Position_longitude_initiale*1000 = ");
Serial.println(Position_longitude_initiale*1000.0);
displayInfo(); // Appel de la fonction displayInfo
}
else if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
}
break;
{
Position_latitude_initiale = gps.location.lat();
Serial.print("Position_latitude_initiale*1000 = ");
Serial.println(Position_latitude_initiale*1000.0);
Position_longitude_initiale = gps.location.lng();
Serial.print("Position_longitude_initiale*1000 = ");
Serial.println(Position_longitude_initiale*1000.0);
displayInfo(); // Appel de la fonction displayInfo
}
else if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
}
break;
} //end while
}