Vous êtes sur la page 1sur 6

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

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
WireSensingService.c
Revision
1.0

Description
Notes
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 "DriveAlongTrailSM.h"
#include "MasterSM.h"
#include "DriveService.h"
#include "PoundDefines.h"
#include "PWMService.h"
#include "ADMulti.h"
/*----------------------------- Module Defines ----------------------------*/
/*---------------------------- Module Functions ---------------------------*/
float AverageDifference(void);
void RotateLeft(void);
void RotateRight(void);
/*---------------------------- Module Variables ---------------------------*/
static uint8_t MyPriority;
static uint32_t results[HOW_MANY];
static float WireSubtract;
static
static
static
static
static

uint32_t
uint32_t
uint32_t
uint32_t
uint32_t

FirstLeftValue = 0;
SecondLeftValue = 0;
ThirdLeftValue = 0;
FourthLeftValue = 0;
FifthLeftValue = 0;

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

static
static
static
static
static

uint32_t
uint32_t
uint32_t
uint32_t
uint32_t

FirstRightValue = 0;
SecondRightValue = 0;
ThirdRightValue = 0;
FourthRightValue = 0;
FifthRightValue = 0;

static uint8_t TimesDetected = 0;


static uint8_t FirstWheelDetected;
//static uint8_t LostTrailCount = 0;

/*------------------------------ Module Code ------------------------------*/


/****************************************************************************
Function
InitWireSensingService
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
Anne Alter, 01/07/16
****************************************************************************/
bool InitWireSensingService(uint8_t Priority)
{
ES_Event ThisEvent;
//set priority
MyPriority = Priority;

//initialize one analog port line


ADC_MultiInit(HOW_MANY);

//read results from analog port line


ADC_MultiRead(results);

//start AD_TIMER
ES_Timer_InitTimer(AD_TIMER, AD_TIMER_LENGTH);

//post the initial transition event


ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}else
{
return false;
}

/****************************************************************************
Function
PostWireSensingService
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

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

Notes

Author
Anne Alter, Derrick Contreras
****************************************************************************/
bool PostWireSensingService(ES_Event ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunWireSensingService
Parameters
ES_Event : the event to process

Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
Runs the analog/digital converting code
Notes

Author
Anne Alter, Derrick Contreras
****************************************************************************/
ES_Event RunWireSensingService(ES_Event ThisEvent)
{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
//If (ThisEvent is ADTimer timeout event)
if(ThisEvent.EventType == ES_TIMEOUT && ThisEvent.EventParam == AD_TIMER)
{
//read results from analog port line
ADC_MultiRead(results);
SecondLeftValue = FirstLeftValue;
ThirdLeftValue = SecondLeftValue;
FourthLeftValue = ThirdLeftValue;
FifthLeftValue = FourthLeftValue;

SecondRightValue = FirstRightValue;
ThirdRightValue = SecondRightValue;
FourthRightValue = ThirdRightValue;
FifthRightValue = FourthRightValue;

//set first left and right wheel analog values


FirstLeftValue = results[PE0];
FirstRightValue = results[PE1];
WireSubtract = AverageDifference();
}

//restart AD timer
ES_Timer_InitTimer(AD_TIMER, AD_TIMER_LENGTH);
//printf("Difference: %d\n\r", FirstLeftValue - FirstRightValue + 20);
if ((TimesDetected == 0) && (FirstLeftValue > 3800)) {
TimesDetected++;
FirstWheelDetected = LEFT_WHEEL;
Stop();
RotateRight();
} else if ((TimesDetected == 0) && (FirstRightValue > 3800)) {
TimesDetected++;
FirstWheelDetected = RIGHT_WHEEL;
Stop();
RotateLeft();

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

} else if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == ORIENT_TIMER))


{
TimesDetected++;
DriveForward();
} else if ((TimesDetected == 2) && (FirstRightValue > 3800))
{
TimesDetected++;
FirstWheelDetected = RIGHT_WHEEL;

} else if ((TimesDetected == 2) && (FirstLeftValue > 3800))


{
TimesDetected++;
FirstWheelDetected = LEFT_WHEEL;
} else if ((TimesDetected == 3) && (FirstWheelDetected == RIGHT_WHEEL) && (FirstRightValue < 3200))
{
ES_Event ThisEvent;
ThisEvent.EventType = WIRE_DETECTED;
PostMasterSM(ThisEvent);
} else if ((TimesDetected == 3) && (FirstWheelDetected == LEFT_WHEEL) && (FirstLeftValue < 3200))
{
ES_Event ThisEvent;
ThisEvent.EventType = WIRE_DETECTED;
PostMasterSM(ThisEvent);
} else if ((TimesDetected == 3) && (FirstLeftValue < 3200) && (FirstRightValue < 3200))
{
TimesDetected = 0;
//disable wire sensing interrupt
HWREG(NVIC_EN3) &= ~BIT0HI;
Stop();
ThisEvent.EventType = LOST_TRAIL;
printf("POST LOST TRAIL EVENT");
PostMasterSM(ThisEvent);
}

return ReturnEvent;

/****************************************************************************
Function
QueryWireSubtract
Parameters
None
Returns

Description
Notes

Author
Anne Alter, Derrick Contreras
****************************************************************************/
float QueryWireSubtract(void)
{
//printf("Wire Subtract %f\n\r", WireSubtract);
return (WireSubtract + 25); //call this offset
}
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
AverageDifference
Parameters
None
Returns

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\WireSensingService.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

Page 5

Description
Notes

Author
Anne Alter, Derrick Contreras
****************************************************************************/
float AverageDifference(void)
{
float LeftWire;
float RightWire;
float Difference;

LeftWire = (FirstLeftValue + SecondLeftValue + ThirdLeftValue + FourthLeftValue + FifthLeftValue);


RightWire = (FirstRightValue + SecondRightValue + ThirdRightValue + FourthRightValue +
FifthRightValue);

Difference = (float)(LeftWire - RightWire)/5;


//printf("Left %f, Right %f, Difference %f\n\r", LeftWire, RightWire, Difference);
return Difference;

/****************************************************************************
Function
RotateLeft
Parameters
None
Returns

Description
Notes

Author
Anne Alter, Derrick Contreras
****************************************************************************/
void RotateRight(void)
{
SetDirection(RIGHT_WHEEL, BACKWARD);
SetDuty(RIGHT_WHEEL, 75);
ES_Timer_InitTimer(ORIENT_TIMER, ORIENT_TIME);
}
/****************************************************************************
Function
RotateLeft
Parameters
None
Returns

Description
Notes

Author
Anne Alter, Derrick Contreras
****************************************************************************/
void RotateLeft(void)
{
SetDirection(LEFT_WHEEL, BACKWARD);
SetDuty(LEFT_WHEEL, 75);

W:\_ME218bTeams\DoYouEvenStateMachine_Team17\ME218B\Final Project\CODE\Source\WireSensingService.c
360
361
362

Page 6

ES_Timer_InitTimer(ORIENT_TIMER, ORIENT_TIME);

Vous aimerez peut-être aussi