Simvik

State transition function

Implement the transition rules of a motor controller. A medium embedded C problem (Function pointers and state machines), run and graded in the browser.

medium · Function pointers and state machines · state-machine

A motor controller is a state machine with these transitions:

  • ST_IDLE + EV_START goes to ST_RUNNING
  • ST_RUNNING + EV_STOP goes to ST_IDLE
  • ST_RUNNING + EV_OVERCURRENT goes to ST_FAULT
  • ST_FAULT + EV_RESET goes to ST_IDLE

Every other combination leaves the state unchanged. In particular a fault is latched: only EV_RESET clears it. Return the next state.

Starter code

state_t fsm_step(state_t state, event_t event) {
  // Return the state that follows `state` when `event` arrives.
  return state;
}

Example tests

Start then stop

state_t s = fsm_step(ST_IDLE, EV_START);
printf("%d", s);
s = fsm_step(s, EV_STOP);
printf(" %d\n", s);

Expected output:

1 0

Fault is latched

state_t s = fsm_step(ST_RUNNING, EV_OVERCURRENT);
printf("%d", s);
s = fsm_step(s, EV_START);
printf(" %d\n", s);

Expected output:

2 2

Learn it first

More Function pointers and state machines problems