X-Git-Url: http://git.droids-corp.org/?p=aversive.git;a=blobdiff_plain;f=projects%2Fmicrob2010%2Ftests%2Fhostsim%2Frobotsim.c;h=85224a39166bb93132215403927093f5a00d55da;hp=6f4dbbfa405615c789439614e20a73b9d31e5a36;hb=a16ba178c7ef15fb408593b0e7c666ed2ed1d253;hpb=1f8274e9bca0d2433f101f267b86bc70dc2ae8c4 diff --git a/projects/microb2010/tests/hostsim/robotsim.c b/projects/microb2010/tests/hostsim/robotsim.c index 6f4dbbf..85224a3 100644 --- a/projects/microb2010/tests/hostsim/robotsim.c +++ b/projects/microb2010/tests/hostsim/robotsim.c @@ -22,6 +22,11 @@ #include #include #include +#include +#include +#include +#include +#include #include #include @@ -41,27 +46,81 @@ #include #include +#include "strat.h" +#include "strat_utils.h" #include "main.h" static int32_t l_pwm, r_pwm; static int32_t l_enc, r_enc; +static int fd; + /* */ -#define FILTER 98 +#define FILTER 97 #define FILTER2 (100-FILTER) /* must be called periodically */ void robotsim_update(void) { static int32_t l_speed, r_speed; + double x, y, a, a2, d; + + /* corners of the robot */ + double xfl, yfl; /* front left */ + double xrl, yrl; /* rear left */ + double xrr, yrr; /* rear right */ + double xfr, yfr; /* front right */ + + x = position_get_x_double(&mainboard.pos); + y = position_get_y_double(&mainboard.pos); + a = position_get_a_rad_double(&mainboard.pos); - /* XXX should lock */ l_speed = ((l_speed * FILTER) / 100) + ((l_pwm * 1000 * FILTER2)/1000); - l_enc += (l_speed / 1000); r_speed = ((r_speed * FILTER) / 100) + ((r_pwm * 1000 * FILTER2)/1000); + + /* basic collision detection */ + a2 = atan2(ROBOT_WIDTH/2, ROBOT_LENGTH/2); + d = norm(ROBOT_WIDTH/2, ROBOT_LENGTH/2); + + xfl = x + cos(a+a2) * d; + yfl = y + sin(a+a2) * d; + if (!is_in_area(xfl, yfl, 0) && l_speed > 0) + l_speed = 0; + + xrl = x + cos(a+M_PI-a2) * d; + yrl = y + sin(a+M_PI-a2) * d; + if (!is_in_area(xrl, yrl, 0) && l_speed < 0) + l_speed = 0; + + xrr = x + cos(a+M_PI+a2) * d; + yrr = y + sin(a+M_PI+a2) * d; + if (!is_in_area(xrr, yrr, 0) && r_speed < 0) + r_speed = 0; + + xfr = x + cos(a-a2) * d; + yfr = y + sin(a-a2) * d; + if (!is_in_area(xfr, yfr, 0) && r_speed > 0) + r_speed = 0; + + /* XXX should lock */ + l_enc += (l_speed / 1000); r_enc += (r_speed / 1000); } +void robotsim_dump(void) +{ + char buf[BUFSIZ]; + int len; + + len = snprintf(buf, sizeof(buf), "%d %d %d\n", + position_get_x_s16(&mainboard.pos), + position_get_y_s16(&mainboard.pos), + position_get_a_deg_s16(&mainboard.pos)); + hostsim_lock(); + write(fd, buf, len); + hostsim_unlock(); +} + void robotsim_pwm(void *arg, int32_t val) { // printf("%p, %d\n", arg, val); @@ -79,3 +138,12 @@ int32_t robotsim_encoder_get(void *arg) return r_enc; return 0; } + +int robotsim_init(void) +{ + mkfifo("/tmp/.robot", 0600); + fd = open("/tmp/.robot", O_WRONLY, 0); + if (fd < 0) + return -1; + return 0; +}