|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | *
|
16 |
| -*/ |
| 16 | + */ |
17 | 17 |
|
18 | 18 | #include <iostream>
|
19 | 19 | #include <vector>
|
|
24 | 24 |
|
25 | 25 | #include <qi/anyobject.hpp>
|
26 | 26 |
|
27 |
| -#include <naoqi_driver/recorder/globalrecorder.hpp> |
28 | 27 | #include <naoqi_driver/message_actions.h>
|
| 28 | +#include <naoqi_driver/recorder/globalrecorder.hpp> |
29 | 29 |
|
30 | 30 | #include "touch.hpp"
|
31 | 31 |
|
32 | 32 | namespace naoqi
|
33 | 33 | {
|
34 | 34 |
|
35 |
| -template<class T> |
| 35 | +template <class T> |
36 | 36 | TouchEventRegister<T>::TouchEventRegister()
|
37 | 37 | {
|
38 | 38 | }
|
39 | 39 |
|
40 |
| -template<class T> |
41 |
| -TouchEventRegister<T>::TouchEventRegister( const std::string& name, const std::vector<std::string> keys, const float& frequency, const qi::SessionPtr& session ) |
42 |
| - : p_memory_( session->service("ALMemory").value()), |
43 |
| - isStarted_(false), |
44 |
| - isPublishing_(false), |
45 |
| - isRecording_(false), |
46 |
| - isDumping_(false) |
| 40 | +template <class T> |
| 41 | +TouchEventRegister<T>::TouchEventRegister(const std::string& name, |
| 42 | + const std::vector<std::string> keys, |
| 43 | + const float& frequency, |
| 44 | + const qi::SessionPtr& session, |
| 45 | + robot::NaoqiVersion naoqi_version) |
| 46 | + : session_(std::move(session)), naoqi_version_(std::move(naoqi_version)), |
| 47 | + p_memory_(session_->service("ALMemory").value()), isStarted_(false), |
| 48 | + isPublishing_(false), isRecording_(false), isDumping_(false) |
47 | 49 | {
|
48 |
| - publisher_ = boost::make_shared<publisher::BasicPublisher<T> >( name ); |
49 |
| - //recorder_ = boost::make_shared<recorder::BasicEventRecorder<T> >( name ); |
50 |
| - converter_ = boost::make_shared<converter::TouchEventConverter<T> >( name, frequency, session ); |
51 |
| - |
52 |
| - converter_->registerCallback( message_actions::PUBLISH, boost::bind(&publisher::BasicPublisher<T>::publish, publisher_, boost::placeholders::_1) ); |
53 |
| - //converter_->registerCallback( message_actions::RECORD, boost::bind(&recorder::BasicEventRecorder<T>::write, recorder_, _1) ); |
54 |
| - //converter_->registerCallback( message_actions::LOG, boost::bind(&recorder::BasicEventRecorder<T>::bufferize, recorder_, _1) ); |
| 50 | + publisher_ = boost::make_shared<publisher::BasicPublisher<T>>(name); |
| 51 | + // recorder_ = boost::make_shared<recorder::BasicEventRecorder<T> >( name ); |
| 52 | + converter_ = boost::make_shared<converter::TouchEventConverter<T>>( |
| 53 | + name, frequency, session); |
| 54 | + |
| 55 | + converter_->registerCallback( |
| 56 | + message_actions::PUBLISH, |
| 57 | + boost::bind(&publisher::BasicPublisher<T>::publish, publisher_, |
| 58 | + boost::placeholders::_1)); |
| 59 | + // converter_->registerCallback( message_actions::RECORD, |
| 60 | + // boost::bind(&recorder::BasicEventRecorder<T>::write, recorder_, _1) ); |
| 61 | + // converter_->registerCallback( message_actions::LOG, |
| 62 | + // boost::bind(&recorder::BasicEventRecorder<T>::bufferize, recorder_, _1) |
| 63 | + // ); |
55 | 64 |
|
56 | 65 | keys_.resize(keys.size());
|
57 | 66 | size_t i = 0;
|
58 |
| - for(std::vector<std::string>::const_iterator it = keys.begin(); it != keys.end(); ++it, ++i) |
| 67 | + for (std::vector<std::string>::const_iterator it = keys.begin(); |
| 68 | + it != keys.end(); ++it, ++i) |
59 | 69 | keys_[i] = *it;
|
60 | 70 |
|
61 | 71 | name_ = name;
|
62 | 72 | }
|
63 | 73 |
|
64 |
| -template<class T> |
| 74 | +template <class T> |
65 | 75 | TouchEventRegister<T>::~TouchEventRegister()
|
66 | 76 | {
|
67 | 77 | stopProcess();
|
68 | 78 | }
|
69 | 79 |
|
70 |
| -template<class T> |
| 80 | +template <class T> |
71 | 81 | void TouchEventRegister<T>::resetPublisher(rclcpp::Node* node)
|
72 | 82 | {
|
73 | 83 | publisher_->reset(node);
|
74 | 84 | }
|
75 | 85 |
|
76 |
| -template<class T> |
77 |
| -void TouchEventRegister<T>::resetRecorder( boost::shared_ptr<naoqi::recorder::GlobalRecorder> gr ) |
| 86 | +template <class T> |
| 87 | +void TouchEventRegister<T>::resetRecorder( |
| 88 | + boost::shared_ptr<naoqi::recorder::GlobalRecorder> gr) |
78 | 89 | {
|
79 |
| - //recorder_->reset(gr, converter_->frequency()); |
| 90 | + // recorder_->reset(gr, converter_->frequency()); |
80 | 91 | }
|
81 | 92 |
|
82 |
| -template<class T> |
| 93 | +template <class T> |
83 | 94 | void TouchEventRegister<T>::startProcess()
|
84 | 95 | {
|
85 | 96 | boost::mutex::scoped_lock start_lock(mutex_);
|
86 | 97 | if (!isStarted_)
|
87 |
| - { |
88 |
| - if (subscriptions_.empty()) |
89 | 98 | {
|
90 |
| - for (const auto& key : keys_) { |
91 |
| - auto subscriber = p_memory_.call<qi::AnyObject>("subscriber", key); |
92 |
| - qi::SignalLink subscription = subscriber.connect("signal", [=](const qi::AnyValue& v){ touchCallback(key, v); }).value(); |
93 |
| - subscriptions_.emplace_back(std::move(subscriber), std::move(subscription)); |
94 |
| - } |
| 99 | + if (subscriptions_.empty()) |
| 100 | + { |
| 101 | + for (const auto& key : keys_) |
| 102 | + { |
| 103 | + auto subscriber = subscribe( |
| 104 | + naoqi_version_, session_, key, |
| 105 | + [=](const qi::AnyValue& v) { touchCallback(key, v); }); |
| 106 | + subscriptions_.push_back(std::move(subscriber)); |
| 107 | + } |
| 108 | + } |
| 109 | + isStarted_ = true; |
95 | 110 | }
|
96 |
| - isStarted_ = true; |
97 |
| - } |
98 | 111 | }
|
99 | 112 |
|
100 |
| -template<class T> |
| 113 | +template <class T> |
101 | 114 | void TouchEventRegister<T>::stopProcess()
|
102 | 115 | {
|
103 | 116 | boost::mutex::scoped_lock stop_lock(mutex_);
|
104 | 117 | if (isStarted_)
|
105 |
| - { |
106 |
| - if (!subscriptions_.empty()){ |
107 |
| - for (const auto& subscription : subscriptions_) { |
108 |
| - try { |
109 |
| - subscription.subscriber.disconnect(subscription.link).value(); |
110 |
| - } catch (const std::exception& e) { |
111 |
| - std::cerr << "Error attempting to clean-up ALMemory subscription: " << e.what() << std::endl; |
| 118 | + { |
| 119 | + if (!subscriptions_.empty()) |
| 120 | + { |
| 121 | + for (const auto& subscription : subscriptions_) |
| 122 | + { |
| 123 | + try |
| 124 | + { |
| 125 | + subscription->unsubscribe().value(); |
| 126 | + } |
| 127 | + catch (const std::exception& e) |
| 128 | + { |
| 129 | + std::cerr << "Error attempting to clean-up " |
| 130 | + "ALMemory subscription: " |
| 131 | + << e.what() << std::endl; |
| 132 | + } |
| 133 | + } |
| 134 | + subscriptions_.clear(); |
112 | 135 | }
|
113 |
| - } |
114 |
| - subscriptions_.clear(); |
| 136 | + isStarted_ = false; |
115 | 137 | }
|
116 |
| - isStarted_ = false; |
117 |
| - } |
118 | 138 | }
|
119 | 139 |
|
120 |
| -template<class T> |
| 140 | +template <class T> |
121 | 141 | void TouchEventRegister<T>::writeDump(const rclcpp::Time& time)
|
122 | 142 | {
|
123 | 143 | if (isStarted_)
|
124 |
| - { |
125 |
| - //recorder_->writeDump(time); |
126 |
| - } |
| 144 | + { |
| 145 | + // recorder_->writeDump(time); |
| 146 | + } |
127 | 147 | }
|
128 | 148 |
|
129 |
| -template<class T> |
| 149 | +template <class T> |
130 | 150 | void TouchEventRegister<T>::setBufferDuration(float duration)
|
131 | 151 | {
|
132 |
| - //recorder_->setBufferDuration(duration); |
| 152 | + // recorder_->setBufferDuration(duration); |
133 | 153 | }
|
134 | 154 |
|
135 |
| -template<class T> |
| 155 | +template <class T> |
136 | 156 | void TouchEventRegister<T>::isRecording(bool state)
|
137 | 157 | {
|
138 | 158 | boost::mutex::scoped_lock rec_lock(mutex_);
|
139 | 159 | isRecording_ = state;
|
140 | 160 | }
|
141 | 161 |
|
142 |
| -template<class T> |
| 162 | +template <class T> |
143 | 163 | void TouchEventRegister<T>::isPublishing(bool state)
|
144 | 164 | {
|
145 | 165 | boost::mutex::scoped_lock pub_lock(mutex_);
|
146 | 166 | isPublishing_ = state;
|
147 | 167 | }
|
148 | 168 |
|
149 |
| -template<class T> |
| 169 | +template <class T> |
150 | 170 | void TouchEventRegister<T>::isDumping(bool state)
|
151 | 171 | {
|
152 | 172 | boost::mutex::scoped_lock dump_lock(mutex_);
|
153 | 173 | isDumping_ = state;
|
154 | 174 | }
|
155 | 175 |
|
156 |
| -template<class T> |
| 176 | +template <class T> |
157 | 177 | void TouchEventRegister<T>::registerCallback()
|
158 | 178 | {
|
159 | 179 | }
|
160 | 180 |
|
161 |
| -template<class T> |
| 181 | +template <class T> |
162 | 182 | void TouchEventRegister<T>::unregisterCallback()
|
163 | 183 | {
|
164 | 184 | }
|
165 | 185 |
|
166 |
| -template<class T> |
167 |
| -void TouchEventRegister<T>::touchCallback(const std::string &key, const qi::AnyValue& value) |
| 186 | +template <class T> |
| 187 | +void TouchEventRegister<T>::touchCallback(const std::string& key, |
| 188 | + const qi::AnyValue& value) |
168 | 189 | {
|
169 | 190 | T msg = T();
|
170 |
| - bool state = value.toFloat() > 0.5f; |
| 191 | + bool state = value.toFloat() > 0.5f; |
171 | 192 |
|
172 | 193 | touchCallbackMessage(key, state, msg);
|
173 | 194 |
|
174 | 195 | std::vector<message_actions::MessageAction> actions;
|
175 | 196 | boost::mutex::scoped_lock callback_lock(mutex_);
|
176 |
| - if (isStarted_) { |
177 |
| - // CHECK FOR PUBLISH |
178 |
| - if ( isPublishing_ && publisher_->isSubscribed() ) |
179 |
| - { |
180 |
| - actions.push_back(message_actions::PUBLISH); |
181 |
| - } |
182 |
| - // CHECK FOR RECORD |
183 |
| - if ( isRecording_ ) |
184 |
| - { |
185 |
| - //actions.push_back(message_actions::RECORD); |
186 |
| - } |
187 |
| - if ( !isDumping_ ) |
188 |
| - { |
189 |
| - //actions.push_back(message_actions::LOG); |
190 |
| - } |
191 |
| - if (actions.size() >0) |
| 197 | + if (isStarted_) |
192 | 198 | {
|
193 |
| - converter_->callAll( actions, msg ); |
| 199 | + // CHECK FOR PUBLISH |
| 200 | + if (isPublishing_ && publisher_->isSubscribed()) |
| 201 | + { |
| 202 | + actions.push_back(message_actions::PUBLISH); |
| 203 | + } |
| 204 | + // CHECK FOR RECORD |
| 205 | + if (isRecording_) |
| 206 | + { |
| 207 | + // actions.push_back(message_actions::RECORD); |
| 208 | + } |
| 209 | + if (!isDumping_) |
| 210 | + { |
| 211 | + // actions.push_back(message_actions::LOG); |
| 212 | + } |
| 213 | + if (actions.size() > 0) |
| 214 | + { |
| 215 | + converter_->callAll(actions, msg); |
| 216 | + } |
194 | 217 | }
|
195 |
| - } |
196 | 218 | }
|
197 | 219 |
|
198 |
| -template<class T> |
199 |
| -void TouchEventRegister<T>::touchCallbackMessage(const std::string &key, bool &state, naoqi_bridge_msgs::msg::Bumper &msg) |
| 220 | +template <class T> |
| 221 | +void TouchEventRegister<T>::touchCallbackMessage( |
| 222 | + const std::string& key, bool& state, naoqi_bridge_msgs::msg::Bumper& msg) |
200 | 223 | {
|
201 | 224 | int i = 0;
|
202 |
| - for(std::vector<std::string>::const_iterator it = keys_.begin(); it != keys_.end(); ++it, ++i) |
203 |
| - { |
204 |
| - if ( key == it->c_str() ) { |
205 |
| - msg.bumper = i; |
206 |
| - msg.state = state?(naoqi_bridge_msgs::msg::Bumper::STATE_PRESSED):(naoqi_bridge_msgs::msg::Bumper::STATE_RELEASED); |
| 225 | + for (std::vector<std::string>::const_iterator it = keys_.begin(); |
| 226 | + it != keys_.end(); ++it, ++i) |
| 227 | + { |
| 228 | + if (key == it->c_str()) |
| 229 | + { |
| 230 | + msg.bumper = i; |
| 231 | + msg.state = state ? (naoqi_bridge_msgs::msg::Bumper::STATE_PRESSED) |
| 232 | + : (naoqi_bridge_msgs::msg::Bumper::STATE_RELEASED); |
| 233 | + } |
207 | 234 | }
|
208 |
| - } |
209 | 235 | }
|
210 | 236 |
|
211 |
| -template<class T> |
212 |
| -void TouchEventRegister<T>::touchCallbackMessage(const std::string &key, bool &state, naoqi_bridge_msgs::msg::HandTouch &msg) |
| 237 | +template <class T> |
| 238 | +void TouchEventRegister<T>::touchCallbackMessage( |
| 239 | + const std::string& key, bool& state, naoqi_bridge_msgs::msg::HandTouch& msg) |
213 | 240 | {
|
214 | 241 | int i = 0;
|
215 |
| - for(std::vector<std::string>::const_iterator it = keys_.begin(); it != keys_.end(); ++it, ++i) |
216 |
| - { |
217 |
| - if ( key == it->c_str() ) { |
218 |
| - msg.hand = i; |
219 |
| - msg.state = state?(naoqi_bridge_msgs::msg::HandTouch::STATE_PRESSED):(naoqi_bridge_msgs::msg::HandTouch::STATE_RELEASED); |
| 242 | + for (std::vector<std::string>::const_iterator it = keys_.begin(); |
| 243 | + it != keys_.end(); ++it, ++i) |
| 244 | + { |
| 245 | + if (key == it->c_str()) |
| 246 | + { |
| 247 | + msg.hand = i; |
| 248 | + msg.state = state |
| 249 | + ? (naoqi_bridge_msgs::msg::HandTouch::STATE_PRESSED) |
| 250 | + : (naoqi_bridge_msgs::msg::HandTouch::STATE_RELEASED); |
| 251 | + } |
220 | 252 | }
|
221 |
| - } |
222 | 253 | }
|
223 | 254 |
|
224 |
| -template<class T> |
225 |
| -void TouchEventRegister<T>::touchCallbackMessage(const std::string &key, bool &state, naoqi_bridge_msgs::msg::HeadTouch &msg) |
| 255 | +template <class T> |
| 256 | +void TouchEventRegister<T>::touchCallbackMessage( |
| 257 | + const std::string& key, bool& state, naoqi_bridge_msgs::msg::HeadTouch& msg) |
226 | 258 | {
|
227 | 259 | int i = 0;
|
228 |
| - for(std::vector<std::string>::const_iterator it = keys_.begin(); it != keys_.end(); ++it, ++i) |
229 |
| - { |
230 |
| - if ( key == it->c_str() ) { |
231 |
| - msg.button = i; |
232 |
| - msg.state = state?(naoqi_bridge_msgs::msg::HeadTouch::STATE_PRESSED):(naoqi_bridge_msgs::msg::HeadTouch::STATE_RELEASED); |
| 260 | + for (std::vector<std::string>::const_iterator it = keys_.begin(); |
| 261 | + it != keys_.end(); ++it, ++i) |
| 262 | + { |
| 263 | + if (key == it->c_str()) |
| 264 | + { |
| 265 | + msg.button = i; |
| 266 | + msg.state = state |
| 267 | + ? (naoqi_bridge_msgs::msg::HeadTouch::STATE_PRESSED) |
| 268 | + : (naoqi_bridge_msgs::msg::HeadTouch::STATE_RELEASED); |
| 269 | + } |
233 | 270 | }
|
234 |
| - } |
235 | 271 | }
|
236 | 272 |
|
237 | 273 | // http://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor
|
238 | 274 | template class TouchEventRegister<naoqi_bridge_msgs::msg::Bumper>;
|
239 | 275 | template class TouchEventRegister<naoqi_bridge_msgs::msg::HandTouch>;
|
240 | 276 | template class TouchEventRegister<naoqi_bridge_msgs::msg::HeadTouch>;
|
241 | 277 |
|
242 |
| -}//namespace |
| 278 | +} // namespace naoqi |
0 commit comments