Project

General

Profile

Revision 1072

Switched to loop based suspending on the robots using simulator_do

now runs much better for the stupid template sample

View differences:

branches/simulator/lib/include/libdragonfly/dragonfly_lib.h
1 1
/**
2 2
 * Copyright (c) 2007 Colony Project
3
 * 
3
 *
4 4
 * Permission is hereby granted, free of charge, to any person
5 5
 * obtaining a copy of this software and associated documentation
6 6
 * files (the "Software"), to deal in the Software without
......
9 9
 * copies of the Software, and to permit persons to whom the
10 10
 * Software is furnished to do so, subject to the following
11 11
 * conditions:
12
 * 
12
 *
13 13
 * The above copyright notice and this permission notice shall be
14 14
 * included in all copies or substantial portions of the Software.
15
 * 
15
 *
16 16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
......
27 27
/**
28 28
 * @file dragonfly_lib.h
29 29
 * @brief Contains other include files
30
 * 
30
 *
31 31
 * Include this file for all the functionality of libdragonfly.
32 32
 *
33 33
 * @author Colony Project, CMU Robotics Club
......
74 74
 *
75 75
 * 	@param config The subsystems that you wish to turn on. Check dragonfly_lib.h for
76 76
 * 			valid values.
77
 * 
77
 *
78 78
 * @see analog_init, usb_init, xbee_init, buzzer_init,
79 79
 * bom_init, orb_init, motors_init, lcd_init
80 80
 **/
81 81
void dragonfly_init(int config);
82 82

  
83
/**
84
 * @brief runs the simulator loop, if runninf in the simulator
85
 *
86
 * When compiled for the simulator, this call updates the simulator by
87
 * suspending the robot process, which allows the simulator core to
88
 * update the world state.
89
 *
90
 * This function should be called at the end of the main program loop.
91
 *
92
 * When compiled on the robot, this call does nothing
93
 **/
94
void simulator_do();
95

  
83 96
/** @} **/ //end addtogroup
84 97

  
85 98
#include <analog.h>
branches/simulator/projects/simulator/libsim/libsim.c
8 8
#include <sys/ipc.h>
9 9
#include <robot_shared.h>
10 10
#include <string.h>
11
#include <dragonfly_lib.h>
11 12

  
12
#define TICK_USEC 100
13

  
14 13
RobotShared* shared_state;
15 14

  
16
void tick(int sig)
17
{
18
  if(raise(SIGTSTP)<0)
19
    printf("could not kill self!\n");
20
}
21

  
22

  
23 15
void dragonfly_init(int config)
24 16
{
25 17
  struct itimerval iv;
......
33 25
    return;
34 26
  }
35 27

  
36
                                             
37
  //printf("hello. I am a robot w/ memory_id %s\n", getenv("memory_id"));
38 28

  
39

  
40
  iv.it_interval.tv_sec = 0;
41
  iv.it_interval.tv_usec = TICK_USEC;
42
  iv.it_value.tv_sec = 0;
43
  iv.it_value.tv_usec = TICK_USEC;
44

  
45
  signal(SIGVTALRM, tick);
46

  
47
  ret = setitimer(ITIMER_VIRTUAL, &iv, NULL);
48

  
49 29
  //printf("setitimer returned %d.\n waiting...\n", ret);
50 30
  fflush(stdout);
51 31

  
......
53 33

  
54 34
}
55 35

  
36
void simulator_do()
37
{
38
     if(raise(SIGTSTP)<0)
39
          fprintf(stderr, "ERROR: could not kill self\n");
40

  
41
}
branches/simulator/projects/simulator/simulator/core/robot.c
44 44
int robots_size = 0;
45 45
int num_robots = 0;
46 46

  
47
int iterator_pos = 0;          
47
int iterator_pos = 0;
48 48

  
49 49
int timeStep = 0;
50 50

  
......
66 66
	memset(robots, 0, sizeof(Robot) * robots_size);
