Drivealongtrailsm C
Drivealongtrailsm C
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
DriveAlongTrailSM.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"
W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\DriveAlongTrailSM.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
144
Page 2
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
Author
Anne Alter, Derrick Contreras
****************************************************************************/
ES_Event RunDriveAlongTrailSM(ES_Event CurrentEvent)
{
//Local Variables: MakeTransition, NextState, EntryEventKind, ReturnEvent
bool MakeTransition = false;
CurrentDriveAlongTrailState_t NextState = CurrentDriveAlongTrailState;
ES_Event EntryEventKind = {ES_ENTRY, 0};
ES_Event ReturnEvent = CurrentEvent; //not consuming the event
//Switch CurrentDriveAlongTrailState
switch(CurrentDriveAlongTrailState)
{
//Case Driving:
case Driving:
//Set CurrentEvent to DuringDriving
CurrentEvent = DuringDriving(CurrentEvent);
//If CurrentEvent.EventType is ES_NO_EVENT
if(CurrentEvent.EventType != ES_NO_EVENT)
{
//Switch CurrentEvent.EventType
switch(CurrentEvent.EventType)
{
//Case: BUMP Event
case BUMP:
printf("BUMP from DriveAlongTrail \r\n");
//NextState is BumperTurnAround
NextState = DriveTurnAround;
//MakeTransition is True
MakeTransition = true;
//EntryEventKind.EventType is not a history entry
EntryEventKind.EventType = ES_ENTRY;
break;
}
}
break;
//Case DriveTurnAround:
case DriveTurnAround:
//Set CurrentEvent to DuringDriveTurnAround
CurrentEvent = DuringDriveTurnAround(CurrentEvent);
//If CurrentEvent.EventType is ES_NO_EVENT
if(CurrentEvent.EventType != ES_NO_EVENT)
{
//Switch CurrentEvent.EventType
switch(CurrentEvent.EventType)
{
case ES_TIMEOUT:
if(CurrentEvent.EventParam == REVERSE_TIMER)
{
Turn180();
printf("TURN 180 FROM DRIVE ALONG TRAIL \r\n");
//NextState is BumperTurnAround
NextState = DriveTurnAround;
//MakeTransition is True
MakeTransition = false;
//EntryEventKind.EventType is not a history entry
EntryEventKind.EventType = ES_ENTRY;
}
else if (CurrentEvent.EventParam == ROTATION_TIMER)
{
W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\DriveAlongTrailSM.c
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
214
215
216
Page 3
//stop
Stop();
printf("STOP FROM DRIVE ALONG TRAIL \r\n");
//NextState is Driving
NextState = Driving;
//MakeTransition is True
MakeTransition = true;
//EntryEventKind.EventType is not a history entry
EntryEventKind.EventType = ES_ENTRY;
}
break;
}
}
break;
}
//If MakeTransition is true
if(MakeTransition == true)
{
//Execute exit function for current state
CurrentEvent.EventType = ES_EXIT;
//Call RunDriveAlongTrailSM function
RunDriveAlongTrailSM(CurrentEvent);
//CurrentDriveAlongTrailState equals NextState
CurrentDriveAlongTrailState = NextState;
//Execute entry function for current state
RunDriveAlongTrailSM(EntryEventKind);
}
}
//return ReturnEvent
return(ReturnEvent);
/****************************************************************************
Function
StartDriveAlongTrailSM
Parameters
None
Returns
None
Description
Does any required initialization for SM
Author
Anne Alter, Derrick Contreras
****************************************************************************/
void StartDriveAlongTrailSM(ES_Event CurrentEvent)
{
//If ES_ENTRY_HISTORY does not equal CurrentEvent.EventType
if(CurrentEvent.EventType != ES_ENTRY_HISTORY)
{
//call the entry state for the state machine
CurrentDriveAlongTrailState = ENTRY_STATE;
}
//call the entry function for the entry state
RunDriveAlongTrailSM(CurrentEvent);
}
/****************************************************************************
Function
QueryDriveAlongTrailSM
Parameters
None
Returns
The current state of the state machine
Description
W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\DriveAlongTrailSM.c
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
Page 4
Author
****************************************************************************/
CurrentDriveAlongTrailState_t QueryDrivingAlongTrail(void)
{
return(CurrentDriveAlongTrailState);
}
/****************************************************************************
Function
QueryHallEffectSubtract
Parameters
None
Returns
The current state of the state machine
Description
Returns the current state of the state machine
Author
Anne Alter, Derrick Contreras
****************************************************************************/
uint32_t QueryHallEffectSubtract(void)
{
return(HallEffectSubtract);
}
/*------------------------------ Private Functions ------------------------------*/
/****************************************************************************
Function
DuringDriving
Parameters
ES_Event
Returns
ES_Event
Description
Processes entry, exit, and lower level events for the SM
Author
Anne Alter, Derrick Contreras
****************************************************************************/
static ES_Event DuringDriving(ES_Event Event)
{
//Define ReturnEvent
ES_Event ReturnEvent = Event;
//If ES_ENTRY event
if((Event.EventType == ES_ENTRY) || (Event.EventType == ES_ENTRY_HISTORY))
{
//enable wire sensing interrupt
HWREG(WTIMER1_BASE+TIMER_O_IMR) |= TIMER_IMR_TATOIM;
HWREG(NVIC_EN3) |= BIT0HI;
}
else if(Event.EventType == ES_EXIT)
{
printf("WIRE SENSE DISABLED \r\n");
//disable wire sensing interrupt
HWREG(WTIMER1_BASE+TIMER_O_IMR) &= ~TIMER_IMR_TATOIM;
}
else
{
}
//Return ReturnEvent
return ReturnEvent;
W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\DriveAlongTrailSM.c
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
Page 5
/****************************************************************************
Function
DuringDriveTurnAround
Parameters
ES_Event
Returns
ES_Event
Description
Processes entry, exit, and lower level events for the SM
Author
Anne Alter, Derrick Contreras
****************************************************************************/
static ES_Event DuringDriveTurnAround(ES_Event Event)
{
//Define ReturnEvent
ES_Event ReturnEvent = Event;
//If ES_ENTRY event
if((Event.EventType == ES_ENTRY) || (Event.EventType == ES_ENTRY_HISTORY))
{
printf("REVERSE CALLED IN DRIVE ALONG TRAIL \r\n");
//drive in reverse
Reverse();
}
else if(Event.EventType == ES_EXIT)
{
}
else
{
}
//Return ReturnEvent
return ReturnEvent;