Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (3.63 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_read_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
// ** Message acknowledging **
95
// ***************************
96

    
97
static void send_done (void)
98
{
99
        wl_send_global_packet (robot_station_group, robot_station_done, NULL, 0, 0);
100
}
101

    
102
// ***********************
103
// ** Receive callbacks **
104
// ***********************
105

    
106
static void station_robot_receive (char type, int source, unsigned char* packet, int length)
107
{
108
        orb_set (0, 0, 255);
109

    
110
        usb_puti (type);
111
        usb_puts (" [");
112
        for (uint8_t i=0; i<length; ++i)
113
        {
114
                if (i!=0) usb_putc (' ');
115
                usb_puti (packet[i]);
116
        }
117
        usb_puts ("]\r\n");
118
        
119
        switch (type)
120
        {
121
                // Command messages
122
                case station_robot_set_orbs:        message_set_orbs        (length, packet); send_done (); break;
123
                case station_robot_set_motors:      message_set_motors      (length, packet); send_done (); break;
124
                case station_robot_set_bom:         message_set_bom         (length, packet); send_done (); break;
125
                case station_robot_set_motors_off:  message_set_motors_off  (length, packet); send_done (); break;
126
                case station_robot_set_motors_time: message_set_motors_time (length, packet); send_done (); break;
127
                case station_robot_reset_encoders:  message_reset_encoders  (length, packet); send_done (); break;
128
                
129
                // Request messages
130
                case station_robot_read_encoders:        message_read_encoders        (length, packet); break;
131
        }
132
        
133
        orb_set (255, 127, 0);
134
}
135

    
136
// **********
137
// ** Main **
138
// **********
139

    
140
int main(void) {
141
    dragonfly_init(ALL_ON);
142
        encoders_init ();
143

    
144
    usb_init ();
145
    usb_puts ("Diagnostic station robot starting up\r\n");
146

    
147
        orb1_set (255, 127, 0);
148
        xbee_init ();
149
        wl_init();
150
        orb2_set (255, 127, 0);
151
        
152
        usb_puts ("PAN ID: ");  usb_puti (xbee_get_pan_id ());  usb_puts ("\r\n");        // -1
153
        usb_puts ("Channel: "); usb_puti (xbee_get_channel ()); usb_puts ("\r\n");        // 0
154
        usb_puts ("Address: "); usb_puti (xbee_get_address ()); usb_puts ("\r\n");        // 4/8
155
        
156
        PacketGroupHandler receive_handler = {station_robot_group, NULL, NULL, &station_robot_receive, NULL};
157
        wl_register_packet_group(&receive_handler);
158
        
159
        while (1)
160
        {
161
                wl_do();
162
        }
163
}