Вы находитесь на странице: 1из 5

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

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
SearchingForTrailSM.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 "SearchingForTrailSM.h"
#include "MasterSM.h"
#include "DriveService.h"
#include "BumperService.h"
#include "PWMService.h" //take this out
#include "PoundDefines.h"

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


#define ENTRY_STATE SearchDriving
/*---------------------------- Module Functions ---------------------------*/
static ES_Event DuringSearchDriving(ES_Event Event);
static ES_Event DuringTurnAround(ES_Event Event);
/*---------------------------- Module Variables ---------------------------*/
static CurrentSearchingForTrailState_t CurrentSearchingForTrailState;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
RunSearchingForTrailSM
Parameters
ES_Event : the event to process

Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\SearchingForTrailSM.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

Description
Author
Anne Alter, Derrick Contreras
****************************************************************************/
ES_Event RunSearchingForTrailSM(ES_Event CurrentEvent)
{
bool MakeTransition = false;
CurrentSearchingForTrailState_t NextState = CurrentSearchingForTrailState;
ES_Event EntryEventKind = {ES_ENTRY, 0};
ES_Event ReturnEvent = CurrentEvent; //not consuming event
//Switch CurrentSearchingForTrailState
switch(CurrentSearchingForTrailState)
{
//Case SearchDriving:
case SearchDriving:
//Set CurrentEvent to DuringSearchDriving
CurrentEvent = DuringSearchDriving(CurrentEvent);
//If CurrentEvent.EventType is ES_NO_EVENT
if(CurrentEvent.EventType != ES_NO_EVENT)
{
//Switch CurrentEvent.EventType
switch(CurrentEvent.EventType)
{
//Case: BUMP
case BUMP:
printf("BUMP from SearchingForTrail\r\n");
//NextState is TurnAround
NextState = TurnAround;
//MakeTransition is True
MakeTransition = true;
//EntryEventKind.EventType is not a history entry
EntryEventKind.EventType = ES_ENTRY;
break;
}
}
break;

case TurnAround:
//Set CurrentEvent to DuringTurnAround
CurrentEvent = DuringTurnAround(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)
{
printf("turn 180 \r\n");
Turn180();
//NextState is TurnAround
NextState = TurnAround;
//MakeTransition is True
MakeTransition = false;
//EntryEventKind.EventType is not a history entry
EntryEventKind.EventType = ES_ENTRY;
}
else if (CurrentEvent.EventParam == ROTATION_TIMER)
{
printf("stop \r\n");
Stop();
//NextState is SearchDriving
NextState = SearchDriving;
//MakeTransition is True
MakeTransition = true;
//EntryEventKind.EventType is not a history entry

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\SearchingForTrailSM.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

}
}
break;

EntryEventKind.EventType = ES_ENTRY;

}
//If MakeTransition is true
if(MakeTransition == true)
{
//Execute exit function for current state
CurrentEvent.EventType = ES_EXIT;
//Call SearchingForTrailSM function
RunSearchingForTrailSM(CurrentEvent);
//CurrentSearchingForTrailState equals NextState
CurrentSearchingForTrailState = NextState;
//Execute entry function for current state
RunSearchingForTrailSM(EntryEventKind);
}
//return ReturnEvent
return(ReturnEvent);

}
/****************************************************************************
Function
StartSearchingForTrailSM
Parameters
None
Returns
None

Description
Does any required initialization for SM

Author
Anne Alter, Derrick Contreras
****************************************************************************/
void StartSearchingForTrailSM(ES_Event CurrentEvent)
{
//If ES_ENTRY_HISTORY does not equal CurrentEvent.EventType
if(ES_ENTRY_HISTORY != CurrentEvent.EventType)
{
//call the entry state for the state machine
CurrentSearchingForTrailState = ENTRY_STATE;
}
//call the entry function for the entry state
RunSearchingForTrailSM(CurrentEvent);

}
/****************************************************************************
Function
QuerySearchingForTrailSM
Parameters
None

Returns
The current state of the state machine

Description
Returns the current state of the state machine

Author
Anne Alter, Derrick Contreras
****************************************************************************/
CurrentSearchingForTrailState_t QuerySearchingForTrail(void)
{
return(CurrentSearchingForTrailState);
}
/*------------------------------ Private Functions ------------------------------*/

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\SearchingForTrailSM.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

/****************************************************************************
Function
DuringSearchDriving
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 DuringSearchDriving(ES_Event Event)
{
//Define ReturnEvent
ES_Event ReturnEvent = Event;
//If ES_ENTRY event
if((Event.EventType == ES_ENTRY) || (Event.EventType == ES_ENTRY_HISTORY))
{
//Motors on
SetDirection(LEFT_WHEEL, FORWARD);
SetDirection(RIGHT_WHEEL, FORWARD);
SetDuty(LEFT_WHEEL, 39);
SetDuty(RIGHT_WHEEL, 41);
}
else if(Event.EventType == ES_EXIT)
{
//Stop motors
Stop();
}
else
{
}

//Return ReturnEvent
return ReturnEvent;

/****************************************************************************
Function
DuringTurnAround
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 DuringTurnAround(ES_Event Event)
{
//Define ReturnEvent
ES_Event ReturnEvent = Event;
//If ES_ENTRY event
if((Event.EventType == ES_ENTRY) || (Event.EventType == ES_ENTRY_HISTORY))
{
Reverse();
}
else if(Event.EventType == ES_EXIT)
{
}

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\SearchingForTrailSM.c
289
290
291
292
293
294
295
296

Page 5

else
{
}

//Return ReturnEvent
return ReturnEvent;

Вам также может понравиться