Project

General

Profile

Revision 394

Cleaned up to begin anew with the new data response code.

View differences:

branches/slam/code/projects/slam/robot/monitor_test.lst
1
   1               		.file	"monitor_test.c"
2
   2               		.arch atmega128
3
   3               	__SREG__ = 0x3f
4
   4               	__SP_H__ = 0x3e
5
   5               	__SP_L__ = 0x3d
6
   6               	__tmp_reg__ = 0
7
   7               	__zero_reg__ = 1
8
   8               		.global __do_copy_data
9
   9               		.global __do_clear_bss
10
  10               		.text
11
  11               	.global	main
12
  13               	main:
13
  14               	/* prologue: frame size=0 */
14
  15 0000 C0E0      		ldi r28,lo8(__stack - 0)
15
  16 0002 D0E0      		ldi r29,hi8(__stack - 0)
16
  17 0004 DEBF      		out __SP_H__,r29
17
  18 0006 CDBF      		out __SP_L__,r28
18
  19               	/* prologue end (size=4) */
19
  20               	/* epilogue: frame size=0 */
20
  21 0008 0C94 0000 		jmp exit
21
  22               	/* epilogue end (size=2) */
22
  23               	/* function main size 6 (0) */
23
  25               	/* File "monitor_test.c": code    6 = 0x0006 (   0), prologues   4, epilogues   2 */
24
DEFINED SYMBOLS
25
                            *ABS*:00000000 monitor_test.c
