5 * @brief CAN-ORTE bridge
7 * Copyright: (c) 2008 CTU Dragons
8 * CTU FEE - Department of Control Engineering
19 #include <sys/socket.h>
20 #include <sys/ioctl.h>
27 #include <can_msg_masks.h>
28 #include <can_msg_def.h>
32 #include <roboorte_robottype.h>
33 #include <can_msg_masks.h>
38 if ((sock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
39 perror("cand: unable to create a socket");
43 can_addr.can_family = AF_CAN;
44 std::strcpy(can_ifreq.ifr_name, "can0");
46 if (ioctl(sock, SIOCGIFINDEX, &can_ifreq) < 0) {
47 perror("cand: SIOCGIFINDEX");
51 can_addr.can_ifindex = can_ifreq.ifr_ifindex;
53 if (!can_addr.can_ifindex) {
54 perror("cand: invalid socket interface");
58 if (bind(sock, (struct sockaddr *)&can_addr, sizeof(can_addr)) < 0) {
59 perror("cand: unable to bind");
66 int set_pwr_ctrl(struct robottype_orte_data *orte_data)
69 if(orte_data->pwr_ctrl.voltage33)
70 data |= CAN_PWR_CTRL_33_ON;
72 data |= CAN_PWR_CTRL_33_OFF;
74 if(orte_data->pwr_ctrl.voltage50)
75 data |= CAN_PWR_CTRL_50_ON;
77 data |= CAN_PWR_CTRL_50_OFF;
79 if(orte_data->pwr_ctrl.voltage80)
80 data |= CAN_PWR_CTRL_80_ON;
82 data |= CAN_PWR_CTRL_80_OFF;
84 can_send(CAN_PWR, 1, &data);
89 int set_pwr_alert(struct robottype_orte_data *orte_data)
92 data = orte_data->pwr_alert.value;
94 can_send(CAN_PWR_ALERT, 1, &data);
99 int send_can_msg(struct robottype_orte_data *orte_data)
103 data[0] = orte_data->can_msg.data1;
104 data[1] = orte_data->can_msg.data2;
105 data[2] = orte_data->can_msg.data3;
106 data[3] = orte_data->can_msg.data4;
107 data[4] = orte_data->can_msg.data5;
108 data[5] = orte_data->can_msg.data6;
109 data[6] = orte_data->can_msg.data7;
110 data[7] = orte_data->can_msg.data8;
112 can_send(orte_data->can_msg.id, orte_data->can_msg.len, data);
115 int set_motor_speed(struct robottype_orte_data *orte_data)
117 unsigned char data[4];
119 data[0] = orte_data->motion_speed.right >> 8;
120 data[1] = orte_data->motion_speed.right & 0xff;
121 data[2] = orte_data->motion_speed.left >> 8;
122 data[3] = orte_data->motion_speed.left & 0xff;
123 can_send(CAN_MOTION_CMD, 4, data);
129 * Sends #CAN_LIFT message.
131 * - data[1] = orte_data->lift.pos & 0xFF; // lower byte
132 * - data[0] = orte_data->lift.pos >> 8; // upper byte
134 int set_lift(struct robottype_orte_data *orte_data)
136 unsigned char data[2];
138 data[0] = orte_data->lift.pos >> 8;
139 data[1] = orte_data->lift.pos & 0xFF;
140 can_send(CAN_LIFT, sizeof(data), data);
146 * Sends #CAN_CHELAE message.
148 * - data[1] = orte_data->chelae.left;
149 * - data[0] = orte_data->chelae.right;
151 int set_chelae(struct robottype_orte_data *orte_data)
153 unsigned char data[2];
155 data[0] = orte_data->chelae.left;
156 data[1] = orte_data->chelae.right;
157 can_send(CAN_CHELAE, sizeof(data), data);
163 * Sends #CAN_BELTS message.
165 * - data[1] = orte_data->belts.left;
166 * - data[0] = orte_data->belts.right;
168 int set_belts(struct robottype_orte_data *orte_data)
170 unsigned char data[2];
172 data[0] = orte_data->belts.left;
173 data[1] = orte_data->belts.right;
174 can_send(CAN_BELTS, sizeof(data), data);
180 * Sends #CAN_HOLDER message.
182 * - data[0] = orte_data->holder.pos;
184 int set_holder(struct robottype_orte_data *orte_data)
186 unsigned char data = orte_data->holder.pos;
187 can_send(CAN_HOLDER, sizeof(data), &data);
193 * Sends #CAN_PUSHER message.
195 * - data[1] = orte_data->pusher.pos & 0xFF; // lower byte
196 * - data[0] = orte_data->pusher.pos >> 8; // upper byte
198 int set_pusher(struct robottype_orte_data *orte_data)
200 unsigned char data[2];
202 data[0] = orte_data->pusher.pos >> 8;
203 data[1] = orte_data->pusher.pos & 0xFF;
204 can_send(CAN_PUSHER, sizeof(data), data);
210 * Sends #CAN_HOKUYO_PITCH message.
212 * - data = orte_data->pusher.pos
214 int set_hokuyo_pitch(struct robottype_orte_data *orte_data)
216 unsigned char data = orte_data->hokuyo_pitch.angle;
218 can_send(CAN_HOKUYO_PITCH, sizeof(data), &data);
223 int can_send(canid_t id, unsigned char length, unsigned char *data)
225 struct can_frame frame;
229 frame.can_dlc = length;
231 for(int i=0; i<length; i++)
232 frame.data[i] = data[i];
234 if ((size = write(sock, &frame, sizeof(struct can_frame))) < 0) {
235 perror("cand: can send: write()");
237 } else if (size < sizeof(struct can_frame)) {
238 perror("cand: can send: incomplete CAN frame\n");
246 * Parse frame ID and react as required
248 int cand_parse_frame(struct robottype_orte_data *orte, struct can_frame frame)
255 unsigned short *las_meas;
256 int las_mi, las_di, las_bcnt;
257 static unsigned short prev_meas;
260 switch(frame.can_id) {
261 /* voltage measurements from power board */
263 /* robot commands (start, ..) */
265 orte->robot_cmd.start = frame.data[0];
266 orte->robot_cmd.stop = frame.data[1];
267 ORTEPublicationSend(orte->publication_robot_cmd);
270 /* global sampling period -- time trigger */
272 orte->corr_trig.seq = frame.data[0];
273 ORTEPublicationSend(orte->publication_corr_trig);
276 /* distances measured by ULoPoS */
278 orte->corr_distances.t1 = (frame.data[1]<<8)|(frame.data[0]);
279 orte->corr_distances.t2 = (frame.data[3]<<8)|(frame.data[2]);
280 orte->corr_distances.t3 = (frame.data[5]<<8)|(frame.data[4]);
281 ORTEPublicationSend(orte->publication_corr_distances);
284 /* positioning by odometry */
285 case CAN_MOTION_ODOMETRY_SIMPLE:
286 orte->motion_irc.right =
287 ((frame.data[0]<<24)|(frame.data[1]<<16)|
288 (frame.data[2]<<8)|(frame.data[3]));
289 orte->motion_irc.left =
290 ((frame.data[4]<<24)|(frame.data[5]<<16)|
291 (frame.data[6]<<8)|(frame.data[7]));
292 ORTEPublicationSend(orte->publication_motion_irc);
296 case CAN_MOTION_STATUS:
297 orte->motion_status.err_left =
298 (frame.data[0]<<8)|(frame.data[1]);
299 orte->motion_status.err_right =
300 (frame.data[2]<<8)|(frame.data[3]);
301 if(++status_cnt == 5) {
302 ORTEPublicationSend(orte->publication_motion_status);
307 double volt33, voltBAT;
308 voltBAT = ((frame.data[0] << 24) | (frame.data[1] << 16) | \
309 (frame.data[2] << 8) | (frame.data[3]))/10000.0;
310 volt33 = ((frame.data[4] << 24) | (frame.data[5] << 16) | \
311 (frame.data[6] << 8) | (frame.data[7]))/10000.0;
312 orte->pwr_voltage.voltage33 = volt33;
313 orte->pwr_voltage.voltageBAT = voltBAT;
317 double volt80, volt50;
318 volt50 = ((frame.data[0] << 24) | (frame.data[1] << 16) | \
319 (frame.data[2] << 8) | (frame.data[3]))/10000.0;
320 volt80 = ((frame.data[4] << 24) | (frame.data[5] << 16) | \
321 (frame.data[6] << 8) | (frame.data[7]))/10000.0;
322 orte->pwr_voltage.voltage80 = volt80;
323 orte->pwr_voltage.voltage50 = volt50;
325 ORTEPublicationSend(orte->publication_pwr_voltage);
328 case CAN_ADC_1: /* data from the sharp sensor measuring distance of the puck (column element) */
329 //orte->puck_distance.distance = ((frame.data[0]<<8)|(frame.data[1]));
330 orte->puck_distance.distance = puckSharp_ir2mm((frame.data[0]<<8)|(frame.data[1]))/1000.0;
331 ORTEPublicationSend(orte->publication_puck_distance);
336 orte->cmu.color = frame.data[0];
337 ORTEPublicationSend(orte->publication_cmu);
341 // printf("received CAN msg with unknown id: %x\n",frame.can_id);
346 void rcv_pwr_ctrl_cb (const ORTERecvInfo *info, void *vinstance,
347 void *recvCallBackParam)
349 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
351 switch (info->status) {
353 set_pwr_ctrl(orte_data);
356 //printf("ORTE deadline occurred - PWR_CTRL receive\n");
361 void rcv_can_msg_cb (const ORTERecvInfo *info, void *vinstance,
362 void *recvCallBackParam)
364 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
366 switch (info->status) {
368 send_can_msg(orte_data);
371 //printf("ORTE deadline occurred - PWR_CTRL receive\n");
375 void rcv_pwr_alert_cb (const ORTERecvInfo *info, void *vinstance,
376 void *recvCallBackParam)
379 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
380 switch (info->status) {
382 set_pwr_alert(orte_data);
385 //printf("ORTE deadline occurred - PWR_CTRL receive\n");
390 void rcv_motion_speed_cb(const ORTERecvInfo *info, void *vinstance,
391 void *recvCallBackParam)
393 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
395 switch (info->status) {
397 /* reversing motion direction, as it is different, than last year */
398 orte_data->motion_speed.left *= -1;
399 orte_data->motion_speed.right *=-1;
400 set_motor_speed(orte_data);
401 /*printf("motor cmd received\n");*/
404 printf("motor cmd deadline occurred, stopping motors\n");
405 orte_data->motion_speed.left = 0;
406 orte_data->motion_speed.right = 0;
407 set_motor_speed(orte_data);
412 void rcv_lift_cb (const ORTERecvInfo *info, void *vinstance,
413 void *recvCallBackParam)
415 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
417 switch (info->status) {
422 // printf("ORTE deadline occurred - lift receive\n");
427 void rcv_chelae_cb (const ORTERecvInfo *info, void *vinstance,
428 void *recvCallBackParam)
430 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
432 switch (info->status) {
434 set_chelae(orte_data);
437 // printf("ORTE deadline occurred - chelae receive\n");
442 void rcv_belts_cb (const ORTERecvInfo *info, void *vinstance,
443 void *recvCallBackParam)
445 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
447 switch (info->status) {
449 set_belts(orte_data);
452 // printf("ORTE deadline occurred - belts receive\n");
457 void rcv_holder_cb (const ORTERecvInfo *info, void *vinstance,
458 void *recvCallBackParam)
460 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
462 switch (info->status) {
464 set_holder(orte_data);
467 // printf("ORTE deadline occurred - holder receive\n");
472 void rcv_pusher_cb (const ORTERecvInfo *info, void *vinstance,
473 void *recvCallBackParam)
475 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
477 switch (info->status) {
479 set_pusher(orte_data);
482 // printf("ORTE deadline occurred - pusher receive\n");
487 void rcv_hokuyo_pitch_cb (const ORTERecvInfo *info, void *vinstance,
488 void *recvCallBackParam)
490 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
492 switch (info->status) {
494 set_hokuyo_pitch(orte_data);
497 // printf("ORTE deadline occurred - hokuyo pitch receive\n");
503 int main(int argc, char *argv[])
506 struct timeval timeout;
511 struct robottype_orte_data orte;
512 struct can_frame frame;
514 if (cand_init() < 0) {
515 printf("cand: init failed\n");
518 printf("cand: init OK\n");
523 /* orte initialization */
524 if(robottype_roboorte_init(&orte)) {
525 printf("Roboorte initialization failed! Exiting...\n");
529 printf("Roboorte OK\n");
531 /* creating publishers */
532 robottype_publisher_pwr_voltage_create(&orte, NULL, NULL);
533 robottype_publisher_motion_status_create(&orte, NULL, NULL);
534 robottype_publisher_motion_irc_create(&orte, NULL, NULL);
535 robottype_publisher_robot_cmd_create(&orte, NULL, NULL);
536 robottype_publisher_cmu_create(&orte, NULL, NULL);
537 robottype_publisher_corr_trig_create(&orte, NULL, NULL);
538 robottype_publisher_corr_distances_create(&orte, NULL, NULL);
539 robottype_publisher_puck_distance_create(&orte, NULL, NULL);
540 printf("Publishers OK\n");
542 /* creating subscribers */
543 robottype_subscriber_pwr_ctrl_create(&orte, rcv_pwr_ctrl_cb, &orte);
544 robottype_subscriber_pwr_alert_create(&orte, rcv_pwr_alert_cb, &orte);
545 robottype_subscriber_motion_speed_create(&orte, rcv_motion_speed_cb, &orte);
546 robottype_subscriber_can_msg_create(&orte, rcv_can_msg_cb, &orte);
547 robottype_subscriber_lift_create(&orte, rcv_lift_cb, &orte);
548 robottype_subscriber_chelae_create(&orte, rcv_chelae_cb, &orte);
549 robottype_subscriber_belts_create(&orte, rcv_belts_cb, &orte);
550 robottype_subscriber_holder_create(&orte, rcv_holder_cb, &orte);
551 robottype_subscriber_pusher_create(&orte, rcv_pusher_cb, &orte);
552 robottype_subscriber_hokuyo_pitch_create(&orte, rcv_hokuyo_pitch_cb, &orte);
554 printf("subscribers OK\n");
558 FD_ZERO(&read_fd_set);
559 FD_SET(sock, &read_fd_set);
561 timeout.tv_usec = 10000;
563 ret = select(FD_SETSIZE, &read_fd_set, NULL, NULL, &timeout);
565 perror("cand: select() error");
569 /* timeout expired, nothing to be done */
573 if (!FD_ISSET(sock, &read_fd_set))
576 /* read data from SocketCAN */
577 size = read(sock, &frame, sizeof(struct can_frame));
580 cand_parse_frame(&orte, frame);