Proportional Servo Control Using PS3 Controller Joystick



So I have successfully added the code to operate a Servo connected to my UltraBorg using the D Pad Fwd and Back buttons. This lifts an arm fitted to the Robot up and down to predetermined upper and lower positions.

What I am trying to do now is move a servo using the RH Joystick on the PS3 Controller to give more precise movement rather than just all the way up or all the way down.

I know how to identify the Joystick Fwd and Back but I am not sure how to link the Joystick Movement to UB.SetServoPosition1(ServoX)

Any help would be great.


Do I assign the value of the joystick position to the variable upDown using:
upDown = joystick.get_axis(axisUpDown)

And then apply the value of upDown to the Servo using:
servo1 = upDown

piborg's picture

The code you have above should work fine, the servo will move between the limits set over the range of the joystick movement, setting the position to 0 when the joystick is centred.

If the movement goes the wrong direction you can swap it by adding a - sign like this:

upDown = joystick.get_axis(axisUpDown)

servo1 = -upDown

It worked but the servor is moving to fast. How do I slow it down?

piborg's picture

You can get the joystick to control the speed of the servo instead of the position by increasing / decreasing the servo value based on the joystick value. This will mean that when the joystick is released the servo will stay put.

All you need to do is add to the servo1 value each time like this:

# Settings
servo1Speed = 0.05

# Setup code
servo1 = 0.0

# Where you read the joystick values
upDown = joystick.get_axis(axisUpDown)

# Where you set positions
servo1 += upDown * servo1Speed

The servo1Speed value controls the maximum speed the servo changes at when the joystick is at full. Larger numbers will move faster and smaller numbers will move slower.

Subscribe to Comments for "Proportional Servo Control Using PS3 Controller Joystick"