1 #ifndef NODE_CANOPEN_MASTER_HPP_
2 #define NODE_CANOPEN_MASTER_HPP_
4 #include <yaml-cpp/yaml.h>
6 #include <lely/coapp/master.hpp>
7 #include <lely/coapp/slave.hpp>
8 #include <lely/ev/exec.hpp>
9 #include <lely/ev/loop.hpp>
10 #include <lely/io2/linux/can.hpp>
11 #include <lely/io2/posix/poll.hpp>
12 #include <lely/io2/sys/io.hpp>
13 #include <lely/io2/sys/sigset.hpp>
14 #include <lely/io2/sys/timer.hpp>
15 #include <rclcpp/rclcpp.hpp>
16 #include <rclcpp_lifecycle/lifecycle_node.hpp>
23 namespace node_interfaces
36 template <
class NODETYPE>
40 std::is_base_of<rclcpp::Node, NODETYPE>::value ||
41 std::is_base_of<rclcpp_lifecycle::LifecycleNode, NODETYPE>::value,
42 "NODETYPE must derive from rclcpp::Node or rclcpp_lifecycle::LifecycleNode");
52 std::shared_ptr<lely::canopen::AsyncMaster>
master_;
53 std::shared_ptr<lely::ev::Executor>
exec_;
56 std::unique_ptr<lely::io::Context>
ctx_;
57 std::unique_ptr<lely::io::Poll>
poll_;
58 std::unique_ptr<lely::ev::Loop>
loop_;
59 std::unique_ptr<lely::io::Timer>
timer_;
60 std::unique_ptr<lely::io::CanController>
ctrl_;
61 std::unique_ptr<lely::io::CanChannel>
chan_;
62 std::unique_ptr<lely::io::SignalSet>
sigset_;
90 RCLCPP_DEBUG(
node_->get_logger(),
"init_start");
99 client_cbg_ =
node_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
100 timer_cbg_ =
node_->create_callback_group(rclcpp::CallbackGroupType::MutuallyExclusive);
102 node_->declare_parameter(
"container_name",
"");
103 node_->declare_parameter(
"master_dcf",
"");
104 node_->declare_parameter(
"master_bin",
"");
105 node_->declare_parameter(
"can_interface_name",
"vcan0");
106 node_->declare_parameter(
"node_id", 0);
107 node_->declare_parameter(
"non_transmit_timeout", 100);
108 node_->declare_parameter(
"config",
"");
110 this->initialised_.store(
true);
111 RCLCPP_DEBUG(
node_->get_logger(),
"init_end");
113 virtual void init(
bool called_from_base) {}
137 int non_transmit_timeout;
145 node_->get_parameter(
"non_transmit_timeout", non_transmit_timeout);
146 node_->get_parameter(
"config", config);
148 this->config_ = YAML::Load(config);
149 this->non_transmit_timeout_ = std::chrono::milliseconds(non_transmit_timeout);
152 this->configured_.store(
true);
166 RCLCPP_DEBUG(this->node_->get_logger(),
"NodeCanopenMaster activate start");
180 io_guard_ = std::make_unique<lely::io::IoGuard>();
181 ctx_ = std::make_unique<lely::io::Context>();
182 poll_ = std::make_unique<lely::io::Poll>(*
ctx_);
183 loop_ = std::make_unique<lely::ev::Loop>(
poll_->get_poll());
185 exec_ = std::make_shared<lely::ev::Executor>(
loop_->get_executor());
186 timer_ = std::make_unique<lely::io::Timer>(*
poll_, *
exec_, CLOCK_MONOTONIC);
196 this->master_set_.store(
true);
197 this->master_->Reset();
198 this->spinner_ = std::thread(
205 catch (
const std::exception & e)
207 RCLCPP_INFO(this->node_->get_logger(), e.what());
209 RCLCPP_INFO(this->node_->get_logger(),
"Canopen master loop stopped");
211 this->activated_.store(
true);
212 RCLCPP_DEBUG(this->node_->get_logger(),
"NodeCanopenMaster activate end");
249 RCLCPP_INFO(
node_->get_logger(),
"Lely Core Context Shutdown");
252 this->spinner_.join();
255 this->activated_.store(
false);
290 this->configured_.store(
false);
292 virtual void cleanup(
bool called_from_base) {}
302 RCLCPP_DEBUG(this->node_->get_logger(),
"Shutting down.");
303 if (this->activated_)
308 if (this->configured_)
314 this->master_set_.store(
false);
315 this->initialised_.store(
false);
316 this->configured_.store(
false);
317 this->activated_.store(
false);
326 virtual std::shared_ptr<lely::canopen::AsyncMaster>
get_master()
Master Exception.
Definition: master_error.hpp:16
Node Canopen Master Interface.
Definition: node_canopen_master_interface.hpp:20
Node Canopen Master.
Definition: node_canopen_master.hpp:38
YAML::Node config_
Definition: node_canopen_master.hpp:67
void shutdown() override
Shutdown the driver.
Definition: node_canopen_master.hpp:300
std::unique_ptr< lely::ev::Loop > loop_
Definition: node_canopen_master.hpp:58
rclcpp::CallbackGroup::SharedPtr client_cbg_
Definition: node_canopen_master.hpp:64
virtual void deactivate(bool called_from_base)
Deactivate hook for derived classes.
Definition: node_canopen_master.hpp:266
void configure() override
Configure the driver.
Definition: node_canopen_master.hpp:122
virtual void init(bool called_from_base)
Definition: node_canopen_master.hpp:113
virtual void shutdown(bool called_from_base)
Definition: node_canopen_master.hpp:319
std::string can_interface_name_
Definition: node_canopen_master.hpp:73
std::string master_bin_
Definition: node_canopen_master.hpp:72
std::shared_ptr< lely::ev::Executor > exec_
Definition: node_canopen_master.hpp:53
std::unique_ptr< lely::io::CanChannel > chan_
Definition: node_canopen_master.hpp:61
void init() override
Initialise Master.
Definition: node_canopen_master.hpp:88
virtual std::shared_ptr< lely::canopen::AsyncMaster > get_master()
Get the master object.
Definition: node_canopen_master.hpp:326
virtual void activate(bool called_from_base)
Activate hook for derived classes.
Definition: node_canopen_master.hpp:223
std::atomic< bool > master_set_
Definition: node_canopen_master.hpp:50
std::unique_ptr< lely::io::Context > ctx_
Definition: node_canopen_master.hpp:56
std::unique_ptr< lely::io::IoGuard > io_guard_
Definition: node_canopen_master.hpp:55
std::unique_ptr< lely::io::Poll > poll_
Definition: node_canopen_master.hpp:57
std::string container_name_
Definition: node_canopen_master.hpp:70
virtual std::shared_ptr< lely::ev::Executor > get_executor()
Get the executor object.
Definition: node_canopen_master.hpp:339
std::chrono::milliseconds non_transmit_timeout_
Definition: node_canopen_master.hpp:69
NODETYPE * node_
Definition: node_canopen_master.hpp:42
virtual void configure(bool called_from_base)
Definition: node_canopen_master.hpp:155
std::unique_ptr< lely::io::CanController > ctrl_
Definition: node_canopen_master.hpp:60
std::shared_ptr< lely::canopen::AsyncMaster > master_
Definition: node_canopen_master.hpp:52
void deactivate() override
Deactivate the driver.
Definition: node_canopen_master.hpp:232
void cleanup() override
Cleanup the driver.
Definition: node_canopen_master.hpp:275
rclcpp::CallbackGroup::SharedPtr timer_cbg_
Definition: node_canopen_master.hpp:65
std::unique_ptr< lely::io::Timer > timer_
Definition: node_canopen_master.hpp:59
std::atomic< bool > activated_
Definition: node_canopen_master.hpp:49
virtual void cleanup(bool called_from_base)
Definition: node_canopen_master.hpp:292
void activate() override
Activate the driver.
Definition: node_canopen_master.hpp:164
std::atomic< bool > initialised_
Definition: node_canopen_master.hpp:47
std::thread spinner_
Definition: node_canopen_master.hpp:75
std::unique_ptr< lely::io::SignalSet > sigset_
Definition: node_canopen_master.hpp:62
NodeCanopenMaster(NODETYPE *node)
Definition: node_canopen_master.hpp:78
std::string master_dcf_
Definition: node_canopen_master.hpp:71
uint8_t node_id_
Definition: node_canopen_master.hpp:68
std::atomic< bool > configured_
Definition: node_canopen_master.hpp:48
Definition: configuration_manager.hpp:28