forked from ALGUS-Savunma-ve-Havacilik/nsumo_software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
358 lines (330 loc) · 9.53 KB
/
test.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
#include "test.h"
#include "line_detection.h"
#include "enemy_detection.h"
#include "drive.h"
#include "trace.h"
#include "motor.h"
#ifdef BUILD_MCU
#include "adc.h"
#include "pwm.h"
#include "gpio.h"
#include "ir_remote.h"
#include "state_machine_ir.h"
#include "millis.h"
#include "vl53l0x.h"
#include "led.h"
#include "qre1113.h"
#else // Simulator
#include "microcontroller_c_bindings.h"
#endif
#ifdef BUILD_MCU
void test_dimming_led()
{
pwm_init();
volatile unsigned int i;
uint16_t dim_value = 0;
uint8_t count_direction = 0;
for (;;) {
if (dim_value >= 50 || dim_value <= 0) {
count_direction ^= 1;
}
dim_value += 5 * (count_direction ? 1 : -1);
pwm_set_duty_cycle(PWM_OUT_0, dim_value);
for (i = 3000; i > 0; i--)
; // busy loop delay
}
}
void test_run_motors()
{
motor_init();
motor_set_duty_cycle(MOTORS_LEFT, 50);
motor_set_duty_cycle(MOTORS_RIGHT, 50);
for (;;)
;
}
void test_adc()
{
adc_conf_t conf = { { false } };
conf.enable[GPIO_PIN_IDX(GPIO_10)] = true;
conf.enable[GPIO_PIN_IDX(GPIO_13)] = true;
conf.enable[GPIO_PIN_IDX(GPIO_14)] = true;
conf.enable[GPIO_PIN_IDX(GPIO_15)] = true;
adc_init(&conf);
adc_values_t adc_values = { 0 };
for (;;) {
adc_read(adc_values);
}
}
void test_qre1113()
{
qre1113_init();
qre1113_voltages_t voltages = { 0 };
while (1) {
qre1113_get_voltages(&voltages);
TRACE_NOPREFIX("Line sensor front left %d\n"
"Line sensor front right %d\n"
"Line sensor back left %d\n"
"Line sensor back right %d",
voltages.front_left, voltages.front_right, voltages.back_left,
voltages.back_right);
__delay_cycles(50000);
}
}
void test_qre1113_time()
{
qre1113_init();
uint32_t last_sample_cnt = adc_total_sample_cnt();
uint32_t last_millis = millis();
while (1) {
uint32_t sample_cnt = adc_total_sample_cnt();
if (sample_cnt - last_sample_cnt >= 100) {
TRACE_NOPREFIX("%d samples in %d ms", sample_cnt - last_sample_cnt,
millis() - last_millis);
last_sample_cnt = sample_cnt;
last_millis = millis();
}
__delay_cycles(50000);
}
}
void test_vl53l0x()
{
vl53l0x_ranges_t ranges;
bool success = vl53l0x_init();
while (success) {
success = vl53l0x_read_range_single(VL53L0X_IDX_FRONT, &ranges[VL53L0X_IDX_FRONT]);
success &= vl53l0x_read_range_single(VL53L0X_IDX_LEFT, &ranges[VL53L0X_IDX_LEFT]);
success &= vl53l0x_read_range_single(VL53L0X_IDX_RIGHT, &ranges[VL53L0X_IDX_RIGHT]);
success &=
vl53l0x_read_range_single(VL53L0X_IDX_FRONT_LEFT, &ranges[VL53L0X_IDX_FRONT_LEFT]);
success &=
vl53l0x_read_range_single(VL53L0X_IDX_FRONT_RIGHT, &ranges[VL53L0X_IDX_FRONT_RIGHT]);
TRACE_NOPREFIX("Range sensor left %d\n"
"Range sensor right %d\n"
"Range sensor front left %d\n"
"Range sensor front %d\n"
"Range sensor front right %d",
ranges[VL53L0X_IDX_LEFT], ranges[VL53L0X_IDX_RIGHT],
ranges[VL53L0X_IDX_FRONT_LEFT], ranges[VL53L0X_IDX_FRONT],
ranges[VL53L0X_IDX_FRONT_RIGHT]);
}
}
void test_enemy_detection_led()
{
led_init();
enemy_detection_init();
while (1) {
enemy_detection_t detection = enemy_detection_get();
if (detection.position != ENEMY_POS_NONE) {
TRACE_NOPREFIX("%s %s", enemy_pos_str(detection.position),
enemy_range_str(detection.range));
led_set_enable(LED_TEST, true);
} else {
led_set_enable(LED_TEST, false);
}
}
}
#if 0
void test_vl53l0x_multiple_time()
{
vl53l0x_ranges_t ranges;
bool success = vl53l0x_init();
int measure_cnt = 0;
uint32_t last_millis = millis();
while (success) {
success = vl53l0x_read_range_multiple(ranges);
measure_cnt++;
if (!(measure_cnt % 100)) {
trace("100 measures in %ld ms\n", millis()-last_millis);
last_millis = millis();
}
}
}
#endif
void test_gpio_input()
{
led_init();
const gpio_config_t gpio_config = { .gpio = GPIO_RANGE_SENSOR_FRONT_INT,
.dir = GPIO_INPUT,
.out = GPIO_LOW,
.resistor = RESISTOR_ENABLED,
.selection = GPIO_SEL_GPIO };
gpio_configure(&gpio_config);
while (1) {
led_set_enable(LED_TEST, gpio_get_input(GPIO_RANGE_SENSOR_FRONT_INT));
}
}
void test_vl53l0x_multiple()
{
vl53l0x_ranges_t ranges;
bool success = vl53l0x_init();
bool fresh_values = false;
while (success) {
success = vl53l0x_read_range_multiple(ranges, &fresh_values);
TRACE_NOPREFIX("left %d"
"right %d"
"fleft %d"
"front %d"
"fright %d",
ranges[VL53L0X_IDX_LEFT], ranges[VL53L0X_IDX_RIGHT],
ranges[VL53L0X_IDX_FRONT_LEFT], ranges[VL53L0X_IDX_FRONT],
ranges[VL53L0X_IDX_FRONT_RIGHT]);
}
}
void test_state_machine_ir()
{
state_machine_ir_init();
bool show_led = true;
ir_remote_init();
while (1) {
ir_key_t key = ir_remote_get_key();
if (key != IR_KEY_NONE) {
state_machine_ir_handle_key(key);
}
led_set_enable(LED_TEST, show_led);
show_led = !show_led;
__delay_cycles(50000);
}
}
void test_ir_receiver()
{
ir_remote_init();
volatile uint16_t keypresses = 0;
for (;;) {
if (ir_remote_get_key() != IR_KEY_NONE) {
keypresses++;
}
}
}
void test_drives_remote()
{
ir_remote_init();
drive_init();
drive_speed_t speed = DRIVE_SPEED_SLOW;
drive_t drive = DRIVE_ARCTURN_SHARP_LEFT;
while (1) {
const ir_key_t key = ir_remote_get_key();
if (key != IR_KEY_NONE) {
TRACE_NOPREFIX("Ir key %d", key);
} else {
__delay_cycles(50000);
continue;
}
switch (key) {
case IR_KEY_0:
speed = DRIVE_SPEED_SLOW;
break;
case IR_KEY_1:
speed = DRIVE_SPEED_MEDIUM;
break;
case IR_KEY_2:
speed = DRIVE_SPEED_FAST;
break;
case IR_KEY_3:
speed = DRIVE_SPEED_FASTEST;
break;
case IR_KEY_4:
drive = DRIVE_ARCTURN_SHARP_LEFT;
break;
case IR_KEY_5:
drive = DRIVE_ARCTURN_MID_LEFT;
break;
case IR_KEY_6:
drive = DRIVE_ARCTURN_WIDE_LEFT;
break;
case IR_KEY_7:
case IR_KEY_8:
case IR_KEY_9:
case IR_KEY_LEFT:
case IR_KEY_RIGHT:
case IR_KEY_STAR:
case IR_KEY_HASH:
case IR_KEY_UP:
case IR_KEY_DOWN:
case IR_KEY_OK:
case IR_KEY_NONE:
drive_stop();
continue;
break;
}
drive_set(drive, false, speed);
__delay_cycles(50000);
}
}
#endif // BUILD_MCU
void test_line_detection()
{
line_detection_init();
while (1) {
line_detection_t line_detection = line_detection_get();
TRACE_NOPREFIX("%s", line_detection_str(line_detection));
}
}
void test_drive_and_line_detect()
{
line_detection_init();
drive_init();
const drive_speed_t speed = DRIVE_SPEED_FASTEST;
drive_t current_drive = DRIVE_FORWARD;
drive_set(current_drive, false, speed);
while (1) {
drive_t new_drive = current_drive;
switch (line_detection_get()) {
case LINE_DETECTION_FRONT:
case LINE_DETECTION_FRONT_LEFT:
case LINE_DETECTION_FRONT_RIGHT:
new_drive = DRIVE_REVERSE;
break;
case LINE_DETECTION_BACK:
case LINE_DETECTION_BACK_LEFT:
case LINE_DETECTION_BACK_RIGHT:
new_drive = DRIVE_FORWARD;
break;
case LINE_DETECTION_NONE:
case LINE_DETECTION_LEFT:
case LINE_DETECTION_RIGHT:
case LINE_DETECTION_DIAGONAL_LEFT:
case LINE_DETECTION_DIAGONAL_RIGHT:
break;
}
if (new_drive != current_drive) {
drive_set(new_drive, false, speed);
current_drive = new_drive;
}
}
}
void test_rotate_and_enemy_detect()
{
enemy_detection_init();
drive_init();
bool rotate = false;
bool new_rotate = false;
while (1) {
const enemy_detection_t detection = enemy_detection_get();
new_rotate = (detection.position != ENEMY_POS_FRONT);
if (new_rotate != rotate) {
if (new_rotate) {
drive_set(DRIVE_ROTATE_LEFT, false, DRIVE_SPEED_FASTEST);
} else {
drive_stop();
}
rotate = new_rotate;
}
}
}
void test_enemy_detection_print()
{
enemy_detection_init();
while (1) {
enemy_detection_t detection = enemy_detection_get();
TRACE_NOPREFIX("%s %s", enemy_pos_str(detection.position),
enemy_range_str(detection.range));
}
}
void test_drive_duty_cycles()
{
drive_init();
drive_set(DRIVE_ARCTURN_WIDE_LEFT, false, DRIVE_SPEED_FASTEST);
while (1) {
millis();
}
}