GPS interferes with motor control

I have a rover with a Mega, a LSM303 compass and Adafruits Ultimate GPS module. I can control it with a transmitter with 2 joysticks connected to a UNO and they communicate via xbee wifi modules. If I disable the call to read the GPS it works fine, when I enable the read GPS function I get two problems. The motors will pulse on for a couple of seconds then off for a few seconds. There is also about a 5 to 10 second delay of the control I am sending from the transmitter. I’m sure there’s a better way to do it but I’ve added delays in the calls to the compass and GPS routines. I reset a timer, at 500 milliseconds I call the compass, at 1500 milliseconds I call the GPS, timer is reset at 3000 milliseconds. I’ve played around with the times but it doesn’t help. The compass and GPS functions are on separate tabs. I couldn’t post all of the main sketch, I exceeded the word limit. So I’ve attached the main sketch as an attachment. I was able to attached the wifi control from the transmitter, in case the GPS is interfering with that, and the GPS read function.

WiFi control

[code]
// Control Rover from Wifi transmitter


void Wifi_Cntrl() {

  // Throttle_Ref used for forward and backward control
  if (Throttle_Ref < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Throttle_Ref readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(Throttle_Ref, 470, 0, 0, motorSpeedASet);
    motorSpeedB = map(Throttle_Ref, 470, 0, 0, motorSpeedBSet);
    Serial.print("Reverse"); Serial.print(","); Serial.print(motorSpeedA); Serial.print(","); Serial.println(motorSpeedB);
  }
  else if (Throttle_Ref > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Throttle_Ref readings for going forward from 100 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(Throttle_Ref, 550, 1023, 0, motorSpeedASet);
    motorSpeedB = map(Throttle_Ref, 550, 1023, 0, motorSpeedBSet);
    Serial.print("Forward"); Serial.print(","); Serial.print(motorSpeedA); Serial.print(","); Serial.println(motorSpeedB);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }

  // Steering_Ref used for left and right control
  if (Steering_Ref < 470) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    motorSpeedA = map(Steering_Ref, 470, 0, 0, motorSpeedASet);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    motorSpeedB = map(Steering_Ref, 470, 0, 0, motorSpeedBSet);
    Serial.print("Right"); Serial.print(","); Serial.print(motorSpeedA); Serial.print(","); Serial.println(motorSpeedB);

    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
    if (motorSpeedA > motorSpeedASet) {
      motorSpeedA = motorSpeedASet;
    }
    if (motorSpeedB > motorSpeedBSet) {
      motorSpeedB = motorSpeedBSet;
    }
  }
  if (Steering_Ref > 550) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    motorSpeedA = map(Steering_Ref, 550, 1023, 0, motorSpeedASet);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    motorSpeedB = map(Steering_Ref, 550, 1023, 0, motorSpeedBSet);
    Serial.print("Left"); Serial.print(","); Serial.print(motorSpeedA); Serial.print(","); Serial.println(motorSpeedB);

    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
    if (motorSpeedA > motorSpeedASet) {
      motorSpeedA = motorSpeedASet;
    }
    if (motorSpeedB > motorSpeedBSet) {
      motorSpeedB = motorSpeedBSet;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

} // end void Wifi_Cntrl

[/code]

GPS Sketch

[code]
void readGPS() {

  clearGPS();
  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
  NMEA1 = GPS.lastNMEA();

  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
  NMEA2 = GPS.lastNMEA();

  //  Serial.println(NMEA1);
  //  Serial.println(NMEA2);
  //  Serial.println("");

} // end readGPS



void clearGPS() {  //Clear old and corrupt data from serial port
  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence

  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }
  GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence

} // end clearGPS

[/code]

Thanks
John

Rover2_Phase6.ino (9.77 KB)

This construction is blocking. Nothing can happen until a complete GPS sentence has been read. There are probably other problems, but this one is serious, and where I stopped looking.

  while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
    c = GPS.read();
  }

There is probably no reason to write blocking code, but I'm not familiar with the Adafruit GPS library. I recommend TinyGPS++ (nonblock if used properly).

I have seen other references to the TinyGPS, up to now I haven't had to change what I'm doing but that was mainly because I was just doing the GPS by itself and didn't have it incorporated with the other functions yet, that was the last part I added in.

Thanks for looking it over and pointing that out to me, I'll look into changing to TinyGPS
John

@jremington That was it! I changed to the TinyGPS++ library and everything works fine, no control delay.

Thanks again for looking into my problem and educating me. Always get great answers on the forum

John