Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / mapping / robot / robot_main.c @ 965

History | View | Annotate | Download (3.92 KB)

1
#include <dragonfly_lib.h>
2
#include <wireless.h>
3
#include <encoders.h>
4
#include "odometry.h"
5

    
6
#define POLLING_INTERVAL 50    //interval in ms to get sensor data
7
#define MAP 1        // packet group for sending data points
8
#define POINT_ODO 2
9

    
10
#define WL_CNTL_GROUP 4
11

    
12
#define CNTL_FORWARD 0
13
#define CNTL_BACK    1
14
#define CNTL_LEFT    2
15
#define CNTL_RIGHT   3
16
#define CNTL_LEFT_CURVE 4
17
#define CNTL_RIGHT_CURVE 5
18
#define CNTL_STOP    6
19
#define CNTL_VEL_UP  7
20
#define CNTL_VEL_DOWN 8
21

    
22
int state;
23
int velocity;
24
char buf[100];
25

    
26
void packet_receive (char type, int source, unsigned char* packet, int length);
27
void set_motion (void);
28

    
29
PacketGroupHandler packetHandler = {WL_CNTL_GROUP, NULL, NULL, &packet_receive, NULL};
30

    
31
int main(void)
32
{
33
    /* initialize components and join the token ring */
34
    dragonfly_init(ALL_ON);
35
    odometry_init();
36
    orb_enable();
37
    wl_init();
38
    wl_set_channel(0xE);
39
    wl_register_packet_group(&packetHandler);
40
    
41
    velocity = 190;
42
    state = CNTL_STOP;
43

    
44
    int x, y; // use when sending odometry data
45
    double theta;
46

    
47
    int store[9];
48
    char *send;   // for transmission
49

    
50
    while(1)
51
    {
52
        wl_do();
53
        delay_ms(POLLING_INTERVAL);        /* until it's time to grab map data */
54
        x = odometry_dx(); // use when sending odometry data
55
        y = odometry_dy();
56
        theta = odometry_angle();
57
        store[0] = x;
58
        store[1] = y;
59
        *((double*)(store + 2)) = theta;
60
        store[4] = range_read_distance(IR1);        // IR1 range
61
        store[5] = range_read_distance(IR2);        // IR2 range
62
        store[6] = range_read_distance(IR3);        // IR3 range
63
        store[7] = range_read_distance(IR4);        // IR4 range
64
        store[8] = range_read_distance(IR5);        // IR5 range
65
        sprintf( buf, "%d %d %f %d %d %d %d %d\n", store[0], store[1],
66
            *((double *)(store+2)), store[4], store[5], store[6],
67
            store[7], store[8] );
68
        usb_puts(buf);
69

    
70
        send = (char*) store;
71

    
72
        wl_send_global_packet(MAP, POINT_ODO, send, 18, 0);
73
    }
74

    
75
    wl_terminate();
76
    return 0;
77
}
78

    
79
void packet_receive (char type, int source, unsigned char* packet, int length) {
80
    sprintf(buf, "received packet: %d\n", type);
81
    usb_puts(buf);
82
    switch (type) {
83
        case CNTL_FORWARD:
84
            state = CNTL_FORWARD;
85
            break;
86
        case CNTL_BACK:
87
            state = CNTL_BACK;
88
            break;
89
        case CNTL_LEFT:
90
            state = CNTL_LEFT;
91
            break;
92
        case CNTL_RIGHT:
93
            state = CNTL_RIGHT;
94
            break;
95
        case CNTL_LEFT_CURVE:
96
            state = CNTL_LEFT_CURVE;
97
            break;
98
        case CNTL_RIGHT_CURVE:
99
            state = CNTL_RIGHT_CURVE;
100
            break;
101
        case CNTL_STOP:
102
            state = CNTL_STOP;
103
            break;
104
        case CNTL_VEL_UP:
105
            if (velocity > 250)
106
                velocity = 250;
107
            else
108
                velocity += 5;
109
            break;
110
        case CNTL_VEL_DOWN:
111
            velocity -= 5;
112
            if (velocity < 150)
113
                velocity = 150;
114
            break;
115
        default:
116
            state = CNTL_STOP;
117
            break;
118
    }
119
    set_motion();
120
}
121

    
122
void set_motion (void) {
123
    switch (state) {
124
        case CNTL_FORWARD:
125
            orb_set_color(GREEN);
126
            move(velocity, 0);
127
            break;
128
        case CNTL_BACK:
129
            orb_set_color(BLUE);
130
            move(-velocity, 0);
131
            break;
132
        case CNTL_LEFT:
133
            orb_set_color(ORANGE);
134
            motor1_set(0, velocity);
135
            motor2_set(1, velocity);
136
            break;
137
        case CNTL_RIGHT:
138
            orb_set_color(YELLOW);
139
            motor1_set(1, velocity);
140
            motor2_set(0, velocity);
141
            break;
142
        case CNTL_LEFT_CURVE:
143
            orb_set_color(MAGENTA);
144
            motor1_set(1, 150);
145
            motor2_set(1, velocity);
146
            break;
147
        case CNTL_RIGHT_CURVE:
148
            orb_set_color(CYAN);
149
            motor1_set(1, velocity);
150
            motor2_set(1, 150);
151
            break;
152
        case CNTL_STOP:
153
            orb_set_color(RED);
154
            move(0, 0);
155
            break;
156
    }
157
}
158