Project

General

Profile

Revision 988

Refactored simulator, set some stuff up to work with multiple robots.

View differences:

branches/simulator/projects/simulator/simulator/core/main.c
1
#include "gtk_gui.h"
1
/**
2
 * @file main.c
3
 * @author Colony Project
4
 * @brief Initializes the program.
5
 *
6
 * Contains only the main function, which
7
 * initializes the program.
8
 **/
9

  
10
#include <stdlib.h>
2 11
#include <stdio.h>
3
#include <unistd.h>
4
#include <stdlib.h>
5
#include <signal.h>
6
#include <sys/wait.h>
7
#include <robot_shared.h>
8
#include <sys/ipc.h>
9
#include <sys/types.h>
10
#include <sys/shm.h>
11
#include <errno.h>
12 12

  
13
int create_robot(char *execname, int id, SimulatorRobot* sim_memory)
14
{
15
  /* do shared memory stuff here */
16
  // key_t key = IPC_PRIVATE;
17
  /*Memory accessible only to children and gives them read/write priveledge*/
18
  //  sim_memory->robotSharedMemoryID = shmget(key, sizeof(RobotShared), IPC_CREAT | 0666);
19
  
20
  /*  if(sim_memory->robotSharedMemoryID < 0)
21
  {
22
	  printf("error getting shared memory\n");
23
	  return -1;
24
  }
13
#include "gtk_gui.h"
14
#include "robot.h"
25 15

  
26
  sim_memory->robotState = shmat(sim_memory->robotSharedMemoryID, NULL, 0);
27
  
28
  if(!(sim_memory->robotState))
29
  {
30
	printf("error attaching parent process\n");
31
	return -1;
32
  }
33
  */
34
  /*Init robot structure here*/
35
  /*  sim_memory->robotState->motor1 = 0;
36
      sim_memory->robotState->motor2 = 0;*/
37
  int pid;
38

  
39
  if((pid = fork())<0){
40
    printf("error with fork!\n");                        
41
    return -1;
42
  }                       
43
  
44
  char var[21];
45
  char *envp[] = {var, NULL};
46

  
47
  if(!pid){
48
    /* restore default sigchld handler */
49
    signal(SIGCHLD, SIG_DFL);
50

  
51
    sprintf(var, "memory_id=%d", 1);//sim_memory->robotSharedMemoryID);
52

  
53
    execve(execname, NULL, envp);  //TODO: keep the other env. stuff around
54
    printf ("error! exec failed\n");
55
	
56
    exit(-1);
57
  }
58
                   
59
  return pid;
60
}
61

  
62
int free_robot(SimulatorRobot sim)                       
16
int main(int argc, char** argv)
63 17
{
64
  return 0;/*
65
 	shmdt(sim.robotState);
66
	return shmctl(sim.robotSharedMemoryID, IPC_RMID, NULL); */
67
}
18
	if (robots_initialize())
19
		return -1;
68 20

  
69
void *sig_chld_handler(int sig)
70
{
71
  pid_t pid;
72
  int cstat;
21
	if(argc<=1){
22
		printf("Usage: simulator <robot execetuable>\n");
23
		exit(-1);
24
	}
25
	robot_create(argv[1]);
73 26

  
74
  printf("SIGCHLD: parent waiting\n");
27
	printf("returned to parent\n");
75 28

  
76
  pid = waitpid(0, &cstat, WUNTRACED);
29
	gtk_gui_run(argc, argv);
77 30

  
78
  printf("got %d from %d\n", cstat, pid);
79

  
80
  kill(pid, SIGCONT);
81

  
82
  return NULL;
31
	return 0;
83 32
}
84 33

  
85
int main(int argc, char** argv)
86
{
87
  signal(SIGCHLD, sig_chld_handler);
88

  
89
  //SimulatorRobot sim;
90

  
91
  if(argc<=1){
92
    printf("Usage: simulator <robot execetuable>\n");
93
    exit(-1);
94
  }
95

  
96
  create_robot(argv[1], 0, NULL/*&sim*/);
97

  
98
  printf("returned to parent\n");
99

  
100
  int x;
101
  /*  for(x = 0; x <10000; x++)
102
      printf("motor 1: %d\n", x);*/
103

  
104
  gtk_gui_run(argc, argv);
105

  
106
  printf("parent exiting.\n");
107

  
108
  return 0;
109
}
110
              
