Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Servo control with a joystick (http://www.chiefdelphi.com/forums/showthread.php?t=84505)

Robototes2412 21-03-2010 22:06

Servo control with a joystick
 
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 21-03-2010 22:33

Re: Servo control with a joystick
 
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:

Code:

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 22-03-2010 02:00

Re: Servo control with a joystick
 
FYI, the general formula for scaling one range of values to another is:

Code:

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 22-03-2010 20:02

Re: Servo control with a joystick
 
Thanks!

Just to verify:

Code:

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


TubaMorg 23-03-2010 09:04

Re: Servo control with a joystick
 
Quote:

Originally Posted by Robototes2412 (Post 941322)
Thanks!

Just to verify:

Code:

    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

Jeremy Germita 09-05-2010 19:31

Re: Servo control with a joystick
 
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


All times are GMT -5. The time now is 09:43.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi