Go Down

Topic: Simple problem using the SMLib state machine (Read 1 time) previous topic - next topic

raaen

I am trying to change one of my programs to use a state machine by using the SMLib library. I have run into a strange issue though.

Here is a simple example with three head/body states to show what i mean:

Code: [Select]

#include <SM.h>

//Define StateMachine
SM sm(s1h,s1b);

void setup() {
  Serial.begin(115200); //for debug
}

void loop() {
  EXEC(sm);
}

State s1h() {
    Serial.println("s1h");
}

State s1b() {
  if(sm.Timeout(1000)) {
    Serial.println("t1");
    sm.Set(s2h,s2b);
  }
  if(sm.Timeout(2000)) {
    Serial.println("t2");
    sm.Set(s3h,s3b);
  }
}

State s2h() {
  Serial.println("s2h");
}

State s2b() {
  if(sm.Timeout(500)) {
    Serial.println("s2b");
    sm.Set(s1h,s1b);
  }
}

State s3h() {
  Serial.println("s3h");
}

State s3b() {
  if(sm.Timeout(500)) {
    Serial.println("s3b");
    sm.Set(s1h,s1b);
  }
}


For som reason the program never enters the second if statement in the s1b state.

Serial output:
Code: [Select]

s1h
t1
s2h
s2b
s1h
t1
s2h
s2b
s1h
t1
s2h
s2b
s1h
t1
s2h
s2b
s1h
t1
s2h
s2b
s1h
t1
s2h
s2b
s1h
t1


It's my first experience with the SMLib so chances are I have misunderstood something, but i just can't figure out why this code doesn't work as expected.
I have read and re-read the description for the library here: http://playground.arduino.cc/Code/SMlib


Any inputs are greatly appreciated.

Thanks,

Rasmus

jurs

It's my first experience with the SMLib so chances are I have misunderstood something, but i just can't figure out why this code doesn't work as expected.
The code is fully working as expected:
Code: [Select]

 if(sm.Timeout(1000)) {
    Serial.println("t1");
    sm.Set(s2h,s2b); // here you leave the current state and switch to another state
  }


After the 1000ms timeout is reached, you set a different state in the state machine.

So you NEVER arrive at the 2000ms timeout in that state:
Code: [Select]
 if(sm.Timeout(2000)) { // No, no never...

raaen

Thanks for your reply- I just realized the same after playing around with the timeouts.

I changed the timer detections to use millis() instead and it is now working as it should.

/Rasmus

jurs

I changed the timer detections to use millis() instead and it is now working as it should.
With the SMlib "Statetime()" function you can request the time in the current state.

terryking228

Hi,

I have a different problem:

Arduino IDE V1.6.6 and 1.6.7 cause any SMLIB example to fail. (1.6.5 and earlier OK) .  The error looks like this (my code example)

---------------------( COPY )---------------------
Arduino: 1.6.7 (Windows 7), Board: "Arduino/Genuino Uno"

_1_1_SMTEST:72: error: 'RoboStartState' was not declared in this scope

 SM RoboGo(RoboStartState);//create simple statemachine

           ^

exit status 1
'RoboStartState' was not declared in this scope

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.
-----------------( END COPY )----------------------

The failure is on the line with the initial creation of the state machine...


WHO is the author of SM ? I can't find that anywhere..

Any suggestions appreciated!  I have lots of Robot Kits out there that use SM !

Regards, Terry King
...In The Woods in Vermont, USA
terry@yourduino.com
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

jurs

#5
Feb 02, 2016, 11:52 am Last Edit: Feb 02, 2016, 11:53 am by jurs
WHO is the author of SM ? I can't find that anywhere..
It looks like you already had discussions with the author of the library.

Do you remember, perhaps:
http://forum.arduino.cc/index.php?topic=202015.msg1491890#msg1491890

But it seems like the author had not posted for several months now.
He also seems to be no update-junky, as his latest forum entry was about Arduino-IDE crashing when using Windows XP (XP was released in 2001 and Microsoft support lifecycle for XP ended in April 2014).

In case that he is not able to provide a future update for his library: His library is just a few lines of code and it should be not too hard to debug any problem with updated IDE versions, I think.

greenmoon

I am encountering the error Terry posted:

'RoboStartState' was not declared in this scope

It's baffling to me, as I am teaching a workshop where we have used this same code on over a dozen robots with great success, but this error just started popping up. Did a recent change to the Arduino IDE affect this?

Go Up