]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.c
something like MAIN FSM's state "go_to_our_static_dispenser" added
[eurobot/public.git] / src / robofsm / robot.c
1 /*
2  * robot_eb2008.c                       08/04/20
3  *
4  * Robot's generic initialization and clean up functions.
5  *
6  * Copyright: (c) 2008 CTU Dragons
7  *            CTU FEE - Department of Control Engineering
8  * License: GNU GPL v.2
9  */
10
11 #include <map.h>
12 #include <movehelper.h>
13 #include <pthread.h>
14 #include <robomath.h>
15 #include <robot.h>
16 #include <robot_orte.h>
17 #include <signal.h>
18 #include <sys/time.h>
19 #include <time.h>
20 #include <unistd.h>
21 #include "map_handling.h"
22 #include <uoled.h>
23 #include <string.h>
24
25 #define MOTION_CONTROL_INIT_ONLY
26 #include "motion-control.h"
27
28 /* Global definition of robot structure */
29 struct robot robot;
30
31 #ifdef CONFIG_LOCK_CHECKING
32 struct lock_log robot_lock_log;
33 #endif
34
35 static void block_signals()
36 {
37         sigset_t sigset;
38         int i;
39
40         sigemptyset(&sigset);
41         for (i=SIGRTMIN; i<=SIGRTMAX; i++)
42                 sigaddset(&sigset, i);
43
44         pthread_sigmask(SIG_BLOCK, &sigset, (sigset_t*)NULL);
45 }
46
47 static void int_handler(int sig)
48 {
49         robot_exit();
50 }
51
52 void fill_in_known_areas_in_map()
53 {
54         /* Do not plan path close to edges */
55 /*      ShmapSetRectangleFlag(0.0, 0.0, 0.199, 2.1, MAP_FLAG_WALL, 0); */
56 /*      ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.199, MAP_FLAG_WALL, 0); */
57 /*      ShmapSetRectangleFlag(0.0, 1.901, 3.0, 2.1, MAP_FLAG_WALL, 0); */
58 /*      ShmapSetRectangleFlag(2.801, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0); */
59
60         //ShmapSetRectangleFlag(0, 1.15, 0.4, 1.55, MAP_FLAG_IGNORE_OBST, 0); /* Left white vert. dispenser */
61         //ShmapSetRectangleFlag(2.7, 1.25, 3, 1.45, MAP_FLAG_IGNORE_OBST, 0); /* Right white vert. dispenser */
62
63         //ShmapSetRectangleFlag(0.50, 1.8, 0.95, 2.2, MAP_FLAG_IGNORE_OBST, 0); /* Blue vert. dispenser */
64         //ShmapSetRectangleFlag(2.30, 1.8, 2.55, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* Red vert. dispenser */
65
66         /* Ignore other obstacles at edges */
67         ShmapSetRectangleFlag(0.0, 0.0, 0.09, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* left */
68         ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.09, MAP_FLAG_IGNORE_OBST, 0); /* bottom */
69         ShmapSetRectangleFlag(0.0, 2.01, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* top */
70         ShmapSetRectangleFlag(2.91, 0.0, 3.0, 2.1, MAP_FLAG_IGNORE_OBST, 0); /* right */
71
72         //ShmapSetRectangleFlag(0.0, 0.0, 0.2, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* left basket */
73         //ShmapSetRectangleFlag(2.8, 0.0, 3.0, 0.6, MAP_FLAG_IGNORE_OBST, 0); /* right basket */
74
75         /* Construction areas (Building zones) */
76         ShmapSetRectangleFlag(0.0, 0.0, 1.5, 2.1, MAP_FLAG_SIMULATED_WALL, 0); /* Central construction area */
77 }
78
79 /** 
80  * Initializes the robot.
81  * Setup fields in robot structure, initializes FSMs and ORTE.
82  * 
83  * @return 0
84  */
85 int robot_init()
86 {
87         int rv;
88
89         pthread_mutex_init(&robot.lock, NULL);
90         pthread_mutex_init(&robot.lock_ref_pos, NULL);
91         pthread_mutex_init(&robot.lock_est_pos, NULL);
92         pthread_mutex_init(&robot.lock_meas_angles, NULL);
93         pthread_mutex_init(&robot.lock_joy_data, NULL);
94         pthread_mutex_init(&robot.lock_disp, NULL);
95
96         fsm_main_loop_init(&robot.main_loop);
97         
98         /* FSM initialization */
99         fsm_init(&robot.fsm.main, "main", &robot.main_loop);
100         fsm_init(&robot.fsm.motion, "motion", &robot.main_loop);
101         fsm_init(&robot.fsm.display, "display", &robot.main_loop);
102         fsm_init(&robot.fsm.act, "actuators", &robot.main_loop);
103
104         /* FIXME: there should be team color initilization */
105 #ifdef WE_ARE_RED
106         robot.team_color = RED;
107 #endif
108 #ifdef WE_ARE_GREEN
109         robot.team_color = GREEN;
110 #endif
111
112         if (robot.team_color == RED) {
113                 printf("We are RED!\n");
114         } else {
115                 printf("We are GREEN!\n");
116         }
117
118 #ifdef CONFIG_OPEN_LOOP
119         printf("OPEN_LOOP enabled\n");
120 #endif
121
122         robot.map = ShmapInit(1);
123         fill_in_known_areas_in_map();
124
125         signal(SIGINT, int_handler);
126         signal(SIGTERM, int_handler);
127         block_signals();
128
129         /* initial values */
130         robot.orte.motion_speed.left = 0;
131         robot.orte.motion_speed.right = 0;
132
133         robot.orte.pwr_ctrl.voltage33 = 1;
134         robot.orte.pwr_ctrl.voltage50 = 1;
135         robot.orte.pwr_ctrl.voltage80 = 1;
136
137         robot.fsm.motion.state = &fsm_state_motion_init;
138
139         robot.fsm.act.state = &fsm_state_act_wait_for_command; // puck handling actuators FSM's initial state
140
141         /* Only activate display if it is configured */
142         robot.sercom = uoled_init(serial_comm);
143         if (strcmp(robot.sercom->devname, "/dev/null") != 0)
144                 robot.fsm.display.state = &fsm_state_disp_init;
145
146         robot.obstacle_avoidance_enabled = true;
147         robot.state = JUST_STARTED;
148
149         /* init ORTE domain, create publishers, subscribers, .. */
150         rv = robot_init_orte();
151         act_init(&robot.orte);
152
153         return rv;
154 }
155
156 /** 
157  * Starts the robot FSMs and threads.
158  *
159  * @return 0
160  */
161 int robot_start()
162 {
163         int rv = 0;
164
165         pthread_attr_t tattr;
166         struct sched_param param;
167         pthread_t thr_obstacle_forgeting;
168         int ret;
169
170         ret = motion_control_init();
171         if(ret) {
172                 perror("motion_control_init");
173                 robot_exit();
174         }
175
176
177         /* Obstacle forgeting thread */
178         pthread_attr_init (&tattr);
179         pthread_attr_getschedparam (&tattr, &param);
180         param.sched_priority = OBST_FORGETING_PRIO;
181         rv = pthread_attr_setschedparam (&tattr, &param);
182         if (rv) {
183                 perror("robot_start: pthread_attr_setschedparam()");
184                 goto err;
185         }
186         rv = pthread_create(&thr_obstacle_forgeting, 
187                             &tattr, thread_obstacle_forgeting, NULL);
188         if (rv) {
189                 perror("robot_start: pthread_create");
190                 goto err;
191         }
192
193         fsm_main_loop(&robot.main_loop);
194
195 err:
196         return rv;
197 }
198
199 /** 
200  * Signals all the robot threads to finish.
201  */
202 void robot_exit()
203 {
204         /* stop FSMs */
205         fsm_exit(&robot.fsm.main);
206         fsm_exit(&robot.fsm.motion);
207         fsm_exit(&robot.fsm.display);
208         fsm_exit(&robot.fsm.act);
209 }
210
211 /** 
212  * Stops the robot. All resources alocated by robot_init() or
213  * robot_start() are dealocated here.
214  */
215 void robot_destroy()
216 {
217         motion_control_done();
218         
219         robottype_roboorte_destroy(&robot.orte);
220
221         fsm_destroy(&robot.fsm.main);
222         fsm_destroy(&robot.fsm.motion);
223         fsm_destroy(&robot.fsm.display);
224         fsm_destroy(&robot.fsm.act);
225         DBG("robofsm: stop.\n");
226 }