naoqidriver
basic.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #include <iostream>
19 #include <vector>
20 
21 #include <boost/make_shared.hpp>
22 
23 #include <ros/ros.h>
24 
25 #include <qi/anyobject.hpp>
26 
29 
30 namespace naoqi
31 {
32 
33 template <typename Converter, typename Publisher, typename Recorder>
35 {
36 }
37 
38 template <typename Converter, typename Publisher, typename Recorder>
39 EventRegister<Converter, Publisher, Recorder>::EventRegister( const std::string& key, const qi::SessionPtr& session )
40  : key_(key),
41  p_memory_( session->service("ALMemory") ),
42  isStarted_(false),
43  isPublishing_(false),
44  isRecording_(false),
45  isDumping_(false)
46 {
47  publisher_ = boost::make_shared<Publisher>( key_ );
48  recorder_ = boost::make_shared<Recorder>( key_ );
49  converter_ = boost::make_shared<Converter>( key_, 0, session, key_ );
50 
51  converter_->registerCallback( message_actions::PUBLISH, boost::bind(&Publisher::publish, publisher_, _1) );
52  converter_->registerCallback( message_actions::RECORD, boost::bind(&Recorder::write, recorder_, _1) );
53  converter_->registerCallback( message_actions::LOG, boost::bind(&Recorder::bufferize, recorder_, _1) );
54 
55  signal_ = p_memory_.call<qi::AnyObject>("subscriber", key_);
56 }
57 
58 template <typename Converter, typename Publisher, typename Recorder>
60 {
61 }
62 
63 template <typename Converter, typename Publisher, typename Recorder>
65 {
66  publisher_->reset(nh);
67 }
68 
69 template <typename Converter, typename Publisher, typename Recorder>
70 void EventRegister<Converter, Publisher, Recorder>::resetRecorder( boost::shared_ptr<naoqi::recorder::GlobalRecorder> gr )
71 {
72  recorder_->reset(gr, converter_->frequency());
73 }
74 
75 template <typename Converter, typename Publisher, typename Recorder>
77 {
78  boost::mutex::scoped_lock start_lock(mutex_);
79  if (!isStarted_)
80  {
81  registerCallback();
82  isStarted_ = true;
83  }
84 }
85 
86 template <typename Converter, typename Publisher, typename Recorder>
88 {
89  boost::mutex::scoped_lock stop_lock(mutex_);
90  if (isStarted_)
91  {
92  unregisterCallback();
93  isStarted_ = false;
94  }
95 }
96 
97 template <typename Converter, typename Publisher, typename Recorder>
99 {
100  if (isStarted_)
101  {
102  recorder_->writeDump(time);
103  }
104 }
105 
106 template <typename Converter, typename Publisher, typename Recorder>
108 {
109  recorder_->setBufferDuration(duration);
110 }
111 
112 template <typename Converter, typename Publisher, typename Recorder>
114 {
115  boost::mutex::scoped_lock rec_lock(mutex_);
116  isRecording_ = state;
117 }
118 
119 template <typename Converter, typename Publisher, typename Recorder>
121 {
122  boost::mutex::scoped_lock pub_lock(mutex_);
123  isPublishing_ = state;
124 }
125 
126 template <typename Converter, typename Publisher, typename Recorder>
128 {
129  boost::mutex::scoped_lock dump_lock(mutex_);
130  isDumping_ = state;
131 }
132 
133 template <typename Converter, typename Publisher, typename Recorder>
135 {
136  signalID_ = signal_.connect("signal", (boost::function<void(qi::AnyValue)>(boost::bind(&EventRegister<Converter, Publisher, Recorder>::onEvent,
137  this))));
138 }
139 
140 template <typename Converter, typename Publisher, typename Recorder>
142 {
143  signal_.disconnect(signalID_);
144 }
145 
146 template <typename Converter, typename Publisher, typename Recorder>
148 {
149  std::vector<message_actions::MessageAction> actions;
150  boost::mutex::scoped_lock callback_lock(mutex_);
151  if (isStarted_) {
152  // CHECK FOR PUBLISH
153  if ( isPublishing_ && publisher_->isSubscribed() )
154  {
155  actions.push_back(message_actions::PUBLISH);
156  }
157  // CHECK FOR RECORD
158  if ( isRecording_ )
159  {
160  actions.push_back(message_actions::RECORD);
161  }
162  if ( !isDumping_ )
163  {
164  actions.push_back(message_actions::LOG);
165  }
166  if (actions.size() >0)
167  {
168  converter_->callAll( actions );
169  }
170  }
171 }
172 
173 }//namespace
std::string key_
Definition: basic.hpp:83
void writeDump(const ros::Time &time)
Definition: basic.hxx:98
qi::AnyObject signal_
Definition: basic.hpp:81
boost::shared_ptr< Publisher > publisher_
Definition: basic.hpp:77
Definition: message_actions.h:13
qi::AnyObject p_memory_
Definition: basic.hpp:80
void isRecording(bool state)
Definition: basic.hxx:113
void startProcess()
Definition: basic.hxx:76
Definition: audio.cpp:29
Definition: message_actions.h:12
void resetPublisher(ros::NodeHandle &nh)
Definition: basic.hxx:64
Definition: message_actions.h:11
void setBufferDuration(float duration)
Definition: basic.hxx:107
boost::shared_ptr< Converter > converter_
Definition: basic.hpp:76
void onEvent()
Definition: basic.hxx:147
void stopProcess()
Definition: basic.hxx:87
~EventRegister()
Definition: basic.hxx:59
void unregisterCallback()
Definition: basic.hxx:141
EventRegister()
Constructor for recorder interface.
Definition: basic.hxx:34
boost::shared_ptr< Recorder > recorder_
Definition: basic.hpp:78
void isPublishing(bool state)
Definition: basic.hxx:120
void resetRecorder(boost::shared_ptr< naoqi::recorder::GlobalRecorder > gr)
Definition: basic.hxx:70
GlobalRecorder concept interface.
Definition: basic.hpp:45
void registerCallback()
Definition: basic.hxx:134
void isDumping(bool state)
Definition: basic.hxx:127