root / branches / simulator / projects / simulator / simulator / core / motion.c @ 1020
History | View | Annotate | Download (1.89 KB)
1 |
#include <stdio.h> |
---|---|
2 |
#include <math.h> |
3 |
|
4 |
#include "motion.h" |
5 |
|
6 |
#include "robot.h" |
7 |
|
8 |
#define CUTOFF 120 |
9 |
#define ABS(x) (x<0?-x:x) |
10 |
#define TIME 1 /*sec*/ |
11 |
#define ROBOT_WIDTH 131 /*mm*/ |
12 |
|
13 |
#define MOTOR_CONVERSION_FACTOR 10.0 |
14 |
|
15 |
/** move will move a robot from its initial position, (x,y), and theta (in radians) to a new position given speed.
|
16 |
* (x,y) and theta will be updated by the move function instead of returning a value
|
17 |
* (x,y) is some kind of absolute position in the "world", let's make (0,0) the top left of the "world"
|
18 |
* theta will an angle be between 0 - 2*Pi (0 being faces east)
|
19 |
* speed is between 0 - 255, there is some magical cutoff point before the motors actually starts running
|
20 |
* move will return 0 if successful
|
21 |
**/
|
22 |
int move_robot(Robot* r)
|
23 |
{ |
24 |
short speed1 = r->shared->motor1;
|
25 |
short speed2 = r->shared->motor2;
|
26 |
float theta = r->pose.theta;
|
27 |
|
28 |
if (theta < 0 || theta > 2*M_PI) return 1; |
29 |
if (speed1 < 0 || speed1 > 255) return 1; |
30 |
if (speed2 < 0 || speed2 > 255) return 1; |
31 |
|
32 |
/* if speed is lower than the cut off, don't move */
|
33 |
if (ABS(speed1) < CUTOFF) {
|
34 |
speed1 = 0;
|
35 |
} |
36 |
if (ABS(speed2) < CUTOFF) {
|
37 |
speed2 = 0;
|
38 |
} |
39 |
speed1 = (speed1 - CUTOFF); |
40 |
speed2 = (speed2 - CUTOFF); |
41 |
double radius;
|
42 |
if (speed1 == speed2) {
|
43 |
/* go straight */
|
44 |
r->pose.x += cos(theta) * speed1 / MOTOR_CONVERSION_FACTOR; |
45 |
r->pose.y += sin(theta) * speed1 / MOTOR_CONVERSION_FACTOR; |
46 |
return 0; |
47 |
} |
48 |
radius = ROBOT_WIDTH * speed1 / (speed1 - speed2); |
49 |
|
50 |
double t = speed1 / radius / MOTOR_CONVERSION_FACTOR;
|
51 |
|
52 |
double newx = (radius * sin(t)) / MOTOR_CONVERSION_FACTOR;
|
53 |
double newy = (radius - radius * cos(t)) / MOTOR_CONVERSION_FACTOR;
|
54 |
|
55 |
r->pose.x += newx * cos(theta); |
56 |
r->pose.y += newx * sin(theta); |
57 |
|
58 |
r->pose.x += newy * - sin(theta); |
59 |
r->pose.y += newy * cos(theta); |
60 |
|
61 |
int divide = (t+r->pose.theta)/(2 * M_PI); |
62 |
r->pose.theta = (t+r->pose.theta) - (2 * M_PI * divide);
|
63 |
if (r->pose.theta<0) r->pose.theta += 2 * M_PI; |
64 |
|
65 |
return 0; |
66 |
} |
67 |
|