Revision 1159
Added robot code
Added robot/station communication
trunk/code/projects/diagnostic_station/station/robot_comm.c | ||
---|---|---|
1 |
#include <dragonfly_lib.h> |
|
2 |
#include <wireless.h> |
|
3 |
|
|
4 |
#include "robot_comm.h" |
|
5 |
#include "../common/comm_station_robot.h" |
|
6 |
|
|
7 |
static void send_packet (char type, char *data, int len) |
|
8 |
{ |
|
9 |
wl_send_global_packet (station_robot_group, type, data, len, 0); |
|
10 |
|
|
11 |
// TODO: is it guaranteed that the package will be sent on the next call to wl_do, or |
|
12 |
// do we have to call wl_do in a loop? |
|
13 |
wl_do (); |
|
14 |
} |
|
15 |
|
|
16 |
void robot_set_orbs (uint8_t red1, uint8_t green1, uint8_t blue1, uint8_t red2, uint8_t green2, uint8_t blue2) |
|
17 |
{ |
|
18 |
char data[6]; |
|
19 |
|
|
20 |
data[0]=red1; |
|
21 |
data[1]=green1; |
|
22 |
data[2]=blue1; |
|
23 |
|
|
24 |
data[3]=red2; |
|
25 |
data[4]=green2; |
|
26 |
data[5]=blue2; |
|
27 |
|
|
28 |
send_packet (station_robot_set_orbs, data, 6); |
|
29 |
} |
|
30 |
|
|
31 |
void robot_set_motors (uint8_t direction1, uint8_t speed1, uint8_t direction2, uint8_t speed2) |
|
32 |
{ |
|
33 |
char data[4]; |
|
34 |
|
|
35 |
data[0]=direction1; |
|
36 |
data[1]=speed1; |
|
37 |
data[2]=direction2; |
|
38 |
data[3]=speed2; |
|
39 |
|
|
40 |
send_packet (station_robot_set_motors, data, 4); |
|
41 |
} |
|
42 |
|
|
43 |
void robot_set_bom (uint16_t bitmask) |
|
44 |
{ |
|
45 |
char data[2]; |
|
46 |
|
|
47 |
data[0]=WORD_BYTE_0(bitmask); |
|
48 |
data[1]=WORD_BYTE_1(bitmask); |
|
49 |
|
|
50 |
send_packet (station_robot_set_bom, data, 2); |
|
51 |
} |
trunk/code/projects/diagnostic_station/station/test_encoders.c | ||
---|---|---|
1 | 1 |
#include "test_encoders.h" |
2 | 2 |
#include "global.h" |
3 | 3 |
|
4 |
#define num_measurements 5 |
|
5 |
#define velocity 200 |
|
6 |
#define delay 500 |
|
7 |
|
|
8 |
void test_encoders_direction (uint8_t direction1, uint8_t direction2) |
|
9 |
{ |
|
10 |
// reset_encoders (); |
|
11 |
for (uint8_t m=0; m<num_measurements; ++m) |
|
12 |
{ |
|
13 |
robot_set_motors (direction1, velocity, direction2, velocity); |
|
14 |
delay_ms (delay); |
|
15 |
|
|
16 |
robot_set_motors (direction1, 0, direction2, 0); |
|
17 |
delay_ms (delay); // FIXME wait for steady state! |
|
18 |
|
|
19 |
// send (turn motor on for time) |
|
20 |
// wait (done) |
|
21 |
// wait_stopped |
|
22 |
// send (read encoders) |
|
23 |
// receive (data) |
|
24 |
// read encoders |
|
25 |
} |
|
26 |
|
|
27 |
} |
|
28 |
|
|
29 |
|
|
4 | 30 |
void test_encoders (void) |
5 | 31 |
{ |
6 | 32 |
usb_puts("Testing encoders" NL); |
7 |
|
|
8 |
//for (velocities up/down) |
|
9 |
//{ |
|
10 |
// send (set velocity) |
|
11 |
// wait (steady) |
|
12 |
// |
|
13 |
// reset_encoders (); |
|
14 |
// for (measurements) |
|
15 |
// { |
|
16 |
// measure left/right |
|
17 |
// wait |
|
18 |
// } |
|
19 |
//} |
|
20 |
|
|
33 |
|
|
34 |
test_encoders_direction (FORWARD, BACKWARD); |
|
35 |
test_encoders_direction (BACKWARD, FORWARD); |
|
36 |
|
|
21 | 37 |
//send data |
22 | 38 |
|
23 | 39 |
usb_puts("Testing encoders finished" NL); |
trunk/code/projects/diagnostic_station/station/robot_comm.h | ||
---|---|---|
1 |
#ifndef _robot_comm_h |
|
2 |
#define _robot_comm_h |
|
3 |
|
|
4 |
void robot_set_orbs (uint8_t red1, uint8_t green1, uint8_t blue1, uint8_t red2, uint8_t green2, uint8_t blue2); |
|
5 |
void robot_set_motors (uint8_t direction1, uint8_t speed1, uint8_t direction2, uint8_t speed2); |
|
6 |
void robot_set_bom (uint16_t bitmask); |
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
#endif |
trunk/code/projects/diagnostic_station/station/test_motors.c | ||
---|---|---|
1 | 1 |
#include "test_motors.h" |
2 | 2 |
#include "global.h" |
3 | 3 |
|
4 |
#include "robot_comm.h" |
|
5 |
|
|
6 |
// NB don't produce overflows! |
|
7 |
#define vel_min 160 |
|
8 |
#define vel_max 250 |
|
9 |
#define vel_inc 10 |
|
10 |
#define vel_delay 200 |
|
11 |
|
|
12 |
static void test_motors_direction_velocity (uint8_t direction1, uint8_t direction2, uint8_t velocity) |
|
13 |
{ |
|
14 |
robot_set_motors (direction1, velocity, direction2, velocity); |
|
15 |
delay_ms (vel_delay); |
|
16 |
|
|
17 |
// wait (steady) |
|
18 |
// |
|
19 |
// reset_encoders (); |
|
20 |
// for (measurements) |
|
21 |
// { |
|
22 |
// measure left/right |
|
23 |
// wait |
|
24 |
// } |
|
25 |
} |
|
26 |
|
|
27 |
static void test_motors_direction (uint8_t direction1, uint8_t direction2) |
|
28 |
{ |
|
29 |
// Use 16 bit variable for vel to avoid problems with overflow (if vel=250 and 10 is added, it would be 260-256=4 |
|
30 |
// which is still smaller than vel_max). There are more elegant solutions to this problem, but this one is |
|
31 |
// easy and performance is not an issue here anyway. |
|
32 |
|
|
33 |
for (uint16_t vel=vel_min; vel<=vel_max; vel+=vel_inc) |
|
34 |
test_motors_direction_velocity (direction1, direction2, vel); |
|
35 |
|
|
36 |
for (uint16_t vel=vel_max; vel>=vel_min; vel-=vel_inc) |
|
37 |
test_motors_direction_velocity (direction1, direction2, vel); |
|
38 |
} |
|
39 |
|
|
4 | 40 |
void test_motors (void) |
5 | 41 |
{ |
6 | 42 |
usb_puts("Testing motors" NL); |
7 |
|
|
8 |
// reset_encoders (); |
|
9 |
//for (num measurements) |
|
10 |
//{ |
|
11 |
// send (turn motor on for time) |
|
12 |
// wait (done) |
|
13 |
// wait_stopped |
|
14 |
// send (read encoders) |
|
15 |
// receive (data) |
|
16 |
// read encoders |
|
17 |
//} |
|
18 | 43 |
|
44 |
test_motors_direction (FORWARD, BACKWARD); |
|
45 |
test_motors_direction (BACKWARD, FORWARD); |
|
46 |
|
|
47 |
robot_set_motors (FORWARD, 0, FORWARD, 0); |
|
48 |
|
|
19 | 49 |
//send data |
20 | 50 |
|
51 |
|
|
21 | 52 |
usb_puts("Testing motors finished" NL); |
22 | 53 |
} |
trunk/code/projects/diagnostic_station/station/main.c | ||
---|---|---|
1 | 1 |
#include <dragonfly_lib.h> |
2 |
#include <wireless.h> |
|
3 |
#include <xbee.h> |
|
2 | 4 |
|
5 |
#include "../common/comm_station_robot.h" |
|
6 |
|
|
3 | 7 |
#include "global.h" |
4 | 8 |
#include "station_hardware.h" |
5 | 9 |
|
6 | 10 |
#include "test_comm.h" |
7 | 11 |
#include "test_bom.h" |
8 | 12 |
#include "test_rangefinders.h" |
13 |
#include "test_motors.h" |
|
9 | 14 |
#include "test_encoders.h" |
10 |
#include "test_motors.h" |
|
11 | 15 |
|
12 | 16 |
bool communication_ok=false; |
13 | 17 |
|
... | ... | |
47 | 51 |
|
48 | 52 |
usb_puts (NL); |
49 | 53 |
usb_puts ("Diagnostic station interactive mode" NL); |
50 |
usb_puts ("Test (s)elf, (a)ll, (c)omm, (b)om, (r)angefinders, (e)ncoders, (m)otors" NL);
|
|
54 |
usb_puts ("Test (s)elf, (a)ll, (c)omm, (b)om, (r)angefinders, (m)otors, (e)ncoders" NL);
|
|
51 | 55 |
|
52 | 56 |
char choice = usb_getc (); |
53 | 57 |
switch (choice) { |
... | ... | |
55 | 59 |
case 'a': case 'A': test_all (); break; // test_all will test comm itself |
56 | 60 |
case 'b': case 'B': if (require_comm ()) test_bom (true, true); break; |
57 | 61 |
case 'r': case 'R': if (require_comm ()) test_rangefinders (); break; |
62 |
case 'm': case 'M': if (require_comm ()) test_motors (); break; |
|
58 | 63 |
case 'e': case 'E': if (require_comm ()) test_encoders (); break; |
59 |
case 'm': case 'M': if (require_comm ()) test_motors (); break; |
|
60 | 64 |
case 's': case 'S': self_test (); break; |
61 | 65 |
default: break; // ignore it |
62 | 66 |
// usb_puts("Received invalid input "); |
... | ... | |
79 | 83 |
#include <dragonfly_lib.h> |
80 | 84 |
#include <wireless.h> |
81 | 85 |
|
82 |
// dragonfly_init(ALL_ON); |
|
83 |
// wl_init(); |
|
84 |
// |
|
85 |
// while (1) |
|
86 |
// { |
|
87 |
// wl_do(); |
|
88 |
// do_stuff(); |
|
89 |
// } |
|
90 |
// return 0; |
|
86 |
dragonfly_init(0); |
|
91 | 87 |
|
92 |
dragonfly_init(0); |
|
93 |
usb_init (); |
|
94 |
orb_init (); |
|
88 |
usb_init (); |
|
95 | 89 |
|
96 | 90 |
usb_puts(NL NL NL); |
97 | 91 |
usb_puts("Diagnostic station version " version_string " starting" NL); |
92 |
|
|
93 |
orb_init_pwm (); |
|
98 | 94 |
hardware_init (); |
99 |
|
|
95 |
|
|
96 |
orb1_set (255, 0, 0); usb_puts("Initializing wireless" NL); |
|
97 |
xbee_init (); |
|
98 |
wl_init(); |
|
99 |
orb2_set (255, 0, 0); usb_puts("Done" NL); |
|
100 |
|
|
100 | 101 |
if (button2_read ()) |
101 | 102 |
server_main (); |
102 | 103 |
else |
trunk/code/projects/diagnostic_station/robot/main.c | ||
---|---|---|
1 |
#include <dragonfly_lib.h> |
|
2 |
#include <wireless.h> |
|
3 |
#include <xbee.h> |
|
4 |
|
|
5 |
#include "../common/comm_station_robot.h" |
|
6 |
|
|
7 |
static void message_set_orbs (int length, uint8_t *data) |
|
8 |
{ |
|
9 |
if (length>=6) |
|
10 |
{ |
|
11 |
orbs_set (data[0], data[1], data[2], data[3], data[4], data[5]); |
|
12 |
} |
|
13 |
} |
|
14 |
|
|
15 |
static void message_set_motors (int length, uint8_t *data) |
|
16 |
{ |
|
17 |
if (length>=4) |
|
18 |
{ |
|
19 |
motor1_set (data[0], data[1]); |
|
20 |
motor2_set (data[2], data[3]); |
|
21 |
} |
|
22 |
} |
|
23 |
|
|
24 |
static void message_set_bom (int length, uint8_t *data) |
|
25 |
{ |
|
26 |
// TODO test |
|
27 |
if (length>=2) |
|
28 |
{ |
|
29 |
uint16_t bitmask=WORD(data[0], data[1]); |
|
30 |
|
|
31 |
bom_set_leds (bitmask); |
|
32 |
|
|
33 |
if (bitmask==0) |
|
34 |
bom_off (); |
|
35 |
else |
|
36 |
bom_on (); |
|
37 |
} |
|
38 |
} |
|
39 |
|
|
40 |
void station_robot_receive(char type, int source, unsigned char* packet, int length) |
|
41 |
{ |
|
42 |
switch (type) |
|
43 |
{ |
|
44 |
case station_robot_set_orbs: message_set_orbs (length, packet); break; |
|
45 |
case station_robot_set_motors: message_set_motors (length, packet); break; |
|
46 |
case station_robot_set_bom: message_set_bom (length, packet); break; |
|
47 |
// TODO default: error message, red orbs, pause. Test! |
|
48 |
// TODO: error signal in the individual handlers if the number of parameters is too low. Test! |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
|
|
53 |
int main(void) { |
|
54 |
dragonfly_init(ALL_ON); |
|
55 |
|
|
56 |
usb_init (); |
|
57 |
usb_puts ("Diagnostic station robot starting up\r\n"); |
|
58 |
|
|
59 |
orb_init_pwm (); |
|
60 |
xbee_init (); |
|
61 |
|
|
62 |
orb1_set (255, 127, 0); |
|
63 |
wl_init(); |
|
64 |
orb2_set (255, 127, 0); |
|
65 |
|
|
66 |
//usb_puts ("PAN ID: "); usb_puti (xbee_get_pan_id ()); usb_puts ("\r\n"); // -1 |
|
67 |
//usb_puts ("Channel: "); usb_puti (xbee_get_channel ()); usb_puts ("\r\n"); // 0 |
|
68 |
//usb_puts ("Address: "); usb_puti (xbee_get_address ()); usb_puts ("\r\n"); // 4/8 |
|
69 |
|
|
70 |
PacketGroupHandler receive_handler = {station_robot_group, NULL, NULL, &station_robot_receive, NULL}; |
|
71 |
wl_register_packet_group(&receive_handler); |
|
72 |
|
|
73 |
while (1) |
|
74 |
{ |
|
75 |
wl_do(); |
|
76 |
} |
|
77 |
} |
trunk/code/projects/diagnostic_station/common/comm_station_robot.h | ||
---|---|---|
1 |
#ifndef _comm_station_robot_h |
|
2 |
#define _comm_station_robot_h |
|
3 |
|
|
4 |
// TODO: define a channel and a PAN (and use it on both the robot and the station) |
|
5 |
|
|
6 |
// The message group (0..15) |
|
7 |
#define station_robot_group 1 |
|
8 |
|
|
9 |
// The message types |
|
10 |
#define station_robot_set_orbs 1 |
|
11 |
#define station_robot_set_motors 2 |
|
12 |
#define station_robot_set_bom 3 |
|
13 |
// TODO: add motors off (and use everywhere) |
|
14 |
// TODO: add motors_for_some_time (and use in test_encoders) |
|
15 |
|
|
16 |
#define WORD_BYTE_0(word) ( word &0xFF) |
|
17 |
#define WORD_BYTE_1(word) ((word>>8)&0xFF) |
|
18 |
#define WORD(byte0, byte1) (byte1<<8 | byte0) |
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
#endif |
Also available in: Unified diff