Sunteți pe pagina 1din 4

/*

MarioMovementSM

This state machine syncs mario forward movement with 60 seconds.


Currently it takes 6 seconds to reach from start to end. Idea is to move the
mario for 1 sec
and then stop it for 9 sec

First Version: Nov 27, Sunday

Data private to the module:


MyPriority

States:
PseudoState_MarioMovementSM, //PseudoState
MarioMoving,
MarioNotMoving

Intaking Events:
ES_TIMEOUT
MOVE_MARIO (from ControlSM when we have START_BUTTON_PRESSED event from Setup
to Ingame)

Posting Events:

*/

/*----------------------------- Include Files -----------------------------*/


// Basic includes for a program using the Events and Services Framework

/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/

#include "SR_HillAndMotor.h"
#include "ControlSM.h"
#include "MarioMovementSM.h"

// Hardware
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"

// Event & Services Framework


#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"

//Including A/D converter and PWM input


#include "ADMulti.h"
#include "PWM16Tiva.h"

/*----------------------------- Module Defines ----------------------------*/


// define constants for the states for this machine
// and any other local defines
#define ONE_SEC 976
#define HALF_SEC (ONE_SEC / 2)
#define NINE_SEC (ONE_SEC * 9)

// readability defines

//defines for the flag checks


#define POSITION_PORT_HI BIT3HI
#define POSITION_PORT_LO BIT3LO

#define FLAG_UP_PIN_HI BIT0HI


#define FLAG_UP_PIN_LO BIT0LO

#define FLAG_DOWN_PIN_HI BIT1HI


#define FLAG_DOWN_PIN_LO BIT1LO

#define MARIO_START_PIN_HI BIT2HI


#define MARIO_START_PIN_LO BIT2LO

#define MARIO_END_PIN_HI BIT3HI


#define MARIO_END_PIN_LO BIT3LO

#define POSITION_SYSCTL_PRGPIO SYSCTL_PRGPIO_R3


#define POSITION_GPIO_BASE GPIO_PORTD_BASE

/*---------------------------- Module Functions ---------------------------*/


/* prototypes for private functions for this machine, things like during
functions, entry & exit functions.They should be functions relevant to the
behavior of this state machine
*/

/*---------------------------- Module Variables ---------------------------*/


// everybody needs a state variable, you may need others as well
static uint8_t MyPriority;
static MarioMovementSM_States_t CurrentState;
static ControlSM_States_t ControlSM_GameState; //like a constant to check
if the ControlSM is in the InGame state

/****************************************************************************
Function
InitMarioMovementSM

Parameters
uint8_t : the priorty of this service

Returns
bool, false if error in initialization, true otherwise

Description
Saves away the priority, and does any
other required initialization for this service
Notes

Author
****************************************************************************/
bool InitMarioMovementSM(uint8_t Priority)
{
MyPriority = Priority;
printf("\r \n Initializing MarioMovementSM");

//Initialize the hardware pin to read the analog input


//The hardware for the Pipe leds and hill leds is already initialized

ES_Event_t ThisEvent;
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}
/****************************************************************************
Function
PostMarioMovementSM

Parameters
ES_Event_t ThisEvent ,the event to post to the queue

Returns
bool false if the Enqueue operation failed, true otherwise

Description
Posts an event to this state machine's queue
Notes

Author
****************************************************************************/
bool PostMarioMovementSM(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunMarioMovementSM

Parameters
ES_Event_t : the event from its service queue to process in this State
Machine
Types of ES_Event_t: VISUAL_BLAST_OFF, VISUAL_BLAST_ON, GLOW_RANDOM_PIPE,
ES_TIMEOUT

Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description
add your description here
Notes

Author

****************************************************************************/

ES_Event_t RunMarioMovementSM(ES_Event_t ThisEvent)


{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors

switch (CurrentState)
{
case PseudoState_MarioMovementSM: // If current state is initial
Psedudo State
{
if (ThisEvent.EventType == ES_INIT) // only respond to ES_Init
{
// this is where you would put any actions associated with th

//initializing ControlSM_GameState to the InGame state


ControlSM_GameState = InGame;

//switching state
CurrentState = MarioNotMoving;
}
}
break;

case MarioNotMoving:
{
//guard conditions. Should not move to MarioMoving if Mario is already at
the end.
//Should also make movements only when ControlSM is in InGame state
if ((!(HWREG(POSITION_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &
MARIO_END_PIN_HI)) && (getControlSM_State() == ControlSM_GameState))
{
if ((ThisEvent.EventType == ES_TIMEOUT) || (ThisEvent.EventType ==
MOVE_MARIO))
{
//Restart timer for 1 sec movement
ES_Timer_InitTimer(MarioMovementTimer, HALF_SEC);

//Move Mario towards right


uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT0LO & BIT1LO);
uint8_t NewValue = Value_Masked | (BIT1HI); //Bit 1 need to be 1 for
Mario start -->
SR_HillAndMotor_Write(NewValue);
PWM_TIVA_SetDuty(100, 2); //Initializing Mario Forward direction

//switch state
CurrentState = MarioMoving;
}
}
}
break;

case MarioMoving:
{
if (ThisEvent.EventType == ES_TIMEOUT)
{
//Restart timer for 9 sec pause
ES_Timer_InitTimer(MarioMovementTimer, NINE_SEC);

//stop mario movement


uint8_t LastSR2_Value = SR_HillAndMotor_GetCurrentRegister();
uint8_t Value_Masked = LastSR2_Value & (BIT0LO & BIT1LO);
uint8_t NewValue = Value_Masked; //Bit 1 nned to be 1 for Mario start --
>
SR_HillAndMotor_Write(NewValue);
PWM_TIVA_SetDuty(0, 2); //Initializing Mario Forward direction

//switching state
CurrentState = MarioNotMoving;
}
}
break;
}
return ReturnEvent;
}

S-ar putea să vă placă și