Control the travel distance of linear actuator

Hallo everyone,

I have the following problem: as the title says, im trying to control the distance of linear actuator to an object with arduino.
I have a ToF (time of flight) laser distance sensor attached to the head of the actuator to measure the current distance to an object placed infront of the actuator. Also with help of a potentiometer, a value for the target distance can be set.

The goal is that the actuator shaft travel to the distance set by the potentiometer and keep that distance, so if i move the object 1cm to the actuator the actuator retract in 1cm distance, and if i move it away the actuator extends in the same distance the object is moved.

I tried coding it with If and else if statments, so that it retracts when the set value smaller than the measured value and extends if the opposite is true, but the problem is that the actuator extends to the end and retracts to the end and does not stop at the desired distance.

If you have any ideas please help.

Im using BTS7960 as a motor driver and 4 inches high speed linear actuator from firgelli.

Regards

Ali

Show us how you have this connected to the Arduino and show the code and results when you are using the potentiometer. Is it working as you intend and giving the correct values between 0 and 1023?

upload your code (using code tags </>)
upload a schematic of the circuit
upload output of serial monitor of a run of code
upload a photo of system
what is the ToF sensor ?

What exact sensor? Have you tested just the ToF sensor with some,e code, and observed it supplying plausible values for you exact physical circumstances?

That would be a step one. Without performing successfully such an experiment to go further would be a waste of time.

a7

Post a link to the datasheet of the actuator. Laser stuff sounds like overdoing things a lot.

Unfortunatly i have everything in the lab in my university so i can't show it right now. The potentiometer worked as intended. I even divided 1023 with 10.23 so i can get vlaues in mm (the maximum distance is 10cm).

the sensor is VL53L0X. Yes i have tested the sensor and it works fine.

You need a position feedback sensor to know when you've moved 1cm

Its only got end of travel switches. You need something with continuous position feedback.

But isn't that the ToF sensor? I wonder if the problem is that the feedback loop gain and dynamics are wrong - you need to have the speed of movement reduce as the actuator approaches the target distance; and probably have some other smoothing in the loop. So the speed you drive the actuator at will vary with the distance.

Yes, but that only gives the target position. You need to know where the actuator is at all times so you can slow to a stop when close. The ToF tells them where the free object is. Then the sensor on the actuator tells where you are vs where you need to be.

I tried with an PID Controller that computes the output (speed) with if-statements, but it didn't work correctly. i guess im missing something.

Here is how :-

 if(soll < ist){
  analogWrite(10, 0);
  analogWrite(11, output); 
}
 else if(soll > ist){
  analogWrite(10, output);
  analogWrite(11, 0); 
}
 else if(soll = ist){
  analogWrite(10, 0);
  analogWrite(11, 0); 
}

what can i do after getting the zt-x? how can i then code it so it works as desired?

This is not a PID controller. It is called a "bang-bang" controller.

For PID control, the Arduino PID library is convenient.

yes. sorry this was a section of the code.

Here is the complete code:-

#include <PID_v1.h>
#include "Adafruit_VL53L0X.h"


Adafruit_VL53L0X lox = Adafruit_VL53L0X();
double soll, ist, output;

//PID Parameters
double Kp=50, Ki=5, Kd=0;

//PID Instance
PID myPID(&ist, &output, &soll, Kp, Ki, Kd, DIRECT);

//Aktor
byte speed = 0;
int RPWM = 10;
int LPWM = 11;

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

  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }
  
  Serial.println("Bachleorarbeit");
  delay (500);
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
    
  }
  // Aktor

  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

//turn the pid on
myPID.SetMode(AUTOMATIC);
//Adjust PID values
myPID.SetTunings(Kp, Ki, Kd);
}


void loop() {

//Istwert lesen
  VL53L0X_RangingMeasurementData_t measure;
  ist=measure.RangeMilliMeter;

  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
  Serial.println();
  Serial.print("ist measurement... ");
  Serial.print(ist);
  Serial.println("(mm) "); 
  } else {
    Serial.println(" out of range ");
  }
    
  delay(500);

//Sollwert einstellen
  Serial.print("soll Measurement...:");
  soll=analogRead(2)/10.22;
  Serial.print(soll);
  Serial.println("(mm) ");
  delay(500);

//PID
myPID.Compute();

//output
  Serial.print("output");
  Serial.print(output);
  delay(500);

 if(soll < ist){
  analogWrite(10, 0);
  analogWrite(11, output); 
}
 else if(soll > ist){
  analogWrite(10, output);
  analogWrite(11, 0); 
}
 else if(soll = ist){
  analogWrite(10, 0);
  analogWrite(11, 0); 
}

/*
//Aktor
 if(soll < ist){
  speed = 255;
  analogWrite(10, 0);
  analogWrite(11, speed); 
}
 else if(soll > ist){
  speed = 255;
  analogWrite(11, 0);
  analogWrite(10, speed); 
}
 else{
  analogWrite(10, 0);
  analogWrite(11, 0); 
}
*/

}

You need to choose the proper K values (look up "PID tuning"). Study the library documentation and the example code.

Its NOT going to work without position feedback. Your PID needs to know where it is to know how far/fast to move. Speed profile is trapezoid, stopped, ramp up, fly at speed, ramp down, stopped.

Why not? The one thing has to be a stated distance from the other thing, and to the extent it is not and in which way, the one thing has to move towards or away from the other thing.

a7

I don't quite understand. Why can't i use the VL53L0X ToF sensor as a position feedback sensor. It reads the distance all the time. Do i need an actuator with a built-in position feedback? like a built-in potentiometer or sensors?