scaling irregular values for servo

tony_g

Senior Member
another servo scaling post i know, but this one has baffled me.
i am playing with a wii nunchuk controller and reading the i2c data.

now so far all works great and i can see the values in the debug screen and i can use the joystick x and y axis to control the two servos through their full range no problem.

the issue is that i would like to also change the control from the stick to the x and y accelorometer axis and control the servos by tilting the controller in either direction.

this works fine however due to the sensor not giving data in the same linear fashion as the sticks do, i.e 0-255 it is making scaling the values somewhat of a challenge.

the x axis accelorometer data goes from min 68 to max 183 and the y axis min 78 to max 183.

now with the joystick i have no issues scaling 0-255 to 75-225 but with the two accelorometer axis it has got me a bit more stumped :confused:

tony
 
Last edited:

Goeytex

Senior Member
This should do it.

For X-axis conversion ..... New_Val = Val * 150 / 115 - 13
For Y axis conversion ..... New_Val = Val * 150 / 105 - 36

You will need to use word values for the math to prevent overflow

Example for X axis:

Code:
Symbol Val = W0
Symbol New_Val = W1

readadc c.1,Val
New_Val  = Val  * 150 / 115 - 13 [COLOR="#008000"] ' Could simplify to (b0 * 30 / 23 - 13)[/COLOR] 
servopos b.5,New_Val
 

tony_g

Senior Member
thanks goeytex,that worked just as needed.

it threw me off quite a bit not scaling it from linear values and patiently and repetitively mulling over constant equations often leads to a headache lol.
thanks for your help there. ;)
 

bluejets

Senior Member
Could you explain where the -13 and -36 came from.
I can see the range x = 115 is 183-68
and y = 105 is 183-78
and I can see the mid point is 225-75 = 150
 

Billo

Senior Member
The -13 comes from rounding off 75-68*(150/115) and the -36 comes from rounding off 75-78*(150/105)

See this post.
 

Goeytex

Senior Member
The -13 is an offset (constant) and has nothing to do with rounding since nothing was really rounded except the computed offset value necessary with Picaxe interger math. The offset is a part of the underlying math.

Imagine a line on a graph at a given slope. When scaled the line will be tilted at a different angle. To scale we have to get the slope angle of one line to match the other. The lines will be parallel but offset. Since neither line starts at zero (75,68) we have to offset the scaled line up or down until the two are overlay on an extended graph. Then we extend the lines down to zero. Where one line intersects zero the other will be some negative value. This is the offset. Here is the simplified formula:

new_val = val * Scaling Factor + offset


Here is an easy way to do it using the OP's values.

 

Attachments

bluejets

Senior Member
Thanks Goeytex.
I was afraid I would have to do a degree in rocket science to understand the previous explanation from Billo. Sorry, but fact.
 

westaust55

Moderator
Note that for byte variable results (ie 0 - 255) the PICAXE does it's internal computations to/with 16-bit.
As such there is no need for word variables for calcs where the intermediate values are in the range 256 to 65535 but then final result is < 256.
 
Top