ros melodic library with custom message

Dependents:   Robot_team1_QEI_Douglas Robot_team1

Committer:
florine_van
Date:
Tue Dec 03 09:39:29 2019 +0000
Revision:
3:b964e3f71102
Parent:
0:020db18a476d
Clean code and remove unused lines

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scarter1 0:020db18a476d 1 /*
scarter1 0:020db18a476d 2 * Software License Agreement (BSD License)
scarter1 0:020db18a476d 3 *
scarter1 0:020db18a476d 4 * Copyright (c) 2011, Willow Garage, Inc.
scarter1 0:020db18a476d 5 * All rights reserved.
scarter1 0:020db18a476d 6 *
scarter1 0:020db18a476d 7 * Redistribution and use in source and binary forms, with or without
scarter1 0:020db18a476d 8 * modification, are permitted provided that the following conditions
scarter1 0:020db18a476d 9 * are met:
scarter1 0:020db18a476d 10 *
scarter1 0:020db18a476d 11 * * Redistributions of source code must retain the above copyright
scarter1 0:020db18a476d 12 * notice, this list of conditions and the following disclaimer.
scarter1 0:020db18a476d 13 * * Redistributions in binary form must reproduce the above
scarter1 0:020db18a476d 14 * copyright notice, this list of conditions and the following
scarter1 0:020db18a476d 15 * disclaimer in the documentation and/or other materials provided
scarter1 0:020db18a476d 16 * with the distribution.
scarter1 0:020db18a476d 17 * * Neither the name of Willow Garage, Inc. nor the names of its
scarter1 0:020db18a476d 18 * contributors may be used to endorse or promote prducts derived
scarter1 0:020db18a476d 19 * from this software without specific prior written permission.
scarter1 0:020db18a476d 20 *
scarter1 0:020db18a476d 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
scarter1 0:020db18a476d 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
scarter1 0:020db18a476d 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
scarter1 0:020db18a476d 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
scarter1 0:020db18a476d 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
scarter1 0:020db18a476d 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
scarter1 0:020db18a476d 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
scarter1 0:020db18a476d 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
scarter1 0:020db18a476d 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
scarter1 0:020db18a476d 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
scarter1 0:020db18a476d 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
scarter1 0:020db18a476d 32 * POSSIBILITY OF SUCH DAMAGE.
scarter1 0:020db18a476d 33 */
scarter1 0:020db18a476d 34
scarter1 0:020db18a476d 35 #ifndef ROS_SUBSCRIBER_H_
scarter1 0:020db18a476d 36 #define ROS_SUBSCRIBER_H_
scarter1 0:020db18a476d 37
scarter1 0:020db18a476d 38 #include "rosserial_msgs/TopicInfo.h"
scarter1 0:020db18a476d 39
scarter1 0:020db18a476d 40 namespace ros
scarter1 0:020db18a476d 41 {
scarter1 0:020db18a476d 42
scarter1 0:020db18a476d 43 /* Base class for objects subscribers. */
scarter1 0:020db18a476d 44 class Subscriber_
scarter1 0:020db18a476d 45 {
scarter1 0:020db18a476d 46 public:
scarter1 0:020db18a476d 47 virtual void callback(unsigned char *data) = 0;
scarter1 0:020db18a476d 48 virtual int getEndpointType() = 0;
scarter1 0:020db18a476d 49
scarter1 0:020db18a476d 50 // id_ is set by NodeHandle when we advertise
scarter1 0:020db18a476d 51 int id_;
scarter1 0:020db18a476d 52
scarter1 0:020db18a476d 53 virtual const char * getMsgType() = 0;
scarter1 0:020db18a476d 54 virtual const char * getMsgMD5() = 0;
scarter1 0:020db18a476d 55 const char * topic_;
scarter1 0:020db18a476d 56 };
scarter1 0:020db18a476d 57
scarter1 0:020db18a476d 58 /* Bound function subscriber. */
scarter1 0:020db18a476d 59 template<typename MsgT, typename ObjT = void>
scarter1 0:020db18a476d 60 class Subscriber: public Subscriber_
scarter1 0:020db18a476d 61 {
scarter1 0:020db18a476d 62 public:
scarter1 0:020db18a476d 63 typedef void(ObjT::*CallbackT)(const MsgT&);
scarter1 0:020db18a476d 64 MsgT msg;
scarter1 0:020db18a476d 65
scarter1 0:020db18a476d 66 Subscriber(const char * topic_name, CallbackT cb, ObjT* obj, int endpoint = rosserial_msgs::TopicInfo::ID_SUBSCRIBER) :
scarter1 0:020db18a476d 67 cb_(cb),
scarter1 0:020db18a476d 68 obj_(obj),
scarter1 0:020db18a476d 69 endpoint_(endpoint)
scarter1 0:020db18a476d 70 {
scarter1 0:020db18a476d 71 topic_ = topic_name;
scarter1 0:020db18a476d 72 };
scarter1 0:020db18a476d 73
scarter1 0:020db18a476d 74 virtual void callback(unsigned char* data)
scarter1 0:020db18a476d 75 {
scarter1 0:020db18a476d 76 msg.deserialize(data);
scarter1 0:020db18a476d 77 (obj_->*cb_)(msg);
scarter1 0:020db18a476d 78 }
scarter1 0:020db18a476d 79
scarter1 0:020db18a476d 80 virtual const char * getMsgType()
scarter1 0:020db18a476d 81 {
scarter1 0:020db18a476d 82 return this->msg.getType();
scarter1 0:020db18a476d 83 }
scarter1 0:020db18a476d 84 virtual const char * getMsgMD5()
scarter1 0:020db18a476d 85 {
scarter1 0:020db18a476d 86 return this->msg.getMD5();
scarter1 0:020db18a476d 87 }
scarter1 0:020db18a476d 88 virtual int getEndpointType()
scarter1 0:020db18a476d 89 {
scarter1 0:020db18a476d 90 return endpoint_;
scarter1 0:020db18a476d 91 }
scarter1 0:020db18a476d 92
scarter1 0:020db18a476d 93 private:
scarter1 0:020db18a476d 94 CallbackT cb_;
scarter1 0:020db18a476d 95 ObjT* obj_;
scarter1 0:020db18a476d 96 int endpoint_;
scarter1 0:020db18a476d 97 };
scarter1 0:020db18a476d 98
scarter1 0:020db18a476d 99 /* Standalone function subscriber. */
scarter1 0:020db18a476d 100 template<typename MsgT>
scarter1 0:020db18a476d 101 class Subscriber<MsgT, void>: public Subscriber_
scarter1 0:020db18a476d 102 {
scarter1 0:020db18a476d 103 public:
scarter1 0:020db18a476d 104 typedef void(*CallbackT)(const MsgT&);
scarter1 0:020db18a476d 105 MsgT msg;
scarter1 0:020db18a476d 106
scarter1 0:020db18a476d 107 Subscriber(const char * topic_name, CallbackT cb, int endpoint = rosserial_msgs::TopicInfo::ID_SUBSCRIBER) :
scarter1 0:020db18a476d 108 cb_(cb),
scarter1 0:020db18a476d 109 endpoint_(endpoint)
scarter1 0:020db18a476d 110 {
scarter1 0:020db18a476d 111 topic_ = topic_name;
scarter1 0:020db18a476d 112 };
scarter1 0:020db18a476d 113
scarter1 0:020db18a476d 114 virtual void callback(unsigned char* data)
scarter1 0:020db18a476d 115 {
scarter1 0:020db18a476d 116 msg.deserialize(data);
scarter1 0:020db18a476d 117 this->cb_(msg);
scarter1 0:020db18a476d 118 }
scarter1 0:020db18a476d 119
scarter1 0:020db18a476d 120 virtual const char * getMsgType()
scarter1 0:020db18a476d 121 {
scarter1 0:020db18a476d 122 return this->msg.getType();
scarter1 0:020db18a476d 123 }
scarter1 0:020db18a476d 124 virtual const char * getMsgMD5()
scarter1 0:020db18a476d 125 {
scarter1 0:020db18a476d 126 return this->msg.getMD5();
scarter1 0:020db18a476d 127 }
scarter1 0:020db18a476d 128 virtual int getEndpointType()
scarter1 0:020db18a476d 129 {
scarter1 0:020db18a476d 130 return endpoint_;
scarter1 0:020db18a476d 131 }
scarter1 0:020db18a476d 132
scarter1 0:020db18a476d 133 private:
scarter1 0:020db18a476d 134 CallbackT cb_;
scarter1 0:020db18a476d 135 int endpoint_;
scarter1 0:020db18a476d 136 };
scarter1 0:020db18a476d 137
scarter1 0:020db18a476d 138 }
scarter1 0:020db18a476d 139
scarter1 0:020db18a476d 140 #endif