-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
428 lines (354 loc) · 9.67 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
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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* Cruise control for the kp starlet
Written by Ryan Taylor
*/
// INCUDES
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "speed.h"
#include "rit128x96x4.h"
#include "display.h"
#include "init.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_uart.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "debounce.h"
#include "data_process.h"
#include "motor_control.h"
#include "driverlib/systick.h"
#include "driverlib/timer.h"
#include "my_adc.h"
//*****************************************************************************
// Constants
//*****************************************************************************
#define MAX_24BIT_VAL 0X0FFFFFF
#define BUF_SIZE 8
//#define RAND_MAX 300
//GLOBALE VARIABLES
int encoder_1 = 0;
int encoder_2 = 0;
int prev_state = 0;
long time_last_2 = 0;
bool thing = 0;
bool select_prev = 0;
static circBuf_t speed_buffer; // Buffer
cruise_data speed_set_data;
int index = 0;
char UART_char_data[120];
char UART_char_data_old[120];
//float time_array[5];
//float speed_array[5];
bool read_data = 0;
acc_time acc_times; // acceleration testing
float current_acc_time = 0;
bool started = 0;
int refresh_rate = 0;
float system_timer = 0;
// rate!
#define PMTK_SET_NMEA_UPDATE_5HZ "$PMTK220,200*2C\r\n"
#define PMTK_SET_NMEA_UPDATE_10HZ "$PMTK220,100*2F\r\n"
// turn on GPRMC and GGA
#define PMTK_SET_NMEA_OUTPUT_RMCGGA "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n"
// request for updates on antenna status
#define PGCMD_ANTENNA "$PGCMD,33,1*6C\r\n"
#define PGCMD_NOANTENNA "$PGCMD,33,0*6D\r\n"
// Interrupt handler
void PinChangeIntHandler (void) {
unsigned long ul_A_Val;
unsigned long ul_B_Val;
int current_state;
// Clear the interrupt (documentation recommends doing this early)
GPIOPinIntClear (GPIO_PORTF_BASE, GPIO_PIN_7);
GPIOPinIntClear (GPIO_PORTF_BASE, GPIO_PIN_5);
ul_A_Val = GPIOPinRead (GPIO_PORTF_BASE, GPIO_PIN_7); // Read the pin
ul_B_Val = GPIOPinRead (GPIO_PORTF_BASE, GPIO_PIN_5);
if (!ul_A_Val){ //Check what state the pins at and assign that state to "current state"
if(!ul_B_Val){
current_state = 1;
}
else{
current_state = 2;
}
}
else{
if(ul_B_Val){
current_state = 3;
}
else{
current_state = 4;
}
}
// Check if the previous state is different from the current state.
// Determine what direction the encoder is spinning
if (current_state != prev_state){
if (abs(prev_state-current_state) == 1){
if(current_state>prev_state){
encoder_1 --;
}
else{
encoder_1 ++;
}
}
else{
if(current_state<prev_state){
encoder_1 --;
}
else{
encoder_1 ++;
}
}
}
prev_state = current_state; // Assign current state for next time the interrupt runs
}
void store_char(long UART_character){
if (UART_character == '$'){
UART_char_data_old[0] = '\0';
strcpy(UART_char_data_old, UART_char_data);
index = 0;
read_data = 0;
UART_char_data[index] = UART_character;
index++;
}
else{
UART_char_data[index] = UART_character;
index++;
}
}
void UARTIntHandler(void) {
unsigned long ulStatus;
long UART_character = 0;
// Get the interrupt status.
//
ulStatus = UARTIntStatus(UART0_BASE, true);
// Clear the asserted interrupts.
//
UARTIntClear(UART0_BASE, ulStatus);
// Loop while there are characters in the receive FIFO.
//
while(UARTCharsAvail(UART0_BASE)) {
// Read the next character from the UART and write it back to the UART.
//
//UARTCharPutNonBlocking(UART0_BASE, UARTCharGetNonBlocking(UART0_BASE));
UART_character = UARTCharGetNonBlocking(UART0_BASE);
store_char(UART_character);
}
}
int read_button_screen(int screen_old, bool fix){
int screen = screen_old;
/*BUTTON_DATA
* 0 bit | DOWN
* 1 bit | UP
* 2 bit | LEFT
* 3 bit | RIGHT
* 4 bit | SELECT
*/
int button_data = return_button();
bool down = bit_check(button_data, 0);
bool up = bit_check(button_data, 1);
bool left = bit_check(button_data, 2);
//bool right = bit_check(button_data, 3);
bool select = bit_check(button_data, 4);
if (fix == 0){
screen = 4;//no fix screen
}
else{
if (select == 1 || (fix == 1 && screen == 4)){// back function
screen = 0;
}
else if (screen == 0 && left == 1){
screen = 2;
}
else if ((up || down) == 1 && screen == 0){ // set speed
screen = 1;
}
}
if (screen_old != screen){
clearDisplay();
}
return screen;
}
void select_read(void){
int button_data = return_button();
bool select = bit_check(button_data, 4);
if(select != select_prev && select == 1){
if (speed_set_data.enable == 1){
speed_set_data.enable = 0;
}
else{
speed_set_data.enable = 1;
}
}
select_prev = select;
}
void send_data(void){
int i = 0;
while(i <= 100000){i++;}
i = 0;
UARTSend((unsigned char *)PMTK_SET_NMEA_UPDATE_5HZ, 18, 0);
while(i <= 100000){i++;}
i = 0;
UARTSend((unsigned char *)PMTK_SET_NMEA_OUTPUT_RMCGGA, 53,0);
while(i <= 1000000){i++;}
UARTSend((unsigned char *)PGCMD_NOANTENNA, 16,0);
}
// This function stores the altitude in a buffer "speed_buffer" for analysis later
void store_speed(float single_speed){
if (single_speed > 150 ){
single_speed = 150;
}
speed_buffer.data[speed_buffer.windex] = single_speed;
speed_buffer.windex ++;
if (speed_buffer.windex >= speed_buffer.size){
speed_buffer.windex = 0;
}
}
float analysis_speed(void){
int i = 0;
float speed_sum = 0;
for (i = 0; i < BUF_SIZE; i++)
speed_sum += speed_buffer.data[i];
return (speed_sum/BUF_SIZE);
}
float max_acc_func(float acc, float max){
if (acc > max){
return acc;
}
return max;
}
// Assumes 0 <= max <= RAND_MAX
// Returns in the half-open interval [0, max]
/*long random_at_most(long max) {
unsigned long
// max <= RAND_MAX < ULONG_MAX, so this is okay.
num_bins = (unsigned long) max + 1,
num_rand = (unsigned long) RAND_MAX + 1,
bin_size = num_rand / num_bins,
defect = num_rand % num_bins;
long x;
do {
x = rand();
}
// This is carefully written not to overflow
while (num_rand - defect <= (unsigned long)x);
// Truncated division is intentional
return x/bin_size;
}*/
void send_info(int fake_speed){// in knots
long current_time = SysTickValueGet();
long diff = 0;
char buf[90];
fake_speed = (int)fake_speed;
if (current_time <= time_last_2){
diff = time_last_2 - current_time;
}
else {
diff = (time_last_2 + MAX_24BIT_VAL) - current_time;
}
if (diff > 2000000) {
sprintf(buf, "$GPRMC,194509.000,A,4042.6142,N,07400.4168,W,%d,221.11,160412,,,A*77\n", fake_speed);
UARTSend((unsigned char *)buf, 85, 1);
time_last_2 = current_time;
}
}
void Timer1IntHandler(void){
TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
system_timer += 0.1;
speed_set_data.old = speed_set_data.speed;
if (started == 1){
current_acc_time += 0.1;
}
}
void acceleration_test(float speed){
if (speed < 3){
current_acc_time = 0;
init_acc_time(&acc_times); //reset time
}
if (speed > 3 && started == 0) {
started = 1;
}
if (speed > 20 && acc_times.acc20 == 0){
acc_times.acc20 = current_acc_time;
}
if (speed > 40 && acc_times.acc40 == 0){
acc_times.acc40 = current_acc_time;
}
if (speed > 60 && acc_times.acc60 == 0){
acc_times.acc60 = current_acc_time;
}
if (speed > 80 && acc_times.acc80 == 0){
acc_times.acc80 = current_acc_time;
}
if (speed > 100 && acc_times.acc100 == 0) {
acc_times.acc100 = current_acc_time;
started = 0; //finished
}
}
// Main function
void main(void) {
reset_peripheral(); initClock(); initTimer(); initDisplay(); initPin();
initGPIO(); initADC(); initConsole(); int i = 0;
//init_password();
send_data();
initCircBuf (&speed_buffer, BUF_SIZE); init_set_speed_data(&speed_set_data);
int screen = 0; int screen_prev = 0; float speed = 0; float buffed_speed = 0;
int fake_speed = 0; float acc = 0; float max_acc = 0; //float fuel_eco = 0;
float distance = 0;
bool fix = 0; uint8_t satillite = 0; float quality = 0; clock time;
int aim_pos = 0; unsigned long adc = 0; //int error_stepper = 0;
IntMasterEnable();
while(1){
//reading data
read_data = split_data(UART_char_data_old, read_data); // decode data
speed = read_speed(); //read data into variables
adc = run_adc()/7;
//calculations
aim_pos = speed_feedback(buffed_speed, encoder_1/40, speed_set_data.speed);
if (speed_set_data.enable == 1){
step_motor_control(encoder_1/40, aim_pos);
}
//sending fake data
fake_speed = (int)adc;//= random_at_most(100/1.852);
send_info(fake_speed);//knots
//storing data
store_speed(speed);
buffed_speed = analysis_speed();
acc = read_acceleration(buffed_speed);
max_acc = max_acc_func(acc, max_acc);
time = read_time();
satillite = read_satillite();
fix = read_fix();
quality = read_quality();
debounce_button(); // debounce buttons
screen = read_button_screen(screen, fix);
distance = read_distance();
select_read(); //need a mosfet for turning power off
// select adds a an on and off switch yo
if (screen == 1){
if(screen_prev != 1 && screen == 1){
speed_set_data.speed = buffed_speed;
}
speed_set_data.speed = set_speed(speed_set_data.speed); // set the speed to cruise at
}
if (screen == 2){ //0 to 100
acceleration_test(speed);
}
// refresh chainging
if (fix == 1 && speed_set_data.old == speed_set_data.speed && refresh_rate < 4){
UARTSend((unsigned char *)PMTK_SET_NMEA_UPDATE_5HZ, 18, 0);
refresh_rate += 1;
}
if (i >= 50){
display(screen, buffed_speed, acc, max_acc, speed_set_data.speed, satillite,
encoder_1/40, time, distance, quality, UART_char_data_old, aim_pos, adc, acc_times);
i = 0;
}
screen_prev = screen;
i++;
}
}