Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / diagnostic_station / station / comm_robot.c @ 1220

History | View | Annotate | Download (6.33 KB)

1
#include <dragonfly_lib.h>
2
#include <wireless.h>
3
#include <string.h>
4

    
5
#include "global.h"
6
#include "comm_robot.h"
7
#include "../common/comm_station_robot.h"
8

    
9
//char new_data = 0;
10
//char finished = 0;
11
//char data_received[10];
12

    
13
uint8_t received_buffer[10]; // Maximum length of a packet to be handled
14
uint8_t received_length;
15
bool received_data;
16

    
17

    
18

    
19
// *********************
20
// ** Motor direction **
21
// *********************
22

    
23
/**
24
  * All the functions in this file use these motor settings: a pair (direction/velocity) maps to a pair motor_direction/
25
  * motor_velocity. motor_direction and motor_velocity are the values passed to the library functions and also sent
26
  * to the robot. direction also includes an "off" direction. If the direction is off, the motor_velocity is always 0.\
27
  **/
28

    
29
char *motor_direction_string (uint8_t direction)
30
{
31
        if (direction==motor_direction_forward)
32
                return "forward";
33
        else if (direction==motor_direction_backward)
34
                return "backward";
35
        else if (direction==motor_direction_off)
36
                return "off";
37
        else
38
                return "?";
39
}
40

    
41
static uint8_t motor_direction (uint8_t direction, uint8_t velocity)
42
{
43
        if (direction==motor_direction_backward)
44
                return BACKWARD;
45
        else
46
                return FORWARD;
47
}
48

    
49
static uint8_t motor_velocity (uint8_t direction, uint8_t velocity)
50
{
51
        if (direction==motor_direction_off)
52
                return 0;
53
        else
54
                return velocity;
55
}
56

    
57

    
58
// ***************************
59
// ** Common comm functions        **
60
// ***************************
61

    
62
// A packet that is followed by a "done" packet from the robot.
63
static void send_command_packet (char type, char *data, int len)
64
{
65
        wl_send_global_packet (station_robot_group, type, data, len, 0);
66
        
67
        // TODO: is it guaranteed that the packet will be sent on the next call to wl_do, or
68
        // do we have to call wl_do in a loop?
69

    
70
        // TODO: wait for "done" from robot.
71

    
72
        wl_do ();
73
}
74

    
75
// A packet that is followed by a data packet from the robot
76
static void send_request_packet(char type, char *data, int len)
77
{
78
        received_data=false;
79
        
80
        // Send the request packet
81
        wl_send_global_packet (station_robot_group, type, data, len, 0);
82
        
83
        // When the reply is received, the receive handler will set received_data to the buffer containing the data. Wait
84
        // until this happens, periodically calling the wl_do function to handle incoming messages.
85
        
86
        while(!received_data)
87
        {
88
                wl_do();
89
        }
90
}
91

    
92

    
93
// *************************
94
// ** Individual commands **
95
// *************************
96

    
97
void robot_set_orbs (uint8_t red1, uint8_t green1, uint8_t blue1, uint8_t red2, uint8_t green2, uint8_t blue2)
98
{
99
        char data[6];
100

    
101
        data[0]=red1;
102
        data[1]=green1;
103
        data[2]=blue1;
104
        
105
        data[3]=red2;
106
        data[4]=green2;
107
        data[5]=blue2;
108

    
109
        send_command_packet (station_robot_set_orbs, data, 6);
110
}
111

    
112
/** Direction is motor_direction_off/forward/backward, not FORWARD/BACKWARD */
113
void robot_set_motors (uint8_t direction1, uint8_t speed1, uint8_t direction2, uint8_t speed2)
114
{
115
        char data[4];
116
        
117
        data[0]=motor_direction (direction1, speed1);
118
        data[1]=motor_velocity  (direction1, speed1);
119
        data[2]=motor_direction (direction2, speed2);
120
        data[3]=motor_velocity  (direction2, speed2);
121
        
122
        send_command_packet (station_robot_set_motors, data, 4);
123
}
124

    
125
/** Direction is direction_off/forward/backward, not FORWARD/BACKWARD */
126
void robot_set_motors_time (uint8_t direction1, uint8_t speed1, uint8_t direction2, uint8_t speed2, uint16_t time_ms)
127
{
128
        char data[6];
129
        
130
        data[0]=motor_direction (direction1, speed1);
131
        data[1]=motor_velocity  (direction1, speed1);
132
        data[2]=motor_direction (direction2, speed1);
133
        data[3]=motor_velocity  (direction2, speed2);
134
        data[4]=WORD_BYTE_0(time_ms);
135
        data[5]=WORD_BYTE_1(time_ms);
136
        
137
        send_command_packet (station_robot_set_motors, data, 6);
138
}
139

    
140
void robot_set_motors_off (void)
141
{
142
        send_command_packet (station_robot_set_motors_off, NULL, 0);
143
}
144

    
145
void robot_set_bom (uint16_t bitmask)
146
{
147
        char data[2];
148
        
149
        data[0]=WORD_BYTE_0(bitmask);
150
        data[1]=WORD_BYTE_1(bitmask);
151
        
152
        send_command_packet (station_robot_set_bom, data, 2);
153
}
154

    
155
void robot_reset_encoders (void)
156
{
157
        send_command_packet (station_robot_reset_encoders, NULL, 0);
158
}
159

    
160

    
161
// *************************
162
// ** Individual requests **
163
// *************************
164

    
165
bool robot_read_encoders (int16_t *left, int16_t *right)
166
{
167
        send_request_packet (station_robot_read_encoders, NULL, 0);
168
        
169
        // Now the received data is in received_data/received_length
170
        if (received_length>=4)
171
        {
172
                *left =WORD(received_buffer[0], received_buffer[1]);
173
                *right=WORD(received_buffer[2], received_buffer[3]);
174
                return true;
175
        }
176
        else
177
        {
178
                return false;
179
        }
180
}
181

    
182
uint16_t robot_read_bom (uint8_t num)
183
{
184
        // FIXME implement
185
        return 0;
186
}
187

    
188
uint16_t robot_read_rangefinder (uint8_t num)
189
{
190
        // FIXME implement
191
        return 0;
192
}
193

    
194

    
195
// ***********************
196
// ** Receive callbacks **
197
// ***********************
198

    
199
static void robot_station_receive (char type, int source, unsigned char* packet, int length)
200
{
201
        // Dump the raw packet
202
        //usb_puts ("# [");
203
        //for (uint8_t i=0; i<length; ++i)
204
        //{
205
        //        if (i!=0) usb_putc (' ');
206
        //        usb_puti (packet[i]);
207
        //}
208
        //usb_putc (']');
209
        
210
        // We received some data from the robot. It is not handled immediately as some function is waiting for it.
211
        
212
        // Note the fact that we received some data
213
        received_data=true;
214
        
215
        // The packet buffer might be overwritten after this function returns, so we have to make a copy of the data.
216
        
217
        // First, determine the number of bytes to copy. It is the minimum of the received length and the size of the data
218
        // buffer the data is copied to.
219
        received_length=length;
220
        if (sizeof (received_buffer)<received_length) received_length=sizeof (received_buffer);
221
        
222
        // Now copy the data
223
        memcpy (received_buffer, packet, received_length);
224
}
225

    
226

    
227
// ********************
228
// ** Initialization **
229
// ********************
230

    
231
// Must not be a local variable because the library doesn't make a copy of it.
232
PacketGroupHandler receive_handler={robot_station_group, NULL, NULL, &robot_station_receive, NULL};
233

    
234
void comm_robot_init ()
235
{
236
        received_data=NULL;
237
        received_length=0;
238

    
239
        usb_puts("# Initializing robot communication" NL);
240
        xbee_init ();
241
        wl_init();
242
        usb_puts("# Done" NL);
243
        
244
        //PacketGroupHandler xxx_receive_handler = {robot_station_group, NULL, NULL, &robot_station_receive, NULL};
245
        wl_register_packet_group(&receive_handler);
246
}