Dear all,
I have a problem I have attempted to address a few times now with limited success. I think I even tried to ask this forum once or at least seriously considered it before - I will try to be more clear about what I'm trying to do this time.
There is a signed word variable (steer_input) with $8000 representing 0 returned by a piece of code. Negative number means steer right is requested, positive - steer left. I need to set up accumulator to monitor how big the number has grown to either side from 0 to stop it from growing past, say, dec200 to stop issuing stepper motor commands past that number in that direction. So, limits would be $7F38 (dec32568) and $80C8 (dec32968). On top of that, I do want to reach the limits, so in case the next addition to the accumulator would add up to more than the limit, I would like to calculate the remainder within the limit and output that in steer_input. I have coded this, which seem to make sense, but pushes the steering to one side and is stuck there. I have tried to debug this in the simulator, but I'm just lost as to what the numbers should be after a couple of iterations.
Thank you all for your time,
Edmunds
I have a problem I have attempted to address a few times now with limited success. I think I even tried to ask this forum once or at least seriously considered it before - I will try to be more clear about what I'm trying to do this time.
There is a signed word variable (steer_input) with $8000 representing 0 returned by a piece of code. Negative number means steer right is requested, positive - steer left. I need to set up accumulator to monitor how big the number has grown to either side from 0 to stop it from growing past, say, dec200 to stop issuing stepper motor commands past that number in that direction. So, limits would be $7F38 (dec32568) and $80C8 (dec32968). On top of that, I do want to reach the limits, so in case the next addition to the accumulator would add up to more than the limit, I would like to calculate the remainder within the limit and output that in steer_input. I have coded this, which seem to make sense, but pushes the steering to one side and is stuck there. I have tried to debug this in the simulator, but I'm just lost as to what the numbers should be after a couple of iterations.
Code:
Symbol rght_str_lim = $7F38 '(32568; -200)
Symbol left_str_lim = $80C8 '(32968; +200)
#macro RunSteering()
steer_input = PLine + DLine 'Add everything up into a steering input
if steer_input >= $8000 then
steer_input = -steer_input / InputDiv
acc_str_input = prv_acc_str_input + steer_input 'add up accumulated steer input in a different variable [adding a negative number?! CHECK!!!]
if acc_str_input > rght_str_lim then 'check if accumulated value within limits, if YES...
clear StrRghtLimFlag '... clear right limit flag
clear StrLeftLimFlag 'stepping right, so there is some space to the left now
low DIR 'set steering direction to RIGHT
prv_acc_str_input = acc_str_input 'set the old variable to a new value
RunSteeringStepper
else 'check if accumulated value within limits, if NO...
if prv_acc_str_input != rght_str_lim then 'if there is still space within the limit ...
steer_input = prv_acc_str_input - rght_str_limit '...set steer_input to that space
clear StrRghtLimFlag '... clear right limit flag
clear StrLeftLimFlag 'stepping right, so there is some space to the left now
low DIR 'set steering direction to RIGHT
prv_acc_str_input = rght_str_limit 'set the old variable to a new value
RunSteeringStepper
else 'if there is no space within the limit...
set StrRghtLimFlag '... set right limit flag
clear StrLeftLimFlag 'should not be possible, but just in case
endif
endif
else
steer_input = steer_input / InputDiv
acc_str_input = prv_acc_str_input + steer_input
if acc_str_input < left_str_lim then
clear StrLeftLimFlag
clear StrRghtLimFlag
high DIR
prv_acc_str_input = acc_str_input
RunSteeringStepper
else
if prv_acc_str_input != left_str_lim then
steer_input = left_str_lim - prv_acc_str_input
clear StrLeftLimFlag
clear StrRghtLimFlag
high DIR
prv_acc_str_input = left_str_limit
RunSteeringStepper
else
set StrLeftLimFlag
clear StrRghtLimFlag
endif
endif
endif
#endmacro
#macro RunSteeringStepper(steer_input)
high EN 'Enable the device to steer, disable to save power and limit heat
for counter0 = 0 to steer_input 'Increase the number of loops to steer 'more' per cycle or decrease for 'less'
pulsout STCK, 200 'About a minimum to get decent power
pauseus 120 'pauseus is double the length of pulsout
next
low EN
#endmacro
Edmunds
Last edited: