Working Maveric

StateMachine.h

Committer:
mettrque
Date:
2017-08-22
Revision:
10:36a2131f636c
Parent:
6:ef95300898b2

File content as of revision 10:36a2131f636c:

/*
 * StateMachine.h
 * Copyright (c) 2017, ZHAW
 * All rights reserved.
 *
 *  Created on: 02.05.2017
 *      Author: Quentin Mettraux
 */

#ifndef STATE_MACHINE_H_
#define STATE_MACHINE_H_

#include <cstdlib>
#include <deque>
#include <stdint.h>
#include <mbed.h>
#include "Controller.h"
#include "LowpassFilter.h"
#include "Signal.h"
#include "TaskMoveTo.h"
#include "TaskWait.h"

/**
 * This class implements the state machine of the robot controller.
 * It is an active class that uses a thread which is triggered periodically
 * by a timer interrupt.
 */
class StateMachine {
    
    public:
        
        static const int32_t    STATE_ERROR = -1; // State Error
        static const int32_t    STATE_OFF = 0;  // State Off
        static const int32_t    STATE_SWITCH_ON = 1; // State Switch On
        static const int32_t    STATE_ON = 2;   // State On
        static const int32_t    STATE_MOVE_MANUALLY = 3; // State Move Manually
        static const int32_t    STATE_MOVE_TO_POSE = 4; // State Move to Pose
        static const int32_t    STATE_STOPPING = 5; // State Stopping
        static const int32_t    STATE_SWITCH_OFF = 6; // State Switch Off 
        
        
                    StateMachine(Controller& controller, DigitalOut& errorLamp, AnalogIn& voltage24V, AnalogIn& voltage36V, Serial& tiBoard);
        virtual     ~StateMachine();
        void        setEnable(bool enable);
        void        setEnableMoving(bool enableMoving);
        void        setEnablePositioning(bool enablePositioning);
        int32_t     getState();
        float       getVoltage24V();
        float       getVoltage36V();
        void        setTranslationalVelocity(float translationalVelocity);
        void        setRotationalVelocity(float rotationalVelocity);
        void        setTaskMoveTo(float x, float y, float alpha);
        void        setTaskWait(float waitingTime);        
        int32_t     getTaskListLength();
        void        resetWatchdogTimer();
        
    private:
        
        static const uint32_t   STACK_SIZE = 1024;  // stack size of thread, given in [bytes]
        static const float      PERIOD;             // the period of the timer interrupt, given in [s]
        static const float      DISTANCE_THRESHOLD;     // minimum allowed distance to obstacle in [m]
        static const float      TRANSLATIONAL_VELOCITY; // translational velocity in [m/s]
        static const float      ROTATIONAL_VELOCITY;    // rotational velocity in [rad/s]       
        static const float      SWITCH_ON_DELAY;
        static const float      SWITCH_OFF_DELAY;
        static const float      WATCHDOG_TIMEOUT;
        static const float      VOLTAGE_GAIN_24V;
        static const float      VOLTAGE_GAIN_36V;        
        static const float      VOLTAGE_FILTER_FREQUENCY;
        
        Controller&             controller;
        DigitalOut&             errorLamp;
        AnalogIn&               voltage24V;
        AnalogIn&               voltage36V;
        Serial&                 tiBoard;
        
        bool                    enable;
        bool                    enableMoving;  
        bool                    enablePositioning;     
        int32_t                 state;
        float                   filteredVoltage24V;
        float                   filteredVoltage36V;
        float                   translationalVelocity;
        float                   rotationalVelocity;
        LowpassFilter           voltageFilter24V;
        LowpassFilter           voltageFilter36V;
        Timer                   timer;
        Timer                   watchdogTimer;
        Signal                  signal;
        deque<Task*>            taskList;
        Thread                  thread;
        Ticker                  ticker;
        
        void                    sendSignal();
        void                    run();
};

#endif /* STATE_MACHINE_H_ */