UMRT Arm Firmware Library
mks_stepper_controller.hpp
1 //
2 // Created by Noah Reeder on 2025-04-22
3 //
4 
5 #ifndef UMRT_ARM_FIRMWARE_LIB_MKS_STEPPER_CONTROLLER_HPP
6 #define UMRT_ARM_FIRMWARE_LIB_MKS_STEPPER_CONTROLLER_HPP
7 
8 #include <boost/signals2.hpp>
9 #include <chrono>
10 #include <string>
11 #include <unordered_set>
12 #include <vector>
13 
14 #include "mks_enums.hpp"
15 
16 // Forward declaring these classes so that ros2_socketcan can be a private dependency
17 namespace drivers::socketcan {
18  class SocketCanReceiver;
19  class SocketCanSender;
20  class CanId;
21 } // namespace drivers::socketcan
22 
38 public:
48  const std::string& can_interface, std::shared_ptr<const std::unordered_set<uint16_t>> motor_ids,
49  const uint8_t norm_factor = 1
50  );
51 
55  ~MksStepperController() noexcept;
56 
67  bool setSpeed(const uint16_t motor, const int16_t speed, const uint8_t acceleration = 0);
68 
80  bool sendStep(const uint16_t motor, const uint32_t num_steps, const int16_t speed, const uint8_t acceleration = 0);
81 
92  bool seekPosition(const uint16_t motor, const int32_t position, const int16_t speed, const uint8_t acceleration = 0);
93 
100  bool getPosition(const uint16_t motor);
101 
106  [[nodiscard]] bool isSetup() const;
107 
115 
116  // ==========================
117  // Events
118  // ==========================
119 
127  boost::signals2::signal<void(uint16_t, bool)> ESetSpeed;
128 
136  boost::signals2::signal<void(uint16_t, MksMoveResponse)> ESendStep;
137 
145  boost::signals2::signal<void(uint16_t, MksMoveResponse)> ESeekPosition;
146 
154  boost::signals2::signal<void(uint16_t, int32_t)> EGetPosition;
155 
156 protected:
163  void handleCanMessage(const std::vector<uint8_t>& message, drivers::socketcan::CanId& info);
164 
173  void handleESetSpeed(const std::vector<uint8_t>& message, drivers::socketcan::CanId& info);
174 
175  void handleESendStep(const std::vector<uint8_t>& message, drivers::socketcan::CanId& info);
176 
177  void handleESeekPosition(const std::vector<uint8_t>& message, drivers::socketcan::CanId& info);
178 
179  void handleEGetPosition(const std::vector<unsigned char>& message, drivers::socketcan::CanId& info);
181 
185  const uint8_t norm_factor;
186 
187 private:
191  bool setup_completed;
192 };
193 
194 #endif //UMRT_ARM_FIRMWARE_LIB_MKS_STEPPER_CONTROLLER_HPP
Abstracts CAN bus communication to MKS SERVO57D/42D/35D/28D stepper motor driver modules.
Definition: mks_stepper_controller.hpp:37
bool seekPosition(const uint16_t motor, const int32_t position, const int16_t speed, const uint8_t acceleration=0)
Sends a MksCommands::SEEK_POS_BY_STEPS command to move a motor to specific step position.
Definition: mks_stepper_controller.cpp:116
void update(const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero())
Polls for CAN messages.
Definition: mks_stepper_controller.cpp:169
bool sendStep(const uint16_t motor, const uint32_t num_steps, const int16_t speed, const uint8_t acceleration=0)
Sends a MksCommands::SEND_STEP command to move a motor a fixed number of steps.
Definition: mks_stepper_controller.cpp:85
boost::signals2::signal< void(uint16_t, int32_t)> EGetPosition
Boost signal triggered when getPosition responses are received.
Definition: mks_stepper_controller.hpp:154
boost::signals2::signal< void(uint16_t, bool)> ESetSpeed
Boost signal triggered when setSpeed responses are received.
Definition: mks_stepper_controller.hpp:127
boost::signals2::signal< void(uint16_t, MksMoveResponse)> ESeekPosition
Boost signal triggered when seekPosition responses are received.
Definition: mks_stepper_controller.hpp:145
~MksStepperController() noexcept
Destroys an MksStepperController.
Definition: mks_stepper_controller.cpp:37
MksStepperController(const std::string &can_interface, std::shared_ptr< const std::unordered_set< uint16_t >> motor_ids, const uint8_t norm_factor=1)
Initializes an MksStepperController.
Definition: mks_stepper_controller.cpp:20
bool setSpeed(const uint16_t motor, const int16_t speed, const uint8_t acceleration=0)
Sends a MksCommands::SET_SPEED command to set the speed of a motor.
Definition: mks_stepper_controller.cpp:39
boost::signals2::signal< void(uint16_t, MksMoveResponse)> ESendStep
Boost signal triggered when sendStep responses are received.
Definition: mks_stepper_controller.hpp:136
bool isSetup() const
Returns whether the CAN bus connection has been fully established.
Definition: mks_stepper_controller.cpp:167
void handleCanMessage(const std::vector< uint8_t > &message, drivers::socketcan::CanId &info)
Handles received CAN messages and sends out signals as appropriate.
Definition: mks_stepper_controller.cpp:221
bool getPosition(const uint16_t motor)
Sends a MksCommands::CURRENT_POS command to query the current position of a motor in steps.
Definition: mks_stepper_controller.cpp:151
Enums for interacting with commands described in MKS_COMMANDS.hpp.
MksMoveResponse
Status code for the response to move commands.
Definition: mks_enums.hpp:21