rtai_ros_bridge.cpp
Go to the documentation of this file.
00001 #include "ros/ros.h"
00002 #include "std_msgs/UInt8MultiArray.h"
00003 
00004 #include <mbx.h>
00005 
00006 
00007 #ifndef TASKNAME
00008         #define TASKNAME "ROSTSK" 
00009 #endif
00010 #ifndef MAILBOX_NAME_IN
00011         #define MAILBOX_NAME_IN "DRIMBX" //"ROS_IN"
00012 #endif
00013 #ifndef MAILBOX_NAME_OUT
00014         #define MAILBOX_NAME_OUT "DROMBX" //"ROSOUT"
00015 #endif
00016 #ifndef SAMPLETIME
00017         #define SAMPLETIME 500   /* must correspond to base time of Simulink/RTW target */
00018 #endif                           /* sample rate is 500Hz for bipedRobin */
00019 
00020 static RT_TASK *mytask;
00021 
00022 MBX *mbxI, *mbxO;
00023 int closingMbxFlagI = 0;
00024 int closingMbxFlagO = 0;
00025 
00026 int initializeRTMBX(void);
00027 int terminateRTMBX(void);
00028 
00029 
00035 void rtaiDataCallback(const std_msgs::UInt8MultiArray rtaiData) {
00036   int state=0;
00037   char dataIn[100000];
00038   ROS_INFO("Received MSG size to be sent: %d", rtaiData.data.size());
00039   copy(rtaiData.data.begin(), rtaiData.data.end(), (char*)&dataIn);
00040   if ((state = sendViaMbx(mbxI, (void*)&dataIn, rtaiData.data.size()))) {
00041     ROS_INFO("problem when sending via mbx %d", state);
00042   }
00043 }
00044 
00045 int main(int argc, char **argv)
00046 {
00047   /* init ROS */
00048   ros::init(argc, argv, "ros_rtai_bridge_publisher");
00049   ros::NodeHandle n;
00050   ros::Publisher rtai_dataOut = n.advertise<std_msgs::UInt8MultiArray>("data_from_rtai", 1000, true);
00051   ros::Subscriber rtai_dataIn = n.subscribe<std_msgs::UInt8MultiArray>("data_to_rtai", 1000, rtaiDataCallback);
00052   ros::Rate loop_rate(SAMPLETIME); 
00053 
00054   /* init RTAI */
00055   ROS_INFO("Starting ROS RTAI bridge ...");
00056   if (initializeRTMBX() <= 0) {
00057     ROS_INFO("Error creating MBX Connection");
00058     ros::shutdown();
00059   }
00060 
00061   char dataOut[100000];
00062   ROS_INFO("Entering the loop ...");
00063   while (ros::ok()) { // SIGINT causes ros::ok() to be false 
00064     int count=0;
00065     count = receiveViaMbx(mbxO, (void*) &dataOut, sizeof(dataOut));
00066 
00067     if (count > 0) { //publish received data to ROS
00068       std_msgs::UInt8MultiArray msgOut;
00069       //TODO: fill out header of array!
00070       for (int i=0; i<count; i++) {
00071         msgOut.data.push_back(dataOut[i]);
00072       }
00073       rtai_dataOut.publish(msgOut);
00074     }
00075 
00076     ros::spinOnce();
00077     loop_rate.sleep();
00078   }
00079 
00080   ROS_INFO("Terminating ROS RTAI bridge ...");
00081   terminateRTMBX();
00082 
00083   return 0;
00084 }
00085 
00086 int initializeRTMBX(void) {
00087         rt_allow_nonroot_hrt();
00088 
00089         if (!(mytask = rt_task_init(nam2num(TASKNAME), 100, 0, 0))) {
00090                 ROS_INFO("Error creating task TASK");
00091                 return -1;
00092         }
00093         ROS_INFO("Initialized RTAI task...");
00094 
00095         mlockall(MCL_CURRENT|MCL_FUTURE);
00096 
00097         //opening MBX_IN
00098         if (openExistingMbx(&mbxI, MAILBOX_NAME_IN)) {
00099                 ROS_INFO("Mailbox DRIMBX not opened yet, going to open mailbox ...");
00100                 closingMbxFlagI = 1;
00101                 if (openNewMbxBuf(&mbxI, MAILBOX_NAME_IN, 4804, 100)) {
00102                         ROS_INFO("Could not open mailbox DRIMBX");
00103                         return -1;
00104                 } else {
00105                         ROS_INFO("Opened mailbox IN...");
00106                 }
00107         } else {
00108                 ROS_INFO("Mailbox IN was already opened ...");
00109         }
00110 
00111         //opening MBX_OUT
00112         if (openExistingMbx(&mbxO, MAILBOX_NAME_OUT)) {
00113                 ROS_INFO("Mailbox DROMBX not opened yet, going to open mailbox ...");
00114                 closingMbxFlagO = 1;
00115                 if (openNewMbxBuf(&mbxO, MAILBOX_NAME_OUT, 4804, 100)) {
00116                         ROS_INFO("Could not open mailbox DROMBX");
00117                         return -1;
00118                 } else {
00119                         ROS_INFO("Opened mailbox OUT...");
00120                 }
00121 
00122         } else {
00123                 ROS_INFO("Mailbox OUT was already opened ...");
00124         }
00125 
00126         return 1;
00127 }
00128 
00129 int terminateRTMBX(void) {
00130         if (closingMbxFlagI) {
00131                 ROS_INFO("Closing MBX I ...");
00132                 closeMbx(mbxI);
00133         }
00134         if (closingMbxFlagO) {
00135                 ROS_INFO("Closing MBX O ...");
00136                 closeMbx(mbxO);
00137         }
00138         munlockall();
00139         rt_task_delete(mytask);
00140         ROS_INFO("Deleted RTAI task...\n");
00141         return 1;
00142 }


rtai_ros_bridge
Author(s): Johannes Mayr
autogenerated on Mon Jan 6 2014 11:08:15