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