-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultraSonar_main.c
87 lines (82 loc) · 2.29 KB
/
ultraSonar_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
/*
* ultraSonar_main.c
*
* Created on: 2019Äê11ÔÂ2ÈÕ
* Author: jiez
*/
#include "ultraSonar_main.h"
UART_Handle initUart()
{
UART_Params uartParams;
uartParams.readMode = UART_MODE_BLOCKING;
uartParams.readDataMode = UART_DATA_BINARY;
uartParams.baudRate = 9600;
uartParams.readEcho = UART_ECHO_OFF;
uartParams.stopBits = UART_STOP_ONE;
uartParams.parityType = UART_PAR_NONE;
uartParams.dataLength = UART_LEN_8;
return UART_open(Board_UART0, &uartParams);
}
void insertionSort(uint32_t arr[], int n)
{
int i, key, j;
for(i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while(j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j - 1;
}
arr[j+1] = key;
}
}
void *sonarRead(void *arg0)
{
UART_Handle ultra_uart = initUart();
uint8_t data[SONIC_DATA_LENGTH];
int count = 0;
uint32_t arr[10];
char dataNeeded[DISTANCE_LENGTH];
if(ultra_uart == NULL){
dbgTerminalError();
}
while(1)
{
UART_read(ultra_uart, data, sizeof(data));
if(data[0] == 'R'){
dataNeeded[0] = data[1];
dataNeeded[1] = data[2];
dataNeeded[2] = data[3];
uint32_t cur;
sscanf(dataNeeded, "%d", &cur);
arr[count] = cur;
count++;
}
if (count == SEND_SPEED)
{
insertionSort(arr, sizeof(arr)/sizeof(arr[0]));
uint32_t sum = 0;
int i;
for(i = 2; i < 8; i++){
sum += arr[i];
}
msgQueue_sensor msgUltra;
uint32_t final = sum / (SEND_SPEED - 4);
snprintf(dataNeeded[0], sizeof(dataNeeded[0]), final >> 16);
snprintf(dataNeeded[1], sizeof(dataNeeded[1]), final >> 8);
snprintf(dataNeeded[2], sizeof(dataNeeded[2]), final);
// snprintf(dataNeeded, sizeof(dataNeeded), "%d", final);
// dataNeeded[2] = dataNeeded[1];
// dataNeeded[1] = dataNeeded[0];
// dataNeeded[0] = '0';
memcpy(msgUltra.data, dataNeeded, sizeof(dataNeeded));
msgUltra.dataType = ULTRA_MSG_TYPE;
sendMsgToSensorQ(&msgUltra);
count = 0;
sum = 0;
}
}
return (NULL);
}