ros2_canopen master
C++ ROS CANopen Library
Loading...
Searching...
No Matches
node_canopen_base_driver.hpp
Go to the documentation of this file.
1// Copyright 2022 Christoph Hellmann Santos
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef NODE_CANOPEN_BASE_DRIVER
16#define NODE_CANOPEN_BASE_DRIVER
17
21#include "std_msgs/msg/string.hpp"
22#include "std_srvs/srv/trigger.hpp"
23
24#include "canopen_interfaces/msg/co_data.hpp"
25#include "canopen_interfaces/srv/co_read.hpp"
26#include "canopen_interfaces/srv/co_write.hpp"
27
28namespace ros2_canopen
29{
30namespace node_interfaces
31{
32template <class NODETYPE>
34{
35 static_assert(
36 std::is_base_of<rclcpp::Node, NODETYPE>::value ||
37 std::is_base_of<rclcpp_lifecycle::LifecycleNode, NODETYPE>::value,
38 "NODETYPE must derive from rclcpp::Node or rclcpp_lifecycle::LifecycleNode");
39
40protected:
44 std::mutex driver_mutex_;
45 std::shared_ptr<ros2_canopen::LelyDriverBridge> lely_driver_;
46 uint32_t period_ms_;
50
51 // nmt state callback
52 std::function<void(canopen::NmtState, uint8_t)> nmt_state_cb_;
53 // rpdo callback
54 std::function<void(COData, uint8_t)> rpdo_cb_;
55 // emcy callback
56 std::function<void(COEmcy, uint8_t)> emcy_cb_;
57
58 std::shared_ptr<ros2_canopen::SafeQueue<ros2_canopen::COEmcy>> emcy_queue_;
59 std::shared_ptr<ros2_canopen::SafeQueue<ros2_canopen::COData>> rpdo_queue_;
60 rclcpp::TimerBase::SharedPtr poll_timer_;
61
62 // Diagnostic components
63 std::atomic<bool> diagnostic_enabled_;
65 std::shared_ptr<diagnostic_updater::Updater> diagnostic_updater_;
66 std::shared_ptr<DiagnosticsCollector> diagnostic_collector_;
67
68 virtual void poll_timer_callback();
69 void nmt_listener();
70 virtual void on_nmt(canopen::NmtState nmt_state);
71 void rdpo_listener();
72 virtual void on_rpdo(COData data);
73 void emcy_listener();
74 virtual void on_emcy(COEmcy emcy);
75 virtual void diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper & stat);
76
77public:
78 NodeCanopenBaseDriver(NODETYPE * node);
79
81 {
82 if (nmt_state_publisher_thread_.joinable())
83 {
85 }
86 if (rpdo_publisher_thread_.joinable())
87 {
89 }
90 }
91
92 virtual void init(bool called_from_base);
93
94 virtual void configure(bool called_from_base);
95
96 virtual void activate(bool called_from_base);
97
98 virtual void deactivate(bool called_from_base);
99
100 virtual void cleanup(bool called_from_base);
101
102 virtual void shutdown(bool called_from_base);
103
104 virtual void add_to_master();
105
106 virtual void remove_from_master();
107
113 void register_nmt_state_cb(std::function<void(canopen::NmtState, uint8_t)> nmt_state_cb)
114 {
115 nmt_state_cb_ = std::move(nmt_state_cb);
116 }
117
123 void register_rpdo_cb(std::function<void(COData, uint8_t)> rpdo_cb)
124 {
125 rpdo_cb_ = std::move(rpdo_cb);
126 }
127
133 void register_emcy_cb(std::function<void(COEmcy, uint8_t)> emcy_cb)
134 {
135 emcy_cb_ = std::move(emcy_cb);
136 }
137};
140} // namespace node_interfaces
141} // namespace ros2_canopen
142
143#endif
Definition node_canopen_base_driver.hpp:34
uint32_t period_ms_
Definition node_canopen_base_driver.hpp:46
virtual ~NodeCanopenBaseDriver()
Definition node_canopen_base_driver.hpp:80
rclcpp::TimerBase::SharedPtr poll_timer_
Definition node_canopen_base_driver.hpp:60
uint32_t diagnostic_period_ms_
Definition node_canopen_base_driver.hpp:64
void register_emcy_cb(std::function< void(COEmcy, uint8_t)> emcy_cb)
Register a callback for EMCY.
Definition node_canopen_base_driver.hpp:133
void rdpo_listener()
Definition node_canopen_base_driver_impl.hpp:423
virtual void add_to_master()
Add the driver to master.
Definition node_canopen_base_driver_impl.hpp:265
std::shared_ptr< DiagnosticsCollector > diagnostic_collector_
Definition node_canopen_base_driver.hpp:66
void nmt_listener()
Definition node_canopen_base_driver_impl.hpp:365
virtual void configure(bool called_from_base)
Configure the driver.
std::thread nmt_state_publisher_thread_
Definition node_canopen_base_driver.hpp:41
std::shared_ptr< diagnostic_updater::Updater > diagnostic_updater_
Definition node_canopen_base_driver.hpp:65
virtual void on_rpdo(COData data)
Definition node_canopen_base_driver_impl.hpp:399
void register_nmt_state_cb(std::function< void(canopen::NmtState, uint8_t)> nmt_state_cb)
Register a callback for NMT state change.
Definition node_canopen_base_driver.hpp:113
virtual void poll_timer_callback()
Definition node_canopen_base_driver_impl.hpp:450
std::atomic< bool > diagnostic_enabled_
Definition node_canopen_base_driver.hpp:63
std::shared_ptr< ros2_canopen::SafeQueue< ros2_canopen::COEmcy > > emcy_queue_
Definition node_canopen_base_driver.hpp:58
void register_rpdo_cb(std::function< void(COData, uint8_t)> rpdo_cb)
Register a callback for RPDO.
Definition node_canopen_base_driver.hpp:123
std::function< void(COData, uint8_t)> rpdo_cb_
Definition node_canopen_base_driver.hpp:54
std::thread emcy_publisher_thread_
Definition node_canopen_base_driver.hpp:43
std::mutex driver_mutex_
Definition node_canopen_base_driver.hpp:44
virtual void diagnostic_callback(diagnostic_updater::DiagnosticStatusWrapper &stat)
Definition node_canopen_base_driver_impl.hpp:525
int boot_timeout_ms_
Definition node_canopen_base_driver.hpp:48
int sdo_timeout_ms_
Definition node_canopen_base_driver.hpp:47
virtual void remove_from_master()
Remove the driver from master.
Definition node_canopen_base_driver_impl.hpp:340
void emcy_listener()
Definition node_canopen_base_driver_impl.hpp:497
std::thread rpdo_publisher_thread_
Definition node_canopen_base_driver.hpp:42
std::shared_ptr< ros2_canopen::SafeQueue< ros2_canopen::COData > > rpdo_queue_
Definition node_canopen_base_driver.hpp:59
std::function< void(canopen::NmtState, uint8_t)> nmt_state_cb_
Definition node_canopen_base_driver.hpp:52
bool polling_
Definition node_canopen_base_driver.hpp:49
std::function< void(COEmcy, uint8_t)> emcy_cb_
Definition node_canopen_base_driver.hpp:56
std::shared_ptr< ros2_canopen::LelyDriverBridge > lely_driver_
Definition node_canopen_base_driver.hpp:45
virtual void on_nmt(canopen::NmtState nmt_state)
Definition node_canopen_base_driver_impl.hpp:394
virtual void on_emcy(COEmcy emcy)
Definition node_canopen_base_driver_impl.hpp:404
Node Canopen Driver.
Definition node_canopen_driver.hpp:60
void shutdown()
Shutdown the driver.
Definition node_canopen_driver.hpp:344
void deactivate()
Deactivate the driver.
Definition node_canopen_driver.hpp:265
void cleanup()
Cleanup the driver.
Definition node_canopen_driver.hpp:305
void activate()
Activate the driver.
Definition node_canopen_driver.hpp:223
void init()
Initialise the driver.
Definition node_canopen_driver.hpp:127
NodeCanopenBaseDriver< rclcpp_lifecycle::LifecycleNode > NCBDLifecycleNode
Definition node_canopen_base_driver.hpp:139
NodeCanopenBaseDriver< rclcpp::Node > NCBDNode
Definition node_canopen_base_driver.hpp:138
Definition configuration_manager.hpp:28
Definition exchange.hpp:26
Definition exchange.hpp:34