How to control continuous rotation servo with Spark?

Hi,
I’m doing a mini project with Spark and a servo. I’ve a parallax continuous rotation servo. I’m not sure how to make the servo make exactly one 360 degree rotation and stop. I need to do it in reverse as well. Using myServo.write(90), I was able to position a 180 degree servo to position at 90 and reverse using myServo.write(0). However, on my continuous rotation, I’m unable to do so. Also, my servo is running out of the center position. Each time it is having a new center. This makes my project unreliable. How can I fix the above problems?

Have you had a try with the calibration procedure described here?
http://learn.parallax.com/KickStart/900-00008

If you’ve got a long running sketch or multiple full rotations with no way to recalibrate your “home” position, you might also conside some means of position sensing (e.g. light barrier, hall sensor, …).

1 Like

@ScruffR, Yes, I’ve already tried those calibration guidelines from Parallax website. Not helping me in anyways. The home position outruns each time from the actual starting point :cry:

OK, not having fiddled with these servos before I can only be of theoretical help, but to me it looks like a timing issue.

As I understand the workings of these servos to keep track of the position you need to keep track of the exact timing and speed at any given time. While this shouldn’t be too far off if the only code running on your system is your own, it might be more tricky with the cloud interfering on the timing.
But a look at your code would help us here to understand the cause of the problem a bit better :wink:

Without extra hardware, you might get better results if you make use of millis()/micros() in conjunction with servo.writeMicroseconds(). While one controls the speed the other can be used to measure the time you had your servo run that speed.

But the most reliable way to find back to your home position might still be by using a position sensor, like suggested above.

2 Likes

Hi @sriram155

I think @ScruffR is correct and you are going to have trouble getting a continuous rotation servo to go exactly one revolution without another sensor like an encoder of some sort. You are fighting against the accuracy of the core’s clock that generates the control signal versus the “clock” inside the servo that measures the control signal and my guess is that the servo is just not very accurate.

If you are not interested in positions except one full rotation either way then you could use a slot interrupter with one narrow slit. Another approach would be a magnet on the wheel or shaft that triggers a reed switch or hall-effect sensor at “top dead center” of the wheel or shaft.

2 Likes

@ScruffR, here is the script I’m trying out. Let me know if you find anything fishy…I can get successful results with a 180 degree servo just by replacing the 360 with 180 in the loop. However, on a continuous rotation servo, it goes haywire.

Servo myservo;

int Status(String command);
int State = -1;

int pos = 0;

void setup() 
{ 
  Spark.function("motor", Status);
  Spark.variable("MotorState", &State, INT);
  pinMode(D7,OUTPUT);
  myservo.attach(D0);
} 


int Status(String command)
{
    if (command.toInt() == 1)
    {
        for(pos = 0; pos < 360; pos += 1)
        {
            myservo.write(pos);
            delay(15);
            digitalWrite(D7,HIGH);
            return 1;
        }
    }
    else if (command.toInt() == 0)
    {
        for(pos = 360; pos>=1; pos-=1)
        {
            myservo.write(pos);
            delay(15);
            digitalWrite(D7,LOW);
            return 0;
        }
    }
    else return -1;
}

Hi @bko, thanks for the suggestions. I’ve designed some gears to match the continuous rotation servo. Now, with this problem, I’m thinking of re-designing the gears and just use a 180 degree rotation. All i wanted was a +/- 90 degree rotation. I only have 180 degree and continuous rotation servo. Initially designed a 4:1 gear reduction to get my 90 degree turn. However, the continuous servo is not working as expected. I’ve posted my code above. Let me know if you find anything that could help resolve the issue.

FYI: I’ve already calibrated the servo based on the Parallax calibration instruction.