1

how can I see the values that come from the different joystick axis? For example, if i press the joystick half up, that there is a value of 1600 (all the way up: 3200). There is the event.axis, but it can just be 0 or 1.

I already connected the ps3 controller using the pygame library and wrote the program to control a dc motor, everything works fine, but i want to use the pulse with modulation to control the dc motor. So the more I go with the joystick up, the faster it rotates. Because of that I need the exact values to use the different pulse with modulations and not just 0 or 1.

Hope that you guys understand me.

Regards

1

You will probably want to have a "calibration" procedure that directs the user to manipulate all the joystick controls that are not simple "up"-"down" buttons. You will need to ask the user to take each control to the extremes that each one has and have the code notice what values are detected at those extremes - on some systems (old PC analogue joysticks with 15-pin Sub-D connectors being one example) the actual range of value encountered may only be a small fraction of the potential range. Then you can interpolate subsequent values on that scale Xn by:

For an axis x let the minimum and maximum values detected be xmin and xmax then a value xn can be converted to a percentage with:

        ( xn - xmin  )
value = -------------  * 100%
        ( xmax - xmin )

This can be enhanced by also recording the "default" or centre position when the controls are released (if there are centering mechanisms) or held there by the user (if not). This can be used to provide a "dead-zone" (range of values) for which the control can be considered to be in its "default" position which is particularly useful if you want to emulate a clockwise-stop-anticlockwise type motor control so that there has to be a minimum amount of movement away from the default position before an current is supplied to the motor. This is also a type of hysteresis and makes the design of electronics a bit easier if you are using an 'H'-type control system as in this "dead-zone" all legs of the "H" are deactivated - without it there is a risk that the design might try to drive all four legs at once instead of only two diagonally opposite ones!

For your case: xmin is 0 and xmax is 3200 so the formula degenerates to:

         xn
value = ---- * 100 %
        3200

Dropping in the centre value of 1600 gives the expected result of 50%.


Revision

Given your end-use case you may want to do the calculations slightly differently by producing a value that ideally goes from -1.0 to 1.0 with 0.0 in the middle, now we want to consider only half of the range and using the one with the larger magnitude to work with. For this the "minimum" is the value at the control's centre and the "maximum" is the extreme value in the relevant direction. Consider a case where the minimum control value is 100 (not 0), the maximum control is 3150 (not 3200) and the centre value is 1680. The upper "half" is 3150 - 1680 = 1470, the lower "half" is 1680 - 100 = 1580. The lower has the larger magnitude and we want the result then to be -1.0 so posting those values in gives:

        (  xn - 1680 )
value = --------------  * -1.0
        ( 100 - 1680 )

Let us try this formula with the values we have for the extremes and the centre value:

xn    ||   100 |  1680 |  3150
------------------------------
value || -1.00 |  0.00 |  0.93

You would than want to add a "dead-zone" so that if( abs( value ) < 0.1 ) then value = 0.00 so that you do not try putting a very small amount of current through the motor - depending on the type of DC motor it probably won't produce any significant torque for small currents anyhow and this will prevent anything happening until there is a definite movement of the joystick control from the default position.

I think the event.axis value tells you which axis (control) produced the event that the event is reporting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.