1111#include " kodlab_mjbots_sdk/robot_base.h"
1212#include " kodlab_mjbots_sdk/mjbots_hardware_interface.h"
1313#include " kodlab_mjbots_sdk/lcm_subscriber.h"
14+ #include " kodlab_mjbots_sdk/lcm_publisher.h"
1415#include " lcm/lcm-cpp.hpp"
1516#include " real_time_tools/timer.hpp"
1617#include " real_time_tools/hard_spinner.hpp"
@@ -145,8 +146,9 @@ class MjbotsControlLoop : public AbstractRealtimeObject {
145146 bool logging_ = false ; // / Boolean to determine if logging is in use
146147 bool input_ = false ; // / Boolean to determine if input is in use
147148 std::string logging_channel_name_; // / Channel name to publish logs to, leave empty if not publishing
148- lcm::LCM lcm_; // / LCM object
149- LogClass log_data_; // / object containing log data
149+ std::shared_ptr<lcm::LCM> lcm_; // / LCM object shared pointer
150+ LcmPublisher<LogClass> log_pub_; // / log LCM publisher
151+ std::shared_ptr<LogClass> log_data_; // / LCM log message data shared pointer
150152 LcmSubscriber<InputClass> lcm_sub_; // / LCM subscriber object
151153 float time_now_ = 0 ; // / Time since start in micro seconds
152154};
@@ -160,6 +162,9 @@ MjbotsControlLoop<log_type, input_type, robot_type>::MjbotsControlLoop(std::vect
160162template <class log_type , class input_type , class robot_type >
161163MjbotsControlLoop<log_type, input_type, robot_type>::MjbotsControlLoop(std::vector<std::shared_ptr<kodlab::mjbots::JointMoteus>> joint_ptrs, const ControlLoopOptions &options)
162164 : AbstractRealtimeObject(options.realtime_params.main_rtp, options.realtime_params.can_cpu),
165+ lcm_ (std::make_shared<lcm::LCM>()),
166+ log_pub_(lcm_, options.log_channel_name),
167+ log_data_(log_pub_.get_message_shared_ptr()),
163168 lcm_sub_(options.realtime_params.lcm_rtp, options.realtime_params.lcm_cpu, options.input_channel_name)
164169{
165170 robot_ = std::make_shared<robot_type>( joint_ptrs,
@@ -177,6 +182,9 @@ MjbotsControlLoop<log_type, input_type, robot_type>::MjbotsControlLoop(std::vect
177182template <class log_type , class input_type , class robot_type >
178183MjbotsControlLoop<log_type, input_type, robot_type>::MjbotsControlLoop(std::shared_ptr<robot_type>robot_in, const ControlLoopOptions &options)
179184 : AbstractRealtimeObject(options.realtime_params.main_rtp, options.realtime_params.can_cpu),
185+ lcm_ (std::make_shared<lcm::LCM>()),
186+ log_pub_(lcm_, options.log_channel_name),
187+ log_data_(log_pub_.get_message_shared_ptr()),
180188 lcm_sub_(options.realtime_params.lcm_rtp, options.realtime_params.lcm_cpu, options.input_channel_name) {
181189 // Create robot object
182190 robot_ = robot_in;
@@ -214,16 +222,16 @@ void MjbotsControlLoop<log_type, input_type, robot_type>::SetupOptions(const Con
214222template <class log_type , class input_type , class robot_type >
215223void MjbotsControlLoop<log_type, input_type, robot_type>::AddTimingLog(float t, float margin, float message_duration) {
216224 if (logging_) {
217- log_data_. timestamp = t;
218- log_data_. margin = margin;
219- log_data_. message_duration = message_duration;
225+ log_data_-> timestamp = t;
226+ log_data_-> margin = margin;
227+ log_data_-> message_duration = message_duration;
220228 }
221229}
222230
223231template <class log_type , class input_type , class robot_type >
224232void MjbotsControlLoop<log_type, input_type, robot_type>::PublishLog() {
225233 if (logging_)
226- lcm_. publish (logging_channel_name_, &log_data_ );
234+ log_pub_. Publish ( );
227235}
228236
229237template <class log_type , class input_type , class robot_type >
0 commit comments