Yep it had to happen! I promise I keep it simple(ish).
Now I have the PS3 controller values as inputs and the servo PWM module as outputs and I need a way of linking the two together. For now, the link input -> output is going to be simple to allow manual control but later this will be altered to add automation.
The joystick values are from -1.0 .. +1.0 and so the first thing is to add +1 to the value to make positive 0.0 .. +2.0. The PWM is an integer value as opposed to floating-point so we’ll correct that later. The output values we need for the PWM are 150 (min value) to 600 (max value); so we are scaling 0.0 .. +2.0 into 150 .. 600. OK so far?
I promise I keep it simple
Tough bit now!
- CorrectedInput = input +1
- Span = (max – min) / 2
- Output = (CorrectedInput * Span) + min
- IntegerOutput = int(Output)
- Line 1, is input from the PS3 controller of 0.0 .. +2.0.
- Line 2, calculates the required span of the output (max – min, e.g. 450) and divided by 2 (e.g. 225) to correct for the input being 0.0 .. +2.0.
- Line 3, CorrectedInput multiplied by the Span gives the required value (e.g. 0.0 .. +450.0) and adding min gives the offset (+150.0 .. +600.0).
- Line 4, converts the floating point number into integer for the PMW module to use.
In practice I found a couple of issues:
- The servos don’t travel full range and so I reduced min & max to suit.
- The joystick inputs were backwards to what was required for the direction of the vehicle and so, I subtracted the CorrectedInput from 2.0 to swap around the value (e.g. +2.0 .. 0.0 as opposed to 0.0 .. +2.0).
A snippet of code to do this might be :
# Axis_IPs  is the input from axis 0 on the joystick # PWM_OPs  is the output to channel 0 on the PWM module max = 600 min = 150 span = (max - min) / 2 CorrectedInput = 2 - (Axis_IPs  + 1) Output = (CorrectedInput * span ) + min PWM_OPs  = int(Output)