![]() |
Infinite loop means no control
Our regional finished up yesterday. Python worked out very well for us, but I just now realized that the autonomous code I wrote was causing us a serious problem.
Thursday and Friday, everything was working fine. On Saturday, one of our mechanisms that was used during autonomous mode broke down. In order to keep our thrower motor from running continuously during the match we just tied down the limit switch that normally stopped and started the motor. The problem was that the autonomous code watched for that switch to be toggled twice -- once for each ball thrown. No toggle meant that the autonomous code just kept on running in a loop waiting for the switch to toggle. The moral of the story is: You absolutely must check which mode you are in during every loop. Failure to do so may mean that your program will not change modes when the field changes mode. I think this is an issue no matter which programming method is used, but I'm not really sure. I know it caught me in python. |
A better way?
I think what I am going to do is wrap wpilib.Wait in Python.
Loops are already required to call Wait regularly, so why not check for mode changes there? What it could do is check and save the current mode and throw an exception any time the mode changes. That exception could then be caught at the top level mode dispatch tree. In fact, I'd recommend something like this as the best way to go to insure that the correct section of code is always running. Some people might say that you should not use exceptions to control program flow, but I think this will simplify programs significantly. I haven't tested anything yet, though, so I don't know what problems may come up. |
Re: Infinite loop means no control
This is not a problem specific to python, this could happen with any programming language. If your code doesn't yield, then it will never be able to switch modes. We always program our robot in a loop that looks like this:
Code:
while self.IsEnabled() and self.IsOperatorControl():You might try looking at the Commands/Events stuff that WPILib introduced this year, there's support for it in Python despite a lack of documentation (in all languages, really). I will admit, there are a variety of examples that come with RobotPy, and I don't really like many of them. This might be part of the problem. Hopefully I (or someone) will find some time to clean them up at some point. |
Re: Infinite loop means no control
Quote:
Could a knowledgeable LabVIEW person please comment? |
Re: Infinite loop means no control
Quote:
I could do: Code:
while not throwerToReady():If you have to go through the whole loop each time, you must have to create some kind of state machine. By tooling Wait to watch for mode changes the simple style could work. |
Re: Infinite loop means no control
Quote:
|
Re: Infinite loop means no control
all teams should have a method for exiting auto mode manually in case you get stuck in auto and can't switch to manual.
just get the state of a button in an if statement and have a variable turn true. add Code:
or myVariable: |
Re: Infinite loop means no control
Quote:
|
Re: Infinite loop means no control
I implemented my plan to throw an exception on every mode change and it is working pretty well.
I wrapped wpilib.Wait Code:
from mode import check_modeand mode.py looks like this: Code:
MODE_DIS = 0 # DisabledAs long as any looping code calls this custom wait function, as soon as the mode changes an exception will be thrown. In robot.py I catch the mode change and any other exception that happens to be thrown. Code:
def run():Seems to be working well, but if anyone sees a potential problem, I'd be glad to hear about it. I like the idea about having an emergency autonomous escape button. With the code I have, it could just be a button that raises an exception. |
| All times are GMT -5. The time now is 19:51. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi