EtherCAT Control Framework v0.9
Implementation of EtherCAT protocol using IgH EtherCAT library for robot controller.
ecat_lifecycle.hpp
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * $Id$
4 *
5 * Copyright (C) 2021 Veysi ADIN, UST KIST
6 *
7 * This file is part of the IgH EtherCAT master userspace program in the ROS2 environment.
8 *
9 * The IgH EtherCAT master userspace program in the ROS2 environment is free software; you can
10 * redistribute it and/or modify it under the terms of the GNU General
11 * Public License as published by the Free Software Foundation; version 2
12 * of the License.
13 *
14 * The IgH EtherCAT master userspace program in the ROS2 environment is distributed in the hope that
15 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with the IgH EtherCAT master userspace program in the ROS environment. If not, see
21 * <http://www.gnu.org/licenses/>.
22 *
23 * ---
24 *
25 * The license mentioned above concerns the source code only. Using the
26 * EtherCAT technology and brand is only permitted in compliance with the
27 * industrial property and similar rights of Beckhoff Automation GmbH.
28 *
29 * Contact information: veysi.adin@kist.re.kr
30 *****************************************************************************/
31/*****************************************************************************
32 * \file ecat_lifecycle.hpp
33 * \brief Ethercat lifecycle node implementation and real-time communication
34 * loop implemented in this file.
35 *******************************************************************************/
36#include "ecat_node.hpp"
37#include "timing.hpp"
38/******************************************************************************/
40#include <rclcpp_lifecycle/lifecycle_node.hpp>
41#include <rclcpp_lifecycle/lifecycle_publisher.hpp>
42/******************************************************************************/
44#include "ecat_msgs/msg/gui_button_data.hpp"
46#include "ecat_msgs/msg/data_received.hpp"
47#include "ecat_msgs/msg/data_sent.hpp"
48/******************************************************************************/
49#include <rclcpp/strategies/message_pool_memory_strategy.hpp> // /// Completely static memory allocation strategy for messages.
50#include <rclcpp/strategies/allocator_memory_strategy.hpp>
52
58#include <rttest/rttest.h> // To get number of allocation, statistics related memory allocation
59
60#include <tlsf_cpp/tlsf.hpp> // C++ wrapper for Miguel Masmano Tello's implementation of the TLSF memory allocator
61// Implements the allocator_traits template
62
63/******************************************************************************/
64using rclcpp::strategies::message_pool_memory_strategy::MessagePoolMemoryStrategy;
65using rclcpp::memory_strategies::allocator_memory_strategy::AllocatorMemoryStrategy;
66
67template<typename T = void>
68using TLSFAllocator = tlsf_heap_allocator<T>;
69
70using namespace rclcpp_lifecycle ;
71using namespace EthercatCommunication ;
73{
74class EthercatLifeCycle : public LifecycleNode
75{
76 public:
79 private:
87 node_interfaces::LifecycleNodeInterface::CallbackReturn on_configure(const State &);
88
95 node_interfaces::LifecycleNodeInterface::CallbackReturn on_activate(const State &);
96
102 node_interfaces::LifecycleNodeInterface::CallbackReturn on_deactivate(const State &);
103
109 node_interfaces::LifecycleNodeInterface::CallbackReturn on_cleanup(const State &);
110
116 node_interfaces::LifecycleNodeInterface::CallbackReturn on_shutdown(const State &);
117
124 node_interfaces::LifecycleNodeInterface::CallbackReturn on_error(const State &);
125
126 rclcpp::TimerBase::SharedPtr timer_;
128 LifecyclePublisher<ecat_msgs::msg::DataReceived>::SharedPtr received_data_publisher_;
130 LifecyclePublisher<ecat_msgs::msg::DataSent>::SharedPtr sent_data_publisher_;
132 rclcpp::Subscription<sensor_msgs::msg::Joy>::SharedPtr joystick_subscriber_;
133 rclcpp::Subscription<ecat_msgs::msg::GuiButtonData>::SharedPtr gui_subscriber_;
134
135
136 ecat_msgs::msg::DataReceived received_data_;
137 ecat_msgs::msg::DataSent sent_data_;
138 std::unique_ptr<EthercatNode> ecat_node_;
139
140
145 void HandleControlNodeCallbacks(const sensor_msgs::msg::Joy::SharedPtr msg);
146
153
160
169 static void *PassCycylicExchange(void *arg);
170
177
184 void StartPdoExchange(void *instance);
185
192 int GetComState();
193
197 void ReadFromSlaves();
198
204 int PublishAllData();
211
216 void EnableMotors();
217
223
229
235
241
247
253
263
269
286 void HandleGuiNodeCallbacks(const ecat_msgs::msg::GuiButtonData::SharedPtr gui_sub);
287
293 int GetDriveState(const int& statusWord);
294
300 int EnableDrivers();
301
308 int8_t EnableDrivesViaSDO(int index);
315 int8_t DisableDrivesViaSDO(int index);
316
329 private :
332 struct sched_param ethercat_sched_param_ = {};
333 pthread_attr_t ethercat_thread_attr_;
334 int32_t err_;
336 uint8_t al_state_ = 0;
338 uint32_t command_ = 0x004F;
341 ecat_msgs::msg::GuiButtonData gui_buttons_status_;
342 uint8_t gui_node_data_ = 1;
343 uint8_t emergency_status_ = 1 ;
344 // Will be used as a parameter for taking timing measurements.
345 std::int32_t measurement_time = 0 ;
347};
348}
Opens, reads from and publishes joystick events.
Definition: joy_node_linux.cpp:52
Definition: ecat_lifecycle.hpp:75
ecat_msgs::msg::DataReceived received_data_
Definition: ecat_lifecycle.hpp:136
int EnableDrivers()
CKim - This function checks status word, clears any faults and enables torque of the motor driver.
Definition: ecat_lifecycle.cpp:1147
void UpdateVelocityModeParameters()
Acquired data from subscribed controller topic will be assigned as motor speed parameter.
Definition: ecat_lifecycle.cpp:1405
void UpdateMotorStatePositionMode()
Updates motor control word and motor state in position mode based on CIA402 state machine,...
Definition: ecat_lifecycle.cpp:1060
Controller controller_
Definition: ecat_lifecycle.hpp:339
void UpdateControlParameters()
Updates control parameters based on selected mode.
Definition: ecat_lifecycle.cpp:892
int StartEthercatCommunication()
Starts EtherCAT communcation.
Definition: ecat_lifecycle.cpp:554
int PublishAllData()
Publishes all data that master received and will be sent.
Definition: ecat_lifecycle.cpp:951
int32_t err_
Definition: ecat_lifecycle.hpp:334
static void * PassCycylicExchange(void *arg)
Helper function to enter pthread_create, since pthread's are C function it doesn't accept class membe...
Definition: ecat_lifecycle.cpp:584
Timing timer_info_
Definition: ecat_lifecycle.hpp:346
void UpdateCyclicPositionModeParameters()
Acquired data from subscribed controller topic will be assigned as motor cyclic target position param...
Definition: ecat_lifecycle.cpp:1278
void WriteToSlavesInCyclicTorqueMode()
Writes target torque and control word in cyclic sync. torque mode.
Definition: ecat_lifecycle.cpp:1515
node_interfaces::LifecycleNodeInterface::CallbackReturn on_cleanup(const State &)
Cleans up all variables and datas assigned by Ethercat lifecycle node.
Definition: ecat_lifecycle.cpp:127
void StartPdoExchange(void *instance)
Realtime cyclic Pdo exchange function which will constantly read/write values from/to slaves.
Definition: ecat_lifecycle.cpp:589
node_interfaces::LifecycleNodeInterface::CallbackReturn on_error(const State &)
There isn't any error recovery functionality for this node, just resets nodes. Reconfiguration is nee...
Definition: ecat_lifecycle.cpp:172
LifecyclePublisher< ecat_msgs::msg::DataReceived >::SharedPtr received_data_publisher_
This lifecycle publisher will be used to publish received feedback data from slaves.
Definition: ecat_lifecycle.hpp:128
void WriteToSlavesInPositionMode()
Writes target position and control word to motor in profile position mode.
Definition: ecat_lifecycle.cpp:1183
int GetDriveState(const int &statusWord)
CKim - This function checks status word and returns state of the motor driver.
Definition: ecat_lifecycle.cpp:1109
int SetConfigurationParameters()
Assigns operation mode parameters based on selected operation mode.
Definition: ecat_lifecycle.cpp:499
void UpdateCyclicTorqueModeParameters()
Updates cylic torque mode parameters based on controller inputs.
Definition: ecat_lifecycle.cpp:1536
uint32_t motor_state_[g_kNumberOfServoDrivers]
Definition: ecat_lifecycle.hpp:337
pthread_attr_t ethercat_thread_attr_
Definition: ecat_lifecycle.hpp:333
uint8_t al_state_
Application layer of slaves seen by master.(INIT/PREOP/SAFEOP/OP)
Definition: ecat_lifecycle.hpp:336
rclcpp::Subscription< ecat_msgs::msg::GuiButtonData >::SharedPtr gui_subscriber_
Definition: ecat_lifecycle.hpp:133
struct sched_param ethercat_sched_param_
Definition: ecat_lifecycle.hpp:332
uint32_t command_
Definition: ecat_lifecycle.hpp:338
node_interfaces::LifecycleNodeInterface::CallbackReturn on_activate(const State &)
Activates Ethercat lifecycle node and starts real-time Ethercat communication. All publishing is done...
Definition: ecat_lifecycle.cpp:77
int8_t EnableDrivesViaSDO(int index)
Enables drives supporting CiA402 in specified index.
Definition: ecat_lifecycle.cpp:1563
void UpdatePositionModeParameters()
Acquired data from subscribed controller topic will be assigned as motor target position parameter.
Definition: ecat_lifecycle.cpp:971
int PublishReceivedData()
Publishes received data from slaves under the /slave_feedback name.
Definition: ecat_lifecycle.cpp:960
int GetComState()
Gets master's communication state.
Definition: ecat_lifecycle.cpp:966
uint8_t gui_node_data_
Definition: ecat_lifecycle.hpp:342
~EthercatLifeCycle()
Definition: ecat_lifecycle.cpp:39
void WriteToSlavesVelocityMode()
Updates data that will be sent to slaves. This updated data will be published as well.
Definition: ecat_lifecycle.cpp:934
node_interfaces::LifecycleNodeInterface::CallbackReturn on_shutdown(const State &)
Shuts down EtherCAT lifecycle node, releases Ethercat master.
Definition: ecat_lifecycle.cpp:155
int8_t DisableDrivesViaSDO(int index)
Disables drives supporting CiA402 in specified index.
Definition: ecat_lifecycle.cpp:1580
void UpdateCyclicVelocityModeParameters()
Acquired data from subscribed controller topic will be assigned as motor speed parameter.
Definition: ecat_lifecycle.cpp:1343
void HandleControlNodeCallbacks(const sensor_msgs::msg::Joy::SharedPtr msg)
This function handles callbacks from control node. It will update received values from controller nod...
Definition: ecat_lifecycle.cpp:191
int SetComThreadPriorities()
Sets Ethercat communication thread's properties After this function called user must call StartEtherc...
Definition: ecat_lifecycle.cpp:375
void HandleGuiNodeCallbacks(const ecat_msgs::msg::GuiButtonData::SharedPtr gui_sub)
CKim - This function checks status word and returns state of the motor driver.
Definition: ecat_lifecycle.cpp:236
uint8_t emergency_status_
Definition: ecat_lifecycle.hpp:343
rclcpp::Subscription< sensor_msgs::msg::Joy >::SharedPtr joystick_subscriber_
This subscriber will be used to receive data from controller node.
Definition: ecat_lifecycle.hpp:132
EthercatLifeCycle()
Definition: ecat_lifecycle.cpp:5
std::int32_t measurement_time
Definition: ecat_lifecycle.hpp:345
pthread_t ethercat_thread_
pthread create required parameters.
Definition: ecat_lifecycle.hpp:331
node_interfaces::LifecycleNodeInterface::CallbackReturn on_deactivate(const State &)
Deactivates Ethercat lifecycle node, turns of real-time communication.
Definition: ecat_lifecycle.cpp:95
void UpdateMotorStateVelocityMode()
Updates motor control world and motor state in velocity mode based on CIA402.
Definition: ecat_lifecycle.cpp:1435
void ReadFromSlaves()
Reads data from slaves and updates received data structure to be published.
Definition: ecat_lifecycle.cpp:912
rclcpp::TimerBase::SharedPtr timer_
Definition: ecat_lifecycle.hpp:126
int InitEthercatCommunication()
Encapsulates all configuration steps for the EtherCAT communication with default slaves....
Definition: ecat_lifecycle.cpp:427
void EnableMotors()
Enables connected motor drives based on CIA402.
Definition: ecat_lifecycle.cpp:1476
std::unique_ptr< EthercatNode > ecat_node_
Definition: ecat_lifecycle.hpp:138
ecat_msgs::msg::DataSent sent_data_
Definition: ecat_lifecycle.hpp:137
ecat_msgs::msg::GuiButtonData gui_buttons_status_
Values will be sent by controller node and will be assigned to variables below.
Definition: ecat_lifecycle.hpp:341
node_interfaces::LifecycleNodeInterface::CallbackReturn on_configure(const State &)
Ethercat lifecycle node configuration function, node will start with this function For more informati...
Definition: ecat_lifecycle.cpp:44
LifecyclePublisher< ecat_msgs::msg::DataSent >::SharedPtr sent_data_publisher_
This lifecycle publisher will be used to publish sent data from master to slaves.
Definition: ecat_lifecycle.hpp:130
const uint32_t g_kNumberOfServoDrivers
Number of connected servo drives.
Definition: ecat_globals.hpp:47
tlsf_heap_allocator< T > TLSFAllocator
Definition: joy_node_linux.cpp:46
ROS2 Headers.
Definition: ecat_node.hpp:51
Definition: ecat_lifecycle.hpp:73
Definition: timing_node.hpp:41