2

Me and my friends are building a boat which needs to steer towards certain coordinates. We have a GPS and a compass on board. We have written code which calculates the direction that the boat has to steer.

When we plug in our GPS together with the compass, the system works. When we add the servo to the circuit, the servo will swing back and forth continuously (even when in the code we only attach the servo and don't control it). If we unplug the GPS, the servo will stop swinging.

Why don't the GPS and servo function well in the same Arduino circuit?

Edit: Sorry guys, for not being specific enough. Let me give it another try:

I made a little diagram of the circuit we built:

enter image description here

I guess the code we made is too long to show up here (more than 300 lines of code), so I only took out the gps part of the code (wich happens to be most of the code).

#include <SoftwareSerial.h>
#include <Servo.h> 
#include <TinyGPS.h>

TinyGPS gps;
SoftwareSerial ss(1,0);

static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);

void setup()
{

  Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
  Serial.println("Sats HDOP Latitude  Longitude  Fix  Date       Time     Date Alt    Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  Serial.println("          (deg)     (deg)      Age                      Age  (m)    --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  Serial.println("-------------------------------------------------------------------------------------------------------------------------------------");


  ss.begin(9600);

}

void loop()
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;

  print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  gps.f_get_position(&flat, &flon, &age);
  print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
  print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  print_date(gps);
  print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
  print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
  print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
  print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
  print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
  print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);

  gps.stats(&chars, &sentences, &failed);
  print_int(chars, 0xFFFFFFFF, 6);
  print_int(sentences, 0xFFFFFFFF, 10);
  print_int(failed, 0xFFFFFFFF, 9);
  //Serial.println();

  smartdelay(1000);

}


 static void smartdelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void print_float(float val, float invalid, int len, int prec)
{
  if (val == invalid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartdelay(0);
}

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartdelay(0);
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    Serial.print("********** ******** ");
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
        month, day, year, hour, minute, second);
    Serial.print(sz);
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  smartdelay(0);
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartdelay(0);
}
Just van Til
  • 33
  • 1
  • 6
  • 3
    Just let me look in my crystal ball and see your schematic and the code.. – Eugene Sh. Mar 27 '15 at 14:31
  • 1
    Sounds like you have something connected wrong or the code is not doing quite what you expect. Regardless, without a schematic showing how the parts are connected, part numbers for the servo and other parts, and your code, can't do more than guess. – I. Wolfe Mar 27 '15 at 14:54
  • You should also describe what "the system works" means. Does "working" mean that it steers towards the correct coordinates? How does it steer without a servo? – Greg d'Eon Mar 27 '15 at 17:26
  • Thanks guys. I added some more information. Thanks for your fast reactions! – Just van Til Mar 30 '15 at 02:42

2 Answers2

3

All the other comments notwithstanding (about your steering logic, etc), the specific issue you're having is a conflict between the SoftwareSerial and Servo libraries. I had the same thing (for the same kind of project, ironically enough):

http://forum.arduino.cc/index.php?topic=243689.msg1745181#msg1745181

Paraphrasing from the thread

The issue appears to have been that both libraries use the same hardware timer, and end up conflicting. You can either use HardwareSerial (if you have a spare port), or use the ServoTimer2 library, instead.

Incidentally, my project doing the same thing lives here: https://github.com/kolosy/ArduSailor

kolosy
  • 2,288
  • 6
  • 29
  • 51
  • Wauw, thank you so much! We'll work on it tommorow morning. Sounds like we do have the same problem. We weren't able to find anybody with the same problem on the internet though. Awesome! – Just van Til Mar 30 '15 at 03:07
  • clearly, you were ;) – kolosy Mar 30 '15 at 03:33
0

The autopilot on my boat uses both the GPS and a magnetic compass.

When I direct it to steer to a GPS waypoint, the pilot first gets the bearing to the waypoint from the GPS, then steers the boat to that heading as indicated by the compass. Once the boat is pointing towards the destination, the autopilot then turns its attention to the Cross Track Error from the GPS, and makes small corrections to the magnetic heading to minimize the cross-track error. Once on-course, the autopilot ignores the GPS bearing-to-waypoint (and never looks at the GPS Course Made Good).

The steering correction must be done in small steps, allowing time for the boat to react between corrections.

Peter Bennett
  • 57,014
  • 1
  • 48
  • 127