Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"In state" callback function not executed? #39

Open
fkromer opened this issue May 11, 2021 · 1 comment
Open

"In state" callback function not executed? #39

fkromer opened this issue May 11, 2021 · 1 comment

Comments

@fkromer
Copy link

fkromer commented May 11, 2021

I've created the following example.

#include "Fsm.h"

#define STATE_1_TO_2_EVENT 1
#define STATE_2_TO_1_EVENT 2

void on_state_1_on_enter()
{
  Serial.println("Entering State 1");
}

void on_state_1_in_state()
{
  Serial.println("State 1");
}

void on_state_1_on_exit()
{
  Serial.println("Exiting State 1");
}

void on_state_2_on_enter()
{
  Serial.println("Entering State 2");
}

void on_state_2_in_state()
{
  Serial.println("State 2");
}

void on_state_2_on_exit()
{
  Serial.println("Exiting State 2");
}

State state_1(&on_state_1_on_enter, &on_state_1_in_state, &on_state_1_on_exit);
State state_2(&on_state_2_on_enter, &on_state_2_in_state, &on_state_2_on_exit);
Fsm fsm(&state_1);

void on_trans_1_to_2() {
    Serial.println("State 1 -> State 2");
}

void on_trans_2_to_1() {
    Serial.println("State 2 -> State 1");
}

// standard arduino functions
void setup()
{
  Serial.begin(9600);

  fsm.add_transition(&state_1, &state_2,
                     STATE_1_TO_2_EVENT,
                     &on_trans_1_to_2);
  fsm.add_transition(&state_2, &state_1,
                     STATE_2_TO_1_EVENT,
                     &on_trans_2_to_1);
  fsm.run_machine();
}

void loop()
{
  delay(2000);
  fsm.trigger(STATE_1_TO_2_EVENT);
  delay(2000);
  fsm.trigger(STATE_2_TO_1_EVENT);
}

If I run the example the serial console output is as follows:

Entering State 1
State 1
Exiting State 1
State 1 -> State 2
Entering State 2
Exiting State 2
State 2 -> State 1
Entering State 1
Exiting State 1
State 1 -> State 2
Entering State 2
Exiting State 2
(...)

The "in state" callback function for state 1 on_state_1_in_state() is executed only once, implicitly as part of Fsm fsm(&state_1);. The callback function for state 2 on_state_2_in_state() is not executed at all. Have I not used the API correctly?

@Jason-CHK
Copy link

I think you need to call fsm.run_machine(); in loop() instead of setup(), like this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants