fsm

Dependencies:   mbed QEI HIDScope MODSERIAL FXOS8700Q

Opzet_Eli.cpp

Committer:
ehopman
Date:
2019-10-07
Revision:
1:268bf7dbb15c
Parent:
0:50f25a675c72
Child:
2:5730195cf595

File content as of revision 1:268bf7dbb15c:

// Robot states
#include "mbed.h"
#include "HIDScope.h"
#include "MODSERIAL.h"

// Define objects
DigitalOut led1(LED_RED);
DigitalOut led2(LED_BLUE);
DigitalOut led3(LED_GREEN);
DigitalIn ptc1(PTC6); //Button 1
DigitalIn ptc2(PTA4); //Button 2

// Functions 
enum states{START,KAL_ME,KAL_EMG,MOVE_START,READY_START,DEMO,MOVE,WAIT,OFF};

states CurrentState = START;
bool StateChanged = true;         // this is the initialization of the first state

void StateMachine(void)
{
    switch(CurrentState)
    {
        case START:     
            if (StateChanged)
            {
                pc.printf("Start state, red led is on. If button 1 pressed, go to kal_me state");
                led1 = 0;  // Red led is on 
                led2 = 1;
                led3 = 1;
                StateChanged = false;
            }
            if (ptc1 = 0) // State switches when button pressed
            {
                CurrentState = KAL_ME;
                StateChanged = true;
            }
        break;      // end of state START
                
        case KAL_ME:
            if (StateChanged)
            {
                pc.printf("Calibration ME state, red ld flickers slow. 
                while(true)         // Red lef flickers slow
                {
                    led2 = 1;
                    led3 = 1;
                    led1 = !led1;
                    wait(0.7);
                }
                
                // FUNCTION Move to mechanical stop, include v_motor
                // FUNCTION Reset encoders
                
                StateChanged = false;
            }
            
            if  (v_motor == 0; t_passed > 2) // FUNCTION t_passed, included in v_motor?
            {
                CurrentState = KAL_EMG;
                StateChanged = true;
            }
        break;      // end of state KAL_ME
        
        case KAL_EMG:
            if (StateChanged)
            {
                while(true)     // Red led flickers fast
                {
                    led2 = 1;
                    led3 = 1;
                    led1 = !led1;
                    wait(0.2);
                }
                //FUNCTION Measure EMG_max
                
                StateChanged = false;
            }
            
            if (EMG < 0.1*EMG_max && t_passed > 2)
            {
                CurrentState = MOVE_START;
                StateChanged = true;
            }
        break;      // end of state KAL_EMG
        
        case MOVE_START:
            if(StateChanged)
            {
                led1 = 1;
                led2 = 1; 
                led3 = 0;   // Green led is on
            
            // define start position
            
            if //position realised and tmie passed
            {
                // Stop moving
                CurrentState = READY_START;
                StateChanged = true;
            } 
        break;      // end of state MOVE_START
        
        case READY_START:
            while(true)
            {
                led1 = 1;
                led2 = 1;
                led3 = 0;
            }
            
            if (ptc1 == 0) 
            {
                CurrentState = DEMO;
                StateChanged = true;
            }
            else if (ptc2 == 0) // or EMG signal >= 20% max
            {
                CurrentState = MOVE;
                StateChanged = true;
            }                
        break;      // end of state READY_START
        
        case DEMO:
            //perform straight movements in some way, dunno how to make that here yet
            if //position = end and time 
            {
                CurrentState = MOVE_START;
                StateChanged = true;
            }
        break;      // end of state DEMO
        
        case MOVE:
            if // no movement for 2 sec after input
            {
                CurrentState = WAIT;
                StateChanged = true;
            }
            else if // input signal
            {
                // move in direction linked to EMG
            }
            // Iets met dat oranje lichtje maar snap die stap niet helemaal 
        break;      // end of state MOVE
        
        case WAIT:
            if (ptc1 == 0)
            {
                CurrentState = READY_START;
                StateChanged = true;
            )
            else if (ptc2 == 0) // or EMG>= 20%max
            {
                CurrentState = MOVE;
                StateChanged = true;
            }
            else if (ptc1 == 0) //maar dan heel lang achter elkaar
        break;      // end of state WAIT
        
        case OFF:
            // turns all of 
        break;      // end of state OFF
        
        default:
        TurnMotorsoff();
        printf("Unknown state reached");
    }               // End of the switch, all states are prescribed
}

int main(void)      // wat hier in moet snap ik nog niet 
{
        // hier moeten dingen komen
        while (true)
        {
            CheckForCommandFromTerminal();
            StateMachine();
        }
}