X-Git-Url: http://git.droids-corp.org/?p=aversive.git;a=blobdiff_plain;f=projects%2Fmicrob2010%2Ftests%2Fhostsim%2Frobotsim.c;fp=projects%2Fmicrob2010%2Ftests%2Fhostsim%2Frobotsim.c;h=990104b07ea68425113f012edb5522783d69b1f9;hp=7ed49f48a8cb7d5507dd03878885b5c9fd4c51bf;hb=87ccd3af8abb0da3e0fa98dc8e9216fc7b676f97;hpb=145b19e73ce5751b365bc53769189addeecee81b diff --git a/projects/microb2010/tests/hostsim/robotsim.c b/projects/microb2010/tests/hostsim/robotsim.c index 7ed49f4..990104b 100644 --- a/projects/microb2010/tests/hostsim/robotsim.c +++ b/projects/microb2010/tests/hostsim/robotsim.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -44,6 +45,8 @@ #include #include +#include "strat.h" +#include "strat_utils.h" #include "main.h" static int32_t l_pwm, r_pwm; @@ -59,11 +62,47 @@ static int fd; 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); }