2016-08-30 11:37:40 +00:00
|
|
|
/*
|
|
|
|
* collision_ctrl.c
|
|
|
|
*
|
|
|
|
* Created on: Aug 30, 2016
|
|
|
|
* Author: tkl
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "board_devices.h"
|
|
|
|
#include "queue.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "stack.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "driver.h"
|
|
|
|
#include "shell.h"
|
|
|
|
#include "drive_ctrl.h"
|
|
|
|
#include "engine_ctrl.h"
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
struct collision_ctrl_object {
|
|
|
|
int distance_mm;
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
struct collision_ctrl_object collision_ctrl_object;
|
|
|
|
struct thread_context collision_ctrl_ctx;
|
|
|
|
stack_t collision_ctrl_stack[256];
|
|
|
|
|
|
|
|
static void collision_ctrl_loop(void *arg)
|
|
|
|
{
|
|
|
|
drv_open(&pwm5_c2); /* trigger pwm */
|
|
|
|
drv_open(&pwm2_c4); /* read pwm */
|
2016-09-19 14:18:04 +00:00
|
|
|
drv_open(&rng);
|
2016-08-30 11:37:40 +00:00
|
|
|
while(1) {
|
|
|
|
int pulse_ns = drv_ioctl(&pwm2_c4, IOCTL_PWM_GET_PULSE_WIDTH_NS, NULL);
|
|
|
|
collision_ctrl_object.distance_mm = 343 * pulse_ns / 1000 / 2;
|
2016-09-20 07:06:12 +00:00
|
|
|
#if 0
|
2016-08-30 11:37:40 +00:00
|
|
|
if(collision_ctrl_object.distance_mm < 300) {
|
|
|
|
char print_buffer[50];
|
|
|
|
if(engine_ctrl_get_current_speed_value() != 0) {
|
|
|
|
drive_ctrl_halt();
|
|
|
|
sprintf(print_buffer, "collision detected (%dmm)...\r\n", collision_ctrl_object.distance_mm);
|
|
|
|
shell_write(print_buffer, strlen(print_buffer));
|
|
|
|
sprintf(print_buffer, "going back...\r\n");
|
|
|
|
shell_write(print_buffer, strlen(print_buffer));
|
|
|
|
drive_ctrl_backward();
|
2016-09-19 14:18:04 +00:00
|
|
|
sleep_ms(500);
|
2016-08-30 11:37:40 +00:00
|
|
|
drive_ctrl_halt();
|
|
|
|
|
|
|
|
sprintf(print_buffer, "start turning...\r\n");
|
|
|
|
shell_write(print_buffer, strlen(print_buffer));
|
|
|
|
drive_ctrl_turn_left();
|
2016-09-19 14:21:18 +00:00
|
|
|
sleep_ms(drv_read(&rng, NULL, 0) % 3500 + 500); // rand between 500 and 4000
|
2016-08-30 11:37:40 +00:00
|
|
|
drive_ctrl_halt();
|
|
|
|
|
|
|
|
sprintf(print_buffer, "restart forward...\r\n");
|
|
|
|
shell_write(print_buffer, strlen(print_buffer));
|
|
|
|
drive_ctrl_forward();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(print_buffer, "collision detected while not driving 8(\r\n");
|
|
|
|
shell_write(print_buffer, strlen(print_buffer));
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 07:06:12 +00:00
|
|
|
#endif
|
2016-08-30 11:37:40 +00:00
|
|
|
sleep_ms(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int collision_ctrl_init(void)
|
|
|
|
{
|
|
|
|
thread_create(
|
|
|
|
&collision_ctrl_ctx,
|
|
|
|
collision_ctrl_stack,
|
|
|
|
sizeof(collision_ctrl_stack) / sizeof(stack_t),
|
|
|
|
collision_ctrl_loop,
|
|
|
|
NULL,
|
|
|
|
THREAD_PRIO_LOW);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int collision_ctrl_get_distance_mm(void)
|
|
|
|
{
|
|
|
|
return collision_ctrl_object.distance_mm;
|
|
|
|
}
|
2016-09-20 07:06:12 +00:00
|
|
|
|
|
|
|
bool collision_ctrl_is_collision(void)
|
|
|
|
{
|
|
|
|
if(collision_ctrl_object.distance_mm < 400)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|