root / trunk / code / projects / diagnostic_station / robot / main.c @ 1198
History | View | Annotate | Download (2.48 KB)
1 |
#include <dragonfly_lib.h> |
---|---|
2 |
#include <wireless.h> |
3 |
#include <xbee.h> |
4 |
|
5 |
#include "../common/comm_station_robot.h" |
6 |
#include "../../../lib/include/libdragonfly/encoders.h" |
7 |
|
8 |
static void message_set_orbs (int length, uint8_t *data) |
9 |
{ |
10 |
if (length>=6) |
11 |
{ |
12 |
orbs_set (data[0], data[1], data[2], data[3], data[4], data[5]); |
13 |
} |
14 |
} |
15 |
|
16 |
static void message_set_motors (int length, uint8_t *data) |
17 |
{ |
18 |
if (length>=4) |
19 |
{ |
20 |
motor1_set (data[0], data[1]); |
21 |
motor2_set (data[2], data[3]); |
22 |
} |
23 |
} |
24 |
|
25 |
static void message_set_motors_time (int length, uint8_t *data) |
26 |
{ |
27 |
if (length>=6) |
28 |
{ |
29 |
motor1_set (data[0], data[1]); |
30 |
motor2_set (data[2], data[3]); |
31 |
delay_ms (WORD(data[4], data[5])); |
32 |
motors_off (); |
33 |
} |
34 |
} |
35 |
|
36 |
static void message_set_motors_off (int length, uint8_t *data) |
37 |
{ |
38 |
motors_off (); |
39 |
} |
40 |
|
41 |
static void message_set_bom (int length, uint8_t *data) |
42 |
{ |
43 |
if (length>=2) |
44 |
{ |
45 |
uint16_t bitmask=WORD(data[0], data[1]); |
46 |
|
47 |
bom_set_leds (bitmask); |
48 |
|
49 |
if (bitmask==0) |
50 |
bom_off (); |
51 |
else
|
52 |
bom_on (); |
53 |
} |
54 |
} |
55 |
|
56 |
static void message_send_encoders (int length, uint8_t *data){ |
57 |
int left = encoder_read(LEFT);
|
58 |
int right = encoder_read(RIGHT);
|
59 |
int vals[2]; |
60 |
vals[0] = left;
|
61 |
vals[1] = right;
|
62 |
wl_send_global_packet (robot_station_group, robot_station_data_encoders, (char*)vals, 8, 0); |
63 |
} |
64 |
|
65 |
void station_robot_receive(char type, int source, unsigned char* packet, int length) |
66 |
{ |
67 |
switch (type)
|
68 |
{ |
69 |
case station_robot_set_orbs: message_set_orbs (length, packet); break; |
70 |
case station_robot_set_motors: message_set_motors (length, packet); break; |
71 |
case station_robot_set_bom: message_set_bom (length, packet); break; |
72 |
case station_robot_set_motors_off: message_set_motors_off (length, packet); break; |
73 |
case station_robot_set_motors_time: message_set_motors_time (length, packet); break; |
74 |
case station_robot_read_encoders: message_send_encoders (length, packet); break; |
75 |
} |
76 |
} |
77 |
|
78 |
|
79 |
int main(void) { |
80 |
dragonfly_init(ALL_ON); |
81 |
|
82 |
usb_init (); |
83 |
usb_puts ("Diagnostic station robot starting up\r\n");
|
84 |
|
85 |
orb_init_pwm (); |
86 |
xbee_init (); |
87 |
|
88 |
orb1_set (255, 127, 0); |
89 |
wl_init(); |
90 |
orb2_set (255, 127, 0); |
91 |
|
92 |
//usb_puts ("PAN ID: "); usb_puti (xbee_get_pan_id ()); usb_puts ("\r\n"); // -1
|
93 |
//usb_puts ("Channel: "); usb_puti (xbee_get_channel ()); usb_puts ("\r\n"); // 0
|
94 |
//usb_puts ("Address: "); usb_puti (xbee_get_address ()); usb_puts ("\r\n"); // 4/8
|
95 |
|
96 |
PacketGroupHandler receive_handler = {station_robot_group, NULL, NULL, &station_robot_receive, NULL}; |
97 |
wl_register_packet_group(&receive_handler); |
98 |
|
99 |
while (1) |
100 |
{ |
101 |
wl_do(); |
102 |
} |
103 |
} |