DPRG
DPRG List  



[DPRG] Return of the AVR Mega-Geek

Subject: [DPRG] Return of the AVR Mega-Geek
From: Jon Hylands jon at huv.com
Date: Fri Feb 19 07:13:07 CST 2010

On Thu, 18 Feb 2010 23:37:10 -0600, "Dick Swan"
<dickswan at sbcglobal.net> wrote:

>·         Drive straight for two feet 
>·         Turn right 90 degrees. 
>·         Grab balls from storage rack 
>·         Turn left 180 degrees. 
>·         Drive straight for four feet 
>·         Drop balls into scoring container 
>·         And so on for the next task/activity. 
>
>It?s most natural to program this as a linear sequence of function calls. You could also fit this into a ?main loop? architecture but then you need to have variables for managing the state (and various sub states) of each activity and you have to program the activities so that instead of a single function call they need to be designed to be repetitively called until they return a ?activity finished? return code whereupon you move to the next state. 

I personally find it much simpler to implement scenarios like the
above using a finite state machine. The Arduino has a really nicely
implemented FSM class available that works well, and fits nicely into
the main loop metaphor. 

If you program the above using a linear sequence of calls, its very
difficult to ensure that the sensors are being read and responded to
correctly all the time.

For instance, my latest mini-sumo, Seeker 2x, although not implemented
with the Arduino library, could be, and we're actually planning on
doing the port sometime later this year.

http://www.huv.com/miniSumo/seeker2x

Here's what my main loop looks like:

	while (1)	// outer loop is once every 10 ms
	{
		// pause for 10ms...
		previousCount = gTickCount;
		while (gTickCount == previousCount);
		LEDHeartbeat ();
		processSensors ();
		processRemote ();
		fsmExecute ();
	}

I have the following states:

enum mainStates {
	NO_STATE = 0,
	SETUP_STATE = 1,
	WAITING_FOR_BUTTON_STATE = 2,
	PAUSE_STATE = 3,
	SHUTDOWN_STATE = 4,
	REMOTE_RED_BUTTON_STATE = 5,
	REMOTE_GREEN_BUTTON_STATE = 6,
	LAUNCH_STATE = 7,
	LAUNCH_SWEEP_STATE = 8,
	HUNT_STATE = 9,
	HUNT_PIVOT_STATE = 10,
	HOMING_STATE = 11,
	FOUND_EDGE_STATE = 12,
	FOUND_SIDE_STATE = 13,
	BACKOFF_STATE = 14,
	BACKOFF_SPIN_STATE = 15,
	BACKOFF_STRAIGHT_STATE = 16,
	BACKOFF_STRAIGHT_TURN_STATE = 17};

Each state is fairly simple, and does one or two things.

I can't imagine trying to implement this using anything other than a
finite state machine...

Later,
Jon


More information about the DPRG mailing list