Go to Post We need to grow smart not grow fast. - Andrew Schreiber [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 07-12-2008, 21:52
jtdowney jtdowney is offline
Boiler Up
AKA: John Downey
FRC #4302 (Robophins)
Team Role: Mentor
 
Join Date: Sep 2006
Rookie Year: 2006
Location: Chicago
Posts: 300
jtdowney has a brilliant futurejtdowney has a brilliant futurejtdowney has a brilliant futurejtdowney has a brilliant futurejtdowney has a brilliant futurejtdowney has a brilliant futurejtdowney has a brilliant futurejtdowney has a brilliant futurejtdowney has a brilliant futurejtdowney has a brilliant futurejtdowney has a brilliant future
Simple C++ State Machine

I've attached a generic state machine class that could be useful in autonomous programming this year. My team is looking at using the IterativeRobot base class so we needed a way to keep state information in between iterations. I have not yet tested this on a robot but will try to this week as we get our control system setup.

The class uses templates to let you customize things a bit. The first parameter is the state identifier type, we will most likely use an enum, but you could use anything most likely including strings. The other two parameters are for the method callbacks (the class type we are passing an instance of in the constructor and the method signature). You then need to associate states with method callbacks for each state you want to use then set the initial state. During the perodic call the call to CallCurrentState will delegate control to whatever callback you've set using AddStateCallback.

It has been a while since I've written C++ (I work more with C# these days) so this code may be rough. Below is an example I wrote from memory without testing so it definitely won't compile without some work.

Example (not tested):
Code:
#include "StateMachine.h"

enum AutoState
{
    AUTO_START,
    AUTO_TURN,
    AUTO_GOAL1
};

class MyBot : public IterativeRobot
{
private:
    StateMachine<AutoState, MyBot, void (MyBot::*)(void)> *m_state;

public:
    MyBot(void)
    {
        m_state = new StateMachine<AutoState, MyBot, void (MyBot::*)(void)>(this);
        m_state->AddStateCallback(AUTO_START, &MyBot::Autonomous_State_Start);
        m_state->AddStateCallback(AUTO_TURN, &MyBot::Autonomous_State_Turn);
        m_state->AddStateCallback(AUTO_GOAL1, &MyBot::Autonomous_State_Goal1);
    }

    ~MyBot(void)
    {
        delete m_state;
    }

    void AutonomousInit(void)
    {
        m_state->SetCurrentState(AUTO_START);
    }

    void AutonomousPeriodic(void)
    {
        m_state->CallCurrentState();
    }

    void Autonomous_State_Start(void)
    {
        //... DO STUFF ...
        m_state->SetCurrentState(AUTO_TURN);
    }

    void Autonomous_State_Turn(void)
    {
        //... DO STUFF ...
        m_state->SetCurrentState(AUTO_GOAL1);
    }
    void Autonomous_State_Goal1(void)
    {
        //... DO STUFF ...
        m_state->SetCurrentState(AUTO_TURN);
    }
};
Attached Files
File Type: h StateMachine.h (2.3 KB, 341 views)
__________________
John Downey
Lead Robot Inspector - Purdue IndianaFIRST District
Whitney Young Magnet High School/Robophins (FRC 4302) - Mentor (2013-current)
Midwest Regional Planning Committee - Member (2012-current)
Boilermaker Regional Planning Committee - Member (2011-2014)
Robot Inspector (2008-current)
Purdue FIRST Programs - Staff Advisor (2008-2011)
Lafayette-Jefferson High School/Precision Guessworks (FRC 1646) - Mentor (2006-2011)
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
State machine in LabVIEW? JBotAlan National Instruments LabVIEW and Data Acquisition 3 23-10-2008 07:59
Hawaii Gov. Lingle State of State speech-FIRST and STEM MoeMom FIRST In the News... 0 29-01-2008 17:04
2 simple questions RDD Rules/Strategy 4 10-01-2007 16:41
EDU Demo Code: Serial Data Transmitter Using a State-Machine Kevin Watson Programming 3 28-12-2003 22:56
Choosing a machine shop (Re: How important is a machine shop archiver 2001 9 24-06-2002 04:20


All times are GMT -5. The time now is 22:32.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi