(Hopefully) A simple question, possibly about variable scope
Hello! I hope someone can point me in the right direction...
I have an object with several variables and methods. One method is run on a cog and it continuously increments one value in main memory and checks another. This appears to be working. Another method, not run on its own cog, tries to wait until that first value reaches a certain size (I modeled this off of waitcnt) and changes the second value. However, I don't seem to ever escape the loop.
Here is the code that I believe to be malfunctioning:
This is an edited version of the method running on the cog. I trimmed out some of the unnecessary portions that affect neither currentState or totalTicks. It's using the Mouse object to monitor the wheels of a robot, keeping them in sync. I'm taking advantage of the demo board's PS/2 ports and using a hacked mouse as a dual encoder. stateAddress is the address of currentState, and ticksAddress is the address of totalTicks.
My little robot dutifully starts turning on the line "currentState := dir" but never stops; it just keeps spinning on the spot.
Is there anything special about repeat in this case? Is the current value of the variable totalTicks somehow not getting updated in the loop? The rest of the code seems to be working correctly; the motors change direction when currentState is changed and are kept in almost perfect sync. I confess that I don't know for a fact that totalTicks is incrementing, but I can't see why it wouldn't. These are the only places that totalTicks is referenced, other than the cog being started with @totalTicks as an argument to the method.
I apologize if this is all overly vague, but can anyone offer some advice? Am I making some obvious newbie mistake?
I have an object with several variables and methods. One method is run on a cog and it continuously increments one value in main memory and checks another. This appears to be working. Another method, not run on its own cog, tries to wait until that first value reaches a certain size (I modeled this off of waitcnt) and changes the second value. However, I don't seem to ever escape the loop.
Here is the code that I believe to be malfunctioning:
[b]pub[/b] Turn (dir) | t ApplyBrakes t := totalTicks + TURNING_TICKS currentState := dir [b]repeat[/b] [b]while[/b] totalTicks < t [b]waitcnt[/b](clkfreq / 16 + [b]cnt[/b]) ' Let cog run ApplyBrakes currentState := FORWARD
This is an edited version of the method running on the cog. I trimmed out some of the unnecessary portions that affect neither currentState or totalTicks. It's using the Mouse object to monitor the wheels of a robot, keeping them in sync. I'm taking advantage of the demo board's PS/2 ports and using a hacked mouse as a dual encoder. stateAddress is the address of currentState, and ticksAddress is the address of totalTicks.
[b]pri[/b] RunMotors (stateAddress, ticksAddress) | l, r ' get absolute values of mouse position l := ||(mouse.delta_x) r := ||(mouse.delta_y) ' If one wheel is ahead of the other, slow it down. ' Just cut its power and put it in 'free' mode (0,0). [b]if[/b] r > l [b]outa[/b][noparse][[/noparse]MOTOR_R1..MOTOR_R2]~ [b]long[/b][noparse][[/noparse]ticksAddress] += l [b]elseif[/b] l > r [b]outa[/b][noparse][[/noparse]MOTOR_L1..MOTOR_L2]~ [b]long[/b][noparse][[/noparse]ticksAddress] += r [b]else[/b] [b]long[/b][noparse][[/noparse]ticksAddress] += r ' Reset mouse coordinates mouse.delta_reset ' Let the motors do their thing for a bit. ' Should be about 1/20th of a revolution, i.e. one encoder 'tick' [b]waitcnt[/b](clkfreq / 8 + [b]cnt[/b]) ' 1/8th of a second
My little robot dutifully starts turning on the line "currentState := dir" but never stops; it just keeps spinning on the spot.
Is there anything special about repeat in this case? Is the current value of the variable totalTicks somehow not getting updated in the loop? The rest of the code seems to be working correctly; the motors change direction when currentState is changed and are kept in almost perfect sync. I confess that I don't know for a fact that totalTicks is incrementing, but I can't see why it wouldn't. These are the only places that totalTicks is referenced, other than the cog being started with @totalTicks as an argument to the method.
I apologize if this is all overly vague, but can anyone offer some advice? Am I making some obvious newbie mistake?
Comments
your code below
In this method you never address the Forward? perhaps the code may help
Also it looks like T is never incremented or decremented
Try something like this
Post Edited (grasshopper) : 6/26/2008 8:56:46 PM GMT
Here is hopefully all the missing, relevant code:
I've left out the method ApplyBrakes, but that is known to work. It just sets currentState to BRAKE for a fraction of a second, then STOP. Also, I'm aware that cog isn't actually used for anything except locally in the Start method; it's left over from something else.
Edit: I've added the missing ApplyBrakes method and rolled the other code snippets into the listing above.
Post Edited (David Stokes) : 6/30/2008 5:11:10 PM GMT
I still do not have a good idea of what's wrong. Does anyone else see what I'm doing wrong?
(BTW: I've edited my previous post to roll all the code together into one block, rather than leave Turn and RunMotors in the first post.)
Post Edited (David Stokes) : 6/30/2008 5:13:27 PM GMT
The stack has been increased to 40, just to be sure. Now I have another problem, one which I've seen before and thought I'd fixed: the right wheel's delta seems to always be greater than the left wheel's, so the right wheel stops almost immediately. Since I have LEDs displaying the motor controller's inputs, I can see the right wheel flickering -- this is probably the line before checking the wheels: outa[noparse][[/noparse]MOTOR_L1..MOTOR_R2] := long[noparse][[/noparse]stateAddress]. The last time I saw this happening, I thought I'd solved it by increasing the delay before the wheels were checked, giving the wheels a little more time to turn. Unfortunately, that does not seem to be the problem in this case.
I'm not sure why the right wheel's delta is always greater than the left's... direction doesn't matter (I've tested forwards and backwards) and I'm taking absolute values, anyway. After the deltas are reset they should both read zero, and since the left is moving and the right is not, it should be higher. Is there something fundamentally wrong with my logic?
(BTW: I've tried increasing the stack more and more, going up to 200... no dice. This new problem doesn't seem to be caused by an undersized stack.)
Things are working just fine now... it must have been the small stack all along.
Thanks again!