Skip to content
Snippets Groups Projects
Commit 5105407b authored by fortissBot's avatar fortissBot
Browse files

LRF: Added timeout

refs 7956
parent ae36bd05
No related branches found
No related tags found
No related merge requests found
#include "inc/LaserRangeFinder.h" #include "inc/LaserRangeFinder.h"
int timeout;
/** Initialize the laser range finder. */ /** Initialize the laser range finder. */
void lrf_init() { void lrf_init() {
timeout = 0;
/*
char port[] = "/dev/????????"; char port[] = "/dev/????????";
FILE* fp = popen("dmesg | grep -A 3 \"Arduino Uno\" | grep -o \"ttyACM.\" | tail -n 1","r"); FILE* fp = popen("dmesg | grep -A 3 \"Arduino Uno\" | grep -o \"ttyACM.\" | tail -n 1","r");
fgets(port+5, sizeof(port)-6, fp); fgets(port+5, sizeof(port)-6, fp);
printf("Port: %s\n", port); */
char port[100];
FILE* fp = popen("ls -d /dev/serial/by-id/* | grep \"Arduino\" | tr -d \"\n\"", "r");
fgets(port, sizeof(port), fp);
printf("lrf port=%s\n", port);
pclose(fp); pclose(fp);
uart_setup(port); uart_setup(port);
...@@ -26,22 +36,27 @@ GEN_TYPE_boolean lrf_is_noval() { ...@@ -26,22 +36,27 @@ GEN_TYPE_boolean lrf_is_noval() {
/** Get one distance measurement from the laser range finder. */ /** Get one distance measurement from the laser range finder. */
GEN_TYPE_int lrf_read() { GEN_TYPE_int lrf_read() {
if (uart_filestream == -1) if(timeout>100)
lrf_init();
if (uart_filestream == -1) {
timeout++;
return -1; return -1;
}
//empty buffer //empty buffer
uart_flush(); uart_flush();
//initialize arrays //initialize arrays
int i; int i;
char bytes[6]; char bytes[10];
for (i = 0; i < 6; i++) { for (i = 0; i < sizeof(bytes) / sizeof(bytes[0]); i++) {
bytes[i] = 0; bytes[i] = 0;
} }
int n_read = 0; int n_read = 0;
char value[4]; char value[10];
for (i = 0; i < 4; i++) { for (i = 0; i < sizeof(value) / sizeof(value[0]); i++) {
value[i] = 0; value[i] = 0;
} }
...@@ -50,16 +65,20 @@ GEN_TYPE_int lrf_read() { ...@@ -50,16 +65,20 @@ GEN_TYPE_int lrf_read() {
//receive values //receive values
while (1) { while (1) {
n_read = uart_receive(bytes, 6, 0); n_read = uart_receive(bytes, sizeof(bytes) / sizeof(bytes[0]), 0);
if(n_read <= 0) if(n_read < 0) {
timeout++;
return -1; return -1;
}
for (i = 0; i < n_read; i++) { for (i = 0; i < n_read; i++) {
if (bytes[i] != 0xd && bytes[i] != 0xa) { if (bytes[i] != 0xd && bytes[i] != 0xa) {
value[n++] = bytes[i]; value[n++] = bytes[i];
} else if (atoi(value) != 0) { } else if (atoi(value) != 0) {
d = atoi(value); d = atoi(value);
printf("%d\n",d);
timeout = 0;
return d; return d;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment