Hello There
Would anyone have any idea on how to include code which would stop the actuator from overshooting once it has reached its destination.
To explain the system further I have two actuators linked up one is controlled by an analog joystick and this part works perfect.
The problem for me is the linear actuator section.
The code makes a comparison of the two potentiometer positions. The potentiometer within the actuator should follow the linear potentiometer position. Its does this successfully but continues to move once its reaches its desired position until the overshoot finally dissipates. I realised this could be solved by the inclusions of a PID controller but have not idea how to do this. Any other ideas such as how to add a tolerance to the analogWrite such that once the actuator reaches within a +/-5 of required position it will stop.
void setup() {
Serial.begin (9600);
pinMode (12,OUTPUT); // Direction Motor A
pinMode(3, OUTPUT ); // PWM Motor A
pinMode (9, OUTPUT); // Brakes Motor A
pinMode (13,OUTPUT); // Direction Motor B
pinMode(11, OUTPUT ); // PWM Motor B
pinMode (8, OUTPUT); // Brakes Motor B
}
void loop() {
int Feedback = analogRead (A2);
int Input = analogRead (A4);
int Linpot = analogRead (A5);
Input = map (Input, 0, 1023, 0, 100);
Feedback = map (Feedback, 17, 1023, 0, 100);
Linpot = map (Linpot, 0, 1023, 0, 100)
;Serial.println (Feedback);
Serial.print ("\t");
Serial.println (Input);
Serial.print ("\t");
Serial.print ("\t");
Serial.println (Linpot);
if (Feedback > Input) {
digitalWrite (12,HIGH);
digitalWrite (9, LOW);
analogWrite (3,45 + (Feedback - Input)*3);
}
if (Linpot > Feedback) {
digitalWrite (13,LOW);
digitalWrite (8, LOW);
analogWrite (11,255);
}
else if (Input > Feedback) {
digitalWrite (12,LOW);
digitalWrite (9, LOW);
analogWrite (3,45 + (Input - Feedback)*3);
}
else if (Feedback > Linpot) {
digitalWrite (13,HIGH);
digitalWrite (8, LOW);
analogWrite (11,255);
}
}
Can you explain the purpose/origin of the Input, LinPot, Feedback values? It's 1 variable too much for my taste.
I suspect that the "else if" clauses prevent required motor control.
Also consider that a motor can not stop immediately. Perhaps you should decrease the motor duty cycle when approaching the desired position - this is what can be done by a PID.
I managed to get it working code below. PaulS mate you really need to leave people alone mate I've made 3 posts on here nothing you posted was helpful. Why don't you have a look at yourself and consider how your making people feel.
void setup() {
Serial.begin (9600);
pinMode (12,OUTPUT); // Direction Motor A
pinMode(3, OUTPUT ); // PWM Motor A
pinMode (9, OUTPUT); // Brakes Motor A
pinMode (13,OUTPUT); // Direction Motor B
pinMode(11, OUTPUT ); // PWM Motor B
pinMode (8, OUTPUT); // Brakes Motor B
}
void loop() {
int Feedback = analogRead (A2);
int Input = analogRead (A4);
int Linpot = analogRead (A5);
Input = map (Input, 0, 1023, 0, 100);
Feedback = map (Feedback, 17, 1023, 0, 100);
Linpot = map (Linpot, 0, 1023, 0, 100)
;Serial.println (Feedback);
Serial.print ("\t");
Serial.println (Input);
Serial.print ("\t");
Serial.print ("\t");
Serial.println (Linpot);
//Motor B
{
if (Linpot > Feedback) {
digitalWrite (13,LOW);
digitalWrite (8, LOW);
analogWrite (11,100);
}
else if ((Linpot ) > (Feedback + 10) || (Linpot) > (Feedback - 10)) {
digitalWrite (8, HIGH);
analogWrite (11,0);
}
else if (Feedback > Linpot) {
digitalWrite (13,HIGH);
digitalWrite (8, LOW);
analogWrite (11,100);
}
}
//Motor A
{
if (Feedback > Input) {
digitalWrite (12,HIGH);
digitalWrite (9, LOW);
analogWrite (3,45 + (Feedback - Input)*3);
}
else if (Input > Feedback) {
digitalWrite (12,LOW);
digitalWrite (9, LOW);
analogWrite (3,45 + (Input - Feedback)*3);
}
else if ((Input ) > (Feedback + 10) || (Input) > (Feedback - 10)) {
digitalWrite (9, HIGH);
analogWrite (3,0);
}
}
}