ddsproxyjoy.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2012  DFKI GmbH, Bremen, Germany
00003 *
00004 *  This file is free software: you may copy, redistribute and/or modify it
00005 *  under the terms of the GNU General Public License as published by the
00006 *  Free Software Foundation, either version 3 of the License, or (at your
00007 *  option) any later version.
00008 *
00009 *  This file is distributed in the hope that it will be useful, but
00010 *  WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 *  General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 *
00017 *
00018 *   Author: Ronny Hartanto (ronny.hartanto@dfki.de)
00019 *
00020 *       FILE --- ddsproxyjoy.cpp
00021 *
00022 *  Created on: Aug 3, 2012
00023 */
00024 
00025 #include "ddsproxyjoy.h"
00026 #include "ros/ros.h"
00027 
00028 DDSProxyJoy::DDSProxyJoy() {
00029 }
00030 
00031 DDSProxyJoy::~DDSProxyJoy() {
00032 
00033 }
00034 
00035 void DDSProxyJoy::registerProxy() {
00036         // Init participant for DDS. And typesupport for ddsJoy
00037         ddsJoyTypeSupport_var data = new ddsJoyTypeSupport();
00038         DDSManager::instance().registerType(dds_domain_name, data.in());
00039         m_data_topic = DDSManager::instance().createTopic(dds_domain_name.c_str(),
00040                         dds_message_name.c_str(), data->get_type_name());
00041         DataWriter_ptr writer = DDSManager::instance().createWriter(
00042                         dds_domain_name.c_str(), ros_message_type.c_str(), m_data_topic);
00043         DataReader_ptr reader = DDSManager::instance().createReader(
00044                         dds_domain_name.c_str(), ros_message_type.c_str(),
00045                         m_data_topic.in());
00046         m_data_writer = ddsJoyDataWriter::_narrow(writer);
00047         m_data_reader = ddsJoyDataReader::_narrow(reader);
00048         // Init ROS
00049         pub = nh_.advertise<sensor_msgs::Joy>("joy", 10);
00050         sub = nh_.subscribe("joy", 1, &DDSProxyJoy::messageCallback <sensor_msgs::Joy>, this);
00051         ROS_INFO("DDSJoy Initialization completed.");
00052 }
00053 
00054 template <class T> void DDSProxyJoy::messageCallback(
00055                 const ros::MessageEvent<T const>& event) {
00056         ROS_DEBUG("[Debug]: Received message event of type sensor_msgs::Joy\n");
00057         if ((event.getPublisherName() != node_name) && (event.getPublisherName() != local_publisher_node_name)) {
00058                 ROS_DEBUG("Received message from %s", event.getPublisherName().c_str());
00059                 ros::Time receipt_time = event.getReceiptTime();
00060                 const sensor_msgs::JoyConstPtr& msg = event.getMessage();
00061                 ddsJoy *ddsJoy_msg = new ddsJoy();
00062 
00063                 // Filling in data for *ddsJoy.idl*
00064                 ddsJoy_msg->header.frame_id = std::string(msg->header.frame_id).c_str();
00065                 ddsJoy_msg->header.seq = msg->header.seq;
00066                 ddsJoy_msg->header.stamp.nsec = msg->header.stamp.nsec;
00067                 ddsJoy_msg->header.stamp.sec = msg->header.stamp.sec;
00068                 ddsJoy_msg->axes.length(msg->axes.size());
00069 
00070                 for (int i = 0; i < msg->axes.size(); i++) {
00071                         ddsJoy_msg->axes[i] = msg->axes[i];
00072                 }
00073 
00074                 ddsJoy_msg->buttons.length(msg->buttons.size());
00075 
00076                 for (int i = 0; i < msg->buttons.size(); i++) {
00077                         ddsJoy_msg->buttons[i] = msg->buttons[i];
00078                 }
00079 
00080                 ReturnCode_t ret = m_data_writer->write(*ddsJoy_msg, NULL);
00081                 checkStatus(ret, "ddsJoy::Write");
00082                 // ROS_INFO("Print return code %d", ret);
00083         }
00084 }
00085 
00086 void DDSProxyJoy::update() {
00087         ddsJoySeq_var ddsJoy_msgSeq = new ddsJoySeq();
00088         SampleInfoSeq_var infoSeq = new SampleInfoSeq();
00089 
00090         //ROS_INFO_ONCE("while inner loop");
00091         //ReportStatus_t status
00092         m_data_reader->take(*ddsJoy_msgSeq, *infoSeq, LENGTH_UNLIMITED,
00093                         ANY_SAMPLE_STATE, ANY_VIEW_STATE, ANY_INSTANCE_STATE);
00094         //checkStatus(status, "MsgDataReader::take");
00095         //ROS_INFO("Reading data %d", ddsJoy_msgSeq->length());
00096         //ROS_INFO("Reading info %d",infoSeq->length());
00097         for (ULong i = 0; i < ddsJoy_msgSeq->length(); ++i) {
00098                 //ROS_INFO("publishing message");
00099                 sensor_msgs::Joy ddsJoy_msg;
00100                 ddsJoy *ROS_ddsJoy_msg;
00101                 ROS_ddsJoy_msg = &(ddsJoy_msgSeq[i]);
00102                 ddsJoy_msg.header.stamp.sec = ROS_ddsJoy_msg->header.stamp.sec;
00103                 ddsJoy_msg.header.stamp.nsec = ROS_ddsJoy_msg->header.stamp.nsec;
00104                 ddsJoy_msg.header.seq = ROS_ddsJoy_msg->header.seq;
00105                 ddsJoy_msg.header.frame_id = ROS_ddsJoy_msg->header.frame_id;
00106 
00107                 ddsJoy_msg.axes.resize(ROS_ddsJoy_msg->axes.length());
00108                 for (int i = 0; i < ROS_ddsJoy_msg->axes.length(); i++) {
00109                         ddsJoy_msg.axes[i] = ROS_ddsJoy_msg->axes[i];
00110                 }
00111 
00112                 ddsJoy_msg.buttons.resize(ROS_ddsJoy_msg->buttons.length());
00113                 for (int i = 0; i < ROS_ddsJoy_msg->buttons.length(); i++) {
00114                         ddsJoy_msg.buttons[i] = ROS_ddsJoy_msg->buttons[i];
00115                 }
00116                 pub.publish(ddsJoy_msg);
00117                 m_data_reader->return_loan(*ddsJoy_msgSeq, *infoSeq);
00118                 //checkStatus(status, "MsgDataReader::return_loan");
00119         }
00120 }
00121 
00122 


proxyJoy
Author(s): Ronny Hartanto
autogenerated on Mon Oct 6 2014 06:54:35