Go Down

Topic: Servo problems... (Read 2670 times) previous topic - next topic

rdeans

Hi:

I've written the following code to obtain the direction and distance to a destination from my present location. the results are outputted on a pan/tilt system.. the code compiles and uploads, and in the serial monitor i get the right calculations. However, the servo pans to the right degree, but shakes back and forth every few milliseconds. i would like the servo to go the the degree and stay there for a few secons (perhaps delay the cycle every 5 seconds).. Any help troubleshooting this would be appreciated.

Thanks
:D

Code: [Select]
#include <SoftwareSerial.h>
#include "TinyGPS.h"
#include <Servo.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 3(rx) and 4(tx).
*/

Servo panservo;
Servo tiltservo;

TinyGPS gps;
SoftwareSerial ss(3, 4);

float dest_latitude =42.24707;
float dest_longitude =-83.00085;
float distance;
float distanceangle;

void setup()
{
  Serial.begin(115200);
  ss.begin(9600);
 
  panservo.attach(9);
  tiltservo.attach(10);
  Serial.print("Simple TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
}

void loop()
{
  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

  if (newData)
  {
    float flat, flon;
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);
    Serial.print("LAT=");
    Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
    Serial.print(" LON=");
    Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
    Serial.print(" SAT=");
    Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
    Serial.print(" PREC=");
    Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
   

float distance = (TinyGPS::distance_between(flat, flon, dest_latitude, dest_longitude ));
float distanceangle;
  if (distance <= 100){
  distanceangle =int(-0.6*distance + 120);
Serial.print(" distance is less");
Serial.print( distanceangle);
tiltservo.write(distanceangle);
}

else {
  distanceangle = 60;
Serial.print(" distance is greater ");
  Serial.print( distanceangle);
  tiltservo.write(distanceangle);
}

float course =  (TinyGPS::course_to(flat, flon, dest_latitude, dest_longitude ));
float courseangle;
if (270 <= course && course <= 360){
  courseangle = int(-course + 450);
  Serial.print (" course is case 1 ");
  Serial.print ( courseangle);
  panservo.write(courseangle);
 
}
else if (0 <= course && course <= 90){
courseangle = int(-course + 90);
  Serial.print (" course is case 2 ");
  Serial.print ( courseangle);
   panservo.write(courseangle);
 
}
else if (90 < course && course < 180){
courseangle = 0;
  Serial.print (" course is case 3 ");
  Serial.print ( courseangle);
   panservo.write(courseangle);
     

}

else if (180 <= course && course < 270){
courseangle = 180;
  Serial.print (" course is case 4 ");
  Serial.print ( courseangle);
   panservo.write(courseangle);
   
}

}
  gps.stats(&chars, &sentences, &failed);
  Serial.print(" CHARS=");
  Serial.print(chars);
  Serial.print(" SENTENCES=");
  Serial.print(sentences);
  Serial.print(" CSUM ERR=");
  Serial.println(failed);
 

}

PeterH

Do you really mean every few milliseconds? I can't imagine how you'd be able to tell that.

Since you only update the GPS position fix and recalculate the bearing once per second, and taking at face value your description that the jitter occurs very frequently, I suppose this must be jitter and backlash from the servo itself. In that case perhaps you need to replace your servo with one that is more accurate. Digital servos are designed to avoid that sort of problem.
I only provide help via the forum - please do not contact me for private consultancy.

rdeans

sorry..yes the servo only jitters every few seconds. This is the pan/tilt kit I am using (the servos came with it): <http://www.robotshop.com/ca/productinfo.aspx?pc=RB-Dag-29&lang=en-US>
i can't find the spec sheet for my servo... but this one is very similar:
<http://www.jetfoamy.com/index.php?route=product/product&product_id=80>

thanks for your help
rdeans

rdeans

Hi:

The servo doesn't really jitter... it just does not hold its position. In other words, it rotates about 10 degrees away from the calculated value, and then swings back to the calculated value. This cycle repeats every few seconds.

Thanks

rdeans

PeterH


Hi:

The servo doesn't really jitter... it just does not hold its position. In other words, it rotates about 10 degrees away from the calculated value, and then swings back to the calculated value. This cycle repeats every few seconds.

Thanks

rdeans


You've got debug code in there showing the servo commands - does the output correspond to the movement you're seeing?

Do the GPS position fixes alter when the jitter occurs? If so, is the servo jitter being correctly calculated from the GPS jitter?
I only provide help via the forum - please do not contact me for private consultancy.

rdeans

Yes, the output on the serial monitor corresponds to the movement. This is what I get in the serial monitor throughout about 30 seconds.. in that time, the servo moves about once a second.
Code: [Select]

LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=206 SENTENCES=1 CSUM ERR=0
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=548 SENTENCES=3 CSUM ERR=0
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=963 SENTENCES=5 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=1235 SENTENCES=7 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=1507 SENTENCES=9 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=1779 SENTENCES=11 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=2172 SENTENCES=12 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=2499 SENTENCES=15 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=2771 SENTENCES=17 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=3043 SENTENCES=19 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=3315 SENTENCES=21 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=3763 SENTENCES=23 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=4035 SENTENCES=25 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=4307 SENTENCES=27 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=4579 SENTENCES=29 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=4851 SENTENCES=31 CSUM ERR=1
LAT=42.241939 LON=-83.009277 SAT=6 PREC=190 distance is greater 60.00 course is case 2 39.00 CHARS=5299 SENTENCES=33 CSUM ERR=1


thanks for your help

PaulS

Code: [Select]
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

