Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
enviroment.c
Go to the documentation of this file.
1 
11  /* -- Includes -- */
12 /* system libraries */
13 #include <stdio.h> /* Standard input/output definitions */
14 #include <string.h> /* String function definitions */
15 #include <stdlib.h>
16 /* project libraries */
17 #include "enviroment.h"
18 #include "file.h"
19 #include "config.h"
20 
21 /* -- Defines -- */
22 
23 /* -- Functions -- */
24 
31 enviroment_t *enviroment_load(char *room_def_path, char *tags_def_path)
32 {
33  // Init Enviroment
34  enviroment_t *env = (enviroment_t *) malloc(sizeof(enviroment_t));
35  // Load room points
36  env->room_num = enviroment_room_load(&env->room, room_def_path);
37  // Load tags
38  env->tags_num = enviroment_tags_load(&env->tags, tags_def_path);
39  // Calculate dimensions
41 
42  return env;
43 }
44 
51 {
52  // Room definition
53  free(env->room);
54  // Tag definition
55  free(env->tags);
56  // Enviroment definition
57  free(env);
58 }
59 
66 int enviroment_room_load(enviroment_room_point_t **room, const char *filename)
67 {
68  char line[128];
69  char *end_ptr, *pch;
70  int chars_read = 0, i = 0, num = 0;
71  file_t *fs;
72 
73  // One file
74  fs = file_open(filename, s_FILE_MODE_READ);
75 
76  // Read till End Of File
77  while(chars_read != EOF)
78  {
79  // Read line
80  chars_read = file_readln(fs, line, sizeof(line));
81  // If line is empty or commented (first char is hash ('#') sign) then continue to next line
82  if(chars_read == 0 || chars_read == EOF || line[0] == '#')
83  {
84  continue;
85  }
86  else
87  {
88  // First number in file specifies number of points
89  if(num == 0)
90  {
91  // Save number of points as a integer
92  num = strtol(line, &end_ptr, 10);
93 
94  // Allocate memory
95  (*room) = malloc(num * sizeof(enviroment_room_point_t));
96  }
97  else
98  {
99  // Save each point in struct
100  pch = strtok(line, " ,");
101  (*room)[i].x = strtol(pch, &end_ptr, 10);
102  pch = strtok(NULL, " ,");
103  (*room)[i].y = strtol(pch, &end_ptr, 10);
104 
105  i++;
106  }
107  }
108  }
109 
110  // Close file
111  file_close(fs);
112 
113  // Return number of points
114  return num;
115 }
116 
123 int enviroment_tags_load(enviroment_tag_t **tags, const char *filename)
124 {
125 
126  char line[128];
127  char *end_ptr, *pch;
128  int chars_read = 0, i = 0, num = 0;
129  file_t *fs;
130 
131  // One file
132  fs = file_open(filename, s_FILE_MODE_READ);
133 
134  // Read till End Of File
135  while(chars_read != EOF)
136  {
137  // Read line
138  chars_read = file_readln(fs, line, sizeof(line));
139  // If line is empty or commented (first char is hash ('#') sign) then continue to next line
140  if(chars_read == 0 || chars_read == EOF || line[0] == '#')
141  {
142  continue;
143  }
144  else
145  {
146  // First number in file specifies number of points
147  if(num == 0)
148  {
149  // Save number of points as a integer
150  num = strtol(line, &end_ptr, 10);
151 
152  // Allocate memory
153  (*tags) = malloc(num * sizeof(enviroment_tag_t));
154  }
155  else
156  {
157  // Save each point in struct
158  // X
159  pch = strtok(line, " ,");
160  (*tags)[i].x = strtol(pch, &end_ptr, 10);
161  // Y
162  pch = strtok(NULL, " ,");
163  (*tags)[i].y = strtol(pch, &end_ptr, 10);
164  // ID
165  pch = strtok(NULL, " ,");
166  memcpy((*tags)[i].id, pch, 10);
167  (*tags)[i].id[10] = '\0';
168  // Enable
169  pch = strtok(line, " ,");
170  (*tags)[i].enable = strtol(pch, &end_ptr, 10);
171 
172  // Increase counter
173  i++;
174  }
175  }
176  }
177 
178  // Close file
179  file_close(fs);
180 
181  // Return number of points
182  return num;
183 }
184 
191 {
192  unsigned int i, max_width = 0, max_height = 0;
193 
194  // Find room size
195  for(i = 0; i < env->room_num; i++)
196  {
197  // Width
198  if(max_width < env->room[i].x)
199  {
200  max_width = env->room[i].x;
201  }
202  // Height
203  if(max_height < env->room[i].y)
204  {
205  max_height = env->room[i].y;
206  }
207  }
208 
209  // Set room dimensions
210  env->room_max_width = max_width;
211  env->room_max_height = max_height;
212 }
213 
219 int enviroment_tag_check(enviroment_t *env, char tag_id[11])
220 {
221  int i;
222 
223  // No Tag is read (just heart beat)
224  if(strcmp(tag_id, s_CONFIG_RFID_EMPTY_TAG) == 0)
225  {
226  return s_ENVIROMENT_TAG_ZEROS;
227  }
228 
229  // Search for read tag
230  for(i = 0; i < env->tags_num; i++)
231  {
232  if(strcmp(env->tags[i].id, tag_id) == 0)
233  {
234  // Check if tag is enabled
235  if(env->tags[i].enable > 0)
236  {
237  return i;
238  }
239  else
240  {
242  }
243  }
244  }
245 
247 }
unsigned char enable
Definition: enviroment.h:42
int enviroment_tags_load(enviroment_tag_t **tags, const char *filename)
Definition: enviroment.c:123
Room point definition - x, y.
Definition: enviroment.h:22
int file_readln(file_t *fs, char *buffer, size_t buflen)
Definition: file.c:145
file_t * file_open(const char *name, const char *mode)
Definition: file.c:31
void enviroment_calc_room_dimensions(enviroment_t *env)
Definition: enviroment.c:190
int enviroment_room_load(enviroment_room_point_t **room, const char *filename)
Definition: enviroment.c:66
#define s_FILE_MODE_READ
Definition: file.h:31
int file_close(file_t *fs)
Definition: file.c:82
File structure.
Definition: file.h:23
int enviroment_tag_check(enviroment_t *env, char tag_id[11])
Definition: enviroment.c:219
#define s_CONFIG_RFID_EMPTY_TAG
Definition: config.h:83
enviroment_tag_t * tags
Definition: enviroment.h:52
#define s_ENVIROMENT_TAG_UNKNOWN
Definition: enviroment.h:64
void enviroment_destroy(enviroment_t *env)
Definition: enviroment.c:50
enviroment_t * enviroment_load(char *room_def_path, char *tags_def_path)
Definition: enviroment.c:31
#define s_ENVIROMENT_TAG_ZEROS
Definition: enviroment.h:63
Tag definition - x, y, id.
Definition: enviroment.h:37
enviroment_room_point_t * room
Definition: enviroment.h:51
#define s_ENVIROMENT_TAG_DISABLED
Definition: enviroment.h:65
Enviroment Structure - room points, tags, room point number, tag number.
Definition: enviroment.h:49