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