Using a while loop where appropriate, instead of twisting a for loop out of shape, would be a good idea.

Code: [Select]
float distanceangle;
  if (distance <= 100){
  distanceangle =int(-0.6*distance + 120);

Declare a variable of type float. Compute a float value. Cast it to an int and store in the float. I wonder why more people don't do that.

The indenting of
                     your code makes
          it really
                             hard to
follow. Use the Tools + Auto Format
                         menu item to do something
      about it.

Simplifying your output would be useful. Print millis(), distanceangle, and courseangle only. If you are not changing the value written to the servo on any given pass through loop, but the servo still moves, it is underpowered or undersized.

How are you powering the servos?

PeterH

Looks to me as if the GPS input is being received on average twice a second, there was only one error, the GPS position was consistent throughout and the commanded servo angle was 39 degrees throughout.

If the servo moved during that time then either there's a problem with the servo itself (or wiring/power supply etc), or memory corruption causing the servo's internal state to be corrupted, or a timing issue preventing the Servo class from outputting the PWM pulses accurately. It seems unlikely that a timing issue would occur consistently so the third option seems unlikely. I suggest you check the free memory immediately before doing the gps.encode and see whether you're anywhere near running out. I haven't looked at the gps implementation but presumably it's got a buffer big enough to hold at least a complete sentence, so it'll be a bit of a memory hog. It's also possible that gps.encode itself uses significant stack or heap space , so unless your freeMemory check shows you have hundreds of bytes to spare I would suggest you also look under the covers of the GPS library and see whether the memory consumption is likely to be static, or changing significantly over time. (If it's changing, then your free memory checks won't tell you what the peak consumption is unless you put them INSIDE the library.)
I only provide help via the forum - please do not contact me for private consultancy.

rdeans

hi:

I'm running the servos off of the 5V power supply on the Arduino. I've made the changes to code:
Code: [Select]
#include <SoftwareSerial.h>
#include "TinyGPS.h"
#include <Servo.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 3(rx) and 4(tx).
*/

Servo panservo;
Servo tiltservo;

TinyGPS gps;
SoftwareSerial ss(3, 4);

float dest_latitude =42.24707;
float dest_longitude =-83.00085;
float distance;
float distanceangle;

void setup()
{
  Serial.begin(115200);
  ss.begin(9600);

  panservo.attach(9);
  tiltservo.attach(10);
  Serial.print("Simple TinyGPS library v. ");
  Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
}

void loop()
{
  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
Serial.print("Millis() is ");
Serial.print(millis()); 
  }
   
  }

 
  if (newData)
  {
    float flat, flon;
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);



    float distance = (TinyGPS::distance_between(flat, flon, dest_latitude, dest_longitude ));
    float distanceangle;
    if (distance <= 100){
      distanceangle =int(-0.6*distance + 120);
      Serial.print(" distance is less");
      Serial.print( distanceangle);
      tiltservo.write(distanceangle);
    }

    else {
      distanceangle = 60;
      Serial.print(" distance is greater ");
      Serial.print( distanceangle);
      tiltservo.write(distanceangle);
    }

    float course =  (TinyGPS::course_to(flat, flon, dest_latitude, dest_longitude ));
    float courseangle;
    if (270 <= course && course <= 360){
      courseangle = int(-course + 450);
      Serial.print (" course is case 1 ");
      Serial.print ( courseangle);
      panservo.write(courseangle);

    }
    else if (0 <= course && course <= 90){
      courseangle = int(-course + 90);
      Serial.print (" course is case 2 ");
      Serial.print ( courseangle);
      panservo.write(courseangle);

    }
    else if (90 < course && course < 180){
      courseangle = 0;
      Serial.print (" course is case 3 ");
      Serial.print ( courseangle);
      panservo.write(courseangle);


    }

    else if (180 <= course && course < 270){
      courseangle = 180;
      Serial.print (" course is case 4 ");
      Serial.print ( courseangle);
      panservo.write(courseangle);

    }

  }


}


This is what I'm getting in the Serial monitor:
Millis() is 150
Millis() is 159
Millis() is 166
Millis() is 174
Millis() is 182
Millis() is 190
Millis() is 198
Millis() is 206
etc...

The distanceangle and courseangle values have not changed.

Thanks

rdeans

PeterH


I'm running the servos off of the 5V power supply on the Arduino.


I suspect that's your problem then.
I only provide help via the forum - please do not contact me for private consultancy.

rdeans

thanks..

what power supply should I run the servos off of?

zoomkat


thanks..
what power supply should I run the servos off of?


External at ~6v.

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

PaulS

Quote
This is what I'm getting in the Serial monitor:

That's a little hard to believe. That means that you are getting, parsing and using a complete sentence from the GPS approximately every 8 milliseconds, at 9600 baud, or 125 per sentences per second. I don't believe that that is the case.

PeterH


That means that you are getting, parsing and using a complete sentence from the GPS approximately every 8 milliseconds, at 9600 baud, or 125 per sentences per second.


No, it doesn't mean that. It means that a single character is being received in that time - which is credible.
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

Quote
No, it doesn't mean that.

The Serial.print() statements are inside the if(gps.decode(c)) block, which, according to the comment, returns true only when the end of sentence marker arrives. So, that looks to me like they should happen only when the $ at the end of the NMEA sentence arrives.

Never mind. I went back and looked at the code. I assumed some curly braces that were not really there.

OP: I wanted you to print the time only when you got a complete sentence, so we could see how long that was taking.

It's a good idea to always use curly braces, so that you can add code to a block (or not), and it is clear that the code is (or is not) part of a block.

Go Up