67 67
	for (i = 0; i < robots_size; i++)
68 68
		robots[i].id = -1;
69
	
69

  
70 70
	finished = 0;
71 71
	pthread_mutex_init(&all_finished_mutex, NULL);
72 72
	pthread_cond_init(&all_finished_cond, NULL);
......
87 87
	key_t key = IPC_PRIVATE;
88 88
	//Memory accessible only to children with r/w privileges
89 89
	r->sharedMemID = shmget(key, sizeof(RobotShared), IPC_CREAT | 0666);
90
	
91 90

  
91

  
92 92
	//hack!! TODO: remove!!
93 93
	r->pose.x=first_available_id*100;
94 94
	r->pose.y=first_available_id*50;
......
101 101
	}
102 102

  
103 103
	r->shared = (RobotShared*)shmat(r->sharedMemID, NULL, 0);
104
	
104

  
105 105
	if(!(r->shared))
106
	{	
106
	{
107 107
		//Free shared memory region
108 108
		if (!shmctl(r->sharedMemID, IPC_RMID, NULL))
109 109
			fprintf(stderr, "Failed to free shared memory.\n");
110
		       
110

  
111 111
		fprintf(stderr, "Error attaching memory to parent.\n");
112 112
		return -1;
113 113
	}
114
	
114

  
115 115
	// Initialize robot structure here
116 116
	r->shared->motor1 = 0;
117 117
	r->shared->motor2 = 0;
......
124 124
			//Free Shared Memory Region
125 125
			if (!shmdt(r->shared))
126 126
				fprintf(stderr, "Failed to free shared memory.\n");
127
			
127

  
128 128
			if (!shmctl(r->sharedMemID, IPC_RMID, NULL))
129 129
				fprintf(stderr, "Failed to free shared memory.\n");
130 130

  
......
133 133
			return -1;
134 134
		}
135 135
	}
136
	
137 136

  
137

  
138 138
	if(!pid && execname)
139 139
	{
140 140
	  char var[21];
......
167 167
			  robots[i].id = -1;
168 168

  
169 169
		}
170
	}      
170
	}
171 171

  
172 172
	//Log creation
173 173
	if(execname != NULL)
......
212 212

  
213 213
	if (id < first_available_id)
214 214
		first_available_id = 0;
215
	
216
	
215

  
216

  
217 217
	return 0;
218 218
}
219
         
219

  
220 220
void robot_destroy_all()
221 221
{
222 222
	int i;
......
236 236
	if (iterator_pos >= robots_size)
237 237
		return NULL;
238 238
	do iterator_pos++;
239
	while (iterator_pos < robots_size && 
239
	while (iterator_pos < robots_size &&
240 240
			robots[iterator_pos].id == -1);
241
	
241

  
242 242
	if (iterator_pos >= robots_size)
243 243
		return NULL;
244
	
244

  
245 245
	return &(robots[iterator_pos]);
246 246
}
247 247

  
......
288 288
		}
289 289
		finished = 0;
290 290

  
291
    //sleep to avoid 100% CPU usage
292
    usleep(10000);
293

  
291 294
		ret = pthread_mutex_unlock(&all_finished_mutex);
292 295
		if(ret)
293 296
		  fprintf(stderr, "hmmm, error unlocking. errno: %d", errno);
......
299 302
				robot_update(i);
300 303

  
301 304
				printf("commit %d\n", i);
302
				commit(&robots[i],i,timeStep); 
305
				commit(&robots[i],i,timeStep);
303 306
			}
304 307
		for (i = 0; i < robots_size; i++)
