stop a cog while it's busy?
xanadu
Posts: 3,347
I'm using coginit and cogstop. Inside the new cog is a timer, and while the timer is running the cog won't shut off using cogstop. Is there a way to make the cog stop even though it is executing an instruction or can the new cog only stop between instructions?
Thanks!
Thanks!

Comments
-Phil
"z", "Z": coginit(4,patrol, @stack[0]) 'camera patrol mode on xbee.str(string(" Cam Patrol On ")) "c", "C": cogstop(4) 'camera patrol mode off xbee.str(string(" Cam Patrol Off "))The above code listens for z or c character on XBee.
pub patrol servo.set(2, P_POS1, 2) 'init pos servo.set(3, T_POS1, 2) pause (15000) repeat servo.set(2, P_POS2, 2) P_POS := P_POS2 'sync pause (30000) servo.set(3, T_POS2, 2) T_POS := T_POS2 'sync pause (5000) servo.set(2, P_POS1, 2) P_POS := P_POS1 'sync pause (30000) servo.set(3, T_POS1, 2) T_POS := T_POS1 'sync pause (5000)The above code moves the pan tilt servos to predefined positions.
Thanks a lot!
repeat until P_POS2 := P_LIMIT servo.set(2, P_POS2, 2) P_POS2 := P_POS2 + 1That way it's not waiting until the end like below?
repeat servo.set(2, P_POS2, 2) P_POS := P_POS2 pause (30000)[Edit] Am I mistaken or is that my servo object (jm_servo8_adv) you're using? That would explain why the servo doesn't stop -- the movement to destination is still in progress. Maybe I should add a halt method that will stop the servo where it is.
pub patrol repeat until P_POS == P_POS2 'P_POS pan current position, P_POS2 pan limit right servo.set(2, P_POS, 2) 'move the pan servo to the limit P_POS := P_POS + 1 'slowlyI thought that without the pause the cog has to stop instantly because it's going line to line fast.
Here is how I'm stopping the cog:
Hey JM thanks, I'm using your servo object. Working great again on yet another project. What you said makes sense I hadn't even thought about the servo object. I'm still really confused about why shutting down my cog that calls your object wouldn't stop the servo though. I'm trying to write a demo code not involving servos just LEDs and timers to work it out.
Add these to methods to your copy of the object:
pub halt(ch) '' Halts servo at current position '' -- allows iteruption of programmed move if ((ch => 0) and (ch =< CH_LAST)) ' legal channel? target[ch] := pos[ch] ' set target to current pos pub halt_all '' Halt all servos at current position bytemove(@target, @pos, 8) ' update all targetsThen you can update you patrol method to look like this -- it's more work, but it allows the method to stop the servos before committing on-demand hari-kari:
pub patrol | t servo.set(2, P_POS1, 2) 'init pos servo.set(3, T_POS1, 2) pause (15000) repeat servo.set(2, P_POS2, 2) P_POS := P_POS2 'sync t := cnt repeat 30000 waitcnt(t += clkfreq / 1_000) if (killpatrol == true) servo.halt(2) servo.halt(3) cogstop(cogid) servo.set(3, T_POS2, 2) T_POS := T_POS2 'sync t := cnt repeat 5000 waitcnt(t += clkfreq / 1_000) if (killpatrol == true) servo.halt(2) servo.halt(3) cogstop(cogid) servo.set(2, P_POS1, 2) P_POS := P_POS1 'sync t := cnt repeat 30000 waitcnt(t += clkfreq / 1_000) if (killpatrol == true) servo.halt(2) servo.halt(3) cogstop(cogid) servo.set(3, T_POS1, 2) T_POS := T_POS1 'sync t := cnt repeat 5000 waitcnt(t += clkfreq / 1_000) if (killpatrol == true) servo.halt(2) servo.halt(3) cogstop(cogid)There may be a way to make this more elegant but without the full listing and understanding the intent, I'm taking the easy route. Of course, killpatrol is a global variable. You need to set this back to false before relaunching patrol.
pub patrol | idx, delay, t idx := 0 repeat case idx 0: servo.set(2, P_POS1, 2) servo.set(3, T_POS1, 2) delay := 15_000 1: servo.set(2, P_POS2, 2) P_POS := P_POS2 delay := 30_000 2: servo.set(3, T_POS2, 2) T_POS := T_POS2 delay := 5_000 3: servo.set(2, P_POS1, 2) P_POS := P_POS1 delay := 30_000 4: servo.set(3, T_POS1, 2) T_POS := T_POS1 delay := 5_000 t := cnt repeat delay waitcnt(t += clkfreq / 1_000) if (killpatrol == true) servo.halt(2) servo.halt(3) cogstop(cogid) idx += 1 if (idx == 5) idx := 1servo.halt(2) servo.halt(3) cogstop(4)I'm adding in the method, by your example so I can keep the vars in sync for coming out of patrol into manual, otherwise I'd probably just do the above only.
Thanks!!
Or I'll just use this... hehe thanks!