26
/var/tmp//cc3WAcUk.s:3      *ABS*:0000003f __SREG__
27
/var/tmp//cc3WAcUk.s:4      *ABS*:0000003e __SP_H__
28
/var/tmp//cc3WAcUk.s:5      *ABS*:0000003d __SP_L__
29
/var/tmp//cc3WAcUk.s:6      *ABS*:00000000 __tmp_reg__
30
/var/tmp//cc3WAcUk.s:7      *ABS*:00000001 __zero_reg__
31
/var/tmp//cc3WAcUk.s:13     .text:00000000 main
32

  
33
UNDEFINED SYMBOLS
34
__do_copy_data
35
__do_clear_bss
36
__stack
37
exit
branches/slam/code/projects/slam/robot/robot_main.c
1
#include "dragonfly_lib.h"
2
#include "../../libwireless/lib/wireless.h"
3
#include "../../libwireless/lib/wl_token_ring.h"
4
#include "smart_run_around_fsm.h"
5
#include "../slam_defs.h"
6

  
7
#define BOM_POST_DELAY 0
8
#define PACKET_BUFFER_SIZE 101 
9

  
10
//Packet Handler Functions
11
void receive_handle(char type, int source, unsigned char* packet, int length);
12
void unregister(void);
13

  
14
int make_request_packet(char** packet);
15
void send_response_packet(int dest);
16

  
17
unsigned int response_flag = 0; //Will represent the robot to respond to or 0 for no robot.
18
char control_flag=0;             //Non zero when the robot is to be controlled.
19
int IR[5];
20

  
21
int main(void)
22
{
23

  
24
  dragonfly_init(ALL_ON);
25
  
26
  usb_init();
27
  usb_puts("USB initialized\n\n\r");
28
  
29
  wl_init();
30
 
31
  wl_token_ring_register();
32
  wl_token_ring_join();
33

  
34
  PacketGroupHandler packet_group_handler;
35
  packet_group_handler.groupCode=SLAM_PACKET_GROUP;
36
  packet_group_handler.timeout_handler=NULL;
37
  packet_group_handler.handle_response=NULL;
38
  packet_group_handler.handle_receive=receive_handle;
39
  packet_group_handler.unregister=unregister;
40

  
41
  wl_register_packet_group(&packet_group_handler);
42
  
43
  int i;
44
  while(1){
45
    wl_do();
46
    //Uncomment this if you want the robot to do something. 
47
    //run_around_FSM(); 
48
    
49
    for(i=0;i<5;i++){
50
      IR[i]=range_read_distance(i);
51
    }
52
    if(response_flag){
53
      send_response_packet(response_flag);
54
      response_flag=0;
55
    }
56
    if(control_flag){
57
      //FILL IN HERE
58
    }
59

  
60
  }
61

  
62
  wl_unregister_packet_group(&packet_group_handler); 
63
}
64

  
65
void send_response_packet(int dest){
66
    
67
    usb_puts("Sending a response packet to ");
68
    usb_puti(dest);
69
    usb_puts(".\n\r");
70
    
71
    char buf[PACKET_BUFFER_SIZE]; 
72
    int len = make_request_packet((char**)&buf), i;
73
    char packet[len];
74
    for(i=0; i< len ; i++){
75
      packet[i]=buf[i];
76
    } 
77
    
78
    wl_send_robot_to_robot_packet(SLAM_PACKET_GROUP, ROBOT_RESPONSE, packet, len, dest, 0);
79
}
80

  
81
void receive_handle(char type, int source, unsigned char* packet, int length){
82
  if(type==REQUEST_PACKET_TYPE){
83
    usb_puts("Received a request packet\n\r");
84
    response_flag = source;
85
  }
86
  //Not yet implemented:
87
  else if(type == CONTROL_PACKET_TYPE){
88
    control_flag = 1;
89
    //Put packet data into some global.
90
  }
91
}
92

  
93

  
94
/**
95

  
96
Packets are structured {IR1, IR2, IR3, IR4, IR5,
97
BomID1, ... , BomValue1, BomID2, ... , BomValue2, etc.} 
98
Total size is 5 + 3*numRobots. So Xbee should support
99
up to 31 robots before we need to restructure the 
100
communication between the computer and the robot.
101

  
102
*/
103
int make_request_packet(char** packet){
104
  int i;
105
  for(i=0;i<5;i++){
106
      (*packet)[i] = IR[i];
107
  } 
108
  i=5; 
109
  wl_token_iterator_begin();
110
  int id = wl_get_xbee_id();  
111
  
112
  while(wl_token_iterator_has_next()){
113
    int robot_in_view = wl_token_iterator_next(); 
114
    
115
    //Robot int's are two bytes. 
116
    (*packet)[i] = robot_in_view; 
117
    i+=2;
118
    
119
    int bom_number = wl_token_get_sensor_reading(id,robot_in_view);
120
    //Bom reading of 255 denotes an unseen node. 
121
    if(bom_number<0) bom_number = 255; 
122
    bom_number %= 256;
123
    char bom_value = (char) bom_number;
124
    (*packet)[i] = bom_value;
125
    i++;
126
  }
127
  //Size is one bom byte towards every robot, and 5 IR bytes. 
128
  return wl_token_get_num_robots()+5;
129
}
130

  
131
void unregister(void){
132
  return;
133
}
branches/slam/code/projects/slam/computer/slam_analyzer.c
1
#include "slam_analyzer.h"
2
#include <stdio.h>
3
#include <stdlib.h>
4

  
5
void slam_analyze(RobotData* head){
6
  printf("Analyzing..\n");
7
}
8

  
9
void slam_print_data(RobotData* head){
10
  if(head==NULL) printf("No data to display.\n");
11
  RobotData* iter = head;
12
  BomNode* bomIter;
13
  char i;
14
  while(iter!= NULL){
15
    bomIter = iter->bom_head;
16
    printf("Robot %x:\n",iter->id);
17
    for(i=0;i<5;i++)
18
      printf("\tSharp IR %i : %i\n\n",i,iter->IR[i]);
19
    if(BomIter == NULL){
20
        printf("No Bom Data to Display\n\n\n");
21
    }
22
    else{
23
      while(bomIter!=NULL){
24
        printf("\tBom reading for robot %x : %i",BomIter->id, BomIter->value);
25
        bomIter = bomIter->next;
26
      }
27
    }
28
  }
29
}
branches/slam/code/projects/slam/computer/slam_analyzer.h
1
#include "../slam_defs.h"
2
void slam_analyze(RobotData* head);
3
void slam_print_data(RobotData* head);
branches/slam/code/projects/slam/computer/computer_main.c
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <unistd.h>
4
#include <pthread.h>
5
#include "../../libwireless/lib/wireless.h"
6
#include "../../libwireless/lib/wl_token_ring.h"
7
#include "slam_analyzer.h"
8

  
9
//Currently request information 2 times a second.
10
#define SLAM_REQUEST_DELAY 500000
11

  
12
void handle_receive(char type, int source, unsigned char* packet, int length);
13
void unregister(void);
14

  
15
void slam_request_thread(void*);
16

  
17
RobotData* getRobot(int id);
18

  
19
RobotData* head_bot;
20
RobotData* current_bot;
21

  
22
int main(void)
23
{
24
  PacketGroupHandler* pgh = malloc(sizeof(PacketGroupHandler)); 
25
  pgh->groupCode = SLAM_PACKET_GROUP;
26
  pgh->timeout_handler = NULL;
27
  pgh->handle_response = NULL;
28
  pgh->handle_receive = handle_receive;
29
  pgh->unregister = unregister;
30

  
31
  
32
  wl_init();
33
  wl_register_packet_group(pgh);
34
  wl_token_ring_register();
35
  
36
  //wl_token_ring_join();
37

  
38

  
39
  printf("Computer SLAM initialized\n\n");
40
 
41
  //Run the requesting thread simultaneously.
42
  pthread_t request_thread;
43
  pthread_create(&request_thread,NULL,slam_request_thread,NULL);
44
  while(1){
45
      wl_do(); //Receive packets (responses to Request packets.)
46
  }
47
	return 0;
48
}
49

  
50
//Blocking thread.
51
void slam_request_thread(void* asdf){
52
    
53
    //int count = 0;
54
    
55
    while(1){
56
      
57
      wl_token_iterator_begin();
58
      
59
      while(wl_token_iterator_has_next()){
60
        int next = wl_token_iterator_next();
61
        printf("Sending a request packet to %i\n",next);
62
        wl_send_robot_to_robot_packet(SLAM_PACKET_GROUP,REQUEST_PACKET_TYPE,
63
          NULL,0,next,0);  
64
      }
65
      usleep(50000);
66
    }
67
    usleep(50000);
68
}
69

  
70
void handle_receive(char type, int source, unsigned char* packet, int length){
71
  printf("Received a packet\n");
72
  if(type == ROBOT_RESPONSE){
73
    int i=0; 
74
    current_bot = getRobot(source);
75
    if(current_bot == NULL){
76
      //Add the new robot to the front of the list.
77
      current_bot = malloc(sizeof(RobotData));
78
      current_bot->next = head_bot; //Works even if head_bot is null.
79
      head_bot = current_bot;
80
    }
81
    for(i=0;i<5;i++){
82
      current_bot->IR[i] = packet[i];
83
      printf("*");
84
    }
85
    i++; //i=5 
86
   
87
    //Bom Handling.
88
    //Clear the current information.
89
    BomNode* head = current_bot->bom_head;
90
    if(head!=NULL){
91
      BomNode* next = head->next;
92
      while(next!=NULL){
93
        free(head);
94
        head = next;
95
        next = head->next;
96
      }
97
      free(head);
98
    }
99
    //All of the memory allocated for the BomList is free.
100

  
101
    //The remainder of the packet is bom data. 
102
    BomNode* bom_node;
103
    current_bot->bom_head = bom_node;
104
    //i still points to the first bom information spot at 5. 
105
    while(i<length){
106
      printf("*"); 
107
      bom_node = malloc(sizeof(BomNode));
108
      bom_node->id = ((short int)((short)packet[i++]<<8) + packet[i++]);
109
      if(i>=length){
110
        printf("Packet not constructed correctly. Terminate. \n");
111
        exit(0);
112
      }
113
      bom_node->value = packet[i++];
114
      if(i>=length){
115
        printf("Packet not constructed correctly. Terminate. \n");
116
        exit(0);
117
      }
118
      bom_node = bom_node->next;
119
    }
120
  }
121
}
122

  
123
RobotData* getRobot(int id){
124
  RobotData* next = head_bot; 
125
  while(next->id != id && next->next!=NULL);
126
  
127
  if(next->id != id) return NULL;
128
  else return next;
129
}
130

  
131
void unregister(void){
132
  return;
133
}

Also available in: Unified diff