2 * robot_orte.c 08/04/21
6 * Copyright: (c) 2008 CTU Dragons
7 * CTU FEE - Department of Control Engineering
12 #include <roboorte_generic.h>
13 #include <roboorte_eb2008.h>
15 #include <robot_eb2008.h>
16 #include <movehelper_eb2008.h>
19 #include <laser-nav.h>
20 #include "map_handling.h"
24 /* ----------------------------------------------------------------------
25 * PUBLISHER CALLBACKS - GENERIC
26 * ---------------------------------------------------------------------- */
28 void send_ref_pos_cb(const ORTESendInfo *info, void *vinstance,
29 void *sendCallBackParam)
31 struct ref_pos_type *instance = (struct ref_pos_type *)vinstance;
34 *instance = robot.ref_pos;
35 ROBOT_UNLOCK(ref_pos);
38 void send_est_pos_cb(const ORTESendInfo *info, void *vinstance,
39 void *sendCallBackParam)
41 struct est_pos_type *instance = (struct est_pos_type *)vinstance;
44 *instance = robot.est_pos;
45 ROBOT_UNLOCK(est_pos);
48 void send_dummy_cb(const ORTESendInfo *info, void *vinstance,
49 void *sendCallBackParam)
53 /* ----------------------------------------------------------------------
54 * SUBSCRIBER CALLBACKS - GENERIC
55 * ---------------------------------------------------------------------- */
57 void rcv_motion_irc_cb(const ORTERecvInfo *info, void *vinstance,
58 void *recvCallBackParam)
60 struct motion_irc_type *instance = (struct motion_irc_type *)vinstance;
61 static struct motion_irc_type prevInstance;
62 static int firstRun = 1;
63 /* spocitat prevodovy pomer */
64 double n = (double)(28.0 / 1.0);
65 /* vzdalenost na pulz - 4.442 je empiricka konstanta :) */
66 double c = (M_PI*2*ROBOT_WHEEL_RADIUS_M) / (n * 4*500.0);
73 switch (info->status) {
76 prevInstance = *instance;
81 aktk0 = ((prevInstance.left - instance->left) >> 8) * c;
82 aktk1 = ((instance->right - prevInstance.right) >> 8) * c;
83 prevInstance = *instance;
85 deltaU = (aktk0 + aktk1) / 2;
86 deltAlfa = (aktk1 - aktk0) / (2.0*ROBOT_ROTATION_RADIUS_M);
88 struct mcl_robot_odo *odo = malloc(sizeof(struct mcl_robot_odo));
89 memset(odo, 0, sizeof(*odo));
92 odo->dangle = deltAlfa;
93 FSM_SIGNAL(LOC, EV_ODO_RECEIVED, odo);
94 robot.hw_status[STATUS_MOTION] = HW_STATUS_OK;
97 robot.hw_status[STATUS_MOTION] = HW_STATUS_FAILED;
98 DBG("ORTE deadline occurred - motion_irc receive\n");
103 void rcv_motion_speed_cb(const ORTERecvInfo *info, void *vinstance,
104 void *recvCallBackParam)
106 switch (info->status) {
110 DBG("ORTE deadline occurred - motion_speed receive\n");
115 void rcv_motion_status_cb(const ORTERecvInfo *info, void *vinstance,
116 void *recvCallBackParam)
118 switch (info->status) {
122 DBG("ORTE deadline occurred - motion_status receive\n");
127 void rcv_joy_data_cb(const ORTERecvInfo *info, void *vinstance,
128 void *recvCallBackParam)
130 struct joy_data_type *instance = (struct joy_data_type *)vinstance;
132 switch (info->status) {
134 ROBOT_LOCK(joy_data);
135 robot.joy_data = *instance;
136 ROBOT_UNLOCK(joy_data);
139 DBG("ORTE deadline occurred - joy_data receive\n");
144 void rcv_pwr_voltage_cb(const ORTERecvInfo *info, void *vinstance,
145 void *recvCallBackParam)
147 switch (info->status) {
149 robot.hw_status[STATUS_POWER]=HW_STATUS_OK;
152 robot.hw_status[STATUS_POWER]=HW_STATUS_FAILED;
153 DBG("ORTE deadline occurred - pwr_voltage receive\n");
158 void rcv_pwr_ctrl_cb(const ORTERecvInfo *info, void *vinstance,
159 void *recvCallBackParam)
161 switch (info->status) {
165 DBG("ORTE deadline occurred - pwr_ctrl receive\n");
170 void rcv_robot_cmd_cb(const ORTERecvInfo *info, void *vinstance,
171 void *recvCallBackParam)
173 struct robot_cmd_type *instance = (struct robot_cmd_type *)vinstance;
174 static struct robot_cmd_type last_instance;
179 } state = JUST_STARTED;
181 switch (info->status) {
183 if (instance->start != last_instance.start) {
184 printf("state %d ", state);
186 printf("plugged in\n");
188 printf("plugged out\n");
193 if (!instance->start) {
194 FSM_SIGNAL(MAIN, EV_START, NULL);
195 state = LASER_STARTED;
199 if (instance->start) {
200 FSM_SIGNAL(MAIN, EV_START, NULL);
201 state = COMPETITION_STARTED;
204 case COMPETITION_STARTED:
205 if (!instance->start) {
211 last_instance = *instance;
214 DBG("ORTE deadline occurred - pwr_ctrl receive\n");
219 /* ----------------------------------------------------------------------
220 * SUBSCRIBER CALLBACKS - EB2008
221 * ---------------------------------------------------------------------- */
223 void rcv_servos_cb(const ORTERecvInfo *info, void *vinstance,
224 void *recvCallBackParam)
226 switch (info->status) {
230 DBG("ORTE deadline occurred - servos receive\n");
235 void rcv_drives_cb(const ORTERecvInfo *info, void *vinstance,
236 void *recvCallBackParam)
238 struct drives_type *instance = (struct drives_type *)vinstance;
240 switch (info->status) {
243 robot.drives = *instance;
244 ROBOT_UNLOCK(drives);
247 DBG("ORTE deadline occurred - drives receive\n");
252 void rcv_laser_data_cb(const ORTERecvInfo *info, void *vinstance,
253 void *recvCallBackParam)
255 struct laser_data_type *instance = (struct laser_data_type *)vinstance;
257 switch (info->status) {
259 struct mcl_laser_measurement *meas_angles;
260 meas_angles = malloc(sizeof(*meas_angles));
261 memset(meas_angles, 0, sizeof(*meas_angles));
263 meas_angles->count = 1;
264 meas_angles->val[0] =
265 (double)TIME2ANGLE(instance->period,
267 // DBG("a=%f\n", RAD2DEG(robot.meas_angles.val[0]));
270 FSM_SIGNAL_TIMED(LOC, EV_LASER_RECEIVED, meas_angles, 20/*ms*/, &sent);
272 DBG("%s: MCL is busy - not updating\n", __FUNCTION__);
275 robot.hw_status[STATUS_LASER] = HW_STATUS_OK;
279 robot.hw_status[STATUS_LASER] = HW_STATUS_FAILED;
280 DBG("%s: ORTE deadline occurred\n", __FUNCTION__);
285 void rcv_cmu_cb(const ORTERecvInfo *info, void *vinstance,
286 void *recvCallBackParam)
288 struct cmu_type *instance = (struct cmu_type *)vinstance;
289 static enum ball_color last_color = NO_BALL;
290 static unsigned char first = 1;
292 switch (info->status) {
295 robot.cmu = *instance;
298 last_color = robot.cmu.color;
301 if (robot.cmu.color != NO_BALL) {
302 if (last_color != robot.cmu.color) {
303 last_color = robot.cmu.color;
304 FSM_SIGNAL(MAIN, EV_BALL_INSIDE, NULL);
307 robot.hw_status[STATUS_CMU] = HW_STATUS_OK;
311 robot.hw_status[STATUS_CMU] = HW_STATUS_FAILED;
312 DBG("%s: ORTE deadline occurred\n", __FUNCTION__);
317 void rcv_bumper_cb(const ORTERecvInfo *info, void *vinstance,
318 void *recvCallBackParam)
320 struct bumper_type *instance = (struct bumper_type *)vinstance;
322 switch (info->status) {
325 robot.bumper = *instance;
326 ROBOT_UNLOCK(bumper);
330 DBG("%s: ORTE deadline occurred\n", __FUNCTION__);
337 static int cmp_double(const void *v1, const void *v2)
339 const double *d1 = v1, *const d2 = v2;
348 static inline double *sharp_ptr(struct sharps_type *sharps, int index)
351 case 0: return &sharps->front_left; break;
352 case 1: return &sharps->front_right; break;
353 case 2: return &sharps->left; break;
354 case 3: return &sharps->right; break;
355 default: return &sharps->front_left; break;
360 void rcv_sharps_cb(const ORTERecvInfo *info, void *vinstance,
361 void *recvCallBackParam)
363 struct sharps_type *instance = (struct sharps_type *)vinstance;
364 static struct sharps_type history[HIST_CNT];
365 static double history_sorted[HIST_CNT];
368 //struct sharps_type *for_mcl;
370 switch (info->status) {
373 robot.sharps = *instance;
374 ROBOT_UNLOCK(sharps);
376 for (i=0; i<HIST_CNT; i++) {
377 history[i].front_left = 0.8;
378 history[i].front_right = 0.8;
379 history[i].left = 0.8;
380 history[i].right = 0.8;
384 if (ind >= HIST_CNT) {
387 history[ind] = *instance;
389 for (s=0; s<4; s++) {
390 for (i=0; i<HIST_CNT; i++) {
391 history_sorted[i] = *sharp_ptr(&history[i], s);
393 qsort(history_sorted, HIST_CNT, sizeof(history_sorted[0]), cmp_double);
394 *sharp_ptr(instance, s) = history_sorted[HIST_CNT/2]; /* Get median */
397 update_map(instance);
401 DBG("%s: ORTE deadline occurred\n", __FUNCTION__);
406 void robot_init_orte()
408 robot.orte.strength = 20;
409 robot.gorte.strength = 20;
411 eb2008_roboorte_init(&robot.orte);
412 generic_roboorte_init(&robot.gorte);
414 /* creating publishers */
415 generic_publisher_motion_irc_create(&robot.gorte, NULL, &robot.gorte);
416 generic_publisher_motion_speed_create(&robot.gorte, NULL, &robot.gorte);
417 generic_publisher_motion_status_create(&robot.gorte, NULL, &robot.gorte);
418 generic_publisher_ref_pos_create(&robot.gorte, send_ref_pos_cb, &robot.gorte);
419 generic_publisher_est_pos_create(&robot.gorte, send_est_pos_cb, &robot.gorte);
420 generic_publisher_joy_data_create(&robot.gorte, NULL, &robot.gorte);
421 generic_publisher_pwr_voltage_create(&robot.gorte, NULL, &robot.gorte);
422 generic_publisher_pwr_ctrl_create(&robot.gorte, NULL, &robot.gorte);
424 eb2008_publisher_servos_create(&robot.orte, send_dummy_cb, &robot.orte);
425 eb2008_publisher_drives_create(&robot.orte, send_dummy_cb, &robot.orte);
426 eb2008_publisher_laser_cmd_create(&robot.orte, NULL, NULL);
428 /* create generic subscribers */
429 generic_subscriber_motion_irc_create(&robot.gorte, rcv_motion_irc_cb, &robot.gorte);
430 generic_subscriber_motion_speed_create(&robot.gorte, rcv_motion_speed_cb, &robot.gorte);
431 generic_subscriber_motion_status_create(&robot.gorte, rcv_motion_status_cb, &robot.gorte);
432 generic_subscriber_joy_data_create(&robot.gorte, rcv_joy_data_cb, &robot.gorte);
433 generic_subscriber_pwr_voltage_create(&robot.gorte, rcv_pwr_voltage_cb, &robot.gorte);
434 generic_subscriber_pwr_ctrl_create(&robot.gorte, rcv_pwr_ctrl_cb, &robot.gorte);
435 generic_subscriber_robot_cmd_create(&robot.gorte, rcv_robot_cmd_cb, &robot.gorte);
437 /* create eb2008 subscribers */
438 eb2008_subscriber_servos_create(&robot.orte, rcv_servos_cb, &robot.orte);
439 eb2008_subscriber_drives_create(&robot.orte, rcv_drives_cb, &robot.orte);
440 eb2008_subscriber_laser_data_create(&robot.orte, rcv_laser_data_cb, &robot.orte);
441 eb2008_subscriber_cmu_create(&robot.orte, rcv_cmu_cb, &robot.orte);
442 eb2008_subscriber_bumper_create(&robot.orte, rcv_bumper_cb, &robot.orte);
443 eb2008_subscriber_sharps_create(&robot.orte, rcv_sharps_cb, &robot.orte);