In my project I`am using a geared motor like a Servo, I adapted a pot (100 k), for reading its potition; My Idea is move it to left or right until reach limit points in both ways (almost a 45 degree from mid potition, both ways) I'm using a L293D like h bridge and PWM.
this is my gear motor:
My test video:
this is my code, a simple one, but worked I want to improved, any suggestion? what about PID?
thanks,
//Programa para control de carro v.1.1, dirección
//Selwins Maturana Oct 2012
int pot2 = A5; //joystick dirección
int enable = 3; //Enable IC L293D - PWM control de velocidad
int right = 9; // Señal para ir hacia la derecha
int left = 7; // Señal para ir hacia la izquierda
int LED = 13; //verificación
int sign;// Señal potenciometro moto reductor
void setup(){
pinMode(enable, OUTPUT);
pinMode(forward, OUTPUT);
pinMode(backward, OUTPUT);
Serial.begin(9600);
}
void loop(){
dir = analogRead(pot2); // joystick
sign = analogRead(A3); //Posición del moto reductor
sign = map(sign, 0,1023, 0, 255);
Serial.println(sign);
delay(1000);
if(dir >= 344 && sign > 105) {
digitalWrite(left, LOW);
analogWrite(enable, 70); // Para que gire despacio
digitalWrite(right, HIGH);
delay(1000); // pausa para que no se pase
digitalWrite(right, LOW);
delay(10);
}
else if (dir <= 334 && sign < 127){
digitalWrite(right, LOW);
analogWrite(enable, 80);
digitalWrite(left, HIGH);
delay(1000);
digitalWrite(left, LOW);
delay(10);
}
else
{
digitalWrite(right,LOW);
digitalWrite(left, LOW);
analogWrite(enable, 0);
}
}
Your logic to decide which way to move the motor (and how fast) looks very strange. I'd expect you to decide what the desired position was based on the joystick, compare that with the actual position to determine how far the motor was from the desired position and in which direction, and determine the motor speed and direction from that. Instead you're just comparing the motor and joystick positions against some limits. Presumably this means that the motor is just supposed to go between two fixed positions rather than follow the joystick. Could you clarify what behaviour you're aiming for?
The technique of setting the motor speed and then waiting for a second before you do anything else pretty much guarantys that the motor will overshoot. If I were you I'd do the position comparison as fast as possible and decide what direction/speed you want the motor to move, tell it to move in that direction / speed and then immediately do the position comparison again etc.
PeterH:
Your logic to decide which way to move the motor (and how fast) looks very strange. I'd expect you to decide what the desired position was based on the joystick, compare that with the actual position to determine how far the motor was from the desired position and in which direction, and determine the motor speed and direction from that. Instead you're just comparing the motor and joystick positions against some limits. Presumably this means that the motor is just supposed to go between two fixed positions rather than follow the joystick. Could you clarify what behaviour you're aiming for?
The technique of setting the motor speed and then waiting for a second before you do anything else pretty much guarantys that the motor will overshoot. If I were you I'd do the position comparison as fast as possible and decide what direction/speed you want the motor to move, tell it to move in that direction / speed and then immediately do the position comparison again etc.
the code part is really simple, I just control the motor's direction with the h - bridge (joystick signal, right or left), and I use the pot signal like to limit. now I'm searching about PID (I`m Mech Engineer but automatization is not my strength) I realize improved as had recomendade (control P) but how can I figure out the constant (Kp) to do like you say speed = (desired position - actual position)*Kp