Project

General

Profile

Revision 986

moved stuff from robot_test into libsim.c
libsim now contains the timer (pause/resume) stuff

Also commented andrew's code because it broke the build

View differences:

branches/simulator/projects/simulator/libsim/libsim.c
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <sys/time.h>
4
#include <signal.h>
5
#include <unistd.h>
6
#include <stdlib.h>
7
#include <sys/shm.h>
8
#include <sys/ipc.h>
9
//#include <robot_shared.h>
10
#include <string.h>
1 11

  
12

  
13
//RobotShared* shared_state;
14

  
15
void *tick(int sig)
16
{
17
  printf("robot process paused. suspending\n");
18

  
19
  //shared_state->motor1++; 
20

  
21
  if(raise(SIGTSTP)<0)
22
    printf("could not kill self!\n");
23

  
24
  printf("robot resumed\n");
25

  
26
  return NULL;
27
}
28

  
29

  
2 30
void dragonfly_init(int config)
3 31
{
32
  struct itimerval iv;
33
  int ret;
4 34

  
35
  /*shared_state = shmat(atoi(envp[0]), NULL, 0);
36
  if(shared_state < 0)
37
  {
38
	  printf("unable to get shared memory region\n");
39
	  return 1;
40
	  }*/
41

  
42
                                             
43
  printf("hello. I am a robot w/ memory_id %s\n", getenv("memory_id"));
44

  
45

  
46
  iv.it_interval.tv_sec = 1;
47
  iv.it_interval.tv_usec = 0;
48
  iv.it_value.tv_sec = 3;
49
  iv.it_value.tv_usec = 0;
50

  
51
  signal(SIGVTALRM, tick);
52

  
53
  ret = setitimer(ITIMER_VIRTUAL, &iv, NULL);
54

  
55
  printf("setitimer returned %d.\n waiting...\n", ret);
56
  fflush(stdout);
57

  
58
  //TODO: clean up code??
59

  
5 60
}
6 61

  
branches/simulator/projects/simulator/simulator/robot_test/robot.c
1
#include <stdio.h>
2
#include <sys/time.h>
3
#include <signal.h>
4
#include <unistd.h>
5
#include <stdlib.h>
6
#include <sys/shm.h>
7
#include <sys/ipc.h>
8
#include <robot_shared.h>
9
#include <string.h>
10

  
11
volatile int i;
12

  
13
RobotShared* shared_state;
14

  
15
void *tick(int sig)
16
{
17
  printf("robot process paused. suspending\n");
18

  
19
  shared_state->motor1++; 
20

  
21
  if(raise(SIGTSTP)<0)
22
    printf("could not kill self!\n");
23

  
24
  printf("robot resumed\n");
25

  
26
  return NULL;
27
}
28

  
29
int main(int argc, char *argp[], char *envp[])
30
{
31
  int ret;
32
  struct itimerval iv;
33

  
34
  shared_state = shmat(atoi(envp[0]), NULL, 0);
35
  if(shared_state < 0)
36
  {
37
	  printf("unable to get shared memory region\n");
38
	  return 1;
39
  }
40

  
41
                                             
42
  printf("hello. I am a robot w/ env[0] %s\n", envp[0]);
43

  
44
  iv.it_interval.tv_sec = 1;
45
  iv.it_interval.tv_usec = 0;
46
  iv.it_value.tv_sec = 3;
47
  iv.it_value.tv_usec = 0;
48

  
49
  signal(SIGVTALRM, tick);
50

  
51
  ret = setitimer(ITIMER_VIRTUAL, &iv, NULL);
52

  
53
  printf("setitimer returned %d.\n waiting...\n", ret);
54
  fflush(stdout);
55

  
56
  i=0;
57

  
58
  while(1)
59
    i++;
60

  
61
  shmdt(shared_state); 
62
  return 0;
63
}
branches/simulator/projects/simulator/simulator/core/main.c
13 13
int create_robot(char *execname, int id, SimulatorRobot* sim_memory)
14 14
{
15 15
  /* do shared memory stuff here */
16
  key_t key = IPC_PRIVATE;
16
  // key_t key = IPC_PRIVATE;
17 17
  /*Memory accessible only to children and gives them read/write priveledge*/
18
  sim_memory->robotSharedMemoryID = shmget(key, sizeof(RobotShared), IPC_CREAT | 0666);
18
  //  sim_memory->robotSharedMemoryID = shmget(key, sizeof(RobotShared), IPC_CREAT | 0666);
19 19
  
20
  if(sim_memory->robotSharedMemoryID < 0)
20
  /*  if(sim_memory->robotSharedMemoryID < 0)
21 21
  {
22 22
	  printf("error getting shared memory\n");
23 23
	  return -1;
......
30 30
	printf("error attaching parent process\n");
31 31
	return -1;
32 32
  }
33
  
33
  */
34 34
  /*Init robot structure here*/
35
  sim_memory->robotState->motor1 = 0;
36
  sim_memory->robotState->motor2 = 0;
37

  
35
  /*  sim_memory->robotState->motor1 = 0;
36
      sim_memory->robotState->motor2 = 0;*/
38 37
  int pid;
39 38

  
40 39
  if((pid = fork())<0){
......
43 42
  }                       
44 43
  
45 44
  char var[21];
46
  sprintf(var, "memory_id=%d", sim_memory->robotSharedMemoryID);
47 45
  char *envp[] = {var, NULL};
48 46

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

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

  
53 53
    execve(execname, NULL, envp);  //TODO: keep the other env. stuff around
54 54
    printf ("error! exec failed\n");
55 55
	
......
61 61

  
62 62
int free_robot(SimulatorRobot sim)                       
63 63
{
64
	shmdt(sim.robotState);
65
	return shmctl(sim.robotSharedMemoryID, IPC_RMID, NULL); 
64
  return 0;/*
65
 	shmdt(sim.robotState);
66
	return shmctl(sim.robotSharedMemoryID, IPC_RMID, NULL); */
66 67
}
67 68

  
68 69
void *sig_chld_handler(int sig)
......
85 86
{
86 87
  signal(SIGCHLD, sig_chld_handler);
87 88

  
88
  SimulatorRobot sim;
89
  //SimulatorRobot sim;
89 90

  
90
  create_robot("robot_test/robot", 0, &sim);
91
  if(argc<=1){
92
    printf("Usage: simulator <robot execetuable>\n");
93
    exit(-1);
94
  }
91 95

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

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

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

  
98 104
  gtk_gui_run(argc, argv);
99 105

  
branches/simulator/projects/simulator/core/test_timer.c
2 2
#include <sys/time.h>
3 3
#include <signal.h>
4 4

  
5
volatile int i;
6

  
5 7
void *tick(int sig)
6 8
{
7
  printf("tick\n");
9
  printf("tick. i=%d\n", i);
8 10
  fflush(stdout);
9 11

  
10 12
  return NULL;
......
27 29
  printf("setitimer returned %d.\n waiting...\n", ret);
28 30
  fflush(stdout);
29 31

  
30
  while(1);
32
  i=0;
31 33

  
34
  while(1)
35
    i++;
36

  
32 37
  return 0;
33 38
}

Also available in: Unified diff