Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / diagnostic_station / robot / main.c @ 1220

History | View | Annotate | Download (3.33 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

    
9
// **********************
10
// ** Command handlers **
11
// **********************
12

    
13
static void message_set_orbs (int length, uint8_t *data)
14
{
15
        if (length>=6)
16
        {
17
                orbs_set (data[0], data[1], data[2], data[3], data[4], data[5]);
18
        }
19
}
20

    
21
static void message_set_motors (int length, uint8_t *data)
22
{
23
        if (length>=4)
24
        {
25
                motor1_set (data[0], data[1]);
26
                motor2_set (data[2], data[3]);
27
        }
28
}
29

    
30
static void message_set_motors_time (int length, uint8_t *data)
31
{
32
        if (length>=6)
33
        {
34
                motor1_set (data[0], data[1]);
35
                motor2_set (data[2], data[3]);
36
                delay_ms (WORD(data[4], data[5]));
37
                motors_off ();
38
        }
39
}
40

    
41
static void message_set_motors_off (int length, uint8_t *data)
42
{
43
        motors_off ();
44
}
45

    
46
static void message_set_bom (int length, uint8_t *data)
47
{
48
        if (length>=2)
49
        {
50
                uint16_t bitmask=WORD(data[0], data[1]);
51
                
52
                bom_set_leds (bitmask);
53
                
54
                if (bitmask==0)
55
                        bom_off ();
56
                else
57
                        bom_on ();
58
        }
59
}
60

    
61
static void message_reset_encoders (int length, uint8_t *data)
62
{
63
        SYNC
64
        {
65
                encoder_rst_dx (LEFT);
66
                encoder_rst_dx (RIGHT);
67
        }
68
}
69

    
70
// **********************
71
// ** Request handlers **
72
// **********************
73

    
74
static void message_send_encoders (int length, uint8_t *data)
75
{
76
        int16_t left, right;
77
        
78
        SYNC
79
        {
80
                left = encoder_get_dx (LEFT);
81
                right = encoder_get_dx (RIGHT);
82
        }
83
        
84
        char senddata[4];
85
        senddata[0]=WORD_BYTE_0 (left);
86
        senddata[1]=WORD_BYTE_1 (left);
87
        senddata[2]=WORD_BYTE_0 (right);
88
        senddata[3]=WORD_BYTE_1 (right);
89

    
90
        wl_send_global_packet (robot_station_group, robot_station_data_encoders, senddata, 4, 0);
91
}
92

    
93

    
94
// ***********************
95
// ** Receive callbacks **
96
// ***********************
97

    
98
static void station_robot_receive (char type, int source, unsigned char* packet, int length)
99
{
100
        orb_set (0, 0, 255);
101

    
102
        usb_puti (type);
103
        usb_puts (" [");
104
        for (uint8_t i=0; i<length; ++i)
105
        {
106
                if (i!=0) usb_putc (' ');
107
                usb_puti (packet[i]);
108
        }
109
        usb_puts ("]\r\n");
110
        
111
        switch (type)
112
        {
113
                // Command messages
114
                case station_robot_set_orbs:        message_set_orbs        (length, packet); break;
115
                case station_robot_set_motors:      message_set_motors      (length, packet); break;
116
                case station_robot_set_bom:         message_set_bom         (length, packet); break;
117
                case station_robot_set_motors_off:  message_set_motors_off  (length, packet); break;
118
                case station_robot_set_motors_time: message_set_motors_time (length, packet); break;
119
                case station_robot_reset_encoders:  message_reset_encoders  (length, packet); break;
120
                
121
                // Request messages
122
                case station_robot_read_encoders:        message_send_encoders        (length, packet); break;
123
        }
124
        
125
        orb_set (255, 127, 0);
126
}
127

    
128
// **********
129
// ** Main **
130
// **********
131

    
132
int main(void) {
133
    dragonfly_init(ALL_ON);
134

    
135
    usb_init ();
136
    usb_puts ("Diagnostic station robot starting up\r\n");
137

    
138
        orb1_set (255, 127, 0);
139
        xbee_init ();
140
        wl_init();
141
        orb2_set (255, 127, 0);
142
        
143
        usb_puts ("PAN ID: ");  usb_puti (xbee_get_pan_id ());  usb_puts ("\r\n");        // -1
144
        usb_puts ("Channel: "); usb_puti (xbee_get_channel ()); usb_puts ("\r\n");        // 0
145
        usb_puts ("Address: "); usb_puti (xbee_get_address ()); usb_puts ("\r\n");        // 4/8
146
        
147
        PacketGroupHandler receive_handler = {station_robot_group, NULL, NULL, &station_robot_receive, NULL};
148
        wl_register_packet_group(&receive_handler);
149
        
150
        while (1)
151
        {
152
                wl_do();
153
        }
154
}