Project

General

Profile

Revision 366

The lib things were just because of a few recompilations.
Data requests project works for IR, the problems with IR were almost definitely the problems with the others (encoder / BOM data).
I'll fix the code for those next time, and then begin work on encoders.
Hopefully I'll be able to abstract the robots out completely and just work with raw data in a month or two.

View differences:

trunk/code/lib/include/libdragonfly/i2c.h
54 54
void i2c_packet_rec (char i2c_byte);
55 55
void i2c_packet_sniff(char data);
56 56
#endif
57

  
trunk/code/lib/src/libwireless/sensor_matrix.c
61 61
	}
62 62
	m->size = DEFAULT_SENSOR_MATRIX_SIZE;
63 63
	m->matrix = (int**)malloc(m->size * sizeof(int*));
64
	if (!(m->matrix)) {
65
	  WL_DEBUG_PRINT("Out of memory - allocating memory for matrix.\r\n");
66
	  free(m);
67
	  return NULL;
68
	}
64 69
	m->joined = (int*)malloc(m->size * sizeof(int));
70
	if (!(m->joined)) {
71
	  WL_DEBUG_PRINT("Out of memory - allocating memory for joined.\r\n");
72
	  free(m->matrix);
73
	  free(m);
74
	  return NULL;
75
	}
65 76
	m->numJoined = 0;
66 77
	if (!(m->matrix) || !(m->joined))