branches/simulator/projects/simulator/simulator/core/robot.c
1
/**
2
 * @file robot.c
3
 * @author Colony Project
4
 * @brief Simulator Robot Management
5
 *
6
 * Contains implementations of functions managing robots
7
 * in the simulator, including process control and memory
8
 * management.
9
 **/
10

  
11
#include <stdlib.h>
12
#include <stdio.h>
13
#include <string.h>
14

  
15
#include <unistd.h>
16
#include <signal.h>
17
#include <sys/wait.h>
18
#include <robot_shared.h>
19
#include <sys/ipc.h>
20
#include <sys/types.h>
21
#include <sys/shm.h>
22
#include <errno.h>
23

  
24
#include "robot.h"
25

  
26
void sig_chld_handler(int sig);
27

  
28
// global variables
29
int first_available_id = 0;
30
// an expanding array of robots
31
Robot* robots = NULL;
32
int robots_size = 0;
33

  
34
int robots_initialize(void)
35
{
36
	if (signal(SIGCHLD, sig_chld_handler) == SIG_ERR)
37
		return -1;
38

  
39
	robots_size = 10;
40
	robots = (Robot*)malloc(sizeof(Robot) * robots_size);
41
	if (!robots)
42
		return -1;
43
	memset(robots, 0, sizeof(Robot) * robots_size);
44

  
45
	return 0;
46
}
47

  
48
/**
49
 * Creates a new robot. Returns its id on
50
 * success, or a negative integer on failure.
51
 **/
52
int robot_create(char *execname)
53
{
54
	int pid;
55
	int id = first_available_id;
56
	Robot* r = &robots[id];
57
	// do shared memory stuff here
58
	key_t key = IPC_PRIVATE;
59
	//Memory accessible only to children with r/w privileges
60
	r->sharedMemID = shmget(key, sizeof(RobotShared), IPC_CREAT | 0666);
61
	
62
	if(r->sharedMemID < 0)
63
	{
64
		fprintf(stderr, "Failed to get shared memory.\n");
65
		return -1;
66
	}
67

  
68
	r->shared = (RobotShared*)shmat(r->sharedMemID, NULL, 0);
69
	
70
	if(!(r->shared))
71
	{
72
		// TODO: do we have to free the shared memory?
73
		fprintf(stderr, "Error attaching memory to parent.\n");
74
		return -1;
75
	}
76
	
77
	// Initialize robot structure here
78
	r->shared->motor1 = 0;
79
	r->shared->motor2 = 0;
80
	r->id = id;
81

  
82
	if((pid = fork()) < 0)
83
	{
84
		// TODO: do we have to free the shared memory?
85
		r->id = 0;
86
		fprintf(stderr, "Failed to fork robot process.\n");
87
		return -1;
88
	}											 
89
	
90

  
91
	if(!pid)
92
	{
93
		char var[21];
94
		char *envp[] = {var, NULL};
95
		/* restore default sigchld handler */
96
		signal(SIGCHLD, SIG_DFL);
97
		sprintf(var, "memory_id=%d", r->sharedMemID);
98
		//TODO: keep the other env. stuff around
99
		execve(execname, NULL, envp);
100
		fprintf(stderr, "exec failed to run child process.\n");
101
		exit(-1);
102
	}
103
	else
104
	{
105
		r->pid = pid;
106
		do {
107
			first_available_id++;
108
		} while (first_available_id < robots_size &&
109
				robots[first_available_id].id != 0);
110
		// resize the array if necessary
111
		if (first_available_id >= robots_size)
112
		{
113
			robots_size *= 2;
114
			robots = realloc(robots, sizeof(Robot) * robots_size);
115
			if (!robots)
116
			{
117
				fprintf(stderr, "Out of memory.\n");
118
				return -1;
119
			}
120
		}
121
	}
122
	
123
	return id;
124
}
125

  
126
/**
127
 * Frees all resources associated with a robot, including
128
 * killing its process if necessary.
129
 *
130
 * @return zero on success, nonzero on failure
131
 **/
