Statistics
| Revision:

root / branches / simulator / projects / simulator / simulator / core / motion.c @ 1091

History | View | Annotate | Download (2.4 KB)

1 1006 bcoltin
#include <stdio.h>
2 1027 chihsiuh
#include <stdlib.h>
3 1006 bcoltin
#include <math.h>
4 1006 bcoltin
5 1006 bcoltin
#include "motion.h"
6 1091 bpoole
#include "rangefinders.h"
7 1010 bcoltin
#include "robot.h"
8 1010 bcoltin
9 1027 chihsiuh
#define CUTOFF 120
10 1027 chihsiuh
#define TIME 1 /*sec*/
11 1006 bcoltin
#define ROBOT_WIDTH 131 /*mm*/
12 1006 bcoltin
13 1020 bcoltin
#define MOTOR_CONVERSION_FACTOR 10.0
14 1020 bcoltin
15 1090 bpoole
#define FUDGE 10 /* minimum rangefinder distance until collision */
16 1090 bpoole
17 1027 chihsiuh
/** move_robot will move a robot from its initial position, (x,y), and theta (in radians) to a new position given speed.
18 1027 chihsiuh
 * (x,y) and theta will be updated by the move_robot function instead of returning a value
19 1027 chihsiuh
 * (x,y) is some kind of absolute position in the "world", (0,0) is the top left of the "world"
20 1027 chihsiuh
 * theta will an angle be between 0 and 2*Pi (0 being faces east and goes clockwise)
21 1027 chihsiuh
 * speed is between 0 and 255, there is some magical cutoff point before the motors actually starts running
22 1006 bcoltin
 * move will return 0 if successful
23 1006 bcoltin
 **/
24 1010 bcoltin
int move_robot(Robot* r)
25 1010 bcoltin
{
26 1090 bpoole
        Pose old_pose = r->pose;
27 1090 bpoole
28 1010 bcoltin
        short speed1 = r->shared->motor1;
29 1010 bcoltin
        short speed2 = r->shared->motor2;
30 1010 bcoltin
        float theta = r->pose.theta;
31 1075 bjy
32 1010 bcoltin
        if (theta < 0 || theta > 2*M_PI) return 1;
33 1075 bjy
        if (speed1 < -255 || speed1 > 255) return 1;
34 1075 bjy
        if (speed2 < -255 || speed2 > 255) return 1;
35 1006 bcoltin
36 1010 bcoltin
        /* if speed is lower than the cut off, don't move */
37 1027 chihsiuh
        if (abs(speed1) < CUTOFF) {
38 1010 bcoltin
                speed1 = 0;
39 1010 bcoltin
        }
40 1027 chihsiuh
        if (abs(speed2) < CUTOFF) {
41 1010 bcoltin
                speed2 = 0;
42 1010 bcoltin
        }
43 1046 chihsiuh
44 1010 bcoltin
        double radius;
45 1010 bcoltin
        if (speed1 == speed2) {
46 1027 chihsiuh
          /* go straight */
47 1027 chihsiuh
          r->pose.x += cos(theta) * speed1 / MOTOR_CONVERSION_FACTOR;
48 1027 chihsiuh
          r->pose.y += sin(theta) * speed1 / MOTOR_CONVERSION_FACTOR;
49 1027 chihsiuh
          return 0;
50 1010 bcoltin
        }
51 1010 bcoltin
        radius = ROBOT_WIDTH * speed1 / (speed1 - speed2);
52 1010 bcoltin
53 1020 bcoltin
        double t = speed1 / radius / MOTOR_CONVERSION_FACTOR;
54 1006 bcoltin
55 1020 bcoltin
        double newx = (radius * sin(t)) / MOTOR_CONVERSION_FACTOR;
56 1020 bcoltin
        double newy = (radius - radius * cos(t)) / MOTOR_CONVERSION_FACTOR;
57 1019 bcoltin
58 1010 bcoltin
        r->pose.x += newx * cos(theta);
59 1010 bcoltin
        r->pose.y += newx * sin(theta);
60 1006 bcoltin
61 1010 bcoltin
        r->pose.x += newy * - sin(theta);
62 1010 bcoltin
        r->pose.y += newy * cos(theta);
63 1019 bcoltin
64 1019 bcoltin
        int divide = (t+r->pose.theta)/(2 * M_PI);
65 1019 bcoltin
        r->pose.theta = (t+r->pose.theta) - (2 * M_PI * divide);
66 1010 bcoltin
        if (r->pose.theta<0) r->pose.theta += 2 * M_PI;
67 1010 bcoltin
68 1090 bpoole
        /* XXX: this is a terrible hack  */
69 1090 bpoole
        update_rangefinders(r);
70 1090 bpoole
        for (divide = 0; divide < 5; divide++) {
71 1091 bpoole
            /* Lets just call this a collision... */
72 1090 bpoole
            if (r->shared->ranges.d[divide] < FUDGE) {
73 1091 bpoole
                /* Restore x,y, but allow rotation */
74 1091 bpoole
                r->pose.x = old_pose.x;
75 1091 bpoole
                r->pose.y = old_pose.y;
76 1091 bpoole
77 1091 bpoole
                /* Rotated robot, need to recalculate */
78 1091 bpoole
                update_rangefinders(r);
79 1090 bpoole
                return 0;
80 1090 bpoole
            }
81 1090 bpoole
        }
82 1090 bpoole
83 1010 bcoltin
        return 0;
84 1006 bcoltin
}