Project

General

Profile

Revision 294

Got the data response to work, however analog8 reads are failing to get good data.

View differences:

trunk/code/lib/src/libwireless/xbee.c
783 783
#ifndef ROBOT
784 784
void xbee_set_com_port(char* port){
785 785
  //printf("Port being passed is %s\n",port);
786
  xbee_com_port = malloc(strlen(port));
786
  xbee_com_port = (char *) malloc(strlen(port));
787 787
  strcpy(xbee_com_port,port); 
788 788
}
789 789
#endif
branches/slam/code/projects/colonet/DataRequests/test/robot/robot_test.c
1 1
#include <dragonfly_lib.h>
2 2
#include <wireless.h>
3
#include <wl_token_ring.h>
4
#define TEST_CODE 12
3 5

  
4 6
void  handle_receive(char type, int source, unsigned char *packet, int length);
5 7

  
8

  
6 9
int main(void){
7
  PacketGroupHandler pgh;
8
  pgh.groupCode=12;
10
  
11
  wl_init();
12
 PacketGroupHandler pgh;
13

  
14
  pgh.groupCode=TEST_CODE;
9 15
  pgh.timeout_handler=NULL;
10 16
  pgh.handle_response=NULL;
11 17
  pgh.handle_receive=handle_receive;
......
13 19

  
14 20
  wl_register_packet_group(&pgh);
15 21
  
22
  wl_token_ring_register(); 
23
  wl_token_ring_join();
24
  
16 25
  while(1){
17 26
    wl_do();
18 27
  }
branches/slam/code/projects/colonet/DataRequests/test/server/test_main.c
2 2
#include <stdio.h>
3 3
#include <unistd.h>
4 4
#include "wireless.h"
5
#include "wl_token_ring.h"
5 6

  
7
#define TEST_CODE 12
8

  
6 9
void handle_receive(char type, int source, unsigned char* packet, int length);
7 10

  
8
unsigned char bom_responded_flag;
11
unsigned char response_flag;
9 12

  
10 13
int main(void){
11 14
  wl_set_com_port("/dev/cu.usbserial-A1000Qu6");
12 15
  wl_init();
16
  
13 17
  PacketGroupHandler* pgh = malloc(sizeof(PacketGroupHandler));
14 18
  pgh->groupCode=12;
15 19
  pgh->timeout_handler=NULL;
16 20
  pgh->handle_response=NULL;
17 21
  pgh->handle_receive=handle_receive;
18 22
  pgh->unregister=NULL;
23
  
19 24
  wl_register_packet_group(pgh);
20
  int i;
25
  wl_token_ring_register();  
26
  int i,robot_id;
27
  response_flag=0;
28
  wl_token_iterator_begin();
21 29
  while(1){
22
    wl_send_robot_to_robot_global_packet (12, 0, NULL, 0, 9, 0);
23
    printf("Sending robot to robot global packet.\n");
24
    for(i=0;i<1000;i++){
30
    while(wl_token_iterator_has_next()){
31
      wl_do();
32

  
33
      robot_id = wl_token_iterator_next();
34

  
35
      wl_send_robot_to_robot_global_packet (12, 0, NULL, 0, robot_id, 0);
36
      printf("Sent robot to robot global packet.\n");
37
      while(response_flag==0){
25 38
         wl_do();
26
         usleep(100);
39
         if(i>5000){
40
            printf("Response timed out\n");
41
            break;
42
         }
43
      }
44
      response_flag =0;
27 45
    }
28 46
  }
29 47
  return -1;
30 48
}
31 49

  
32 50
void handle_receive(char type, int source, unsigned char* packet, int length){
33
    printf("Received the callback!\n");
51
    printf("Received a callback from %i!\n",source);
52
    response_flag=1;
34 53
}
branches/slam/code/projects/colonet/DataRequests/robot/robot_test.c
5 5

  
6 6
void main(void){
7 7
  dragonfly_init(ALL_ON);
8
  range_init();
8 9
  wl_init();
9 10
  wl_token_ring_register();
10 11
  wl_token_ring_join();
11
  data_request_init();
12
  data_response_init();
13
  char* ranges = {IR1,IR2,IR3,IR4,IR5};
14
  char i;
12 15
  while(1){
13 16
    wl_do();
17
    usb_puts("IR Data\n\r");
18
    for(i=0;i<5;i++){
19
      usb_puts("IR ");
20
      usb_puti(i);
21
      usb_puts(": ");
22
      usb_puti(range_read_distance(ranges[i]));
23
      usb_puts("\n\r");
24
    }
14 25
  }
15 26
}
branches/slam/code/projects/colonet/DataRequests/robot/data_response.c
2 2
#include "dragonfly_lib.h"
3 3
#include "wl_token_ring.h"
4 4
#include "wireless.h"
5
#include "serial.h"
5 6

  
6

  
7 7
PacketGroupHandler pgh;
8 8

  
9 9
void handle_receive(char type, int source, unsigned char* packet, int length);
......
11 11
int generate_bom_packet(char** buf);
12 12
int generate_IR_packet(char** buf);
13 13

  
14
void data_request_init(void){
14
void data_response_init(void){
15
  usb_init();
16
  usb_puts("USB initialized!\n\r");
15 17
  range_init();
18
  pgh.groupCode=DATA_REQUEST_GROUP; 
16 19
  pgh.timeout_handler = NULL;
17 20
  pgh.handle_response = NULL;
18 21
  pgh.handle_receive = handle_receive;
19 22
  pgh.unregister = NULL;
20 23
  wl_register_packet_group(&pgh);
24
  usb_puts("Packet Group Registered!\n\r");
21 25
}
22 26

  
23 27
void handle_receive(char type, int source, unsigned char* packet, int length){
24 28
  char buf[MAX_PACKET_LENGTH]; 
25 29
  int size;
30
  usb_puts("Got a packet!\n\r");
26 31
  switch(type){
27 32
  case BOM_TYPE:
28 33
    size = generate_bom_packet(&buf); 
29
    wl_send_robot_to_robot_packet(DATA_REQUEST_GROUP,BOM_TYPE,buf,size,source,0);
34
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,BOM_TYPE,buf,size,source,0);
30 35
  case IR_TYPE:
31 36
    size = generate_IR_packet(&buf); 
32
    wl_send_robot_to_robot_packet(DATA_REQUEST_GROUP,IR_TYPE,buf,size,source,0);
37
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,IR_TYPE,buf,size,source,0);
33 38
  case ENCODER_TYPE:
34 39
    break; 
35 40
    //Not yet supported.
......
40 45
  short int i=0, robot_id; 
41 46
  char bom_data;
42 47
  wl_token_iterator_begin();
43
  while(wl_token_iterator_has_next() && i<=99){
48
  while(wl_token_iterator_has_next() && i<=96){
44 49
    robot_id = wl_token_iterator_next();
45 50
    (*buf)[i++] = (char)((robot_id>>8) & 0xFF);
46 51
    (*buf)[i++] = (char)(robot_id & 0xFF);
......
52 57

  
53 58
int generate_IR_packet(char** buf){
54 59
  *buf[0] = range_read_distance(IR1);   
60
  delay_ms(20);
55 61
  *buf[1] = range_read_distance(IR2);   
62
  delay_ms(20);
56 63
  *buf[2] = range_read_distance(IR3);   
64
  delay_ms(20);
57 65
  *buf[3] = range_read_distance(IR4);   
66
  delay_ms(20);
58 67
  *buf[4] = range_read_distance(IR5); 
68
  char i;
59 69
  return 5;
60 70
}
branches/slam/code/projects/colonet/DataRequests/robot/Makefile
14 14
USE_WIRELESS = 1
15 15

  
16 16
# com1 = serial port. Use lpt1 to connect to parallel port.
17
AVRDUDE_PORT = /dev/cu.usbserial-A4001hAU
17
AVRDUDE_PORT = /dev/cu.usbserial-A4001hyO
18 18
#
19 19
#
20 20
###################################
branches/slam/code/projects/colonet/DataRequests/robot/data_response.h
1 1
#ifndef _DATA_RESPONSE_H
2 2
#define _DATA_REQUESTS_H
3 3

  
4
#define DATA_REQUEST_GROUP 23
4
#define DATA_REQUEST_GROUP 7
5 5
#define BOM_TYPE 0
6 6
#define IR_TYPE 1
7 7
#define ENCODER_TYPE 2
......
10 10

  
11 11
/* This requires the wireless library
12 12
and the token ring to already be initialized.*/
13
void data_request_init(void);
13
void data_response_init(void);
14 14

  
15 15
#endif
branches/slam/code/projects/colonet/DataRequests/server/test_main.c
4 4
#include "data_requests.h"
5 5
#include "wireless.h"
6 6
#include "wl_token_ring.h"
7
#include "pthread.h"
7 8

  
8 9
void IR_handler(unsigned char* data);
9 10
void bom_handler(BomNode* head);
......
12 13
unsigned char bom_responded_flag;
13 14
unsigned char ir_responded_flag;
14 15
unsigned char encoder_responded_flag;
16
void* wl_do_routine(void* arg){
17
  while(1){
18
     wl_do();
19
  }
20
}
15 21

  
22
pthread_t* wl_do_thread;
23

  
16 24
int main(void){
17 25
  wl_set_com_port("/dev/cu.usbserial-A1000Qu6");
18 26
  wl_init();
......
24 32
  ir_responded_flag = 0;
25 33
  encoder_responded_flag = 0;
26 34
  int test_count; 
35

  
36
  wl_do_thread = malloc(sizeof(pthread_t));
37
  pthread_create(wl_do_thread,NULL,wl_do_routine,NULL);
38

  
27 39
  while(1){
28
      wl_do();
29 40
      printf(".");
30 41
      wl_token_iterator_begin();
31 42
      while(wl_token_iterator_has_next()){
......
44 55
        request_IR_data(robot_id);
45 56
        test_count=0;
46 57
        while(!(ir_responded_flag)&&(test_count++<20)){
47
          wl_do();
48 58
          printf("Waiting on %i..\n",robot_id);
49
          usleep(500000);
59
          usleep(5000000);
50 60
        }
51 61
/*       
52 62
        request_encoder_data(robot_id);
......
55 65
        }
56 66
*/
57 67
      }
58
      usleep(500000);
68
      usleep(5000000);
59 69
  }
60 70
}
61 71

  
branches/slam/code/projects/colonet/DataRequests/server/data_requests.c
3 3
#include <stdio.h>
4 4
#include <pthread.h>
5 5
#include "../../../libwireless/lib/wireless.h"
6
#include <string.h>
6 7

  
7 8
PacketGroupHandler pgh;
8 9
pthread_t* dr_thread;
......
43 44
}
44 45

  
45 46
void* pthread_handle(void* arg){
46
  
47 47
  RawData* data = (RawData*)arg;
48 48
  unsigned char* packet = data->packet;
49 49
  int length = data->length;
......
74 74
    case IR_TYPE:
75 75
      /*An IR packet consists of just the 5 bytes representing the IR
76 76
      values, in order.*/
77
      if(length != 4){
77
      if(length != 5){
78 78
          //The packet is incomplete... bail.
79 79
          break;
80 80
      }
81 81
      else{
82
        unsigned char data[4];
83
        for(i=0;i<4;i++){
82
        unsigned char data[5];
83
        for(i=0;i<5;i++){
84 84
          data[i] = packet[i];
85 85
        }
86 86
        IR_handle(data);
......
91 91
      /*I have no idea, unsupported.*/
92 92
      break;
93 93
  }
94
  free(data->packet);
95
  free(data);
94 96
}
97

  
95 98
void receive_handle(char type, int source, unsigned char* packet, int length){
96
  RawData data;
97
  data.packet=packet;
98
  data.source=source;
99
  data.length=length;
100
  data.type=type;
101
  pthread_create(dr_thread,NULL,pthread_handle,(void*)&data);
99
  
100
  RawData* data = malloc(sizeof(RawData));
101
  if(!data){
102
    return;
103
  }
104
  unsigned char* newPacket = malloc(length);
105
  memcpy(newPacket, packet,length);
106
 
107

  
108
  data->packet=newPacket;
109
  data->source=source;
110
  data->length=length;
111
  data->type=type;
112
  
113
  dr_thread = malloc(sizeof(pthread_t));  
114
  pthread_create(dr_thread,NULL,pthread_handle,(void*)data);
102 115
}

Also available in: Unified diff