Revision 1068
Added replay functionality and command line arguments
-l <filename> log simulation to file [currently make sure file does not
exist, overwriting of files not handled well]
-p <filename> run a log file rather than simulation
-w <filename> load in world file
branches/simulator/projects/simulator/simulator/core/logger.c | ||
---|---|---|
1 |
#include "logger.h" |
|
2 |
#include "robot.h" |
|
3 |
#include <stdio.h> |
|
4 |
|
|
5 |
|
|
6 |
int setLogFile(char *name) |
|
7 |
{ |
|
8 |
file = fopen(name, "a+"); |
|
9 |
if(file == 0) |
|
10 |
return -1; |
|
11 |
|
|
12 |
return 0; |
|
13 |
} |
|
14 |
|
|
15 |
void commit(Robot* robot, int index, int timeStep) |
|
16 |
{ |
|
17 |
if(!file) |
|
18 |
{ |
|
19 |
return; |
|
20 |
} |
|
21 |
if(fwrite(&timeStep, sizeof(int), 1, file) < 1) |
|
22 |
{ |
|
23 |
printf("Logging Error\n"); |
|
24 |
return; |
|
25 |
} |
|
26 |
if(fwrite(&index, sizeof(int), 1, file) < 1) |
|
27 |
{ |
|
28 |
printf("Logging Error\n"); |
|
29 |
return; |
|
30 |
} |
|
31 |
if(fwrite(robot, sizeof(Robot), 1, file) < 1) |
|
32 |
{ |
|
33 |
printf("Logging Error\n"); |
|
34 |
return; |
|
35 |
} |
|
36 |
} |
|
37 |
|
|
38 |
int getStep() |
|
39 |
{ |
|
40 |
int toReturn; |
|
41 |
|
|
42 |
if(!file) |
|
43 |
{ |
|
44 |
return 0; |
|
45 |
} |
|
46 |
|
|
47 |
if(fread(&toReturn, sizeof(int), 1, file) < 1) |
|
48 |
{ |
|
49 |
printf("Logging Error\n"); |
|
50 |
return -1; |
|
51 |
} |
|
52 |
return toReturn; |
|
53 |
} |
|
54 |
|
|
55 |
int getLog(Robot* robot) |
|
56 |
{ |
|
57 |
if(!file) |
|
58 |
{ |
|
59 |
return 0; |
|
60 |
} |
|
61 |
if(fread(robot, sizeof(Robot), 1, file) < 1) |
|
62 |
{ |
|
63 |
printf("Logging Error\n"); |
|
64 |
return -1; |
|
65 |
} |
|
66 |
return 0; |
|
67 |
} |
|
68 |
|
branches/simulator/projects/simulator/simulator/core/player.h | ||
---|---|---|
1 |
#ifndef PLAYER_H |
|
2 |
#define PLAYER_H |
|
3 |
|
|
4 |
int playLog(int argc, char ** argv); |
|
5 |
|
|
6 |
#endif |
branches/simulator/projects/simulator/simulator/core/main.c | ||
---|---|---|
12 | 12 |
#include <gtk/gtk.h> |
13 | 13 |
#include <glib.h> |
14 | 14 |
#include <signal.h> |
15 |
#include <unistd.h> |
|
15 | 16 |
|
16 | 17 |
#include "gtk_gui.h" |
17 | 18 |
#include "robot.h" |
18 | 19 |
#include "world.h" |
20 |
#include "logger.h" |
|
21 |
#include "player.h" |
|
19 | 22 |
|
23 |
|
|
24 |
extern char *optarg; |
|
25 |
extern int optind, optopt; |
|
26 |
|
|
20 | 27 |
int main(int argc, char** argv) |
21 | 28 |
{ |
22 | 29 |
if (robots_initialize()) |
23 | 30 |
return -1; |
24 | 31 |
|
25 |
if(argc<=1){
|
|
32 |
if(argc - optind < 0){
|
|
26 | 33 |
printf("Usage: simulator <robot execetuable>\n"); |
27 | 34 |
exit(-1); |
35 |
} |
|
36 |
|
|
37 |
int c; |
|
38 |
char worldLoadedFlag = 0; |
|
39 |
|
|
40 |
while((c = getopt(argc, argv, ":l:p:w:")) != -1) |
|
41 |
{ |
|
42 |
switch(c) |
|
43 |
{ |
|
44 |
case 'l': |
|
45 |
setLogFile(optarg); |
|
46 |
break; |
|
47 |
case 'p': |
|
48 |
setLogFile(optarg); |
|
49 |
playLog(argc, argv); |
|
50 |
return 0; |
|
51 |
break; |
|
52 |
case 'w': |
|
53 |
load_world(optarg, 100); |
|
54 |
worldLoadedFlag = 1; |
|
55 |
break; |
|
56 |
case ':': |
|
57 |
fprintf(stderr, "Argument requires file name\n"); |
|
58 |
break; |
|
59 |
case '?': |
|
60 |
fprintf(stderr, "Unknown argument\n"); |
|
61 |
break; |
|
62 |
} |
|
28 | 63 |
} |
29 | 64 |
|
30 |
load_world("../test/world.txt", 100); |
|
65 |
if(!worldLoadedFlag) |
|
66 |
load_world("../test/world.txt", 100); |
|
31 | 67 |
|
32 | 68 |
|
33 |
robot_create(argv[1]);
|
|
34 |
robot_create(argv[1]);
|
|
35 |
robot_create(argv[1]);
|
|
36 |
robot_create(argv[1]);
|
|
37 |
robot_create(argv[1]);
|
|
38 |
robot_create(argv[1]);
|
|
39 |
robot_create(argv[1]);
|
|
40 |
robot_create(argv[1]);
|
|
41 |
robot_create(argv[1]);
|
|
42 |
robot_create(argv[1]);
|
|
43 |
robot_create(argv[1]);
|
|
44 |
robot_create(argv[1]);
|
|
45 |
robot_create(argv[1]);
|
|
46 |
robot_create(argv[1]);
|
|
47 |
robot_create(argv[1]);
|
|
48 |
robot_create(argv[1]);
|
|
49 |
robot_create(argv[1]);
|
|
50 |
robot_create(argv[1]);
|
|
51 |
robot_create(argv[1]);
|
|
52 |
robot_create(argv[1]);
|
|
69 |
robot_create(argv[optind]);
|
|
70 |
robot_create(argv[optind]);
|
|
71 |
robot_create(argv[optind]);
|
|
72 |
robot_create(argv[optind]);
|
|
73 |
robot_create(argv[optind]);
|
|
74 |
robot_create(argv[optind]);
|
|
75 |
robot_create(argv[optind]);
|
|
76 |
robot_create(argv[optind]);
|
|
77 |
robot_create(argv[optind]);
|
|
78 |
robot_create(argv[optind]);
|
|
79 |
robot_create(argv[optind]);
|
|
80 |
robot_create(argv[optind]);
|
|
81 |
robot_create(argv[optind]);
|
|
82 |
robot_create(argv[optind]);
|
|
83 |
robot_create(argv[optind]);
|
|
84 |
robot_create(argv[optind]);
|
|
85 |
robot_create(argv[optind]);
|
|
86 |
robot_create(argv[optind]);
|
|
87 |
robot_create(argv[optind]);
|
|
88 |
robot_create(argv[optind]);
|
|
53 | 89 |
|
54 | 90 |
|
55 | 91 |
sigset_t set; |
branches/simulator/projects/simulator/simulator/core/logger.h | ||
---|---|---|
1 |
#ifndef LOGGER_H |
|
2 |
#define LOGGER_H |
|
3 |
|
|
4 |
#include "robot.h" |
|
5 |
#include <stdio.h> |
|
6 |
|
|
7 |
#define CREATE_STEP -2 |
|
8 |
|
|
9 |
FILE* file; |
|
10 |
|
|
11 |
int setLogFile( char *name); |
|
12 |
void commit(Robot* robot,int index, int timeStep); |
|
13 |
|
|
14 |
|
|
15 |
int getStep(); |
|
16 |
int getLog(Robot* robot); |
|
17 |
|
|
18 |
|
|
19 |
#endif |
branches/simulator/projects/simulator/simulator/core/robot.c | ||
---|---|---|
22 | 22 |
#include <errno.h> |
23 | 23 |
#include <pthread.h> |
24 | 24 |
#include <err.h> |
25 |
#include <unistd.h> |
|
25 | 26 |
|
26 | 27 |
#include "gtk_gui.h" |
27 | 28 |
|
... | ... | |
29 | 30 |
#include "motion.h" |
30 | 31 |
#include "rangefinders.h" |
31 | 32 |
#include "world.h" |
33 |
#include "logger.h" |
|
34 |
#include "gtk_gui.h" |
|
32 | 35 |
|
36 |
|
|
33 | 37 |
void sig_chld_handler(int sig); |
34 | 38 |
void robot_update(int i); |
35 | 39 |
|
... | ... | |
40 | 44 |
int robots_size = 0; |
41 | 45 |
int num_robots = 0; |
42 | 46 |
|
43 |
int iterator_pos = 0; |
|
47 |
int iterator_pos = 0;
|
|
44 | 48 |
|
49 |
int timeStep = 0; |
|
50 |
|
|
45 | 51 |
// signalled when all robots have run for a bit |
46 | 52 |
pthread_mutex_t all_finished_mutex; |
47 | 53 |
pthread_cond_t all_finished_cond; |
... | ... | |
74 | 80 |
**/ |
75 | 81 |
int robot_create(char *execname) |
76 | 82 |
{ |
77 |
int pid,i; |
|
83 |
int pid,i;
|
|
78 | 84 |
int id = first_available_id; |
79 | 85 |
Robot* r = &robots[id]; |
80 | 86 |
// do shared memory stuff here |
... | ... | |
111 | 117 |
r->shared->motor2 = 0; |
112 | 118 |
r->id = id; |
113 | 119 |
|
114 |
if((pid = fork()) < 0)
|
|
120 |
if(execname != NULL)
|
|
115 | 121 |
{ |
116 |
//Free Shared Memory Region |
|
117 |
if (!shmdt(r->shared)) |
|
118 |
fprintf(stderr, "Failed to free shared memory.\n"); |
|
119 |
|
|
120 |
if (!shmctl(r->sharedMemID, IPC_RMID, NULL)) |
|
121 |
fprintf(stderr, "Failed to free shared memory.\n"); |
|
122 |
if((pid = fork()) < 0) |
|
123 |
{ |
|
124 |
//Free Shared Memory Region |
|
125 |
if (!shmdt(r->shared)) |
|
126 |
fprintf(stderr, "Failed to free shared memory.\n"); |
|
127 |
|
|
128 |
if (!shmctl(r->sharedMemID, IPC_RMID, NULL)) |
|
129 |
fprintf(stderr, "Failed to free shared memory.\n"); |
|
122 | 130 |
|
123 |
r->id = -1; |
|
124 |
fprintf(stderr, "Failed to fork robot process.\n"); |
|
125 |
return -1; |
|
131 |
r->id = -1; |
|
132 |
fprintf(stderr, "Failed to fork robot process.\n"); |
|
133 |
return -1; |
|
134 |
} |
|
126 | 135 |
} |
127 | 136 |
|
128 | 137 |
|
129 |
if(!pid) |
|
138 |
if(!pid && execname)
|
|
130 | 139 |
{ |
131 | 140 |
char var[21]; |
132 | 141 |
/* restore default sigchld handler */ |
... | ... | |
158 | 167 |
robots[i].id = -1; |
159 | 168 |
|
160 | 169 |
} |
170 |
} |
|
171 |
|
|
172 |
//Log creation |
|
173 |
if(execname != NULL) |
|
174 |
{ |
|
175 |
//printf("commiting %d\n", num_robots); |
|
176 |
commit(r, num_robots, CREATE_STEP); |
|
161 | 177 |
} |
178 |
num_robots++; |
|
162 | 179 |
|
163 |
num_robots++; |
|
164 |
|
|
165 | 180 |
return id; |
166 | 181 |
} |
167 | 182 |
|
... | ... | |
198 | 213 |
if (id < first_available_id) |
199 | 214 |
first_available_id = 0; |
200 | 215 |
|
216 |
|
|
201 | 217 |
return 0; |
202 | 218 |
} |
219 |
|
|
220 |
void robot_destroy_all() |
|
221 |
{ |
|
222 |
int i; |
|
223 |
for(i = 0; i < num_robots; i++) |
|
224 |
robot_destroy(i); |
|
225 |
} |
|
203 | 226 |
|
204 | 227 |
void robot_iterator_reset(void) |
205 | 228 |
{ |
... | ... | |
269 | 292 |
if(ret) |
270 | 293 |
fprintf(stderr, "hmmm, error unlocking. errno: %d", errno); |
271 | 294 |
|
295 |
timeStep++; |
|
272 | 296 |
for (i = 0; i < robots_size; i++) |
273 | 297 |
if (robots[i].id != -1) |
298 |
{ |
|
274 | 299 |
robot_update(i); |
275 | 300 |
|
301 |
printf("commit %d\n", i); |
|
302 |
commit(&robots[i],i,timeStep); |
|
303 |
} |
|
276 | 304 |
for (i = 0; i < robots_size; i++) |
277 | 305 |
{ |
278 | 306 |
if (robots[i].id == -1) |
... | ... | |
286 | 314 |
} |
287 | 315 |
} |
288 | 316 |
|
317 |
void* logger_step_loop(void* arg) |
|
318 |
{ |
|
319 |
printf("going into logger\n"); |
|
320 |
int timeStep; |
|
321 |
int index; |
|
322 |
|
|
323 |
int prevStep = -1; |
|
324 |
|
|
325 |
if(fread(&timeStep, sizeof(int), 1, file) < 1) |
|
326 |
{ |
|
327 |
robot_destroy_all(); |
|
328 |
fprintf(stderr, "problem reading timestep\n"); |
|
329 |
|
|
330 |
return (void*)-1; |
|
331 |
} |
|
332 |
|
|
333 |
while(1){ |
|
334 |
prevStep = timeStep; |
|
335 |
do |
|
336 |
{ |
|
337 |
if(fread(&index, sizeof(int), 1, file) < 1) |
|
338 |
{ |
|
339 |
robot_destroy_all(); |
|
340 |
return (void*) -1; |
|
341 |
} |
|
342 |
|
|
343 |
//Update the robot |
|
344 |
if(timeStep == CREATE_STEP) |
|
345 |
{ |
|
346 |
robot_create(NULL); |
|
347 |
} |
|
348 |
|
|
349 |
|
|
350 |
if(getLog(&robots[index]) < 0) |
|
351 |
{ |
|
352 |
robot_destroy_all(); |
|
353 |
return (void*) -1; |
|
354 |
} |
|
355 |
|
|
356 |
//Read in next logfile and check number |
|
357 |
if(fread(&timeStep, sizeof(int), 1, file) < 1) |
|
358 |
{ |
|
359 |
robot_destroy_all(); |
|
360 |
return (void*)-1; |
|
361 |
} |
|
362 |
|
|
363 |
if (prevStep < 0) |
|
364 |
prevStep = timeStep; |
|
365 |
}while(timeStep == prevStep || timeStep == CREATE_STEP); |
|
366 |
|
|
367 |
|
|
368 |
gui_refresh(); |
|
369 |
|
|
370 |
usleep(100); |
|
371 |
} |
|
372 |
|
|
373 |
} |
|
374 |
|
|
289 | 375 |
void robot_update(int i) |
290 | 376 |
{ |
291 | 377 |
Robot* r = &(robots[i]); |
branches/simulator/projects/simulator/simulator/core/robot.h | ||
---|---|---|
39 | 39 |
Robot* robot_iterator_next(void); |
40 | 40 |
|
41 | 41 |
void* robot_event_loop(void* arg); |
42 |
void* logger_step_loop(void* arg); |
|
42 | 43 |
|
43 | 44 |
#endif |
44 | 45 |
|
branches/simulator/projects/simulator/simulator/core/player.c | ||
---|---|---|
1 |
#include <stdlib.h> |
|
2 |
#include <stdio.h> |
|
3 |
#include <gtk/gtk.h> |
|
4 |
#include <glib.h> |
|
5 |
|
|
6 |
#include "gtk_gui.h" |
|
7 |
#include "robot.h" |
|
8 |
#include "world.h" |
|
9 |
#include "logger.h" |
|
10 |
|
|
11 |
int playLog(int argc, char** argv) |
|
12 |
{ |
|
13 |
load_world("../test/world.txt", 100); |
|
14 |
|
|
15 |
g_thread_init(NULL); |
|
16 |
gdk_threads_init(); |
|
17 |
printf("g thread create\n"); |
|
18 |
g_thread_create(logger_step_loop, NULL, TRUE, NULL); |
|
19 |
printf("run\n"); |
|
20 |
gtk_gui_run(argc, argv); |
|
21 |
|
|
22 |
return 0; |
|
23 |
} |
|
24 |
|
|
25 |
|
branches/simulator/projects/simulator/simulator/Makefile | ||
---|---|---|
1 | 1 |
# Add new files here |
2 | 2 |
COMMON_SRCS := |
3 |
CORE_SRCS := main.c robot.c motion.c world.c rangefinders.c |
|
3 |
CORE_SRCS := main.c robot.c motion.c world.c logger.c player.c rangefinders.c |
|
4 |
|
|
4 | 5 |
GUI_SRCS := gtk_gui.c gtk_environment_view.c |
5 | 6 |
|
6 | 7 |
CORE_DIR := core |
Also available in: Unified diff