PDA

View Full Version : Servo control with a joystick

Robototes2412
03-21-2010, 09:06 PM
Hello.

My team wants me to get a servo rigged to be controlled with a joystick. How do I convert joystick values (-1 thru. 1) to servo values (0 thru. 1)?

TubaMorg
03-21-2010, 09:33 PM
Use the Servo class.
It has two methods you can use:

set(double value) where value = 0.0 to 1.0 for full range of motion

setAngle(double degrees) where degrees = position of the servo...values in excess of allowable range of motion simply saturate to the servo limit. The API assumes that the kit servos are linear.

So to answer your question. Since the joys run from -1 to 1, you can simply use a multiple for the set method:

Servo servo = new Servo(1);//Servo in PWM port 1
double servoIn = (joy.getX() +1)*0.5;

// Command to servo
servo.set(servoIn);

That should do it.

FRC4ME
03-22-2010, 01:00 AM
FYI, the general formula for scaling one range of values to another is:

newValue = newMax + (newMax - newMin) * (oldValue- oldMax) / (oldMax - oldMin)

In this case, oldMin and oldMax are -1 and 1, respectively (the joystick range), while newMin and newMax are 0 and 1, respectively (the servo range).

Robototes2412
03-22-2010, 07:02 PM
Thanks!

Just to verify:

public double convert(double input) {
//newValue = newMax + (newMax - newMin) * (oldValue- oldMax) / (oldMax - oldMin)
input = 1 + (1 - 0) * (input- 1) / (1 - -1);
return input;
}

TubaMorg
03-23-2010, 08:04 AM
Thanks!

Just to verify:

public double convert(double input) {
//newValue = newMax + (newMax - newMin) * (oldValue- oldMax) / (oldMax - oldMin)
input = 1 + (1 - 0) * (input- 1) / (1 - -1);
return input;
}

You can verify it by plugging in your input extremes (-1 to +1). I haven't had my coffee yet, but it doesn't seem like -1 converts to 0. Why not try the one I posted? KISS

jeremypg399
05-09-2010, 06:31 PM
I'm probably REALLY late to the party, but here's the solution I was taught.

double servOut = (stick.getRawAxis(1) + 1) / 2;

-1.0 becomes 0 and +1.0 becomes 1.0