root / trunk / code / projects / diagnostic_station / station / comm_robot.c @ 1205
History | View | Annotate | Download (3.63 KB)
1 |
#include <dragonfly_lib.h> |
---|---|
2 |
#include <wireless.h> |
3 |
|
4 |
#include "comm_robot.h" |
5 |
#include "../common/comm_station_robot.h" |
6 |
|
7 |
char new_data = 0; |
8 |
char finished = 0; |
9 |
char data_received[10]; |
10 |
|
11 |
char *motor_direction_string (uint8_t direction)
|
12 |
{ |
13 |
if (direction==motor_direction_forward)
|
14 |
return "forward"; |
15 |
else if (direction==motor_direction_backward) |
16 |
return "backward"; |
17 |
else if (direction==motor_direction_off) |
18 |
return "off"; |
19 |
else
|
20 |
return "?"; |
21 |
} |
22 |
|
23 |
|
24 |
static uint8_t motor_direction (uint8_t direction)
|
25 |
{ |
26 |
if (direction==motor_direction_backward)
|
27 |
return BACKWARD;
|
28 |
else
|
29 |
return FORWARD;
|
30 |
} |
31 |
|
32 |
static uint8_t motor_velocity (uint8_t direction, uint8_t velocity)
|
33 |
{ |
34 |
if (direction==motor_direction_off)
|
35 |
return 0; |
36 |
else
|
37 |
return velocity;
|
38 |
} |
39 |
|
40 |
|
41 |
// A packet that is followed by a "done" packet from the robot.
|
42 |
static void send_command_packet (char type, char *data, int len) |
43 |
{ |
44 |
wl_send_global_packet (station_robot_group, type, data, len, 0);
|
45 |
|
46 |
// TODO: is it guaranteed that the package will be sent on the next call to wl_do, or
|
47 |
// do we have to call wl_do in a loop?
|
48 |
|
49 |
// TODO: wait for "done" from robot.
|
50 |
|
51 |
wl_do (); |
52 |
} |
53 |
|
54 |
// A packet that is followed by a data packet from the robot
|
55 |
static void send_request_packet(char type, char *data, int len) |
56 |
{ |
57 |
wl_send_global_packet (station_robot_group, type, data, len, 0);
|
58 |
while(!new_data){
|
59 |
wl_do(); |
60 |
} |
61 |
new_data = 0;
|
62 |
} |
63 |
|
64 |
|
65 |
|
66 |
void robot_set_orbs (uint8_t red1, uint8_t green1, uint8_t blue1, uint8_t red2, uint8_t green2, uint8_t blue2)
|
67 |
{ |
68 |
char data[6]; |
69 |
|
70 |
data[0]=red1;
|
71 |
data[1]=green1;
|
72 |
data[2]=blue1;
|
73 |
|
74 |
data[3]=red2;
|
75 |
data[4]=green2;
|
76 |
data[5]=blue2;
|
77 |
|
78 |
send_command_packet (station_robot_set_orbs, data, 6);
|
79 |
} |
80 |
|
81 |
/** Direction is motor_direction_off/forward/backward, not FORWARD/BACKWARD */
|
82 |
void robot_set_motors (uint8_t direction1, uint8_t speed1, uint8_t direction2, uint8_t speed2)
|
83 |
{ |
84 |
char data[4]; |
85 |
|
86 |
data[0]=motor_direction (direction1);
|
87 |
data[1]=motor_velocity (direction1, speed1);
|
88 |
data[2]=motor_direction (direction2);
|
89 |
data[3]=motor_velocity (direction2, speed2);
|
90 |
|
91 |
send_command_packet (station_robot_set_motors, data, 4);
|
92 |
} |
93 |
|
94 |
/** Direction is direction_off/forward/backward, not FORWARD/BACKWARD */
|
95 |
void robot_set_motors_time (uint8_t direction1, uint8_t speed1, uint8_t direction2, uint8_t speed2, uint16_t time_ms)
|
96 |
{ |
97 |
char data[6]; |
98 |
|
99 |
data[0]=motor_direction (direction1);
|
100 |
data[1]=motor_velocity (direction1, speed1);
|
101 |
data[2]=motor_direction (direction2);
|
102 |
data[3]=motor_velocity (direction2, speed2);
|
103 |
data[4]=WORD_BYTE_0(time_ms);
|
104 |
data[5]=WORD_BYTE_1(time_ms);
|
105 |
|
106 |
send_command_packet (station_robot_set_motors, data, 6);
|
107 |
} |
108 |
|
109 |
void robot_set_motors_off (void) |
110 |
{ |
111 |
send_command_packet (station_robot_set_motors_off, NULL, 0); |
112 |
} |
113 |
|
114 |
void robot_set_bom (uint16_t bitmask)
|
115 |
{ |
116 |
char data[2]; |
117 |
|
118 |
data[0]=WORD_BYTE_0(bitmask);
|
119 |
data[1]=WORD_BYTE_1(bitmask);
|
120 |
|
121 |
send_command_packet (station_robot_set_bom, data, 2);
|
122 |
} |
123 |
|
124 |
char* robot_read_encoders ()
|
125 |
{ |
126 |
send_request_packet (station_robot_read_encoders, NULL, 0); |
127 |
return data_received;
|
128 |
} |
129 |
|
130 |
void receive_encoders(int* data, int length){ |
131 |
((int*)data_received)[0] = data[0]; |
132 |
((int*)data_received)[1] = data[1]; |
133 |
} |
134 |
|
135 |
void robot_station_receive(char type, int source, unsigned char* packet, int length) |
136 |
{ |
137 |
switch (type)
|
138 |
{ |
139 |
case robot_station_finished: new_data = 1; break; |
140 |
case robot_station_data_encoders: receive_encoders((int*)packet, length); break; |
141 |
} |
142 |
new_data = 1;
|
143 |
} |
144 |
|
145 |
|
146 |
void comm_robot_init ()
|
147 |
{ |
148 |
PacketGroupHandler receive_handler = {robot_station_group, NULL, NULL, &robot_station_receive, NULL}; |
149 |
wl_register_packet_group(&receive_handler); |
150 |
} |