This repository was archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
251 lines (216 loc) · 5.51 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
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
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
// Custom Includes
#include "Defines.h"
#include "Control.h"
#include "I2CInstruction.h"
#include "I2CDriver.h"
#include "UsartAsFile.h"
#include "BNO055.h"
#include "ShiftReg.h"
#include "VL6180x.h"
#include "Encoder.h"
#include "Motor.h"
#include "comms.h"
// Library Includes
#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <string.h>
// Tick buffer - low-pass filtering for encoders using a ring buffer
#define TICK_BUFF_SIZE_BITS 2
#define TICK_BUFF_SIZE (1 << TICK_BUFF_SIZE_BITS)
volatile int16_t tickBuf[TICK_BUFF_SIZE] = {0};
volatile int tickBufIdx = 0;
// Tpp = ticks per period, a measure of how fast the encoders are going
volatile int16_t currTpp = 0;
volatile int16_t goalTpp = 0;
/* Stores the current action state. See defines.h. DO NOT UPDATE ON
* IT'S OWN UNLESS YOU KNOW WHAT YOU'RE DOING... USE THE FN */
volatile Action g_action_mode = ACT_OFF;
/* Add anything you want to print every 50ms */
#ifdef DEBUG
void debug_print(void)
{
// fprintf(usartStream_Ptr, "state: %ul\n", getActionMode());
return;
}
#endif // DEBUG
// Milliseconds since initialization
volatile static uint32_t g_s_millis = 0;
volatile static int8_t g_s_milliFlag = 0;
// Set's the action mode (intelligently)
void setActionMode(Action mode)
{
if (mode == ACT_PUSH_FW || mode == ACT_PUSH_BW)
{
// fprintf(usartStream_Ptr, "HERE2\n");
if (!testPush())
{
if (mode == ACT_PUSH_BW)
{
// fprintf(usartStream_Ptr, "HERE: mode: %d\n", mode);
mode = ACT_MOVE_COR_BW;
}
else
{
mode = ACT_MOVE_COR;
}
}
else
{
adjustHeading(1000);
}
}
if (mode == ACT_MOVE_COR || mode == ACT_MOVE_COR_BW)
{
wallAlignTest();
}
g_action_mode = mode;
}
// Gets the action mode
Action getActionMode()
{
return g_action_mode;
}
// This is run every ~1 ms
void millisTask(void)
{
// Throws an error if millisecond timer fails
static unsigned long lastMilli;
if (lastMilli != g_s_millis && lastMilli != g_s_millis - 1)
{
fprintf(usartStream_Ptr, "last %lu", lastMilli);
fprintf(usartStream_Ptr, "curr %lu\n", g_s_millis);
}
lastMilli = g_s_millis;
if (!g_s_milliFlag)
{
return;
}
g_s_milliFlag = 0;
// Ask for IMU data on every 10 milliseconds
if (!(g_s_millis % 10))
{
bno055Task();
}
// Run the comms task every 5 ms
if (!(g_s_millis % 5))
{
commsTask();
// commsReceiveTask();
// commsUpdateModeTask();
}
// Run PID every 10 milliseconds (offset by 4)
if (!((g_s_millis+4) % 10))
{
switch (g_action_mode)
{
case ACT_ROTATE:
case ACT_MOVE_COR:
case ACT_MOVE_COR_BW:
pidRotate();
break;
case ACT_MOVE:
pidStraightLine();
break;
case ACT_MOVE_BW:
pidStraightLine();
break;
case ACT_STOP:
pidStop();
break;
case ACT_PUSH_FW:
pidRotate();
break;
case ACT_PUSH_BW:
pidRotate();
break;
default:
pidOff();
break;
}
}
// Ask for Encoder data every 5 milliseconds (offset by 3)
if (!((g_s_millis+3) % 5))
{
// Uses a ring buffer to low-pass filter the encoder speeds
tickBuf[tickBufIdx] = getAverageEncoderTicks();
currTpp = tickBuf[tickBufIdx] - tickBuf[(tickBufIdx + 1) % TICK_BUFF_SIZE];
if (!((goalTpp >= 0) ^ (tickBuf[tickBufIdx] >= goalTicksTotal)))
goalTpp = 0;
tickBufIdx = (tickBufIdx + 1) % TICK_BUFF_SIZE;
}
// Ask for Distance data on every 10 milliseconds (offset by 1)
if (!((g_s_millis+1) % 20))
{
VL6180xTask();
}
#ifdef DEBUG
// DEBUG PRINT EVERY 50 ms
if (!(g_s_millis % 20))
{
debug_print();
}
#endif // DEBUG
}
// This handles our millisecond counter overflow
ISR(TIMER0_OVF_vect)
{
// Allow other interrupts to preempt this interrupt
sei();
// Increment milliseconds, then run the millisecond task
g_s_millis++;
g_s_milliFlag = 1;
millisTask();
}
/* This initializes timer0 to overflow about once every 1.02ms and
* interrupt on overflow */
void millisInit(void)
{
// Enables timer0 w/ prescaler div 64
TCCR0B |= (1<<CS01) | (1<<CS00);
// Enable interrupt on overflow
TIMSK0 |= (1<<TOIE0);
}
int main(void)
{
// This disables the JTAG debugging interface
MCUCR |= (1<<JTD);
// This disables clkdiv8 (use clkdiv1)
CLKPR = (1<<CLKPCE);
CLKPR = 0;
// Various initializations
I2CInit(200000);
usartInit(115200);
encoderInit();
// Configures IMU
bno055EnterNDOF();
// Enables interrupts
sei();
// Initializes shift register
srInit();
// Initializes distance sensors
VL6180xInit();
// Initializes millisecond timer
millisInit();
// Initializes motors
motorsInit();
// Start the goal heading at the starting angle
I2CInstruction_ID firstAngBack = bno055Task();
while (I2CBufferContains(firstAngBack))
{
I2CTask();
}
setGoalHeading(bno055GetCurrHeading());
// Main loop
while (1)
{
#ifdef DEBUG
// debug_comms_task();
#endif // DEBUG
I2CTask();
}
}