![]() |
Finite state machine problem
Hello,
I have a problem... One of our team's mentor decided that we would code our robot on a finite state machine format. Our code is available here. However, we can't get any FSM working. Right now, we tested a FSM in BallControl.java. It is initialized in out teleop.java command, and the buttons and motors values are updated in the execute() function. The problem is that, when we enable teleop, it goes straight to state 1 (!). No buttons nor the limitSwitch can change this state. If we disable teleop and re-enable it, it stucks in state 0 (waiting), no buttons can change this state (!). Can someone please help us to correct this problem? Alexandre Croteau (acrilex) Head coder, Team 5179 Sénateurs |
Re: Finite state machine problem
Some General Comments
I am not saying it is impossible to run a state machine using the command/subsystem framework, I'm just guessing it won't be easy. Here are the major issues that are probably causing the behavior you are seeing: Teleop It looks like you intend to use your Teleop command to run your state machine. However, I can't tell where you are starting your Teleop command (what is your trigger to start it?). If you didn't start it anywhere, you might want to create an instance of it in your Robot.robitInit() method and then start it in Robot.teleopInit() (similar to how your autonomous command is started). BallControl.java You have declared States as a local variable, set it to 0 and then used a switch statement on the local variable. This results in a "stuck in state 0" condition. Code:
public static void FSM(){Teleop.java Your Teleop command only calls the FSM() method once when it is initialized. That means that your states will never be updated (and your motor output value will never change from 0). Try relocating your FSM() invocation. Code:
// Called just before this Command runs the first time |
Re: Finite state machine problem
Thank you,
I applied your reccomendations, but I'm worried about one. You said we should move the BallControl.FSM() to Teleop.java's execute function, but wouldn't moving it there make the code stuck on this statement? I think it would execute the first iteration, being stuck at BallControl.FSM() until this command finishes to execute (should never happen if I'm not wrong...), in a kind of infinite loop. I commited your changes and another typo my mentor just pointed out, which was most likely the main problem (in case 0, I typed else if(grabButton = true) insteed of == ). I will get back to you when I test the code during my lunch time. Alexandre Croteau (acrilex) Head coder, Team 5179 |
Re: Finite state machine problem
I do not see any loops in the FSM() method currently (looking at your github repository). So, I don't see how FSM() would ever get stuck in a loop.
Once the Teleop command is started, it's execute() method will be repeatedly invoked by the scheduler until it is interrupted or canceled. A critical thing to understand is how the "Command Based Programming" model works. It is designed to allow multiple commands run simultaneously, but requires cooperation between the commands. Things like the Timer.delay(2) in your FSM() method will typically cause all sorts of problems when using the "Command Based Programming" model. Imagine the following:
I would recommend that you "start over" and not try to combine a state machine with the command based framework (or keep track of states in your commands and not in your subsystems). Our team has been using the Command/Subsystem framework for a few years now and we really like it. There are always going to be special cases where it can be frustrating, but overall it is well thought out and helps to avoid the complexity of large state machines. Our general rule of thumb for Java programmers getting started with this framework is as follows:
Good Luck |
Re: Finite state machine problem
Hello back,
We redesigned our whole code using sample robot. It now works 100% :) Thanks for help, Alexandre Croteau (acrilex) |
| All times are GMT -5. The time now is 09:12. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi