ros2_canopen master
C++ ROS CANopen Library
Loading...
Searching...
No Matches
motor.hpp
Go to the documentation of this file.
1// Copyright 2023 Christoph Hellmann Santos
2// Copyright 2014-2022 Authors of ros_canopen
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16//
17
18#ifndef MOTOR_HPP
19#define MOTOR_HPP
20
21#include <algorithm>
22#include <atomic>
23#include <bitset>
24#include <condition_variable>
25#include <functional>
26#include <iostream>
27#include <limits>
28#include <mutex>
29#include <thread>
30#include "rclcpp/rclcpp.hpp"
31
37
38namespace ros2_canopen
39{
40
49typedef ModeForwardHelper<
50 MotorBase::Velocity, int16_t, 0x6042, 0,
54typedef ModeForwardHelper<
55 MotorBase::Interpolated_Position, int32_t, 0x60C1, 0x01,
58
59class Motor402 : public MotorBase
60{
61public:
63 std::shared_ptr<LelyDriverBridge> driver, ros2_canopen::State402::InternalState switching_state,
64 int homing_timeout_seconds)
65 : MotorBase(),
66 switching_state_(switching_state),
67 monitor_mode_(true),
68 state_switch_timeout_(5),
69 homing_timeout_seconds_(homing_timeout_seconds)
70 {
71 this->driver = driver;
72 }
73
74 virtual bool setTarget(double val);
75 virtual bool enterModeAndWait(uint16_t mode);
76 virtual bool isModeSupported(uint16_t mode);
77 virtual uint16_t getMode();
78 bool readState();
79
87 void handleDiag();
96 bool handleInit();
128
137
154
169 template <typename T, typename... Args>
170 bool registerMode(uint16_t mode, Args &&... args)
171 {
172 return mode_allocators_
173 .insert(std::make_pair(
174 mode,
175 [args..., mode, this]()
176 {
177 if (isModeSupportedByDevice(mode)) registerMode(mode, ModeSharedPtr(new T(args...)));
178 }))
179 .second;
180 }
181
198
199 double get_speed() const { return (double)this->driver->universal_get_value<int32_t>(0x606C, 0); }
200 double get_position() const
201 {
202 return (double)this->driver->universal_get_value<int32_t>(0x6064, 0);
203 }
204
205 void set_diagnostic_status_msgs(std::shared_ptr<DiagnosticsCollector> status, bool enable)
206 {
207 this->enable_diagnostics_.store(enable);
208 this->diag_collector_ = status;
209 }
210
211private:
212 virtual bool isModeSupportedByDevice(uint16_t mode);
213 void registerMode(uint16_t id, const ModeSharedPtr & m);
214
215 ModeSharedPtr allocMode(uint16_t mode);
216
217 bool switchMode(uint16_t mode);
218 bool switchState(const State402::InternalState & target);
219
220 std::atomic<uint16_t> status_word_;
221 uint16_t control_word_;
222 std::mutex cw_mutex_;
223 std::atomic<bool> start_fault_reset_;
224 std::atomic<State402::InternalState> target_state_;
225
226 State402 state_handler_;
227
228 std::mutex map_mutex_;
229 std::unordered_map<uint16_t, ModeSharedPtr> modes_;
230 typedef std::function<void()> AllocFuncType;
231 std::unordered_map<uint16_t, AllocFuncType> mode_allocators_;
232
233 ModeSharedPtr selected_mode_;
234 uint16_t mode_id_;
235 std::condition_variable mode_cond_;
236 std::mutex mode_mutex_;
237 const State402::InternalState switching_state_;
238 const bool monitor_mode_;
239 const std::chrono::seconds state_switch_timeout_;
240 const int homing_timeout_seconds_;
241
242 std::shared_ptr<LelyDriverBridge> driver;
243 const uint16_t status_word_entry_index = 0x6041;
244 const uint16_t control_word_entry_index = 0x6040;
245 const uint16_t op_mode_display_index = 0x6061;
246 const uint16_t op_mode_index = 0x6060;
247 const uint16_t supported_drive_modes_index = 0x6502;
248
249 // Diagnostic components
250 std::atomic<bool> enable_diagnostics_;
251 std::shared_ptr<DiagnosticsCollector> diag_collector_;
252};
253
254} // namespace ros2_canopen
255
256#endif
@ CW_Operation_mode_specific1
Definition command.hpp:65
@ CW_Operation_mode_specific0
Definition command.hpp:64
@ CW_Operation_mode_specific2
Definition command.hpp:66
Definition mode_forward_helper.hpp:31
Definition motor.hpp:60
bool handleDisable()
Disable the drive.
void handleDiag()
Updates the device diagnostic information.
bool handleShutdown()
Shutdowns the drive.
bool handleHalt()
Executes a quickstop.
double get_position() const
Definition motor.hpp:200
virtual bool isModeSupported(uint16_t mode)
Check if Operation Mode is supported.
bool handleEnable()
Enable the drive.
bool handleInit()
Initialise the drive.
double get_speed() const
Definition motor.hpp:199
bool handleRecover()
Recovers the device from fault.
Motor402(std::shared_ptr< LelyDriverBridge > driver, ros2_canopen::State402::InternalState switching_state, int homing_timeout_seconds)
Definition motor.hpp:62
virtual bool enterModeAndWait(uint16_t mode)
Enter Operation Mode.
void handleRead()
Read objects of the drive.
virtual bool setTarget(double val)
Set target.
virtual uint16_t getMode()
Get current Mode.
void handleWrite()
Writes objects to the drive.
virtual void registerDefaultModes()
Tries to register the standard operation modes defined in cia402.
Definition motor.hpp:186
bool registerMode(uint16_t mode, Args &&... args)
Register a new operation mode for the drive.
Definition motor.hpp:170
void set_diagnostic_status_msgs(std::shared_ptr< DiagnosticsCollector > status, bool enable)
Definition motor.hpp:205
Motor Base Class.
Definition base.hpp:32
@ Profiled_Velocity
Definition base.hpp:42
@ Cyclic_Synchronous_Velocity
Definition base.hpp:48
@ Cyclic_Synchronous_Torque
Definition base.hpp:49
@ Cyclic_Synchronous_Position
Definition base.hpp:47
@ Profiled_Position
Definition base.hpp:40
@ Homing
Definition base.hpp:45
@ Profiled_Torque
Definition base.hpp:43
@ Interpolated_Position
Definition base.hpp:46
@ Velocity
Definition base.hpp:41
Definition state.hpp:28
InternalState
Definition state.hpp:50
Definition configuration_manager.hpp:28
ModeForwardHelper< MotorBase::Cyclic_Synchronous_Position, int32_t, 0x607A, 0, 0 > CyclicSynchronousPositionMode
Definition motor.hpp:44
std::shared_ptr< Mode > ModeSharedPtr
Definition mode.hpp:45
ModeForwardHelper< MotorBase::Cyclic_Synchronous_Velocity, int32_t, 0x60FF, 0, 0 > CyclicSynchronousVelocityMode
Definition motor.hpp:46
ModeForwardHelper< MotorBase::Profiled_Velocity, int32_t, 0x60FF, 0, 0 > ProfiledVelocityMode
Definition motor.hpp:41
ModeForwardHelper< MotorBase::Profiled_Torque, int16_t, 0x6071, 0, 0 > ProfiledTorqueMode
Definition motor.hpp:42
ModeForwardHelper< MotorBase::Cyclic_Synchronous_Torque, int16_t, 0x6071, 0, 0 > CyclicSynchronousTorqueMode
Definition motor.hpp:48