forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrake_lcm.cc
61 lines (46 loc) · 1.79 KB
/
drake_lcm.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "drake/lcm/drake_lcm.h"
#include <utility>
#include "drake/common/drake_assert.h"
#include "drake/common/drake_copyable.h"
namespace drake {
namespace lcm {
// This is the actual subscriber to an LCM channel. It simply extracts the
// serialized LCM message and passes it to the `DrakeLcmMessageHandlerInterface`
// object. A single type of subscriber is used to avoid DrakeLcm from being
// templated on the subscriber type.
class DrakeLcm::Subscriber {
public:
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Subscriber)
explicit Subscriber(DrakeLcmMessageHandlerInterface* drake_handler)
: drake_handler_(drake_handler) {}
void LcmCallback(const ::lcm::ReceiveBuffer* rbuf,
const std::string& channel) {
drake_handler_->HandleMessage(channel, rbuf->data, rbuf->data_size);
}
private:
DrakeLcmMessageHandlerInterface* const drake_handler_{};
};
DrakeLcm::DrakeLcm() {}
DrakeLcm::~DrakeLcm() { receive_thread_.reset(); }
void DrakeLcm::StartReceiveThread() {
DRAKE_DEMAND(receive_thread_ == nullptr);
receive_thread_ = std::make_unique<LcmReceiveThread>(&lcm_);
}
void DrakeLcm::StopReceiveThread() {
if (receive_thread_ != nullptr) receive_thread_->Stop();
}
::lcm::LCM* DrakeLcm::get_lcm_instance() { return &lcm_; }
void DrakeLcm::Publish(const std::string& channel, const void* data,
int data_size, double) {
lcm_.publish(channel, data, data_size);
}
void DrakeLcm::Subscribe(const std::string& channel,
DrakeLcmMessageHandlerInterface* handler) {
auto subscriber = std::make_unique<Subscriber>(handler);
auto sub =
lcm_.subscribe(channel, &Subscriber::LcmCallback, subscriber.get());
sub->setQueueCapacity(1);
subscriptions_.push_back(std::move(subscriber));
}
} // namespace lcm
} // namespace drake