You are on page 1of 4

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\BumperService.

c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

Page 1

/****************************************************************************
Module
BumperService.c
Revision
1.0

Description
Author
Anne Alter, Derrick Contreras
Date
2/14/16

****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
//headers for framework
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "TemplateService.h"
#include "termio.h"
//headers to access the pwm and nvic libraries
#include "inc/hw_timer.h"
#include "inc/hw_nvic.h"
#include "inc/hw_pwm.h"
//headers to access the GPIO subsystem
#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"

//headers to access the TivaWare Library


#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"

//headers for other modules in the program


#include "BumperService.h"
#include "PoundDefines.h"
#include "MasterSM.h"
#include "PWMService.h"
#include "InitializeService.h"

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


/*---------------------------- Module Functions ---------------------------*/
/*---------------------------- Module Variables ---------------------------*/
static uint8_t MyPriority;
static CurrentBumperState_t CurrentBumperState;
static uint8_t LastBumperState;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitBumperService
Parameters
uint8_t : the priorty of this service

Returns
bool, false if error in initialization, true otherwise
Description

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\BumperService.c
73
74
75
76
77
78
79
80
81
82
83
84
85
86

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

Page 2

Saves away the priority, and does any


other required initialization for this service

Author
Anne Alter, Derrick Contreras
****************************************************************************/
bool InitBumperService (uint8_t Priority)
{
//Set priority
MyPriority = Priority;
//initialize this event
ES_Event ThisEvent;
//initialize LastButtonState variable by reading buttons 1, 2, and 3
LastBumperState = (HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) &
(LEFT_BUMPER|FRONT_BUMPER|RIGHT_BUMPER));
//Set CurrentState to Debouncing
CurrentBumperState = Debouncing;
//Start debounce timer (timer posts to ButtonDebounceSM)
ES_Timer_InitTimer(DEBOUNCE_TIMER, DEBOUNCE_TIME);
// post the initial transition event
ThisEvent.EventType = ES_INIT;

if (ES_PostToService( MyPriority, ThisEvent) == true)


{
return true;
}else
{
return false;
}

/****************************************************************************
Function
PostBumperService
Parameters
EF_Event 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

Author
Anne Alter, 2/14/16
****************************************************************************/
bool PostBumperService(ES_Event ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
CheckBumperEvents
Parameters
None

Returns
bool, false if queue operation failed, true otherwise
Description

Author
Anne Alter, Derrick Contreras
****************************************************************************/
bool CheckBumperEvents(void)
{
//Initialize ReturnVal
bool ReturnVal = false;

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\BumperService.c
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

Page 3

//initialize ThisEvent
ES_Event ThisEvent;
//Set CurrentButtonState to read from 3 bumper button ports
uint8_t CurrentBumperState = (HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) &
(LEFT_BUMPER|FRONT_BUMPER|RIGHT_BUMPER));
//If CurrentButtonState does not equal LastButtonState
if (CurrentBumperState != LastBumperState) {
//ReturnVal equals true
ReturnVal = true;
//If CurrentButtonState equals front bumper (does not acct for left or right bumpers)
if (((CurrentBumperState & FRONT_BUMPER) != FRONT_BUMPER) || ((CurrentBumperState & LEFT_BUMPER) !=
LEFT_BUMPER) || ((CurrentBumperState & RIGHT_BUMPER) != RIGHT_BUMPER))
{
//Post BUMPER_DOWN event to ButtonService queue
ThisEvent.EventType = BUMPER_DOWN;
PostBumperService(ThisEvent);
printf("BUMPER_DOWN EVENT \r\n");
//Else
}
else
{
//Post BUMPER_UP event to ButtonService queue
ThisEvent.EventType = BUMPER_UP;
PostBumperService(ThisEvent);
}
}
//Set LastBumperState equal to CurrentBumperState
LastBumperState = CurrentBumperState;
//Return ReturnVal
return ReturnVal;
}
/****************************************************************************
Function
RunBumperService
Parameters
ES_Event : the event to process

Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here

Author
Anne Alter, Derrick Contreras
****************************************************************************/
ES_Event RunBumperService(ES_Event CurrentEvent)
{
//Initialize ReturnEvent
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors

//Switch CurrentState
switch (CurrentBumperState) {
//Case Debouncing
case Debouncing:
//If debounce timer timeout
if ((CurrentEvent.EventType == ES_TIMEOUT) && (CurrentEvent.EventParam == DEBOUNCE_TIMER)) {
//CurrentState is Ready2Sample
CurrentBumperState = Ready2Sample;
}
break;
//Case Ready2Sample
case Ready2Sample:
//If BUTTON_UP event
if (CurrentEvent.EventType == BUMPER_UP) {
//Start button debounce timer
ES_Timer_InitTimer(DEBOUNCE_TIMER, DEBOUNCE_TIME);
//CurrentState is Debouncing

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\BumperService.c
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

Page 4

CurrentBumperState = Debouncing;
}
//ElseIf BUMPER_DOWN event
else if (CurrentEvent.EventType == BUMPER_DOWN) {
//Start debounce timer
ES_Timer_InitTimer(DEBOUNCE_TIMER, DEBOUNCE_TIME);
//CurrentState is Debouncing
CurrentBumperState = Debouncing;
//Post ButtonUp event
ES_Event ThisEvent;
ThisEvent.EventType = BUMP;
//Param might be which bumper was detected
//ThisEvent.EventParam
PostMasterSM(ThisEvent);
printf("BUMP event from bumper service \r\n");
}
break;

}
//Return ReturnEvent
return ReturnEvent;
}

You might also like