67 78
	{
trunk/code/lib/src/libdragonfly/analog.c
38 38
#include "analog.h"
39 39
#include "serial.h"
40 40
// Internal Function Prototypes
41
//TODO: if this function is only used in this file, make it static
42 41
void set_adc_mux(int which);
43 42

  
44 43
/**
......
49 48
 * @{
50 49
 **/
51 50

  
52
//TODO: if these variables are only used in this file, make them static
53 51
int adc_loop_running = 0;
54 52
int adc_current_port = 0;
55 53
adc_t an_val[11];
......
60 58
 *
61 59
 * @see analog8, analog10
62 60
 **/
63
void analog_init(int start_conversion) {
61
void analog_init(int start_conversion)
62
{
64 63
	for (int i = 0; i < 11; i++) {
65 64
		an_val[i].adc10 = 0;
66 65
		an_val[i].adc8 = 0;
trunk/code/lib/src/libdragonfly/bom.c
39 39
#include "analog.h"
40 40

  
41 41
//constants
42
//TODO: for better readability, make all of these hex or decimal
43 42
const int lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
44 43

  
45 44
// internal function prototypes
46
//TODO: if these are only used in this file, make them static
47 45
void output_high(int which);
48 46
void output_low(int which);
49 47

  
......
112 110
 **/
113 111
int get_max_bom(void) {
114 112
	int max_bom_temp = 0;
115
	//TODO: renamed a, i, and h to give them meaningful names
116 113
	int a, i, j, h;
117 114
    h = 255;
118 115

  
......
199 196
void output_low(int which) {
200 197
	digital_output(which, 0);
201 198
}
199

  
branches/slam/code/projects/colonet/DataRequests/robot/robot_test.c
7 7
  dragonfly_init(ALL_ON);
8 8
  range_init();
9 9
  wl_init();
10
  wl_set_channel(0xD);
10 11
  wl_token_ring_register();
11 12
  wl_token_ring_join();
12 13
  data_response_init();
......
14 15
  char i;
15 16
  while(1){
16 17
    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
    }
25 18
  }
26 19
}
branches/slam/code/projects/colonet/DataRequests/robot/data_response.c
26 26

  
27 27
void handle_receive(char type, int source, unsigned char* packet, int length){
28 28
  char buf[MAX_PACKET_LENGTH]; 
29
  int size;
29
  int size, i;
30 30
  usb_puts("Got a packet!\n\r");
31 31
  switch(type){
32 32
  case BOM_TYPE:
33 33
    size = generate_bom_packet(&buf); 
34 34
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,BOM_TYPE,buf,size,source,0);
35
  
36
  
35 37
  case IR_TYPE:
36
    size = generate_IR_packet(&buf); 
38

  
39
    usb_puts("Generating an IR packet.\n");
40
    char ranges[] = {IR1,IR2,IR3,IR4,IR5}; 
41
    int i;
42
    short ir_value; 
43
    for(i=0;i<5;i++){
44
      ir_value = (short)range_read_distance(ranges[i]);
45
      buf[2*i] = (char)((ir_value>>8)&255);   
46
      buf[2*i+1] = (ir_value&255);   
47
      usb_puts("Distance :\t");
48
      usb_puti(ir_value);
49
      usb_puts("\n");
50
    }
51
    size = 10;
52

  
53
    usb_puts("Packet Contents: ");
54
    for(i = 0; i<size;i++){
55
        usb_puti(buf[i]);
56
        usb_putc(' ');
57
    }
37 58
    wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,IR_TYPE,buf,size,source,0);
59
  
60
  
61
  
38 62
  case ENCODER_TYPE:
39 63
    break; 
40 64
    //Not yet supported.
......
43 67

  
44 68
int generate_bom_packet(char** buf){
45 69
  short int i=0, robot_id; 
46
  char bom_data;
47 70
  wl_token_iterator_begin();
48 71
  while(wl_token_iterator_has_next() && i<=96){
49 72
    robot_id = wl_token_iterator_next();
......
56 79
}
57 80

  
58 81
int generate_IR_packet(char** buf){
59
  *buf[0] = range_read_distance(IR1);   
60
  delay_ms(20);
61
  *buf[1] = range_read_distance(IR2);   
62
  delay_ms(20);
63
  *buf[2] = range_read_distance(IR3);   
64
  delay_ms(20);
65
  *buf[3] = range_read_distance(IR4);   
66
  delay_ms(20);
67
  *buf[4] = range_read_distance(IR5); 
68
  char i;
69
  return 5;
82
  usb_puts("Generating an IR packet.\n");
83
  char ranges[] = {IR1,IR2,IR3,IR4,IR5}; 
84
  int i;
85
  short ir_value; 
86
  for(i=0;i<5;i++){
87
    ir_value = (short)range_read_distance(ranges[i]);
88
    (*buf)[2*i] = (char)((ir_value>>8)&255);   
89
    (*buf)[2*i+1] = (ir_value&255);   
90
    usb_puts("Distance :\t");
91
    usb_puti(ir_value);
92
    usb_puts("\n");
93
  }
94
  return 10;
70 95
}
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-A4001hyO
17
AVRDUDE_PORT = /dev/cu.usbserial-A4001hAr
18
#AVRDUDE_PORT = /dev/cu.usbserial-A4001hAr
18 19
#
19 20
#
20 21
###################################
branches/slam/code/projects/colonet/DataRequests/server/test_main.c
5 5
#include "wireless.h"
6 6
#include "wl_token_ring.h"
7 7
#include "pthread.h"
8
#include "string.h"
8 9

  
9
void IR_handler(unsigned char* data);
10
void bom_handler(BomNode* head);
11
void encoder_handler(unsigned char* data);
10
void IR_handler(short** data);
11
void bom_handler(BomNode** head);
12
void encoder_handler(unsigned char** data);
12 13

  
13 14
unsigned char bom_responded_flag;
14 15
unsigned char ir_responded_flag;
15 16
unsigned char encoder_responded_flag;
16
void* wl_do_routine(void* arg){
17
/*void* wl_do_routine(void* arg){
17 18
  while(1){
18 19
     wl_do();
19 20
  }
20
}
21
}*/
21 22

  
22 23
pthread_t* wl_do_thread;
23 24

  
24 25
int main(void){
25
  wl_set_com_port("/dev/cu.usbserial-A1000Qu6");
26
  char* com_port = malloc(strlen("/dev/cu.usbserial-A4001hAN"));
27
  strcpy(com_port,"/dev/cu.usbserial-A4001hAN");
28
  wl_set_com_port(com_port);
26 29
  wl_init();
30
  wl_set_channel(0xD); 
27 31
  wl_token_ring_register();
28 32
  data_requests_init(bom_handler,IR_handler,encoder_handler);
29
  
30 33
  int robot_id;
31 34
  bom_responded_flag = 0;
32 35
  ir_responded_flag = 0;
33 36
  encoder_responded_flag = 0;
34
  int test_count; 
37
  int test_count,test_count2; 
35 38

  
36
  wl_do_thread = malloc(sizeof(pthread_t));
37
  pthread_create(wl_do_thread,NULL,wl_do_routine,NULL);
39
  //wl_do_thread = malloc(sizeof(pthread_t));
40
  //pthread_create(wl_do_thread,NULL,wl_do_routine,NULL);
38 41

  
39 42
  while(1){
40 43
      printf(".");
41 44
      wl_token_iterator_begin();
42 45
      while(wl_token_iterator_has_next()){
46
        wl_do();
43 47
        robot_id = wl_token_iterator_next();
44 48

  
45 49
        bom_responded_flag = 0;
......
56 60
        test_count=0;
57 61
        while(!(ir_responded_flag)&&(test_count++<20)){
58 62
          printf("Waiting on %i..\n",robot_id);
59
          usleep(5000000);
60
        }
63
          for(test_count2=0;test_count2<1000;test_count2++){
64
            wl_do();
65
            usleep(500);
66
          } //100 ms Total.
67
        } //Waits for 2 seconds total.
68

  
69

  
61 70
/*       
62 71
        request_encoder_data(robot_id);
63 72
        while(!encoder_responded_flag){
......
65 74
        }
66 75
*/
67 76
      }
68
      usleep(5000000);
77
      for(test_count=0;test_count<1000;test_count++){
78
        wl_do();
79
        usleep(5000); //1 Second total.
80
      }
81

  
69 82
  }
70 83
}
71 84

  
72
void IR_handler(unsigned char* data){
85
void IR_handler(short** data){
73 86
  char i = 0;
74 87
  printf("IR Callback occurred.\n");
75 88
  for(i=0;i<5;i++){
76
    printf("IR %i is %i\n",i+1,(int)data[i]);
89
    printf("IR%i is %i\n",i+1,(int)((*data)[i]));
77 90
  }
78 91
  ir_responded_flag=1;
79 92
}
80 93

  
81
void bom_handler(BomNode* head){
94
void bom_handler(BomNode** head){
82 95
  bom_responded_flag=1;
83 96
}
84 97

  
85
void encoder_handler(unsigned char* data){
98
void encoder_handler(unsigned char** data){
86 99
  encoder_responded_flag=1;
87 100
}
88 101

  
branches/slam/code/projects/colonet/DataRequests/server/data_requests.c
10 10

  
11 11
void receive_handle(char type, int source, unsigned char* packet, int length);
12 12

  
13
void (*bom_handle)(BomNode* head);
14
void (*IR_handle)(unsigned char* data);
15
void (*encoder_handle)(unsigned char* data);
13
void (*bom_handle)(BomNode** head);
14
void (*IR_handle)(short** data);
15
void (*encoder_handle)(unsigned char** data);
16 16

  
17
void data_requests_init(void(*bom_data_handler)(BomNode* head),
18
                        void(*IR_data_handler)(unsigned char* data),
19
                        void(*encoder_data_handler)(unsigned char* data)){
17
void data_requests_init(void(*bom_data_handler)(BomNode** head),
18
                        void(*IR_data_handler)(short** data),
19
                        void(*encoder_data_handler)(unsigned char** data)){
20 20
  bom_handle = bom_data_handler;
21 21
  IR_handle = IR_data_handler;
22 22
  encoder_handle = encoder_data_handler;
......
43 43
  wl_send_robot_to_robot_global_packet(DATA_REQUEST_GROUP,ENCODER_TYPE,NULL,0,robot_id,0);
44 44
}
45 45

  
46
void* pthread_handle(void* arg){
46
/*void* pthread_handle(void* arg){
47 47
  RawData* data = (RawData*)arg;
48 48
  unsigned char* packet = data->packet;
49 49
  int length = data->length;
50 50
  char type = data->type;
51
  
52 51
  int i=0;
53 52
  BomNode* head;
54 53
  switch(type){
......
72 71
      break;
73 72
    
74 73
    case IR_TYPE:
75
      /*An IR packet consists of just the 5 bytes representing the IR
76
      values, in order.*/
77
      if(length != 5){
78
          //The packet is incomplete... bail.
79
          break;
74
      An IR packet consists of just the 5 bytes representing the IR
75
      values, in order.
76
      if(length != 10) break;
77
      short data[10];
78
      for(i=0;i<5;i++){
79
        data[i] = ((short)packet[2*i]<<8)+((short)packet[2*i+1]);
80 80
      }
81
      else{
82
        unsigned char data[5];
83
        for(i=0;i<5;i++){
84
          data[i] = packet[i];
81
      IR_handle(data);
82
      break;
83
    
84
    case ENCODER_TYPE:
85
      /*I have no idea, unsupported.
86
      break;
87
  }
88
  free(data->packet);
89
  free(data);
90
  return NULL;
91
  
92
}*/
93

  
94

  
95
void receive_handle(char type, int source, unsigned char* packet, int length){
96
  int i=0;
97
  BomNode* head;
98
  printf("Packet contents: [ ");
99
  for(i=0;i<length;i++){
100
    printf("%i ",packet[i]);
101
  }
102
  switch(type){
103
    
104
    case  BOM_TYPE:
105
      printf("I shouldn't be here during testing\n");
106
      head = malloc(sizeof(BomNode));
107
      BomNode* iter = head;
108

  
109
      while(i<length){
110
        iter->robot_id = ((short)packet[i]<<8) + ((short)packet[i+1]); 
111
        i+=2;
112
        iter->value = packet[i++];
113
        if(i<length){
114
            iter-> next = malloc(sizeof(BomNode));
115
            iter = iter->next;
85 116
        }
86
        IR_handle(data);
87 117
      }
118
      bom_handle(head);
88 119
      break;
89 120
    
121
    case IR_TYPE:
122
      /*An IR packet consists of just the 10 bytes representing the IR
123
      values as shorts, in order.*/
124
      if(length != 10) break;
125
      short* data = malloc(5*sizeof(short));
126
      for(i=0;i<5;i++){
127
        data[i]=  ((((short)packet[2*i])<<8)&0xff00 ) | ( (short)(packet[2*i+1])&255);
128
      }
129

  
130
      IR_handle(&data);
131
      break;
132
    
90 133
    case ENCODER_TYPE:
91 134
      /*I have no idea, unsupported.*/
92 135
      break;
93 136
  }
94
  free(data->packet);
95
  free(data);
96 137
}
97 138

  
98
void receive_handle(char type, int source, unsigned char* packet, int length){
139

  
140
/*void receive_handle(char type, int source, unsigned char* packet, int length){
99 141
  
100 142
  RawData* data = malloc(sizeof(RawData));
101 143
  if(!data){
102 144
    return;
103 145
  }
146
  
104 147
  unsigned char* newPacket = malloc(length);
105 148
  memcpy(newPacket, packet,length);
106 149
 
107

  
108 150
  data->packet=newPacket;
109 151
  data->source=source;
110 152
  data->length=length;
......
112 154
  
113 155
  dr_thread = malloc(sizeof(pthread_t));  
114 156
  pthread_create(dr_thread,NULL,pthread_handle,(void*)data);
115
}
157
}*/
branches/slam/code/projects/colonet/DataRequests/server/data_requests.h
17 17
} RawData;
18 18

  
19 19
void data_requests_init(
20
                        void(*bom_data_handler)(BomNode* head),
21
                        void(*IR_data_handler)(unsigned char* data),
22
                        void(*encoder_data_handler)(unsigned char* data));
20
                        void(*bom_data_handler)(BomNode** head),
21
                        void(*IR_data_handler)(short** data),
22
                        void(*encoder_data_handler)(unsigned char** data));
23 23

  
24 24
void request_bom_data(int robot_id);
25 25
void request_IR_data(int robot_id);

Also available in: Unified diff