Professional Documents
Culture Documents
Linefollowingsm
Linefollowingsm
Module
LineFollowingSM.c
Description
This module contains the line following functions.
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for the framework and this service
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_ssi.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h" // Define PART_TM4C123GH6PM in project
#include "driverlib/gpio.h"
#include "termio.h"
#include "hw_nvic.h"
#include "hw_pwm.h"
#include "hw_timer.h"
#include "hw_ssi.h"
#include "MotorDrive.h"
#include "ADMulti.h"
#include "LineFollowingSM.h"
#include "MasterSM.h"
#include "math.h"
#include "MotorEncoders.h"
#define SAMPLE_TIME 10
#define DUTY_ADD_TOL 5
// PI gains
static const float iGain = 0.0;
static const float pGain = 0.08;
static const float dGain = 0.5;
// term for moving average
static const float BETA = 0.7;
// PI terms
static float IntegralTerm = 0.0;
static float DerivativeTerm = 0.0;
static float DutyAdd = 0.0;
// nominal Duty Cycle of the motors at 30% for line following
static float LMotorDC = NOMINAL_DC;
static float RMotorDC = NOMINAL_DC;
Parameters
uint8_t: the priorty of this service
Returns
boolean: True if no error, False otherwise
Description
Saves away the priority, and starts
the top level state machine. Hardware init for LineFollowingSM
Notes
Author
S. Park
****************************************************************************/
void InitLineFollowingSM(void)
{
//Initialize the line following control
InitLineFollowingIntResponse();
// Initialize the AD ports (PE0 and PE1)
ADC_MultiInit(2);
}
/****************************************************************************
Function
RunLineFollowingSM
Parameters
ES_Event_t ThisEvent, event to process
Returns
ES_Event_t ThisEvent, event to return
Description
Posts an event to the queue of the SM
Notes
Author
S. Park
****************************************************************************/
ES_Event_t RunLineFollowingSM(ES_Event_t CurrentEvent)
{
bool MakeTransition = false;/* are we making a state transition?
*/
LineFollowingState_t NextState = CurrentState;
ES_Event_t EntryEventKind = { ES_ENTRY, 0 }; // default to normal entry
to new state
ES_Event_t ReturnEvent = { ES_NO_EVENT, 0 }; // assume no error
switch (CurrentState)
{
case FollowingTheLine: // If current state is TurningLeft
{ // Execute During function for TurningLeft
ReturnEvent = CurrentEvent = DuringFollowingTheLine(CurrentEvent);
//process any events
if (CurrentEvent.EventType != ES_NO_EVENT) //If an event is active
{
//If both bumpers are hit
if ((CurrentEvent.EventType == LEFT_BUMPER_HIT) || (CurrentEvent.EventType
== RIGHT_BUMPER_HIT))
{
// Reload station reached, stop motors
StopDrive();
// Transition to Line Following Complete
ReturnEvent.EventType = LINE_FOLLOW_COMPLETE;
}
}
}
break;
}
/****************************************************************************
Function
StartLineFollowingSM
Parameters
ES_Event_t CurrentEvent
Returns
nothing
Description
Does any required initialization for this state machine
Notes
Author
S. Park
****************************************************************************/
void StartLineFollowingSM(ES_Event_t CurrentEvent)
{
// Start with motors stopped
StopDrive();
EnableLineFollowing();
//Initialize to Waiting2LineFollow
CurrentState = FollowingTheLine;
return;
}
/***************************************************************************
private functions
***************************************************************************/
bool Check4LineOn(void)
{
// ES_Event_t LineEvent;
// // Get AD readings of the sensors
//
// CurrentSensorReading_Left = LineSensors[0];
// CurrentSensorReading_Right = LineSensors[1];
// printf("%u\t%u\n\r",LineSensors[0],LineSensors[1]);
// if ((CurrentSensorReading_Left != LastSensorReading_Left)
// || (CurrentSensorReading_Right != LastSensorReading_Right))
// {
// if (AllowLineOn)
// {
// // check if either sensor is above threshold
// if ((CurrentSensorReading_Left > LINE_ON_THRESHOLD*COUNTS_PER_VOLT)
// || (CurrentSensorReading_Right > LINE_ON_THRESHOLD*COUNTS_PER_VOLT))
// {
// AllowLineOn = false;
// printf("found line \r\n");
// LineEvent.EventType = LINE_ON;
// PostMasterSM(LineEvent);
// return true;
// }
// }
// }
//
//
// LastSensorReading_Left = CurrentSensorReading_Left;
// LastSensorReading_Right = CurrentSensorReading_Right;
return false;
}
bool Check4LineAlign(void)
{
// ES_Event_t LineEvent;
// // Get AD readings of the sensors
//
// //TODO: UNCOMMENT IF WE WANT
//
// CurrentSensorReading_Left = LineSensors[0];
// CurrentSensorReading_Right = LineSensors[1];
// CurrentSensorReading_Diff = abs(CurrentSensorReading_Left -
CurrentSensorReading_Right);
// //printf("%u\t%u\t%u\r\n",CurrentSensorReading_Left,
CurrentSensorReading_Right, CurrentSensorReading_Diff);
//
// if (AllowLineAlign)
// {
// if ((CurrentSensorReading_Left != LastSensorReading_Left)
// || (CurrentSensorReading_Right != LastSensorReading_Right))
// {
// // took out && (CurrentSensorReading_Diff<LINE_ALIGNED_DIFF_THRESHOLD)
// // check if either sensor is above 1.9V and values are equal
// if (
// ((CurrentSensorReading_Left > LINE_ALIGNED_THRESHOLD*COUNTS_PER_VOLT)
// || (CurrentSensorReading_Right >
LINE_ALIGNED_THRESHOLD*COUNTS_PER_VOLT))
// )
// {
// AllowLineAlign = false;
// printf("LINE_ALIGNED\r\n");
// LineEvent.EventType = LINE_ALIGNED;
// PostMasterSM(LineEvent);
// return true;
// }
// }
// }
// LastSensorReading_Left = CurrentSensorReading_Left;
// LastSensorReading_Right = CurrentSensorReading_Right;
return false;
}
void InitLineFollowingIntResponse(void)
{
// enable clock to the timer (Wide Timer 2)
HWREG(SYSCTL_RCGCWTIMER) |= SYSCTL_RCGCWTIMER_R2;
// kill a few cycles
while ((HWREG(SYSCTL_PRWTIMER) & SYSCTL_PRWTIMER_R2) != SYSCTL_PRWTIMER_R2)
{}
void LineFollowingIntResponse(void)
{
ES_Event_t LineEvent;
ADC_MultiRead(LineSensors);
if (AllowLineOn)
{
// check if either sensor is above threshold
if ((LineSensors[0] > LINE_ON_THRESHOLD)
|| (LineSensors[1] > LINE_ON_THRESHOLD))
{
AllowLineOn = false;
//printf("found line \r\n");
LineEvent.EventType = LINE_ON;
PostMasterSM(LineEvent);
}
}
if (AllowLineAlign)
{
if ((LineSensors[0] > LINE_ALIGNED_THRESHOLD))
{
AllowLineAlign = false;
//printf("found line \r\n");
LineEvent.EventType = LINE_ALIGNED;
PostMasterSM(LineEvent);
}
}
if (LCR_CONTROL)
{
//printf("following line \r\n");
// calculate the sensor reading difference (Right - Left)
Delta = ((float)LineSensors[1]) - (float)(LineSensors[0]);
//printf("%d\t%d\t%f\n\r",LineSensors[0],LineSensors[1],Delta);
//printf("%d\n\r", LineSensors[0]);
// calculate the integral term & clamp between 0 and nominal DC
IntegralTerm += iGain * Delta;
//caculate with moving average (filters the value)
DerivativeTerm = BETA * DerivativeTerm + (1 - BETA) * (Delta - LastDelta);
IntegralTerm = clamp(IntegralTerm, -50, 50);