liu isad / rosserial_mbed_lib

Dependencies:   MODSERIAL

Fork of rosserial_mbed_lib by nucho

Committer:
isad
Date:
Thu May 08 06:34:18 2014 +0000
Revision:
5:c30c76e8c3f1
Parent:
3:1cf99502f396
rosserial_mbed for ROS hydro

Who changed what in which revision?

UserRevisionLine numberNew contents of line
isad 5:c30c76e8c3f1 1 /*
isad 5:c30c76e8c3f1 2 * Software License Agreement (BSD License)
isad 5:c30c76e8c3f1 3 *
isad 5:c30c76e8c3f1 4 * Copyright (c) 2011, Willow Garage, Inc.
isad 5:c30c76e8c3f1 5 * All rights reserved.
isad 5:c30c76e8c3f1 6 *
isad 5:c30c76e8c3f1 7 * Redistribution and use in source and binary forms, with or without
isad 5:c30c76e8c3f1 8 * modification, are permitted provided that the following conditions
isad 5:c30c76e8c3f1 9 * are met:
isad 5:c30c76e8c3f1 10 *
isad 5:c30c76e8c3f1 11 * * Redistributions of source code must retain the above copyright
isad 5:c30c76e8c3f1 12 * notice, this list of conditions and the following disclaimer.
isad 5:c30c76e8c3f1 13 * * Redistributions in binary form must reproduce the above
isad 5:c30c76e8c3f1 14 * copyright notice, this list of conditions and the following
isad 5:c30c76e8c3f1 15 * disclaimer in the documentation and/or other materials provided
isad 5:c30c76e8c3f1 16 * with the distribution.
isad 5:c30c76e8c3f1 17 * * Neither the name of Willow Garage, Inc. nor the names of its
isad 5:c30c76e8c3f1 18 * contributors may be used to endorse or promote prducts derived
isad 5:c30c76e8c3f1 19 * from this software without specific prior written permission.
isad 5:c30c76e8c3f1 20 *
isad 5:c30c76e8c3f1 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
isad 5:c30c76e8c3f1 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
isad 5:c30c76e8c3f1 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
isad 5:c30c76e8c3f1 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
isad 5:c30c76e8c3f1 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
isad 5:c30c76e8c3f1 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
isad 5:c30c76e8c3f1 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
isad 5:c30c76e8c3f1 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
isad 5:c30c76e8c3f1 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
isad 5:c30c76e8c3f1 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
isad 5:c30c76e8c3f1 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
isad 5:c30c76e8c3f1 32 * POSSIBILITY OF SUCH DAMAGE.
isad 5:c30c76e8c3f1 33 */
isad 5:c30c76e8c3f1 34
isad 5:c30c76e8c3f1 35 #ifndef _ROS_SERVICE_CLIENT_H_
isad 5:c30c76e8c3f1 36 #define _ROS_SERVICE_CLIENT_H_
isad 5:c30c76e8c3f1 37
isad 5:c30c76e8c3f1 38 #include "rosserial_msgs/TopicInfo.h"
isad 5:c30c76e8c3f1 39
isad 5:c30c76e8c3f1 40 #include "publisher.h"
isad 5:c30c76e8c3f1 41 #include "subscriber.h"
isad 5:c30c76e8c3f1 42
isad 5:c30c76e8c3f1 43 namespace ros {
isad 5:c30c76e8c3f1 44
isad 5:c30c76e8c3f1 45 template<typename MReq , typename MRes>
isad 5:c30c76e8c3f1 46 class ServiceClient : public Subscriber_ {
isad 5:c30c76e8c3f1 47 public:
isad 5:c30c76e8c3f1 48 ServiceClient(const char* topic_name) :
isad 5:c30c76e8c3f1 49 pub(topic_name, &req, rosserial_msgs::TopicInfo::ID_SERVICE_CLIENT + rosserial_msgs::TopicInfo::ID_PUBLISHER)
isad 5:c30c76e8c3f1 50 {
isad 5:c30c76e8c3f1 51 this->topic_ = topic_name;
isad 5:c30c76e8c3f1 52 this->waiting = true;
isad 5:c30c76e8c3f1 53 }
isad 5:c30c76e8c3f1 54
isad 5:c30c76e8c3f1 55 virtual void call(const MReq & request, MRes & response)
isad 5:c30c76e8c3f1 56 {
isad 5:c30c76e8c3f1 57 if(!pub.nh_->connected()) return;
isad 5:c30c76e8c3f1 58 ret = &response;
isad 5:c30c76e8c3f1 59 waiting = true;
isad 5:c30c76e8c3f1 60 pub.publish(&request);
isad 5:c30c76e8c3f1 61 while(waiting && pub.nh_->connected())
isad 5:c30c76e8c3f1 62 if(pub.nh_->spinOnce() < 0) break;
isad 5:c30c76e8c3f1 63 }
isad 5:c30c76e8c3f1 64
isad 5:c30c76e8c3f1 65 // these refer to the subscriber
isad 5:c30c76e8c3f1 66 virtual void callback(unsigned char *data){
isad 5:c30c76e8c3f1 67 ret->deserialize(data);
isad 5:c30c76e8c3f1 68 waiting = false;
isad 5:c30c76e8c3f1 69 }
isad 5:c30c76e8c3f1 70 virtual const char * getMsgType(){ return this->resp.getType(); }
isad 5:c30c76e8c3f1 71 virtual const char * getMsgMD5(){ return this->resp.getMD5(); }
isad 5:c30c76e8c3f1 72 virtual int getEndpointType(){ return rosserial_msgs::TopicInfo::ID_SERVICE_CLIENT + rosserial_msgs::TopicInfo::ID_SUBSCRIBER; }
isad 5:c30c76e8c3f1 73
isad 5:c30c76e8c3f1 74 MReq req;
isad 5:c30c76e8c3f1 75 MRes resp;
isad 5:c30c76e8c3f1 76 MRes * ret;
isad 5:c30c76e8c3f1 77 bool waiting;
isad 5:c30c76e8c3f1 78 Publisher pub;
isad 5:c30c76e8c3f1 79 };
isad 5:c30c76e8c3f1 80
isad 5:c30c76e8c3f1 81 }
isad 5:c30c76e8c3f1 82
isad 5:c30c76e8c3f1 83 #endif