Page 1 of 1

Continuous Rotation Servo w/ NXTServo

Posted: 14 Feb 2013, 06:05
by calebalbers
Hey all,
I am working on a project that utilizes the NXTServo from Mindsensors. The project is an arm that uses 9 servos (two are in a Y-harness to accomodate for NXTServo's 8 ports). Anyways, I have the program working with regular servos, but i am having trouble when I try to use continuous rotation servos. I understand that when I give a command to move the servo to a certain position, the servo will continue to rotate as there is no feedback mechanism in place. However, I have no idea how to stop this rotation. My code always gets stuck waiting for that servo's feedback, which it will never get. So... how would I go about setting it up so that it "breaks" the command for the servo to go to a position, or maybe only makes it last for a certain amount of time?

Just to make things a little clearer, I am using NXC along with the standard libraries for NXTServo and PSP-Nx that I got from the Mindsensors website. I have tried variations of the following, basically putting the "if button is pressed, set position to x" statement in a while loop so that it only executes while the button is pressed, but this doesn't fix it.

Code: Select all

// Pos_Servo2 is the value for the position of servo #2, if it wasn't clear :P
while (currState.square == 1) {
       if (currState.square == 1) {
            Pos_Servo2 += 10;
            NXTServo_SetPosition(S1, ServoAddr, 2, Pos_Servo2);
       }
        else {
            Pos_Servo2 = 1500;
            NXTServo_SetPosition(S1, ServoAddr, 2, Pos_Servo2);
       }
}
I tried looking at sample code from Mindsensors but found it not to be of much help. I know that what I am trying to do can be done, I'm just not really sure how to go about it. I'm sure it's something really easy and simple that I am probably just missing. I tend to do that :? Anyways, could anyone point me in the right direction?
Thanks!

Re: Continuous Rotation Servo w/ NXTServo

Posted: 14 Feb 2013, 15:58
by hassenplug
I've not used the servos, so I'm just guessing a bit here.

First, there seems to be an error in your program. The If statement immediately following the while condition should almost always evaluate to true. So, it will most likely not reach the false case.

The false case should correctly stop the servo. (but those lines are never reached)

I think you either want to move the false case outside the while (and remove the if) or remove the condition from the while, so it will always loop.

Hope that helps.

Steve

Re: Continuous Rotation Servo w/ NXTServo

Posted: 14 Feb 2013, 19:31
by calebalbers
Ah, I should have thought of that! So are you saying possibly doing something like this?

Code: Select all

if (currState.square == 1) {
     Pos_Servo2 += 10;
     NXTServo_SetPosition(S1, ServoAddr, 2, Pos_Servo2);
}
else {
     Pos_Servo2 = 1500;
     NXTServo_SetPositioin(S1, ServoAddr, 2, Pos_Servo2);
}
If I'm not mistaken that should make the servo only rotate while the button is pressed? Or would I do better to exchange that IF statement for a WHILE statement, as I fear the IF statement might make the servo studder a bit too much?

Sorry for my lack of understanding, I'm not the most familiar with programming. I appreciate the response and help though. I can't test any code right now, but I should be able to when I get home this evening.
Thank you!

Re: Continuous Rotation Servo w/ NXTServo

Posted: 15 Feb 2013, 04:41
by calebalbers
Well, I was able to play around with the code tonight and I got it working!
What finally worked for me was:

Code: Select all

if (currState.r_j_y < -15) {
    Pos_Servo2 += 5;
    Pos_Servo3 -= 5;
    NXTServo_SetPosition(S1, ServoAddr, 2, Pos_Servo2);
    NXTServo_SetPosition(S1, ServoAddr, 3, Pos_Servo3);
}
else if (currState.r_j_y > 15) {
   Pos_Servo2 -= 5;
   Pos_Servo3 += 5;
   NXTServo_SetPosition(S1, ServoAddr, 2, Pos_Servo2);
   NXTServo_SetPosition(S1, ServoAddr, 3, Pos_Servo3);
}
else {
     Pos_Servo2 = 1500;
     Pos_Servo3 = 1500;
     NXTServo_SetPosition(S1, ServoAddr, 2, Pos_Servo2);
     NXTServo_SetPosition(S1, ServoAddr, 3, Pos_Servo3);
}
As you can see, I modified it to fit two servos that were paired opposite eachother, and I switched the control to the joystick. But anyways, the if, else if, and else statements made it work fairly flawlessly.

Thanks for the help, I'm really glad to have such a great forum here!