Project

General

Profile

Statistics
| Revision:

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

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

    
62
// **********************
63
// ** Request handlers **
64
// **********************
65

    
66
static void message_send_encoders (int length, uint8_t *data)
67
{
68
        int16_t left, right;
69
        
70
        SYNC
71
        {
72
                left = encoder_read (LEFT);
73
                right = encoder_read (RIGHT);
74
        }
75
        
76
        char senddata[4];
77
        senddata[0]=WORD_BYTE_0 (left);
78
        senddata[1]=WORD_BYTE_1 (left);
79
        senddata[2]=WORD_BYTE_0 (right);
80
        senddata[3]=WORD_BYTE_1 (right);
81

    
82
        wl_send_global_packet (robot_station_group, robot_station_data_encoders, senddata, 4, 0);
83
}
84

    
85

    
86
// ***********************
87
// ** Receive callbacks **
88
// ***********************
89

    
90
// TODO: different group for commands and requests
91
static void station_robot_receive (char type, int source, unsigned char* packet, int length)
92
{
93
        // TODO why does it keep receiving 0 messages?
94
        //if (type==0) return;
95

    
96
        // Dump the raw packet
97
        usb_puts ("Recvd [");
98
        for (uint8_t i=0; i<length; ++i)
99
        {
100
                if (i!=0) usb_putc (' ');
101
                usb_puti (packet[i]);
102
        }
103
        usb_puts ("]\r\n");
104

    
105

    
106
        
107
        orb_set (0, 0, 255);
108

    
109
        usb_puts ("Received a message: ");
110
        usb_puti (type);
111
        usb_puts ("\r\n");
112
        
113
        switch (type)
114
        {
115
                // Command messages
116
                case station_robot_set_orbs:        message_set_orbs        (length, packet); break;
117
                case station_robot_set_motors:      message_set_motors      (length, packet); break;
118
                case station_robot_set_bom:         message_set_bom         (length, packet); break;
119
                case station_robot_set_motors_off:  message_set_motors_off  (length, packet); break;
120
                case station_robot_set_motors_time: message_set_motors_time (length, packet); break;
121
                
122
                // Request messages
123
                case station_robot_read_encoders:        message_send_encoders        (length, packet); break;
124
        }
125
        
126
        orb_set (255, 127, 0);
127
}
128

    
129
// **********
130
// ** Main **
131
// **********
132

    
133
int main(void) {
134
    dragonfly_init(0);
135

    
136
        encoders_init ();
137

    
138
    usb_init ();
139
    usb_puts ("Diagnostic station robot starting up\r\n");
140

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