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

fsm.trigger() not working #44

Open
stanleyseow opened this issue Feb 4, 2023 · 1 comment
Open

fsm.trigger() not working #44

stanleyseow opened this issue Feb 4, 2023 · 1 comment

Comments

@stanleyseow
Copy link

I wanna trigger a state change every 10 sec but it is not working..

#define WRITE_EVENT 2

while ( millis() - timer1 > 10000 ) {
Serial.print("<<< ");
Serial.print( millis()/divider );
Serial.println(" triggered stateWriteSerial");
fsm.trigger(WRITE_EVENT);
timer1 = millis();
}

setup() {

fsm.add_transition(&stateIdle, &stateWriteSerial, WRITE_EVENT,NULL );

}

@natehouk
Copy link

You must call fsm.run_machine() at least once from setup() for fsm.trgger() to work due to the predicate in the trigger function preventing it from triggering if not initialized.

void Fsm::trigger(int event)
{
  if (m_initialized)
  {
    // Find the transition with the current state and given event.
    for (int i = 0; i < m_num_transitions; ++i)
    {
      if (m_transitions[i].state_from == m_current_state &&
          m_transitions[i].event == event)
      {
        Fsm::make_transition(&(m_transitions[i]));
        return;
      }
    }
  }
}

Calling fsm.run_machine() will set m_initialized = true

void Fsm::run_machine()
{
  // first run must exec first state "on_enter"
  if (!m_initialized)
  {
    m_initialized = true;
    if (m_current_state->on_enter != NULL)
      m_current_state->on_enter();
  }
  
  if (m_current_state->on_state != NULL)
    m_current_state->on_state();
    
  Fsm::check_timed_transitions();
}

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