This program is porting rosserial_arduino for mbed http://www.ros.org/wiki/rosserial_arduino This program supported the revision of 169 of rosserial.

Dependencies:  

Dependents:   rosserial_mbed robot_S2

Committer:
nucho
Date:
Wed Feb 29 23:00:21 2012 +0000
Revision:
4:684f39d0c346
Parent:
3:1cf99502f396

        

Who changed what in which revision?

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