/*
 * statemachine.h
 *
 * Created: 05/04/2021 13:23:24
 *  Author: Nick-XMG
 */ 


#ifndef STATEMACHINE_H_
#define STATEMACHINE_H_

// ----------------------------------------------------------------------
// 	Application States
// ----------------------------------------------------------------------
typedef enum
{
	SYSTEM_INIT			  = 0,
	SYSTEM_IDLE			  = 1,
	MOTORS_ON			  = 2,
	APP_FAULT			  = 3,
	COMMSTEST			  = 4,
} APPLICATION_FSM_t;

typedef enum
{
	APP_NOFAULT			  = 0,
	ECAT_FAULT			  = 1,
	MASTER_SLAVE_IF_FAULT = 2,
	SPI_POS_SENSOR_FAULT  = 3,
	EMG_INTERFACE_FAULT   = 4,
	APP_MOTORFAULT	      = 5,
} APP_FAULTS_t;

typedef struct APPLICATION_STATE
{
	volatile APPLICATION_FSM_t currentstate;
	volatile APPLICATION_FSM_t previousstate;
	volatile APP_FAULTS_t fault;
} APPLICATION_STATE_t;

volatile APPLICATION_STATE_t applicationStatus;

// ----------------------------------------------------------------------
// Motor States
// ----------------------------------------------------------------------
typedef enum
{
	MOTOR_INIT			  = 0,
	MOTOR_IDLE			  = 1,
	MOTOR_OPEN_LOOP_STATE = 2,
	MOTOR_I_CTRL_STATE    = 3,
	MOTOR_V_CTRL_STATE    = 4,
	MOTOR_P_CTRL_STATE    = 5,
	MOTOR_VI_CTRL_STATE   = 6,
	MOTOR_PVI_CTRL_STATE  = 7,
	MOTOR_STOP			  = 8,
	MOTOR_FAULT			  = 9,	
} MOTOR_FSM_t;

typedef enum
{
	MOTOR_NOFAULT			  = 0xE1,
	MOTOR_HALLSENSORINVALID	  = 0xE2,
	MOTOR_DRIVER_OVER_CURRENT = 0xE3,
} MOTOR_FAULTS_t;

typedef struct MOTOR_STATE
{
	volatile MOTOR_FSM_t currentstate;
	volatile MOTOR_FSM_t previousstate;
	volatile MOTOR_FAULTS_t fault;
} MOTOR_STATE_t;


#endif /* STATEMACHINE_H_ */