Servo feedback signal noise spikes

My Adafruit feedback servos produce large noise spikes at quasi-regular intervals while servo is driven between min and max angles at the rate of 1 degree per loop iteration, as shown in the accompanying image taken from the Arduino Serial Plotter. I would like to understand why this is happening, and eliminate it if possible. Such large spikes in position feedback make the feedback signal unreliable. The pattern shown is not unique to a particular servo as it replicates with others. The sketch driving the servo includes a small delay between setting the reference and reading the analog signal to reduce the chance that servo-motor actuation would generate the noise-spike in the analog channel. Any ideas why these spikes are being generated?

Reference and feedback signal during triangle ramp of servo reference
Feedback_from_Servo.jpg (73.69 KiB) Viewed 305 times

Bruce_Abbott

Posts: 9
Joined: Wed Jan 09, 2019 7:58 pm

Re: Servo feedback signal noise spikes

Please post the code that you are using.

Posts: 81559
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo feedback signal noise spikes

Here is the script used to generate the output shown in the first post:

Code: Select all | TOGGLE FULL SIZE
`// Code based on Bill Earl's examples in his article "Analog Feedback Servos."#include <Servo.h>  Servo myservo;  // Control and feedback pinsint servoPin = 9;int feedbackPin = A0;// Calibration valuesint minDegrees;int maxDegrees;int minFeedback;int maxFeedback;int tolerance = 2; // max feedback measurement errorint servoAngle;int servoRef;int dt = 10;int maxAngle = 160;int minAngle = 0;/*  This function establishes the feedback values for 2 positions of the servo.  With this information, we can interpolate feedback values for intermediate positions*/void calibrate(Servo servo, int analogPin, int minPos, int maxPos){  // Move to the minimum position and record the feedback value  servo.write(minPos);  minDegrees = minPos;  delay(2000); // make sure it has time to get there and settle  minFeedback = analogRead(analogPin);  // Move to the maximum position and record the feedback value  servo.write(maxPos);  maxDegrees = maxPos;  delay(2000); // make sure it has time to get there and settle  maxFeedback = analogRead(analogPin);} void setup() {   Serial.begin(9600);  myservo.attach(servoPin);   calibrate(myservo, feedbackPin, 20, 160);  // calibrate for the 20-160 degree range} void Seek(Servo servo, int analogPin, int pos){  // Start the move...  servo.write(pos);  // Calculate the target feedback value for the final position  int target = map(pos, minDegrees, maxDegrees, minFeedback, maxFeedback);   // Wait until it reaches the target  while(abs(analogRead(analogPin) - target) > tolerance){} // wait...}int getPos(int analogPin)  // Return the servo's actual position based on feedback signal{  return map(analogRead(analogPin), minFeedback, maxFeedback, minDegrees, maxDegrees);}void loop(){  // ramp up   for (int i = minAngle; i <= maxAngle; i++) {     servoRef = i;    myservo.write(servoRef);    //Seek(myservo, feedbackPin, servoRef);    delay(dt);    servoAngle = getPos(feedbackPin);    Serial.print(servoRef);    Serial.print("\t");    Serial.println(servoAngle);  }  // ramp down  for (int i = maxAngle-1; i >= minAngle+1; i--) {    servoRef = i;    myservo.write(servoRef);    //Seek(myservo, feedbackPin, servoRef);    delay(dt);    servoAngle = getPos(feedbackPin);    Serial.print(servoRef);    Serial.print("\t");    Serial.println(servoAngle);  }}`
Attachments
Feedback_servo.ino

Bruce_Abbott

Posts: 9
Joined: Wed Jan 09, 2019 7:58 pm

Re: Servo feedback signal noise spikes

The spikes are most likely due to interference from the servo motor. I'd try increasing the settling time between moves to see if that makes an appreciable difference. The internal PID for the servo is tuned at the factory for some 'typical' load. That means it will be underdamped when unloaded and have a tendency to overshoot and oscillate. So it might take a little longer before the motor activity settles down.

Posts: 81559
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo feedback signal noise spikes

Indeed there is much less spiking when the delay between servo actuation and analog feedback read is increased from 10 ms to 100 ms:
100 ms delay to allow servo to settle, no load on servo
Feedback_from_Servo2.jpg (68.91 KiB) Viewed 271 times

However, when I added a load opposing the direction of servo rotation, I get an even worse problem:
100 ms delay between servo actuation and analog feedback read, with added load resisting direction of servo rotation
Feedback_from_Servo3.jpg (83.59 KiB) Viewed 271 times

Bruce_Abbott

Posts: 9
Joined: Wed Jan 09, 2019 7:58 pm

Re: Servo feedback signal noise spikes

Not too surprising. A typical RC servo control loop is tuned for some arbitrary 'average' load. So it will be overdamped for some and underdamped for others. In either case, when the tuning is less than ideal it takes longer to converge on the target position.

The feedback on these servos is simply a secondary tap into the servo's internal feedback pot. In an ideal world, that should give you position feedback identical to what the servo sees. But in the real world, wires and connections have resistance, so there will be a voltage drop proportional to the current flow. That means when the motor is moving, both the power and ground potential at the servo will be different than at your processor. To minimize that, be sure to keep your wires short and your connections solid.