Project

General

Profile

Revision 149

Updated robot colonet library.

View differences:

trunk/code/projects/colonet/lib/colonet_wireless/colonet_wireless.cpp
96 96
                          1 + PACKET_DATA_LEN, 0);
97 97
  } else {
98 98
    wl_send_robot_to_robot_packet(COLONET_PACKET_GROUP_ID, (char)msg_type,
99
                                  pkt_buffer, 1 + PACKET_DATA_LEN, dest, 0);
99
                                  pkt_buffer, 1 + PACKET_DATA_LEN, dest,
100
                                  COLONET_RESPONSE_PACKET_FRAME_ID);
100 101
  }
101 102
}
102 103

  
trunk/code/projects/colonet/lib/colonet_dragonfly/colonet_dragonfly.c
2 2
 * @brief Colonet library for DRAGONFLY colony robots
3 3
 *
4 4
 * @author Eugene Marinelli
5
 * @date 4/17/07
6
 * 
5
 * @date 10/10/07
6
 *
7 7
 * @bug Handler registration not tested
8 8
 * @bug Request reponding not implemented - only accepts commands.
9 9
 */
10 10

  
11
#include <assert.h>
11 12
#include <dragonfly_lib.h>
13
#include <string.h>
14
#include <wireless.h>
15

  
12 16
#include "colonet_dragonfly.h"
13 17

  
14 18
typedef struct {
......
23 27
static void packet_string_to_struct(ColonetRobotServerPacket* dest_pkt,
24 28
                                    char* pkt_buf);
25 29
static unsigned int two_bytes_to_int(char high, char low);
30
static void colonet_handle_receive(char type, int source,
31
                                   unsigned char* packet, int length);
26 32

  
33
static PacketGroupHandler colonet_pgh;
34

  
27 35
/* Public functions */
28
int colonet_handle_message(unsigned char robot_id, char* pkt_buf) {
36
int colonet_init() {
37
  colonet_pgh.groupCode = COLONET_PACKET_GROUP_ID;
38
  colonet_pgh.timeout_handler = NULL;
39
  colonet_pgh.handle_response = NULL;
40
  colonet_pgh.handle_receive = colonet_handle_receive;
41
  colonet_pgh.unregister = NULL;
42

  
43
  // TODO this should return an error if wl_init has not been called yet.
44
  wl_register_packet_group(&colonet_pgh);
45

  
46
  return 0;
47
}
48

  
49
/* Private functions */
50

  
51
/** @brief Handles colonet packets.  Should be called by parse_buffer
52
 * when it is determined that a colonet message has been received.
53
 *
54
 * @param robot_id The robot id
55
 * @param pkt_buf The packet buffer (e.g. wl_buf)
56
 *
57
 * @return -1 on error (invalid msgId), 0 on success
58
 */
59
void colonet_handle_receive(char type, int source, unsigned char* packet,
60
                            int length) {
29 61
  ColonetRobotServerPacket pkt;
30 62
  unsigned char* args; //up to 7 char args
31 63
  unsigned int int_args[3]; //up to 3 int (2-byte) args
32 64

  
33
  packet_string_to_struct(&pkt, pkt_buf);
65
  printf("received message from %d of length %d\n", source, length);
34 66

  
35
  if (pkt.msg_dest != GLOBAL_DEST && pkt.msg_dest != robot_id) {
36
    //message not intended for us
37
    return 1;
38
  }
67
  assert(length == sizeof(ColonetRobotServerPacket));
68
  packet_string_to_struct(&pkt, packet);
39 69

  
40 70
  args = pkt.data;
41
  
71

  
42 72
  int_args[0] = two_bytes_to_int(args[0], args[1]);
43 73
  int_args[1] = two_bytes_to_int(args[2], args[3]);
44 74
  int_args[2] = two_bytes_to_int(args[4], args[5]);
45 75

  
46
  if(pkt.msg_type == COLONET_REQUEST){
76
  if (type == COLONET_REQUEST) {
47 77
    /* TODO - send back data! */
48 78

  
49
    switch(pkt.msg_code){
79
    switch (pkt.msg_code) {
50 80
      //Sharp
51 81
    case READ_DISTANCE:
52 82
      break;
......
82 112
    case DETECT_BUMP:
83 113
      break;
84 114
    }
85
  }else if(pkt.msg_type == COLONET_COMMAND){
86
    if(pkt.msg_code >= USER_DEFINED_MSG_ID_START && 
115
  } else if (type == COLONET_COMMAND) {
116
    if(pkt.msg_code >= USER_DEFINED_MSG_ID_START &&
87 117
       pkt.msg_code <= USER_DEFINED_MSG_ID_END){
88 118
      if (user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler) {
89 119
        /* Call the user's handler function if it the function's address
90 120
         * is non-zero (has been set) */
91 121
        user_handlers[pkt.msg_code - USER_DEFINED_MSG_ID_START].handler();
92 122
      }
93

  
94
      return 0;
95 123
    }
96 124

  
97 125
    switch(pkt.msg_code){
98 126
    default:
99 127
      printf("%s: Error - message code %d not implemented\n", __FUNCTION__,
100 128
             pkt.msg_code);
101
      return -1;
102 129
      break;
103 130

  
104 131
      //Buzzer
......
117 144
    case BUZZER_OFF:
118 145
      buzzer_off();
119 146
      break;
120
      
121
      //LCD
122
    case LCD_INIT:
123
      lcd_init();
124
      break;
125
    case LCD_CLEAR_SCREEN:
126
      lcd_clear_screen();
127
      break;
128
    case LCD_PUTBYTE:
129
      lcd_putbyte(args[0]);
130
      break;
131
    case LCD_PUTCHAR:
132
      lcd_putchar((char)args[0]);
133
      break;
134
    case LCD_PUTSTR:
135
      lcd_putstr((char*)args);
136
      break;
137
    case LCD_GOTOXY: 
138
      lcd_gotoxy(int_args[0], int_args[1]);
139
      break;
140
    case LCD_PUTINT: 
141
      lcd_putint(int_args[0]);
142
      break;
147

  
143 148
    case ORB_INIT:
144 149
      orb_init();
145 150
      break;
......
179 184

  
180 185
      /*
181 186
    case MOVE:
182
      
187

  
183 188
      break;
184 189
      */
185 190

  
......
224 229
    case BOM_OFF:
225 230
      bom_off();
226 231
      break;
227
    case OUTPUT_HIGH:
228
      output_high(int_args[0]);
229
      break;
230
    case OUTPUT_LOW:
231
      output_low(int_args[0]);
232
      break;
233 232

  
234 233
      //Dio
235 234
    case DIGITAL_OUTPUT:
236 235
      digital_output(int_args[0], int_args[1]);
237 236
      break;
238 237
    }
239
  }else{
238
  } else {
240 239
    printf("%s: Error: Invalid colonet message type", __FUNCTION__);
241
    return -1;
242 240
  }
243

  
244
  return 0;
245 241
}
246 242

  
247 243
/* colonet_add_message
248 244
 * Adds a user-defined message
249 245
 */
250
int colonet_add_message(unsigned char msgId, void (*handler)(void))
251
{
246
int colonet_add_message(unsigned char msgId, void (*handler)(void)) {
252 247
  if(msgId < USER_DEFINED_MSG_ID_START || msgId > USER_DEFINED_MSG_ID_END){
253 248
    return -1;
254 249
  }
......
260 255
  return 0;
261 256
}
262 257

  
263
/* Private functions */
264 258
static void packet_string_to_struct(ColonetRobotServerPacket* dest_pkt,
265 259
                                    char* pkt_buf) {
266
  int i;
267
  
268
  printf("\npacket:");
269
  for(i = 0; i < 16; i++){
270
    printf("%d ", pkt_buf[i]);
271
  }
272

  
273
  printf("\n");
274

  
275
  dest_pkt->prefix[0] = pkt_buf[0];
276
  dest_pkt->prefix[1] = pkt_buf[1];
277
  dest_pkt->token_src = pkt_buf[2];
278
  dest_pkt->token_dest = pkt_buf[3];
279
  dest_pkt->msg_dest = pkt_buf[4];
280
  dest_pkt->msg_type = pkt_buf[5];
281
  dest_pkt->msg_code = pkt_buf[6];
282
  
283
  for(i = 0; i < PACKET_DATA_LEN; i++){
284
    dest_pkt->data[i] = pkt_buf[7+i];
285
  }
286

  
287
  dest_pkt->num_robots = pkt_buf[PACKET_SIZE-2];
288
  dest_pkt->checksum = pkt_buf[PACKET_SIZE-1];
260
  memcpy(dest_pkt, pkt_buf, sizeof(ColonetRobotServerPacket));
289 261
}
290 262

  
291 263
/* two_bytes_to_int(char a, char b)
292 264
 * Returns int of form [high][low]
293 265
 */
294
static unsigned int two_bytes_to_int(char high, char low)
295
{
266
static unsigned int two_bytes_to_int(char high, char low) {
296 267
  return (((unsigned int)high)<<8) + (unsigned int)low;
297 268
}
trunk/code/projects/colonet/lib/colonet_dragonfly/Makefile
5 5

  
6 6
CC = avr-gcc
7 7
CFLAGS = -Wall -Wshadow -Wextra -g -mmcu=$(MCU)
8
INCLUDES = -I../../../xfly_lib -I../
8
INCLUDES = -I../../../xfly_lib -I../ -I../../../libwireless/lib -I../../../../lib/include/libdragonfly
9 9

  
10 10
all: colonet_dragonfly.o
11 11

  
trunk/code/projects/colonet/lib/colonet_dragonfly/colonet_dragonfly.h
5 5
 */
6 6

  
7 7
#ifndef COLONET_DRAGONFLY_H_
8
#define COLONET_DRAGONFLYH_
8
#define COLONET_DRAGONFLY_H_
9 9

  
10 10
#include <colonet_defs.h>
11 11

  
12
/** @brief Handles colonet packets.  Should be called by parse_buffer 
13
 * when it is determined that a colonet message has been received.
14
 *
15
 * @param robot_id The robot id
16
 * @param pkt_buf The packet buffer (e.g. wl_buf)
17
 * 
18
 * @return -1 on error (invalid msgId), 0 on success
12
/**
19 13
 */
20
int colonet_handle_message(unsigned char robot_id, char* packet);
14
int colonet_init(void);
21 15

  
22 16
/** @brief Registers a new colonet message handler function.  If a message
23 17
 * with msgId is received, then handler will be called.
24
 * 
18
 *
25 19
 * @param msgId The message id of the handler to be registered.  Must be
26 20
 * between USER_DEFINED_MSG_ID_START and USER_DEFINED_MSG_ID_END
27 21
 * @param handler The function to be called if a colonet packet with message
28 22
 * id msgId is received
29
 * 
23
 *
30 24
 * @return -1 on error (invalid msgId), 0 on success
31 25
 */
32 26
int colonet_add_message(unsigned char msgId, void (*handler)(void));
trunk/code/projects/colonet/lib/colonet_defs.h
10 10

  
11 11
// PacketGroup id for wireless library.
12 12
#define COLONET_PACKET_GROUP_ID 8
13
#define COLONET_RESPONSE_PACKET_FRAME_ID COLONET_PACKET_GROUP_ID
13 14

  
14 15
// Message types
15 16
typedef enum {COLONET_COMMAND, COLONET_REQUEST,
......
17 18

  
18 19
//Packet properties
19 20
#define PACKET_DATA_LEN 7
20
#define PACKET_SIZE 16
21 21

  
22 22
#define WL_DEFAULT_PAN 3332
23 23

  
......
26 26

  
27 27
// Message dests
28 28
#define GLOBAL_DEST 200
29
#define MAX_NUM_ROBOTS 7
29
//#define MAX_NUM_ROBOTS 7
30 30
#define COLONET_SERVER_RESPONSE_ADDR 201
31 31

  
32 32
#define USER_DEFINED_MSG_ID_START 0xF0
......
34 34
#define USER_DEFINED_MSG_TOTAL 0x0F
35 35

  
36 36
/* This is the packet structure of the data buffer sent from the colonet 
37
 * wireless library to the . */
37
 * wireless library to the robots and from the robots to the colonet wireless
38
 * library. */
38 39
typedef struct {
39 40
  int client_id; // ID number of the client sending the packet.
40 41
  unsigned char msg_code; // Specific instruction for the robot -- see below.
trunk/code/projects/libwireless/lib/wireless.h
125 125
unsigned int wl_get_xbee_id(void);
126 126

  
127 127
/** @} **/ // end defgroup
128

  

Also available in: Unified diff