-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
153 lines (141 loc) · 3.58 KB
/
main.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
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
145
146
147
148
149
150
151
/*
Author: Zack Rauen
*/
#include "MK64F12.h"
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "main.h"
#include "DataTypes.h"
#include "Common.h"
#include "LSC.h"
#include "PID.h"
#include "PIXY.h"
#include "LCD.h"
#include "DrivingControl.h"
#include "ModeControl.h"
#include "Servo.h"
void debugPrint();
void LCD_ButtonTimerInit(void);
void PIT2_IRQHandler(void);
/*----------------------------------------------------------------------------
Main function
*----------------------------------------------------------------------------*/
int main (void) {
// Initialize basic things
hardware_init();
dbg_uart_init();
BOARD_ClockInit();
OSA_Init();
// Initialize my code.
Common_Initialize(); // Initialize core abstraction layer
I2C_Initialize(); // Initialize I2C0 for LCD and PIXY
LCD_Initialize(); // Initialize LCD with standard message
LSC_Initialize(); // Default settings, interrupts enabled, priorities 5 6 7
DrivingControl_Initialize(); // Initialize steering and motor PWMs
LCD_ButtonTimerInit(); // Start button reading from LCD panel
ModeControl_Initialize(); // Begin in STANDBY
while (1) {
switch (mode) {
case STANDBY:
previousMode=mode;
DrivingControl_AllStop();
DrivingData.breakreq = false;
DrivingData.lapGoal = 2;
break;
case ACCURACY:
previousMode=mode;
DrivingControl_SetNormalSpeed(ACCURACY_MODE_SPEED);
DrivingData.breakreq = false;
DrivingControl_FollowLine();
if (DrivingData.done == true)
ModeControl_changeMode(STANDBY);
break;
case SPEED:
previousMode=mode;
DrivingData.lapGoal = 2;
DrivingControl_SetNormalSpeed(SPEED_MODE_SPEED);
DrivingData.breakreq = false;
DrivingControl_FollowLine();
if (DrivingData.done == true)
ModeControl_changeMode(STANDBY);
break;
case DISCOVERY:
previousMode=mode;
DrivingData.breakreq = false;
DrivingData.lapGoal = 1;
DrivingControl_FindLine();
if (DrivingData.breakreq == false)
ModeControl_changeMode(ACCURACY);
break;
default: break;
}
// debugPrint(); // Use for debugging
}
}
void LCD_ButtonTimerInit(void) {
Common_SetupPIT(2, 50, ms);
NVIC_EnableIRQ(PIT2_IRQn);
NVIC_SetPriority(PIT2_IRQn,5);
}
void PIT2_IRQHandler(void) {
Common_ClearPITFlag(2); // Clear interrupt
LCD_ReadButton();
// printf("Button: %d\r\n", LCD_buttonPressed);
switch (LCD_buttonPressed) {
case LCD_BTN_SELECT:
LCD_modeSelected = true;
break;
case LCD_BTN_RIGHT:
if (nextMode == NUM_OF_MODES-1) {
nextMode = 0;
}
else {
nextMode++;
}
LCD_buttonJustPressed = true;
DrivingData.breakreq = true;
break;
case LCD_BTN_LEFT:
if (nextMode == 0) {
nextMode = NUM_OF_MODES-1;
}
else {
nextMode--;
}
LCD_buttonJustPressed = true;
DrivingData.breakreq = true;
break;
default: break;
}
}
void debugPrint() {
char *dirString;
switch (DrivingData.turningDirection) {
case 0:
dirString = "LEFT";
break;
case 1:
dirString = "RIGHT";
break;
case 2:
dirString = "FORWARD";
break;
case 3:
dirString = "BACKWARD";
break;
case 4:
dirString = "NONE";
break;
}
printf("\r\n\r\n");
for (int i = 0; i <= 127; i++) { // ignore the first and last 16 bits in the camera frame
printf("%d",LSC.processedData[i] ? 1 : 0);
}
printf("\r\n\r\n");
printf("Location: %d", LSC.location);
printf(" | Line Length: %d", LSC.lineLength);
printf(" | Intersection Possible?: %s", LSC.intersectionPossible == true ? "YES" : "NO");
printf(" | Left: %f | Right: %f", LeftPID.output, RightPID.output);
printf(" | Direction: %s", dirString);
}