Need help with a math/logic issue...
markaeric
Posts: 282
Fellow propeller-heads,
I'm reviving a project that was originally intended to use the SX, but have since decided that I would like to do it with the prop instead. What I need to do is read a 9-bit absolute position sensor that is not rotation limited (so it jumps from 511 max value to 0 and vice versa), and need to calculate the offset based on the value from the sensor, and the desired value(127). Here's the basic gist of what I got (in no particular programming language):
In the event that the value of offset_var is greater than sensor_value in (case 2), what can I do to get the desired results? For instance:
sensor_value = 4
offset_var = 6
I would like to have 4 - 6 = 510 (9-bit precision)
Would I simply be storing the 9-bit variables in 16-bit words, and then cut off the 7 MSBs?
And since I like to make things difficult for myself apparently, I'll be attempting at writing this in assembly.
Thanks!
Mark S.
I'm reviving a project that was originally intended to use the SX, but have since decided that I would like to do it with the prop instead. What I need to do is read a 9-bit absolute position sensor that is not rotation limited (so it jumps from 511 max value to 0 and vice versa), and need to calculate the offset based on the value from the sensor, and the desired value(127). Here's the basic gist of what I got (in no particular programming language):
if sensor_value < 127 then offset_var = 127 - sensor_value (case 1) target_value = sensor_value + offset_var else if sensor_value > 127 then offset_var = sensor_value - 127 (case 2) target_value = sensor_value - offset_var
In the event that the value of offset_var is greater than sensor_value in (case 2), what can I do to get the desired results? For instance:
sensor_value = 4
offset_var = 6
I would like to have 4 - 6 = 510 (9-bit precision)
Would I simply be storing the 9-bit variables in 16-bit words, and then cut off the 7 MSBs?
And since I like to make things difficult for myself apparently, I'll be attempting at writing this in assembly.
Thanks!
Mark S.
Comments
Oops.. I shouldn't have written it like I did.. The if and else if conditions are executed more or less at the start of the program to determine the offset. It's the case statements that are repeatedly executed during run time, where the sensor_value will range from 0 to 511 regularly.
It does look like your math works, but I have found that I can simply perform the subtraction, and mask off the upper bits that I don't want.. At least as far as I've been able to tell.
Mark S.
edit:
Apparently I can simplify it further by just performing the elseif and (case 2) block of code - no need for the first block at all!
Post Edited (markaeric) : 4/7/2010 5:09:09 AM GMT