Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (1.62 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
/** move will move a robot from its initial position, (x,y), and theta (in radians) to a new position given speed.
14
 * (x,y) and theta will be updated by the move function instead of returning a value
15
 * (x,y) is some kind of absolute position in the "world", let's make (0,0) the top left of the "world"
16
 * theta will an angle be between 0 - 2*Pi (0 being faces east)
17
 * speed is between 0 - 255, there is some magical cutoff point before the motors actually starts running
18
 * move will return 0 if successful
19
 **/
20
int move_robot(Robot* r)
21
{
22
        short speed1 = r->shared->motor1;
23
        short speed2 = r->shared->motor2;
24
        float theta = r->pose.theta;
25

    
26
        if (theta < 0 || theta > 2*M_PI) return 1;
27
        if (speed1 < 0 || speed1 > 255) return 1;
28
        if (speed2 < 0 || speed2 > 255) return 1;
29

    
30
        /* if speed is lower than the cut off, don't move */
31
        if (ABS(speed1) < CUTOFF) {
32
                speed1 = 0;
33
        }
34
        if (ABS(speed2) < CUTOFF) {
35
                speed2 = 0;
36
        }
37
        double radius;
38
        if (speed1 == speed2) {
39
                /* go straight */
40
        r->pose.x += cos(theta) * speed1;
41
        r->pose.y += sin(theta) * speed1;
42
                return 0;
43
        }
44
        radius = ROBOT_WIDTH * speed1 / (speed1 - speed2);
45
        
46
        double t = speed1 / radius;
47

    
48
        double newx = radius * sin(t);
49
        double newy = radius - radius * cos(t);
50

    
51
        r->pose.x += newx * cos(theta);
52
        r->pose.y += newx * sin(theta);
53

    
54
        r->pose.x += newy * - sin(theta);
55
        r->pose.y += newy * cos(theta);
56

    
57
        r->pose.theta = fmod((t + theta), (2 * M_PI));
58
        if (r->pose.theta<0) r->pose.theta += 2 * M_PI;
59

    
60
        return 0;
61
}
62