23:00

Dependencies:   biquadFilter MODSERIAL QEI Servo mbed

Fork of StateMachine_EMg_RKI_PID_MOTOR_DEMO_CLICK by Gaston Gabriël

main.cpp

Committer:
aschut
Date:
2018-10-31
Revision:
1:070092564648
Parent:
0:90750f158475
Child:
2:0a8622662f6d

File content as of revision 1:070092564648:

//Voor het toevoegen van een button:
#include "mbed.h"
#include <iostream>
DigitalOut gpo(D0);

DigitalIn button2(SW3);  
DigitalIn button1(SW2); //or SW2

DigitalOut led1(LED_GREEN);
DigitalOut led2(LED_RED);
DigitalOut led3(LED_BLUE);

Timer t;

enum states {MOTORS_OFF,CALIBRATION,HOMING,DEMO,MOVEMENT,CLICK}; 
int f = 1;
states currentState = MOTORS_OFF; 
bool stateChanged = true; // Make sure the initialization of first state is executed

void ProcessStateMachine(void)
{
  switch (currentState)
  {
    case MOTORS_OFF:
      // Actions
      if (stateChanged)
      {
        // state initialization: rood
        led1 = 1;
        led2 = 0; 
        led3 = 1;
        wait (1);
        stateChanged = false;
      }
    
      // State transition logic: Als button 1 word ingedrukt --> calibratie, anders motor uithouden
        if (!button1)
        {        
        currentState = CALIBRATION;
        stateChanged = true;
        }
        else if (!button2)
        {        
        currentState = HOMING  ;
        stateChanged = true;
        }
        else
        {
        currentState = MOTORS_OFF;
        stateChanged = true;
        }   
           
      break;
      
    case CALIBRATION:
    // Actions
      if (stateChanged)
      {
        // state initialization: oranje
        led1 = 0;
        led2 = 0;
        led3 = 1;
        wait (1);
        
        stateChanged = false;
      }
      
      // State transition logic: automatisch terug naar motors off.

        currentState = MOTORS_OFF;
        stateChanged = true; 
        break; 
      
    case HOMING:
    // Actions
      if (stateChanged)
      {
        // state initialization: green
        t.start();
        led1 = 0;
        led2 = 1;
        led3 = 1;
        wait (1);
        
        stateChanged = false;
      }
          
      // State transition logic: naar DEMO (button1), naar MOVEMENT(button2)
        if (!button1)
        {        
        currentState = DEMO;
        stateChanged = true;
        }
        else if (!button2)
        {        
        currentState = MOVEMENT  ;
        stateChanged = true;
        }
        else if (t>300) 
        {        
        t.stop();
        t.reset();
        currentState = MOTORS_OFF  ;
        stateChanged = true;
        }
        else
        {        
        currentState = HOMING  ;
        stateChanged = true;
        }
        break;
        
        case DEMO:
    // Actions
      if (stateChanged)
      {
        // state initialization: light blue
        led1 = 0;
        led2 = 1;
        led3 = 0;
        wait (1);
        
        stateChanged = false;
      }
          
      // State transition logic: automatisch terug naar HOMING
        currentState = HOMING;
        stateChanged = true;
        break;
              
    case MOVEMENT:
    // Actions
      if (stateChanged)
      {
        // state initialization: purple
        t.start();
        led1 = 1;
        led2 = 0;
        led3 = 0;
        wait (1);
        
        stateChanged = false;
      }
          
      // State transition logic: naar CLICK (button1), naar MOTORS_OFF(button2) anders naar MOVEMENT
        if (!button1)
        {        
        currentState = CLICK;
        stateChanged = true; 
        }
        else if (!button2)
        {        
        currentState = MOTORS_OFF  ;
        stateChanged = true;
        }
        else if (t>300)
        {        
        t.stop();
        t.reset();
        currentState = HOMING  ;
        stateChanged = true;
        }
        else
        {        
        currentState = MOVEMENT  ;
        stateChanged = true;
        }
        break;
        
        case CLICK:
    // Actions
      if (stateChanged)
      {
        // state initialization: blue
        led1 = 1;
        led2 = 1;
        led3 = 0;
        wait (1);
        
        stateChanged = false;
      }
      
      // State transition logic: automatisch terug naar MOVEMENT.

        currentState = MOVEMENT;
        stateChanged = true; 
        break; 
         
}
}
 
int main()
{
    while (true)
    {
    led1 = 1;
    led2 = 1;
    led3 = 1;
    ProcessStateMachine();
    
    }
    
}