Project

General

Profile

Revision 988

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

View differences:

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
              

Also available in: Unified diff