2 * Copyright Droids Corporation (2010)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Olivier MATZ <zer0@droids-corp.org>
28 #include <vect_base.h>
32 #define POS_ACCURACY 10.0 /* 1 cm accuracy max */
34 #define printf(args...) do {} while(0)
37 static int dprint = 0;
38 #define dprintf(args...) if (dprint) printf(args)
40 const point_t beacon0 = { 0, 1050 };
41 const point_t beacon1 = { 3000, 0 };
42 const point_t beacon2 = { 3000, 2100 };
44 /* Fill the 2 circles pointer given as parameter, each of those cross
45 * both beacons b1 and b2. From any point of these circles (except b1
46 * and b2), we see b1 and b2 with the angle of a_rad (which must be
47 * positive). Return 0 on success.
50 * <------------------------->
53 * +----------------------------+
56 * / \ \ a___ | | d / | `.
71 * '`--......---' R (the robot)
74 int8_t angle_to_circles(circle_t *c1, circle_t *c2,
75 const point_t *b1, const point_t *b2,
82 /* reject negative or too small angles */
86 /* get position of O */
87 O.x = (b1->x + b2->x) / 2;
88 O.y = (b1->y + b2->y) / 2;
90 /* get the length l */
95 /* distance from O to the center of the circle */
96 /* XXX div by 0 when pi */
97 d = l / (2 * tan(a_rad));
99 /* get the circle c1 */
105 c1->r = xy_norm(b1->x, b1->y, c1->x, c1->y);
108 /* get the circle c2 */
112 c2->r = xy_norm(b1->x, b1->y, c1->x, c1->y);
118 /* get the position of the robot from the angle of the 3 beacons */
119 int8_t angles_to_posxy(point_t *pos, double a01, double a12, double a20)
121 circle_t c01, c12, c20;
122 point_t dummy_pt, p1, p2, p3;
124 dprintf("a01 = %2.2f\n", a01);
125 dprintf("a12 = %2.2f\n", a12);
126 dprintf("a20 = %2.2f\n", a20);
128 if (angle_to_circles(&c01, NULL, &beacon0, &beacon1, a01))
130 dprintf("circle: x=%2.2f y=%2.2f r=%2.2f\n", c01.x, c01.y, c01.r);
132 if (angle_to_circles(&c12, NULL, &beacon1, &beacon2, a12))
134 dprintf("circle: x=%2.2f y=%2.2f r=%2.2f\n", c12.x, c12.y, c12.r);
136 if (angle_to_circles(&c20, NULL, &beacon2, &beacon0, a20))
138 dprintf("circle: x=%2.2f y=%2.2f r=%2.2f\n", c20.x, c20.y, c20.r);
140 if (circle_intersect(&c01, &c12, &p1, &dummy_pt) == 0)
142 if (circle_intersect(&c12, &c20, &p2, &dummy_pt) == 0)
144 if (circle_intersect(&c20, &c01, &dummy_pt, &p3) == 0)
147 dprintf("p1: x=%2.2f y=%2.2f\n", p1.x, p1.y);
148 dprintf("p2: x=%2.2f y=%2.2f\n", p2.x, p2.y);
149 dprintf("p3: x=%2.2f y=%2.2f\n", p3.x, p3.y);
151 /* if (xy_norm(p1.x, p1.y, p2.x, p2.y) > POS_ACCURACY || */
152 /* xy_norm(p2.x, p2.y, p3.x, p3.y) > POS_ACCURACY || */
153 /* xy_norm(p3.x, p3.y, p1.x, p1.y) > POS_ACCURACY) */
156 pos->x = (p1.x + p2.x + p3.x) / 3.0;
157 pos->y = (p1.y + p2.y + p3.y) / 3.0;
162 /* get the angles of beacons from xy pos */
163 int8_t posxy_to_angles(point_t pos, double *a01, double *a12,
164 double *a20, int err_num, float err_val)
168 a0 = atan2(beacon0.y-pos.y, beacon0.x-pos.x);
169 a1 = atan2(beacon1.y-pos.y, beacon1.x-pos.x);
170 a2 = atan2(beacon2.y-pos.y, beacon2.x-pos.x);
172 if (err_num == 0 || err_num == 3)
173 a0 += (err_val * M_PI/180.);
174 if (err_num == 1 || err_num == 3)
175 a1 += (err_val * M_PI/180.);
176 if (err_num == 2 || err_num == 3)
177 a2 += (err_val * M_PI/180.);
192 int8_t process_move_error(double x, double y, double speed,
193 double period, double angle, double *err)
195 double a01, a12, a20;
204 /* from start to destination */
205 v.x = cos(angle) * speed * period;
206 v.y = sin(angle) * speed * period;
208 /* first process real pos */
209 posxy_to_angles(pos, &a01, &a12, &a20, -1, 0);
211 /* vector covered during measure of a0 and a1 */
212 u.x = v.x * a01 / (2*M_PI);
213 u.y = v.y * a01 / (2*M_PI);
214 pos2.x = pos.x + u.x;
215 pos2.y = pos.y + u.y;
217 /* vector covered during measure of a1 and a2 */
218 u.x = v.x * a12 / (2*M_PI);
219 u.y = v.y * a12 / (2*M_PI);
220 pos3.x = pos2.x + u.x;
221 pos3.y = pos2.y + u.y;
223 dprintf("p0: x=%2.2f y=%2.2f\n", pos.x, pos.y);
224 dprintf("p1: x=%2.2f y=%2.2f\n", pos2.x, pos2.y);
225 dprintf("p2: x=%2.2f y=%2.2f\n", pos3.x, pos3.y);
227 a0 = atan2(beacon0.y-pos.y, beacon0.x-pos.x);
228 a1 = atan2(beacon1.y-pos2.y, beacon1.x-pos2.x);
229 a2 = atan2(beacon2.y-pos3.y, beacon2.x-pos3.x);
241 if (angles_to_posxy(&tmp, a01, a12, a20))
243 *err = pt_norm(&tmp, &pos);
244 if (*err > 50.) /* saturate error to 5cm */