Shop OBEX P1 Docs P2 Docs Learn Events
Need help with a math/logic issue... — Parallax Forums

Need help with a math/logic issue...

markaericmarkaeric Posts: 282
edited 2010-04-07 04:18 in Propeller 1
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):



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

  • kwinnkwinn Posts: 8,697
    edited 2010-04-06 03:37
    In case 2 how do you get a sensor value of 4 when the else statement is for a value greater than 127 ?
  • markaericmarkaeric Posts: 282
    edited 2010-04-06 03:45
    kwinn,

    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.
  • Patrick1abPatrick1ab Posts: 136
    edited 2010-04-06 11:35
    I'm not sure if this is working in every case, but I would do it like this:

    
    ...
    (case 2)
    if offset_var > sensor_value then
      targetvalue := 512 - (offset_var - sensor_value)
    else
    targetvalue := sensor_value - offset_var
    
    
  • markaericmarkaeric Posts: 282
    edited 2010-04-07 04:18
    Thanks Patrick1ab!

    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
Sign In or Register to comment.