As we know, a continuous turn servo loses it's ability to "know" where it's position is, and thus can't be 1:1 controlled with a pot like a regular servo. It is essentially turned into a motor.
I made this code to sort of help remedy that. It puts back some of the control in a continuous servo. The reason I did this was so have a servo that could rotate past 180 degrees. Here's a video and the code:
// Control a continous servo or a motor with a pot in a similar manner as a standard servo.
// How it works: It takes the value of the pot, and stores it in a variable. It waits X amount of time (200 MS works great.)
// Then it takes the value of the pot again, and stores it in another varable. Then, it finds the difference between these two values
// and maps it from 0-180, which tells the servo how fast and in what direction to go.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // Unmapped after it has ben mapped
int unmapped; // The difference before it is mapped
int valueA; // Value of the pot at first reading
int valueB; // Value of the pot at second reading
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
valueA = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
bloop();
}
void bloop() {
delay(200);
valueB = analogRead(potpin);
aloop();
}
void aloop() {
unmapped = (valueB - valueA); // Find difference
val = map(unmapped, -200, 200, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
loop();
}
I'm not sure if it's my board, but for some reason it tends to get stuck in the B loop after a bit. I'm still working on it.
Having loop() call bloop() which calls aloop() which calls loop() is going to eventually crash the program.
This version does the same thing but will not crash the system with recursive function calls.
void loop()
{
valueA = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
delay(200);
valueB = analogRead(potpin);
unmapped = (valueB - valueA); // Find difference
val = map(unmapped, -200, 200, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
}
Does the servo still have the end-of-position physical stops??
I think the pots usually used also have a rotation limit. IF they were continuous-rotation there would be other possibilities.
I source continuous-rotation servos like these: http://?.ws/YD-servos and I need to take one apart to see what the factory did with the pot (or NOT)...
I took apart the servo, removed the gearing, and then sent a command for it to rotate to 90. I rotated the now exposed internal pot until the motor stopped rotating. I then superglued it in place.
I took the gear with the hard stop, and because they were metal gears, the stop was a pin sticking out that I pulled out with pliers.
Finally, I took a large screwdriver and decimated the inside of the gear they went over the internal pot so it wouldn't rotate with it anymore. I reassembled the gears, sealed it back up, and it was now a continuous-turn servo.
I did it this way because I wanted a high torque servo (A Towardpro MG946R) that was continuous and cheap.
That's correct. Sending it degree values changes the speed and direction instead now. 0 is full speed one way and 180 is full speed the other, 90 is stopped.