132
int robot_destroy(int id)
133
{
134
	Robot* r;
135

  
136
	if (id < 0 || id >= robots_size)
137
		return -1;
138
	r = &robots[id];
139
	if (r->id == 0)
140
		return -1;
141

  
142
	if (!shmdt(r->shared))
143
	{
144
		fprintf(stderr, "Failed to free shared memory.\n");
145
		return -1;
146
	}
147
	if (!shmctl(r->sharedMemID, IPC_RMID, NULL))
148
	{
149
		fprintf(stderr, "Failed to free shared memory.\n");
150
		return -1;
151
	}
152

  
153
	memset(r, 0, sizeof(Robot));
154

  
155
	if (id < first_available_id)
156
		first_available_id = 0;
157
	
158
	return 0;
159
}
160

  
161
void sig_chld_handler(int sig)
162
{
163
	pid_t pid;
164
	int cstat;
165

  
166
	printf("SIGCHLD: parent waiting\n");
167

  
168
	pid = waitpid(0, &cstat, WUNTRACED);
169

  
170
	printf("got %d from %d\n", cstat, pid);
171

  
172
	kill(pid, SIGCONT);
173
}
174

  
branches/simulator/projects/simulator/simulator/core/robot.h
1
/**
2
 * @file robot.h
3
 * @author Colony Project
4
 *
5
 * @brief Manages simulated robots.
6
 *
7
 * Contains structures and function prototypes used
8
 * for managing robots in the simulator.
9
 **/
10

  
11
#ifndef __ROBOT_H__
12
#define __ROBOT_H__
13

  
14
#include "robot_shared.h"
15

  
16
typedef struct PoseType
17
{
18
	int x,y; /* position of robot in environment */
19
	int theta; /* orientation of robot in environment */
20
} Pose;
21

  
22
typedef struct RobotType
23
{
24
	// the id is 0 if the robot hasn't been created
25
	int id;
26
	int pid;
27
	int sharedMemID; /* id for shared memory object */
28
	Pose pose; /* position in environment */
29

  
30
	RobotShared* shared;
31
	/* add other sensors, etc here */
32
} Robot;
33

  
34
int robots_initialize(void);
35
int robot_create(char *execname);
36
int robot_destroy(int id);
37

  
38
#endif
39

  
branches/simulator/projects/simulator/simulator/Makefile
1 1
# Add new files here
2 2
COMMON_SRCS :=
3
CORE_SRCS := main.c
3
CORE_SRCS := main.c robot.c
4 4
GUI_SRCS := gtk_gui.c gtk_environment_view.c draw_funcs.c
5 5

  
6 6
CORE_DIR := core
branches/simulator/projects/simulator/common/robot_shared.h
1
/* shared structs for simulator */
1
/**
2
 * @file robot_shared.h
3
 * @author Colony Project
4
 *
5
 * @brief Shared simulator structures.
6
 *
7
 * This file is for structures that are used by
8
 * both the simulator and the simulator library which
9
 * is linked to by robot programs.
10
 **/
2 11

  
3 12
#ifndef __ROBOT_SHARED_H
4 13
#define __ROBOT_SHARED_H
5 14

  
6
typedef struct PositionType
7
{
8
  int x,y; /* position of robot in environment */
9
  int theta; /* orientation of robot in environment */
10
} Position;
11

  
12

  
13 15
/* rangefinders are 1-5, so write convert function for this */
14 16
typedef struct RangeFinderType
15 17
{
16
  short d[5];
18
	short d[5];
17 19
} RangeFinder;
18 20

  
19 21
/* this is what is in the robot shared memory */
......
21 23
{
22 24
	short motor1; /* motor1 value */
23 25
	short motor2; /* motor2 value */
24
  RangeFinder ranges; /* rangefinders */
25
  /* add other sensors, etc here */
26
	RangeFinder ranges; /* rangefinders */
27
	/* add other sensors, etc here */
26 28
} RobotShared;
27 29

  
30
#endif
28 31

  
29
typedef struct SimulatorRobotType
30
{
31
  int robotSharedMemoryID; /* id for shared memory object */
32
  RobotShared* robotState;/*Pointer to shared state*/
33

  
34
  Position pos; /* position in environment */
35
	short motor1; /* motor1 value */
36
	short motor2; /* motor2 value */
37
  RangeFinder ranges; /* rangefinders */
38
  /* add other sensors, etc here */
39
} SimulatorRobot;
40

  
41
#endif

Also available in: Unified diff