Really bad Servo jitter.

Hi, I’m currently working on a robotic arm that is controlled using an MPU6050 accelerometer attached to a glove. The servos for Front/Back and Vertical movement are very jittery and sometimes don’t move at all and make a clicking noise or move very slightly back and forth. The servos mentioned are MG90s Servos which is powered externally with 4 AA batteries. The base servo which is an SG90 works fine and moves according to the accelerometer however, both MG90 servos move back and forth sometimes even when the accelerometer is stationary (Video (excuse the mess)). I’ve been trying to get a somewhat smooth motion like in the base to no avail although the code that reads from the accelerometer and writes to the servo is the same for the base and the two servos mentioned.

Any help is appreciated.

#include <I2Cdev.h>
#include <MPU6050.h>
#include <PWMServo.h> // new library
#include <Servo.h>
#include <Wire.h>

int valx, valx2,valy,prevValx,prevValx2,prevValy;
Servo baseServo;
Servo gripperServo;
Servo vertServo;
Servo frServo;
MPU6050 mpu;
int16_t x,y,z;
const int pinflex = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(38400);
Serial.println("Initialize MPU6050");
Serial.print(mpu.testConnection() ? "Connected" : "Not Connected");
Wire.begin();
mpu.initialize();
gripperServo.attach(9);
baseServo.attach(10);
frServo.attach(11);
vertServo.attach(12);
//TIMSK0 = 0;
}

void loop() {

  // RESETTING // 
  baseServo.write(90);
  frServo.write(90);
  vertServo.write(90);

  
// Flex Sensor Code /////////////////////////////
int flexpos = analogRead(pinflex);
Serial.println("Analog Read = ");
Serial.println(flexpos);
if(flexpos > 875){
  gripperServo.write(80); //closed
}else{
  gripperServo.write(150); //opened
}

// MPU6050 Code///////////////////////////////// 
// Base L/R
mpu.getAcceleration(&x,&y,&z);
valy = map(y, -17000, 17000, 30, 120);
if(valy != prevValy){
  baseServo.write(valy);
  valy == prevValy;
  delay(50);
}

// Vertical U/D
mpu.getAcceleration(&x,&y,&z);
valx = map(x, -17000, 17000, 135, 45);
if(valx != prevValx){
  vertServo.write(valx);
  valx == prevValx;
  delay(50);
}

// Pivot Front/Back
mpu.getAcceleration(&x,&y,&z);
valx2 = map(x, -17000, 17000, 45 ,135);
if(valx2 != prevValx2){
  frServo.write(valx2);
  valx2 == prevValx2;
  delay(50);
}

}

Get another servo, you cannot fix faulty hardware.

You have included PWMServo.h but never seem to use it. Does anything change if you take it out?

The last time you asked about servos I suggested powering them with rechargeable NiMH AA batteries. Is that what you are using?

Steve

zwieblum:
Get another servo, you cannot fix faulty hardware.

I tested the same servos with the example sweep sketch and they worked properly so I don’t think its faulty.

slipstick:
You have included PWMServo.h but never seem to use it. Does anything change if you take it out?

The last time you asked about servos I suggested powering them with rechargeable NiMH AA batteries. Is that what you are using?

Steve

I included the PWMServo library only to realize that it only supported 2 pins and I’ve left it there just in case. I did try and use that library for the two servos but there was no difference. I’m using 4 AA rechargeable NiMH batteries in a simple battery holder that I bought. Each battery being 2500mAh.
When I isolated the servo part of the code that controlled the servos in question it worked fine so I’m really confused right now. I changed the code a bit as a suggestion from someone on the Arduino discord adding the variable delay.

#include <MPU6050.h>
#include <Servo.h>
#include <Wire.h>

int valx, valx2,valy,prevValx,prevValx2,prevValy,timereq;
Servo frServo, vertServo;
MPU6050 mpu;
int16_t x,y,z;

void setup() {
  // put your setup code here, to run once:
Serial.begin(38400);
Serial.println("Initialize MPU6050");
Serial.print(mpu.testConnection() ? "Connected" : "Not Connected");
Wire.begin();
mpu.initialize();
frServo.attach(11);
vertServo.attach(12);
}

void loop() {
  // put your main code here, to run repeatedly:
mpu.getAcceleration(&x,&y,&z);
valx = map(x, -17000, 17000, 135, 45);
valx2 = map (x, -17000, 17000, 45, 135);
  if (valx != prevValx) {
    vertServo.write(valx);
    timereq=abs(valx - prevValx);
    valx = prevValx;
    delay(timereq);
  }

 if (valx2 != prevValx2) {
    frServo.write(valx2);
    timereq=abs(valx2 - prevValx2);
    valx2 = prevValx2;
    delay(timereq);
 }
}

Ok so I think I might’ve fixed the problem, it was the “RESETTING” part of the code as it looped through it and reset its position (I think). Deleting it got rid of the sporadic movement although the servos are not as smooth as I hoped. Still acceptable though.