Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
robot.c
Go to the documentation of this file.
1 
11  /* -- Includes -- */
12 /* system libraries */
13 #include <math.h>
14 #include <string.h>
15 #include <stdlib.h>
16 /* project libraries */
17 #include "robot.h"
18 #include "general.h"
19 #include "config.h"
20 #include "def.h"
21 
22  /* -- Defines -- */
23 
24 /* -- Functions -- */
25 
38 robot_t *robot_init(int x, int y, int a,
39  int move_noise,
40  float turn_noise,
41  int sense_tag_noise,
42  int sense_wall_noise,
43  int radius)
44 {
45  // Allocate memory for robot structure
46  robot_t *robot = (robot_t *) malloc(sizeof(robot_t));
47 
48  // Set initial pose
49  robot->x = x;
50  robot->y = y;
51  robot->a = a;
52 
53  // Set noise
54  robot->move_noise = move_noise;
55  robot->turn_noise = turn_noise;
56  robot->sense_tag_noise = sense_tag_noise;
57  robot->sense_wall_noise = sense_wall_noise;
58 
59  // Set radius
60  robot->radius = radius;
61 
62  return robot;
63 }
64 
70 void robot_destroy(robot_t *robot)
71 {
72  // Free memory
73  free(robot);
74 }
75 
84 void robot_set_pose(robot_t *robot, int x, int y, int a)
85 {
86  robot->x = x;
87  robot->y = y;
88  robot->a = a;
89 }
90 
100 void robot_set_noise(robot_t *robot, int move_noise, float turn_noise, int sense_tag_noise, int sense_wall_noise)
101 {
102  robot->move_noise = move_noise;
103  robot->turn_noise = turn_noise;
104  robot->sense_tag_noise = sense_tag_noise;
105  robot->sense_wall_noise = sense_wall_noise;
106 }
107 
115 void robot_drive(robot_t *robot, int distance, int angle, int uncertain)
116 {
117  // Update angle
118  if(angle != 0)
119  {
120  robot->a = robot->a - ((angle + general_gaussrand(0, robot->turn_noise)) * M_PI/180);
121  robot->a = fmod(robot->a, M_PI * 2);
122  }
123 
124  // Update position
125  if(distance != 0)
126  {
127  int dist_with_noise = distance + general_gaussrand(0, robot->move_noise);
128  robot->x += cos(robot->a) * dist_with_noise;
129  robot->y += sin(robot->a) * dist_with_noise;
130  }
131 
132  // Robot is uncertain about its action, add just large noise
133  if(uncertain > 0)
134  {
135  robot->a = robot->a - (general_gaussrand(0, s_CONFIG_PF_ANGLE_UNCERTANITY) * M_PI/180);
136  robot->a = fmod(robot->a, M_PI * 2);
137 
138  robot->x += cos(robot->a) * s_CONFIG_PF_DISTANCE_UNCERTANITY;
139  robot->y += sin(robot->a) * s_CONFIG_PF_DISTANCE_UNCERTANITY;
140  }
141 }
142 
150 float robot_sense_tag(robot_t *robot, rfid_t *rfids, enviroment_t *envs)
151 {
152  int i;
153  float dist;
154 
155  // Set robot tag as unknown
156  robot->tag_num = -1;
157 
158  // No Tag is read (just heart beat)
159  if(strcmp(rfids->id, s_CONFIG_RFID_EMPTY_TAG) == 0)
160  {
161  return -1;
162  }
163 
164  // Search for read tag
165  for(i = 0; i < envs->tags_num; i++)
166  {
167  if(strcmp(envs->tags[i].id, rfids->id) == 0)
168  {
169  robot->tag_num = i;
170  break;
171  }
172  }
173 
174  // Calculate distance to the tag
175  dist = sqrt(pow(robot->x - envs->tags[robot->tag_num].x, 2) +
176  pow(robot->y - envs->tags[robot->tag_num].y, 2));
177  dist += general_gaussrand(0, robot->sense_tag_noise);
178 
179  return dist;
180 }
181 
189 {
190  int i;
191  float res = 99999, dist;
192 
193  // Calculate distance to closest wall
194  for(i = 0; i < envs->room_num; i++)
195  {
196  dist = sqrt(general_dist2seg(robot->x,
197  robot->y,
198  envs->room[i].point,
199  envs->room[(i + 1) % envs->room_num].point));
200  if(res > dist) {
201  res = dist;
202  }
203  }
204 
205  return res;
206 }
207 
214 float robot_eval_tag(robot_t *robot, enviroment_t *envs)
215 {
216  float prob = 1;
217  float dist;
218 
219  dist = sqrt(pow(robot->x - envs->tags[robot->tag_num].x, 2) +
220  pow(robot->y - envs->tags[robot->tag_num].y, 2));
221 
222  if( robot->x > envs->room_max_width ||
223  robot->y > envs->room_max_height ||
224  robot->x < 0 ||
225  robot->y < 0)
226  {
227  prob = 0.00001;
228  robot->x = rand() % envs->room_max_width;
229  robot->y = rand() % envs->room_max_height;
230  robot->a = fmod(rand(), M_PI * 2);
231  }
232  else
233  {
234  prob = general_gaussian(dist, robot->sense_tag_noise, rand() % s_CONFIG_RFID_SENSE_RADIUS); // !!!
235  if(prob == 0)
236  prob = 0.00001;
237  }
238 
239  robot->weight = prob;
240 
241  return prob;
242 }
243 
251 {
252  float prob = 1;
253  float dist;
254 
255  dist = robot_sense_wall(robot, envs);
256 
257  if( robot->x > envs->room_max_width ||
258  robot->y > envs->room_max_height ||
259  robot->x < 0 ||
260  robot->y < 0)
261  {
262  prob = 0.00001;
263  robot->x = rand() % envs->room_max_width;
264  robot->y = rand() % envs->room_max_height;
265  robot->a = fmod(rand(), M_PI * 2);
266  }
267  else
268  {
269  if(dist > (robot->radius - 10) && dist < (robot->radius + 10)) {
270  prob = 1;
271  }
272  else
273  {
274  prob = 0.1;
275  }
276  }
277 
278  robot->weight = prob;
279 
280  return prob;
281 }
282 
#define s_CONFIG_RFID_SENSE_RADIUS
Definition: config.h:82
float robot_eval_wall(robot_t *robot, enviroment_t *envs)
Definition: robot.c:250
robot_t * robot_init(int x, int y, int a, int move_noise, float turn_noise, int sense_tag_noise, int sense_wall_noise, int radius)
Definition: robot.c:38
int tag_num
Definition: robot.h:40
float general_dist2seg(int x, int y, int p1[], int p2[])
Definition: general.c:154
char id[11]
Definition: rfid.h:27
float robot_eval_tag(robot_t *robot, enviroment_t *envs)
Definition: robot.c:214
float a
Definition: robot.h:29
double general_gaussrand(double mu, double sigma)
Definition: general.c:91
#define s_CONFIG_PF_DISTANCE_UNCERTANITY
Definition: config.h:100
void robot_set_noise(robot_t *robot, int move_noise, float turn_noise, int sense_tag_noise, int sense_wall_noise)
Definition: robot.c:100
void robot_drive(robot_t *robot, int distance, int angle, int uncertain)
Definition: robot.c:115
#define s_CONFIG_PF_ANGLE_UNCERTANITY
Definition: config.h:101
float robot_sense_wall(robot_t *robot, enviroment_t *envs)
Definition: robot.c:188
#define s_CONFIG_RFID_EMPTY_TAG
Definition: config.h:83
int radius
Definition: robot.h:38
enviroment_tag_t * tags
Definition: enviroment.h:52
float weight
Definition: robot.h:31
void robot_set_pose(robot_t *robot, int x, int y, int a)
Definition: robot.c:84
void robot_destroy(robot_t *robot)
Definition: robot.c:70
Robot structure.
Definition: robot.h:25
RFID structure.
Definition: rfid.h:24
int move_noise
Definition: robot.h:33
int sense_tag_noise
Definition: robot.h:35
float general_gaussian(float mu, float sigma, int x)
Definition: general.c:128
float turn_noise
Definition: robot.h:34
enviroment_room_point_t * room
Definition: enviroment.h:51
int sense_wall_noise
Definition: robot.h:36
float robot_sense_tag(robot_t *robot, rfid_t *rfids, enviroment_t *envs)
Definition: robot.c:150
Enviroment Structure - room points, tags, room point number, tag number.
Definition: enviroment.h:49