Motor jumping around when positioning with pot

Hi!

I’m using a monster moto shield to control a linear actuator (12V, 2A, 50 mm stroke). Got it to work with the example code, it follows the 10k pots value but when it aligns with it it starts to jump back and forth. Any ideas how to fix this?

Short video: Dropbox - Error

void loop()
{
  
  sensor1Value = analogRead(pot1);
  sensor1Value = map(sensor1Value, 0, 1023, 20, 966);
  delay(15);
  
  sensor2Value = analogRead(pot2);
  delay(15);

  Serial.println(sensor2Value);
  
  if (sensor1Value - sensor2Value == 0)
{
  motorOff(0);
}
  if (((sensor2Value - sensor1Value) > -40 ) && ((sensor2Value - sensor1Value) < 40))
 {
  motorOff(0);
}
  else if (sensor2Value > sensor1Value) // Ställdon längre ut än pot = motor in
{
  motorGo(0, CCW, 1023);
}
  else if (sensor1Value > sensor2Value) // Ställdon längre in än pot = motor ut
{
  motorGo(0, CW, 1023);
}
  if ((analogRead(cspin[0]) < CS_THRESHOLD) && (analogRead(cspin[1]) < CS_THRESHOLD))
    digitalWrite(statpin, HIGH);
}

Read the how to use the forum stick post and post the code correctly. Correct that first post.

Why do you keep changing the pot every time it gets to the interesting bit. What happens if you leave it alone. You might want to look at the abs() function in some of those if statements.

 if (sensor1Value - sensor2Value == 0)

Is that going to happen? Remember there is a +/-variation in any reading from an A/D

Grumpy_Mike: Read the how to use the forum stick post and post the code correctly. Correct that first post.

Why do you keep changing the pot every time it gets to the interesting bit. What happens if you leave it alone. You might want to look at the abs() function in some of those if statements.

 if (sensor1Value - sensor2Value == 0)

Is that going to happen? Remember there is a +/-variation in any reading from an A/D

Fixed the code. What do you mean "changing the pot every time..."? The pot represents a joystick. I will look into the abs() function.

How can I read the +/- variation from the A/D? Serial monitor?

Grumpy_Mike: if (sensor1Value - sensor2Value == 0)

Is that going to happen?

Probably not. Just a precausion.

Thanks in advance

New (slower) actuator and new code. Works great but still have a minor problem. The extraction functions as it should and the actuator “follows” the pots value. But the retraction doesn’t work as good. If I twist the pot from a extracted actuator position, the actuator doesn’t follow up completely. Any ideas?

void loop()
{
  
  
  sensor1Value = analogRead(pot1);
  sensor1Value = map(sensor1Value, 0, 1023, 65, 992);
  sensor2Value = analogRead(pot2);
  
  
  diff = (sensor2Value - sensor1Value);
  Serial.println(sensor2Value);
  
  if ((diff > -10) && (diff < 10))
{
  motorOff(0);
}
  
  else if (diff > 0)
{
  motorGo(0, CCW, 1023); // motor in
  
}
  else if (diff < 0)
{
  motorGo(0, CW, 1023); // motor out
}
  
  if ((analogRead(cspin[0]) < CS_THRESHOLD) && (analogRead(cspin[1]) < CS_THRESHOLD))
    digitalWrite(statpin, HIGH);
}

New video: Dropbox - Error

Firstly your code is driving the motor full-on or totally off - this is very unsatisfactory way to drive a motor, you should ramp-up and ramp-down the drive to get a much smoother response. When the motor is close to desired position the drive level could be proportional to the difference, thus it will stop smoothly.

However friction will make it stop a little short this way, and be sensitive to load.

So you add an integral term that comes into play when the motor is close to the target and pushes up the drive level a little every cycle when its not on-target. Basically you implement a PID algorithm with only P and I terms. The I term will compensate for any resistance (friction or load) and allow the motor to push when stationary and on-target (perhaps not needed for a lead-screw drive as here, but needed in other situations where the load can turn the motor).

However I am assuming you have PWM motor drive available...

MarkT: Firstly your code is driving the motor full-on or totally off - this is very unsatisfactory way to drive a motor, you should ramp-up and ramp-down the drive to get a much smoother response. When the motor is close to desired position the drive level could be proportional to the difference, thus it will stop smoothly.

However friction will make it stop a little short this way, and be sensitive to load.

So you add an integral term that comes into play when the motor is close to the target and pushes up the drive level a little every cycle when its not on-target. Basically you implement a PID algorithm with only P and I terms. The I term will compensate for any resistance (friction or load) and allow the motor to push when stationary and on-target (perhaps not needed for a lead-screw drive as here, but needed in other situations where the load can turn the motor).

However I am assuming you have PWM motor drive available...

As far as I know linear actuators are not speed-controlled. It's the built in gear ratio that changes the speed/torque.

Anyway, the positioning works now. But the actuator acts strange during retraction. See video above.