]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robofsm/robot.h
robofsm: Odometry autocalibration
[eurobot/public.git] / src / robofsm / robot.h
1 /**
2  * robot.h                      08/04/20
3  *
4  * Robot's data structures for the Eurobot 2008.
5  *
6  * Copyright: (c) 2008 CTU Dragons
7  *            CTU FEE - Department of Control Engineering
8  * License: GNU GPL v.2
9  */
10
11 #ifndef ROBOT_H
12 #define ROBOT_H
13
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <trgenconstr.h>
17 #include <robottype.h>
18 #include <robottype.h>
19 #include <roboorte_robottype.h>
20 #include <robodim.h>
21 #include <roboevent.h>
22 #include <fsm.h>
23 #include <robot_config.h>
24 //#include <ul_log.h>
25
26 //UL_LOG_CUST(ulogd_robot_h); /* Log domain name = ulogd + name of the file */
27
28 /**
29  * Competition parameters
30  */
31 enum team_color {
32         RED = 0,                /* Coordinate transformation does not apply */
33         BLUE                    /* Coordinate transformation applies (in *_trans() functions) */
34 };
35
36 enum robot_start_state {
37         POWER_ON = 0,
38         START_PLUGGED_IN,
39         COMPETITION_STARTED,
40 };
41
42 /**
43  * FSM
44  */
45
46 /* Mapping from FSM ID to field in robot.fsm */
47 #define ROBOT_FSM_MAIN main
48 #define ROBOT_FSM_MOTION motion
49 #define ROBOT_FSM_DISPLAY display
50 #define ROBOT_FSM_ACT act
51
52 #define FSM_GET_BY_ID(fsm_id) (&robot.fsm.ROBOT_FSM_##fsm_id)
53
54 /**
55  * LOCKING MANIPULATION
56  */
57
58 #ifdef CONFIG_LOCK_CHECKING
59 #define LOCK_CHECK_COUNT 10
60 struct lock_log {
61         pthread_mutex_t *locked[LOCK_CHECK_COUNT];
62         int idx;
63 };
64
65 extern struct lock_log robot_lock_log;
66
67 #define __LOCK_CHECK(mutex) robot_lock_log.locked[robot_lock_log.idx++] = mutex;
68 #define __UNLOCK_CHECK(mutex)                                           \
69         if (robot_lock_log.locked[--robot_lock_log.idx] != mutex ||     \
70             robot_lock_log.idx < 0)                                     \
71                 ul_logerr("!!! Locking error %s:%d\n", __FILE__, __LINE__);
72 #else
73 #define __LOCK_CHECK(mutex)
74 #define __UNLOCK_CHECK(mutex)
75 #endif
76 /**
77  * Locks the robot structure.
78  * @param var Field in the structure you are going to work with.
79  */
80 #define ROBOT_LOCK(var)                                          \
81         do {                                                     \
82                 pthread_mutex_lock(&robot.__robot_lock_##var);   \
83                 __LOCK_CHECK(&robot.__robot_lock_##var);         \
84         } while(0)
85
86 /**
87  * Unlocks the robot structure.
88  * @param var Field in the structure, which was locked by ROBOT_LOCK.
89  */
90 #define ROBOT_UNLOCK(var)                                               \
91         do {                                                            \
92                 __UNLOCK_CHECK(&robot.__robot_lock_##var);              \
93                 pthread_mutex_unlock(&robot.__robot_lock_##var);        \
94         } while(0)
95
96 /* Mapping of robot structure fields to locks */
97 //#define __robot_lock_ lock    /* ROBOT_LOCK() */
98 #define __robot_lock_ref_pos            lock_ref_pos
99 #define __robot_lock_est_pos_odo        lock_est_pos_odo
100 #define __robot_lock_est_pos_indep_odo  lock_est_pos_indep_odo
101 #define __robot_lock_joy_data           lock_joy_data
102 #define __robot_lock_meas_angles        lock_meas_angles
103 #define __robot_lock_drives             lock
104 #define __robot_lock_sharps             lock
105 #define __robot_lock_hokuyo             lock
106 #define __robot_lock_cmu                lock
107 #define __robot_lock_bumper             lock
108 #define __robot_lock_disp               lock_disp
109 #define __robot_lock_motion_irc         lock
110 #define __robot_lock_odo_data           lock
111 #define __robot_lock_corr_distances     lock
112 #define __robot_lock_camera_result      lock_camera
113
114 enum robot_status {
115         STATUS_OK,
116         STATUS_WARNING,
117         STATUS_FAILED,
118 };
119
120 enum robot_component {
121         COMPONENT_MOTOR,
122         COMPONENT_ODOMETRY,
123         COMPONENT_CAMERA,
124         COMPONENT_POWER,
125         COMPONENT_HOKUYO,
126         COMPONENT_START,
127         COMPONENT_JAWS,
128         COMPONENT_LIFT,
129
130         ROBOT_COMPONENT_NUMBER
131 };
132
133 /* robot's main data structure */
134 struct robot {
135         pthread_mutex_t lock;
136         pthread_mutex_t lock_ref_pos;
137         pthread_mutex_t lock_est_pos_odo;
138         pthread_mutex_t lock_est_pos_indep_odo;
139         pthread_mutex_t lock_meas_angles;
140         pthread_mutex_t lock_joy_data;
141         pthread_mutex_t lock_disp;
142         pthread_mutex_t lock_camera;
143
144         /* competition parameters */
145         enum team_color team_color;
146
147         /** State variable used for controlling the robot by pluggin
148          * in and out start connector. */
149         enum robot_start_state start_state;
150
151         /** Temporary storage for new trajectory. After the trajectory creation is
152          * finished, this trajectory is submitted to fsmmove. */
153         void *new_trajectory;
154
155         unsigned char isTrajectory;
156         sem_t start;
157
158         struct fsm_main_loop main_loop;
159
160         /* partial FSMs */
161         struct {
162                 struct robo_fsm main;
163                 struct robo_fsm motion;
164                 struct robo_fsm act;
165         } fsm;
166
167         /* actual position */
168         struct robot_pos_type ref_pos;
169         /* estimated position */
170         struct robot_pos_type est_pos_odo;
171         struct robot_pos_type est_pos_indep_odo;
172
173         /** True if est_pos_odo is updated according to reception of motion_irc */
174         bool odometry_works;
175         /** True if est_pos_indep_odo is updated according to reception of motion_indep_odo */
176         bool indep_odometry_works;
177
178         bool use_back_bumpers;
179         bool use_left_bumper;
180         bool use_right_bumper;
181
182         /** True iff at least one wheel rotates backward */
183         bool moves_backward;
184
185         /*
186          * sometimes H8S does not send queried odometry
187          * following flag has been added for EKF estimator,
188          * since is has been hardly disturbed by missing measurement
189          * (taken as sudden zero velocities)
190          */
191         bool motion_irc_received;
192
193         /* orte */
194         struct robottype_orte_data orte;
195
196         /* sensors */
197         struct motion_irc_type motion_irc;      /* motor odometry */
198         struct odo_data_type odo_data;          /* independent odometry */
199         struct corr_distances_type corr_distances;      /* ultrasound */
200
201         struct hokuyo_scan_type hokuyo;
202         bool ignore_hokuyo;
203
204         struct map *map;        /* Map for pathplanning (no locking) */
205
206         enum robot_status status[ROBOT_COMPONENT_NUMBER];
207
208         char corns_conf_center;
209         char corns_conf_side;
210         struct corns_group *corns;
211
212         bool obstacle_avoidance_enabled;
213
214         /** is set to true in separate thread when there is short time left */
215         bool short_time_to_end;
216         bool check_turn_safety;
217         
218         float odo_cal_b;
219         float odo_cal_a;
220         float odo_distance_a;
221         float odo_distance_b;
222 }; /* robot */
223
224 extern struct robot robot;
225
226 #ifdef __cplusplus
227 extern "C" {
228 #endif
229
230 int robot_init() __attribute__ ((warn_unused_result));
231 int robot_start() __attribute__ ((warn_unused_result));
232 void robot_exit();
233 void robot_destroy();
234 void robot_calibrate_odometry();
235
236 void robot_get_est_pos_trans(double *x, double *y, double *phi);
237 void robot_get_est_pos(double *x, double *y, double *phi);
238
239 /* Hack to easily disable display if it is not configured */
240 void serial_comm(int status);
241
242
243
244 FSM_STATE_FULL_DECL(main, init);
245 FSM_STATE_FULL_DECL(motion, init);
246 FSM_STATE_FULL_DECL(disp, init);
247 FSM_STATE_FULL_DECL(act, wait_for_command);
248
249 #ifdef __cplusplus
250 }
251 #endif
252
253 /*Thread priorities*/
254 #define THREAD_PRIO_TRAJ_FOLLOWER 90    /* As high as possible */
255 #define THREAD_PRIO_TRAJ_RECLAC 18
256 #define OBST_FORGETING_PRIO 17  /* Priority of the thread for forgeting detected obstacles. */
257
258
259
260 #endif  /* ROBOT_H */