-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlines.c
More file actions
166 lines (134 loc) · 4.29 KB
/
lines.c
File metadata and controls
166 lines (134 loc) · 4.29 KB
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
#include "hal.h"
#include "ch.h"
#include "chprintf.h"
#include "lines.h"
#include <math.h>
#define LINES_SERIAL &SD6
#define INIT_MESSAGE_LENGTH 3
#define COMMAND_MESSAGE_LENGTH 9
#define RESET_LENGTH 1
#define COMMAND_LENGTH 4
#define VALUES_LENGTH 4
#define NUMBER_OF_SENSORS 16
#define INERTIA 0.95
#define LINES_AWAY_DISTANCE 10
#define PI 3.14159
int32_t round_values[NUMBER_OF_SENSORS];
uint8_t values[4];
uint8_t init_queue = 0;
uint8_t messages_buffer[INIT_MESSAGE_LENGTH + COMMAND_MESSAGE_LENGTH] = {'\n', 0x80, 0xC4,'\n', 'F', 'F', '8', '1', '0', '0', '0', '0'};
static double round_data_values[NUMBER_OF_SENSORS];
static int16_t dx[16] = {20,56,83,98,98,83,56,20,-20,-56,-83,-98,-98,-83,-56,-20};
static int16_t dy[16] = {98,83,56,20,-20,-56,-83,-98,-98,-83,-56,-20,20,56,83,98};
static int16_t result_degree;
static int16_t res_x, res_y;
static int16_t i_see_line = 0;
thread_t *writingp;
thread_t *readingp;
thread_t *workingp;
uint8_t char_to_value(uint8_t character) {
if (character >= 'A') {
character = character + 10 - 'A';
} else {
character = character - '0';
}
return character;
}
void determine_avoiding_direction(void) {
int8_t i;
res_x = 0, res_y = 0;
i_see_line = 0;
for(i = 0; i < NUMBER_OF_SENSORS; i++) {
round_data_values[i] = round_data_values[i] * INERTIA + round_values[i] * (1.0 - INERTIA);
if (round_data_values[i] > 0.99 - INERTIA) {
res_x += dx[i];
res_y += dy[i];
}
if (round_values[i] == 1 && i_see_line == 0) {
i_see_line = 1;
}
}
result_degree = ((int16_t)(atan2(res_y, res_x) / PI * 180) + 405 ) % 360;
}
int16_t check_line(void) {
if (i_see_line == 0) return -1;
else return result_degree;
}
THD_WORKING_AREA(waLineWritingThread, 128);
THD_FUNCTION(LineWritingThread, arg) {
(void)arg;
uint8_t msg_length = 0, msg_start_index = 0;
int8_t i;
init_queue = 1;
while (1) {
if(init_queue == 1) {
msg_length = INIT_MESSAGE_LENGTH;
chMsgSend(readingp, (msg_t)msg_length);
init_queue = 0;
msg_start_index = 0;
} else {
msg_length = COMMAND_MESSAGE_LENGTH;
chMsgSend(readingp, (msg_t)msg_length);
msg_start_index = INIT_MESSAGE_LENGTH;
}
for(i = msg_start_index; i < (msg_start_index + msg_length); i++) {
sdPut(LINES_SERIAL, messages_buffer[i]);
}
}
}
THD_WORKING_AREA(waLineReadingThread, 128);
THD_FUNCTION(LineReadingThread, arg) {
(void)arg;
thread_t *writing_thread;
uint8_t msg_length, received_char;
int8_t i;
while (1) {
writing_thread = chMsgWait();
msg_length = (uint8_t)chMsgGet(writing_thread);
chMsgRelease(writing_thread, MSG_OK);
for(i = 0; i < msg_length; i++) {
received_char = sdGet(LINES_SERIAL);
if(received_char != '\n' && i == 0) {
i--;
} else {
if(msg_length != INIT_MESSAGE_LENGTH) {
if(i > ((RESET_LENGTH + COMMAND_LENGTH) - 1)) {
values[i - (RESET_LENGTH + COMMAND_LENGTH)] = char_to_value(received_char);
}
}
}
}
if(msg_length != INIT_MESSAGE_LENGTH) {
chMsgSend(workingp, (msg_t)1);
}
}
}
THD_WORKING_AREA(waLineWorkingThread, 128);
THD_FUNCTION(LineWorkingThread, arg) {
(void)arg;
thread_t *reading_thread;
int8_t i, j, f;
while (1) {
reading_thread = chMsgWait();
chMsgGet(reading_thread);
f = 0;
for(i = 0; i < 4-FERQUES; i++) {
if(i == 1) f = FERQUES;
for(j = 0; j < 4; j++) {
round_values[4*(i+f) + j] = (values[i] >> j) & 1;
}
}
chMsgRelease(reading_thread, MSG_OK);
determine_avoiding_direction();
}
}
void lines_init(void) {
int8_t i;
for (i = 0; i < NUMBER_OF_SENSORS; i++) {
round_data_values[i] = 0;
}
i_see_line = 0; // 0 --> false, I don't see line
readingp = chThdCreateStatic(waLineReadingThread, sizeof(waLineReadingThread), NORMALPRIO, LineReadingThread, NULL);
writingp = chThdCreateStatic(waLineWritingThread, sizeof(waLineWritingThread), NORMALPRIO, LineWritingThread, NULL);
workingp = chThdCreateStatic(waLineWorkingThread, sizeof(waLineWorkingThread), NORMALPRIO, LineWorkingThread, NULL);
}