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

Allow on_state() functions #10

Open
Thelmos opened this issue Mar 10, 2016 · 7 comments
Open

Allow on_state() functions #10

Thelmos opened this issue Mar 10, 2016 · 7 comments

Comments

@Thelmos
Copy link
Contributor

Thelmos commented Mar 10, 2016

Allow states to have a on_state() function that will run while in that state.

Main program loop will need a call to run_machine() to resolve than on_state() calls and resolve timed transitions.

This example shows how it would work (not tested, can contain errors):

#include <Fsm.h>

/*
 * FSM Library sample with user and timed
 * transitions.
 * Uses a button and Arduino builtin led, 
 * button can be replaced just grounding 
 * pin.
 */

// Used pins
#define LED_PIN     13
#define BUTTON_PIN  8

//Events
#define BUTTON_EVENT  0

int buttonState = 0;

/* state 1:  led off
 * state 2:  led on
 * transition from s1 to s2 when pusshing button
 * transition back from s2 to s1 after 3 seconds
 * SECOND PARAM IS NEW "on_state()" FUNCTION
 */
State state_led_off(&led_off, &check_button, null);
State state_led_on(&led_on, null, null);
Fsm fsm(&state_led_off);

// Transition functions
void led_off()
{
  digitalWrite(LED_PIN, LOW);
}

void led_on()
{
  digitalWrite(LED_PIN, HIGH);
}

void check_button()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    fsm.trigger(BUTTON_EVENT);
  }
}

// standard arduino functions
void setup()
{
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT);

  fsm.add_transition(&state_led_off, &state_led_on,
                     BUTTON_EVENT, null);
  fsm.add_timed_transition(&state_led_on, &state_led_off, 3000, NULL);
}

void loop()
{
  // Call fsm run
  fsm.run_machine();
}
@Thelmos
Copy link
Contributor Author

Thelmos commented Mar 12, 2016

Created pull request #11 solving this and other issues.

@Thelmos Thelmos mentioned this issue Mar 12, 2016
@DeeJee
Copy link

DeeJee commented Mar 16, 2016

I am using this version of the library but I am now getting errors:

collect2.exe*:error: ld returned 1 exit status
Error creating .elf

Is there an include statement missing?

@jonblack
Copy link
Owner

@DeeJee You'll need to post some code. That error can be for multiple reasons (e.g. missing namespace). Try to reproduce the error with as small a program as possible and post it here. Also worth making sure that by "this version" you mean the code in the pull request this issue references?

@DeeJee
Copy link

DeeJee commented Mar 17, 2016

Also worth making sure that by "this version" you mean the code in the pull request this issue references?
Yes, I was referring to the code in the pull request.

I did what you suggested and made a tiny program with only the state machine code in it. It now compiles fine. That leaves me with a quest to find the code that caused the problem. The main version of the state machine works fine with my code but the code from the pull request does not. I think I made a mistake in my own code that I got away with.... until now.

Anyway, thank you for your tip. I should have thought about that myself :o

@DeeJee
Copy link

DeeJee commented Mar 20, 2016

I currently have the state machine in use in my project. It is working fine and I an using all three functions. Thank you for creating this library.

@jonblack
Copy link
Owner

@DeeJee Are you referring to init, check and trigger as the "three functions"?

@DeeJee
Copy link

DeeJee commented Mar 20, 2016

I am referring to the three functions that are passed into the State objects. I believe they are called on_enter, on_state and on_exit.

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

No branches or pull requests

3 participants