305 308
		{
......
321 324
	int index;
322 325

  
323 326
	int prevStep = -1;
324
     
327

  
325 328
	if(fread(&timeStep, sizeof(int), 1, file) < 1)
326
	{   
329
	{
327 330
		robot_destroy_all();
328 331
		fprintf(stderr, "problem reading timestep\n");
329
	   
332

  
330 333
	    return (void*)-1;
331 334
	}
332
		
333
	 while(1){   
335

  
336
	 while(1){
334 337
		prevStep = timeStep;
335 338
		do
336 339
	   	{
......
339 342
				robot_destroy_all();
340 343
			 	return (void*) -1;
341 344
			}
342
			
345

  
343 346
            //Update the robot
344 347
			if(timeStep == CREATE_STEP)
345 348
			{
346
			    robot_create(NULL);	
349
			    robot_create(NULL);
347 350
			}
348 351

  
349
			
352

  
350 353
			if(getLog(&robots[index]) < 0)
351 354
			{
352 355
				robot_destroy_all();
......
359 362
				robot_destroy_all();
360 363
				return (void*)-1;
361 364
			}
362
		   	
365

  
363 366
           	if (prevStep < 0)
364 367
			   	prevStep = timeStep;
365 368
	   	}while(timeStep == prevStep || timeStep == CREATE_STEP);
366
		
367
		
369

  
370

  
368 371
		gui_refresh();
369
		
372

  
370 373
		usleep(100);
371 374
	}
372
	
375

  
373 376
}
374 377

  
375 378
void robot_update(int i)
branches/simulator/projects/template/main.c
6 6
	bom_leds_on(BOM_ALL);
7 7
	motor1_set(1, 200);
8 8
	motor2_set(1, 150);
9
	while(1){}
9
	while(1){
10
       simulator_do();
11
  }
10 12
	return 0;
11 13
}
12 14

  
branches/simulator/projects/libdragonfly/dragonfly_lib.c
1 1
/**
2 2
 * Copyright (c) 2007 Colony Project
3
 * 
3
 *
4 4
 * Permission is hereby granted, free of charge, to any person
5 5
 * obtaining a copy of this software and associated documentation
6 6
 * files (the "Software"), to deal in the Software without
......
9 9
 * copies of the Software, and to permit persons to whom the
10 10
 * Software is furnished to do so, subject to the following
11 11
 * conditions:
12
 * 
12
 *
13 13
 * The above copyright notice and this permission notice shall be
14 14
 * included in all copies or substantial portions of the Software.
15
 * 
15
 *
16 16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
......
46 46
 * init_dragonfly (ALL_ON); - Turns on all basic subsystems. More complex ones that require
47 47
 * 		their own configuration parameters must still be called on their own.
48 48
 *
49
 * init_dragonfly (ANALOG | SERIAL | BUZZER); 
49
 * init_dragonfly (ANALOG | SERIAL | BUZZER);
50 50
 * Initialize ADC, USB, XBee, and Buzzer
51
 * 
51
 *
52 52
 * init_dragonfly (MOTORS | ORB);
53
 * Initialize the motors and ORB. 
53
 * Initialize the motors and ORB.
54 54
 **/
55 55

  
56 56

  
......
63 63
	//Pin G1 is button 2
64 64
	DDRG &= ~(_BV(PING0)|_BV(PING1));
65 65
  PORTG |= _BV(PING0)|_BV(PING1);
66
	
66

  
67 67
  if(config & ANALOG)
68 68
    analog_init(ADC_START);
69
	
69

  
70 70
  if(config & COMM) {
71 71
    //Both default to 115200. Check serial.h for more information.
72 72
    usb_init();
73 73
    xbee_init();
74 74
  }
75
	
75

  
76 76
  if(config & BUZZER) {
77 77
    buzzer_init();
78 78
  }
79
	
79

  
80 80
  if(config & ORB) {
81 81
    orb_init();
82 82
  }
83
	
83

  
84 84
  if(config & MOTORS)
85 85
    motors_init();
86 86

  
87 87
  if(config & LCD)
88 88
    lcd_init();
89
   
89

  
90 90
  // delay a bit for stability
91 91
  _delay_ms(1);
92 92
}
93 93

  
94

  
95
void simulator_do() {
96
  return;
97
}

Also available in: Unified diff