Function Confusion
ajward
Posts: 1,130
Hi All...
I know there's probably a simple answer, but I'll be darned if I can find it!
After getting my tracked vehicle running with the Stingray demo code, I've started using Kye's "PWM2C_HBDEngine" from the OBEX for more control. My code (See below) has functions to ramp up, ramp down and run forward and pivot turn to the right. Running the first three functions on the MSR1 board, Ted ramps up, runs forward for a bit, ramps down and stops. However, when I add the fourth function "pivot_rt", he ramps up runs forward, ramps down, but the vehicle never stops pivoting. What am I missing here?
Any wisdom appreciated!!!
Amanda
PS- Happy Monday!
I know there's probably a simple answer, but I'll be darned if I can find it!
After getting my tracked vehicle running with the Stingray demo code, I've started using Kye's "PWM2C_HBDEngine" from the OBEX for more control. My code (See below) has functions to ramp up, ramp down and run forward and pivot turn to the right. Running the first three functions on the MSR1 board, Ted ramps up, runs forward for a bit, ramps down and stops. However, when I add the fourth function "pivot_rt", he ramps up runs forward, ramps down, but the vehicle never stops pivoting. What am I missing here?
Any wisdom appreciated!!!
Amanda
PS- Happy Monday!
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
_leftFW = 25
_leftBW = 24
_rightFW = 26
_rightBW = 27
_timeStepInMilliseconds = 20
_updateFrequencyInHertz = 8_000
OBJ
hbd: "PWM2C_HBDEngine.spin"
Pub Main
ramp_up
waiTcnt(clkfreq + cnt)
run_fwd
waitcnt(clkfreq + cnt)
ramp_dn
waitcnt(clkfreq + cnt)
pivot_rt
waitcnt(clkfreq + cnt)
PUB ramp_up | timeCounter, frequencyCounter
ifnot(hbd.HBDEngineStart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz))
reboot
timeCounter := ((clkfreq / 1_000) * _timeStepInMilliseconds)
repeat frequencyCounter from 0 to 1000 step 50
hbd.leftDuty(frequencyCounter)
hbd.rightDuty(frequencyCounter)
waitcnt(timeCounter + cnt)
return
PUB run_fwd | timeCounter, frequencyCounter
ifnot(hbd.HBDEngineStart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz))
reboot
timeCounter := ((clkfreq / 1_000) * _timeStepInMilliseconds)
repeat 2
frequencyCounter := 1000
hbd.leftDuty(frequencyCounter)
hbd.rightDuty(frequencyCounter)
waitcnt(timeCounter + cnt)
return
PUB ramp_dn | timeCounter, frequencyCounter
ifnot(hbd.HBDEngineStart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz))
reboot
timeCounter := ((clkfreq / 1_000) * _timeStepInMilliseconds)
repeat frequencyCounter from 1000 to 0 step 50
hbd.leftDuty(frequencyCounter)
hbd.rightDuty(frequencyCounter)
waitcnt(timeCounter + cnt)
return
PUB pivot_rt | timeCounter, frequencyCounter
ifnot(hbd.HBDEngineStart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz))
reboot
timeCounter := ((clkfreq / 1_000) * _timeStepInMilliseconds)
repeat 2
frequencyCounter := 1000
hbd.leftDuty(frequencyCounter)
hbd.rightDuty(-frequencyCounter)
waitcnt(timeCounter + cnt)
return

Comments
I suspect you aren't allowed a negative rightDuty value?
need to write return in each one unless there is a variable you would like to return.
Thanks for your reply Mark!
Yeah... I guess that has to be it. I've used that obex module before and I could swear I've sent negative values to the function before. Of course, I can't find my previous code to verify. <grumble>
Anyhow... thanks again!!!!
Amanda
Amanda
I'm pretty sure you're right about negative values being valid. I think negative values put the motor in reverse.
It would be easier if you attached the code as an archive and then we could also see the child object.
Edit: Duane is right you have to put a negative # to go one way and a positive # to go the other with 0 being stop.
Con _CLKMODE = XTAL1 + PLL16X _XINFREQ = 5_000_000 _leftFW = 24 _leftBW = 25 _rightFW = 26 _rightBW = 27 '_timeStepInMilliseconds = 10 _updateFrequencyInHertz = 8_000 RAMPING = 5 '*100us delay per step duty cycle Var long left_old, right_old Obj hbd : "pwm2c_hbdengine" Pub Main hbd.hbdenginestart(_leftFW, _leftBW, _rightFW, _rightBW, _updateFrequencyInHertz) 'start pwm ' Wheels(-500, 500) 'forward @ half speed waitcnt(clkfreq+cnt) 'pause before setting wheels again Wheels(500, -500) 'reverse @ half speed waitcnt(clkfreq+cnt) 'pause before setting wheels again Wheels(500, 500) 'turn left @ half speed waitcnt(clkfreq+cnt) 'pause before setting wheels again Wheels(-500, -500) 'turn right @ half speed waitcnt(clkfreq+cnt) 'pause before setting wheels again Wheels(0, 0) 'stop Pub Wheels(left, right) repeat if left < left_old 'increment or decrement by one to new speed' hbd.leftduty(--left_old) if left > left_old hbd.leftduty(++left_old) if right < right_old hbd.rightduty(--right_old) if right > right_old hbd.rightduty(++right_old) if left_old == left 'check if both left and right wheels- if right_old == right '-are @ new speeds and return from- left_old := left '-this method when met right_old := right return waitcnt(clkfreq / 10000 * RAMPING + cnt) 'RAMPING * 100us pause per step duty cycle