Good afternoon.
Im trying to control a servo using the rotational speed of a shaft.
I've already achieved to control the servo with the position that the rotary encoder gets.
But now, i would like to control its position using the shaft's rotational speed.
This is the script that I am using to control the servo:
void loop() {
// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);
// If the previous and the current state of the inputCLK are different, then a pulse has occurred
if (currentStateCLK != previousStateCLK) {
// If the inputDT state is different than the inputCLK state, then the encoder is rotating clockwise
if (digitalRead(inputDT) == currentStateCLK) { // Invertida la condición
counter--;
if (counter < 0) {
counter = 0;
}
} else {
// Encoder is rotating counterclockwise
counter++;
if (counter > 180) {
counter = 180;
}
}
// Move the servo
myservo.write(counter);
Serial.print("Position: ");
Serial.println(counter);
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
}
How can I for example make the servo to get in 90º when the shaft achieves 4500 rpm?
My encoder gives 600 pulses when making one turn.
Thank you in advance.
What encoder are you using to get the rpm of the shaft? I there a z output of the encoder with one count per rotation. A 600 ppr quadrature encoder is not best suited for your task.
There are many available tutorials about measuring rpm with an Arduino.
How fast is the shaft rotating? You may want to use interrupts instead of polling to read the encoder.
if this is what you want, the servo will move clockwise (?) as you spin the encoder and return to it's zeroth (CCW position) when you stop.
presumably a speed calculation is possible(!?) with each increment of the encoder by measuring the time between increments (speed = 1/ time) and there is a servo write with each increment, calculation.
what happens if you miss one increment? divide the time by 2, 3? i've found that i need to use an interrupt when turning an encoder with fingers (and doing lottsa other stuff)
If you mean an an RC servo, you cannot control rotation speed, only position by controlling width of input pulses. Speed is fixed by internal controller.
Good afternoon @alto777
The true is that i want to control a brushless esc with the encoder's signal.
The esc moves with the same signal than the servo, so i had explained my problem with the servo because i think that usually we are more familiarized to work with servos.
Im building a hybrid rc car, synchronizing a nitro thermic motor and a brushless electric motor.
So, I will try the solutioin that our buddy @sherzaad sugested to me.
The truth is that you are having more fun than I am.
This could turn out to be very challenging - as far as you have described, you have an open loop control problem. The simple idea of measuring the RPM and dictating to the ESC by its servo signal may not achieve a very satisfactory synchronization.