Go Down

Topic: Help with application of Finite State Machine library (Read 569 times) previous topic - next topic

Charm

For my solar tracker I decided to make use of the available Finite State Machine Library which is a nice tiny library but coded in a way which I do not yet fully comprehend yet. The FSM states are coded as follows and this compiles ok:

Code: [Select]
//setup SolarTracking states
State _Starting             = State (NULL,                Starting,                  Exit);
State _Eval_EWsensor        = State (Entry_Eval_EWsensor, Eval_EWsensor,             Exit);
State _Running_East         = State (NULL,                Running_East,              Exit);
State _Running_West         = State (NULL,                Running_West,              Exit);
State _Eval_NSsensor        = State (Entry_Eval_NSsensor, Eval_NSsensor,             Exit);
State _Running_North        = State (NULL,                Running_North,             Exit);
State _Running_South        = State (NULL,                Running_South,             Exit);
State _Cloud_Chasing        = State (Entry_Cloud_Chasing, Cloud_Chasing,             Exit);
State _Darkness             = State (Enter_Darkness,      Darkness,                  Exit);
State _Waiting_for_Sunrise  = State (Entry_Waiting_for_Sunrise, Waiting_for_Sunrise, Exit);
State _Error                = State (NULL,                Error,                     Exit);

FSM SolarTracking = FSM (_Starting);  


For debugging reasons I want to print the current state the FSM is in through the Exit method of each state in the following way:
Code: [Select]
void Exit()
{
 switch (SolarTracking.getCurrentState())
 {
case  (_Starting):
       Serial.println('Starting');
       break;
case  _Eval_EWsensor:
       Serial.println('Eval_EWsensor');
       break;
case  _Eval_NSsensor:
       Serial.println('Eval_NSsensor');
       break;
case  _Running_East:
       Serial.println('Running_East');
       break;
case  _Running_West:
       Serial.println('Running_West');
       break;
case  _Running_North:
       Serial.println('Running_North');
       break;
case  _Running_South:
       Serial.println('Running_South');
       break;
case  _Cloud_Chasing:
       Serial.println('Cloud_Chasing');
       break;
case  _Darkness:
       Serial.println('Darkness');
       break;
case  _Waiting_for_Sunrise:
       Serial.println('Waiting_for_Sunrise');
       break;
case  _Error:
       Serial.println('Error');
       break;
}      
}


Now the compiler complains about the switch statement and reports: 'switch quantity not an integer'. I can't figure out how to solve this. Your help is much appreciated.

marco_c

Should the arguments in the println() statements be in double quotes as they are strings?
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

wildbill

getCurrentState returns a state object, not the integer switch expects, so you can't use it. It looks like there was once an id field you could have used for this purpose, but it's commented out in the library. One way to achieve the same thing appears to be through use of a bunch of if tests calling the isInState method on the FSM.

Go Up