UMRT Arm Firmware Library
|
Abstracts CAN bus communication to MKS SERVO57D/42D/35D/28D stepper motor driver modules. More...
#include <mks_stepper_controller.hpp>
Public Member Functions | |
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. More... | |
~MksStepperController () noexcept | |
Destroys an MksStepperController. | |
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. More... | |
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. More... | |
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. More... | |
bool | getPosition (const uint16_t motor) |
Sends a MksCommands::CURRENT_POS command to query the current position of a motor in steps. More... | |
bool | isSetup () const |
Returns whether the CAN bus connection has been fully established. More... | |
void | update (const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds::zero()) |
Polls for CAN messages. More... | |
Public Attributes | |
boost::signals2::signal< void(uint16_t, bool)> | ESetSpeed |
Boost signal triggered when setSpeed responses are received. More... | |
boost::signals2::signal< void(uint16_t, MksMoveResponse)> | ESendStep |
Boost signal triggered when sendStep responses are received. More... | |
boost::signals2::signal< void(uint16_t, MksMoveResponse)> | ESeekPosition |
Boost signal triggered when seekPosition responses are received. More... | |
boost::signals2::signal< void(uint16_t, int32_t)> | EGetPosition |
Boost signal triggered when getPosition responses are received. More... | |
Protected Member Functions | |
void | handleCanMessage (const std::vector< uint8_t > &message, drivers::socketcan::CanId &info) |
Handles received CAN messages and sends out signals as appropriate. More... | |
Signal Processing Helper Functions | |||
Helper functions for decoding the parameters of Sysex commands processed by handleSysex before forwarding to their associated signal.
| |||
std::unique_ptr< drivers::socketcan::SocketCanReceiver > | can_receiver | ||
std::unique_ptr< drivers::socketcan::SocketCanSender > | can_sender | ||
std::shared_ptr< const std::unordered_set< uint16_t > > | motor_ids | ||
const uint8_t | norm_factor | ||
void | handleESetSpeed (const std::vector< uint8_t > &message, drivers::socketcan::CanId &info) | ||
void | handleESendStep (const std::vector< uint8_t > &message, drivers::socketcan::CanId &info) | ||
void | handleESeekPosition (const std::vector< uint8_t > &message, drivers::socketcan::CanId &info) | ||
void | handleEGetPosition (const std::vector< unsigned char > &message, drivers::socketcan::CanId &info) | ||
Abstracts CAN bus communication to MKS SERVO57D/42D/35D/28D stepper motor driver modules.
Responses are conveyed through Boost signals.
To get around the limitations introduced by specifying speeds under nominal conditions, a technique which will be referred to as "interpolated normalisation" is used to normalise the units of speed to RPM, assuming 200 full-steps per revolution. In other words, the unit of speed shifts from 160/3 steps/s to 200 steps/min.
This is achieved by micro-stepping the motor at a specified interpolation factor. For example, if an interpolation factor of 16 is used, and the motor is requested to move at 2 RPM, the motor will actually move at 6400 steps/min.
An interpolation factor of 1 can be used to disable interpolation.
MksStepperController::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.
can_interface | SocketCAN network interface corresponding to the CAN bus |
motor_ids | CAN IDs for the motor controllers, used to filter CAN messages so other devices' messages aren't attempted to be decoded |
norm_factor | interpolated normalisation factor to use, see Interpolated Normalisation; defaults to off |
bool MksStepperController::getPosition | ( | const uint16_t | motor | ) |
Sends a MksCommands::CURRENT_POS command to query the current position of a motor in steps.
motor | the ID of the motor to query |
true
if transmitted over the CAN bus
|
protected |
Handles received CAN messages and sends out signals as appropriate.
message | the message payload |
info | auxiliary information associated with the message, e.g. driver ID, bus time |
bool MksStepperController::isSetup | ( | ) | const |
Returns whether the CAN bus connection has been fully established.
true
if so bool MksStepperController::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.
Since this command seeks a position, the sign of the speed is ignored.
motor | the ID of the motor to move |
position | the target position in number of steps from the motor's zero point, maximum of 2^23 - 1 |
speed | the signed target speed to set the motor to, in RPM; note that the absolute value is taken |
acceleration | the speed ramp profile, see MksTest.Constants.MAX_ACCEL; defaults to instantaneous |
true
if transmitted over the CAN bus bool MksStepperController::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.
Direction is controlled by the sign of the target speed. Response callbacks are available through ESendStep.
motor | the ID of the motor to move |
num_steps | the number of steps to move, maximum of 2^24 - 1 |
speed | the signed target speed to set the motor to, in RPM |
acceleration | the speed ramp profile, see MksTest.Constants.MAX_ACCEL; defaults to instantaneous |
true
if transmitted over the CAN bus bool MksStepperController::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.
Response callbacks are available through ESetSpeed. See MksTest.Constants.MAX_SPEED for speed limits.
motor | the ID of the motor to control |
speed | the signed target speed to set the motor to, in RPM |
acceleration | the speed ramp profile, see MksTest.Constants.MAX_ACCEL; defaults to instantaneous |
true
if transmitted over the CAN bus void MksStepperController::update | ( | const std::chrono::nanoseconds & | timeout = std::chrono::nanoseconds::zero() | ) |
Polls for CAN messages.
If an applicable message is received, the appropriate event is signalled.
timeout | maximum time to wait for a message to appear on the bus |
boost::signals2::signal<void(uint16_t, int32_t)> MksStepperController::EGetPosition |
Boost signal triggered when getPosition responses are received.
1st | [uint8_t] motor ID |
2nd | [int32_t] motor position in steps |
boost::signals2::signal<void(uint16_t, MksMoveResponse)> MksStepperController::ESeekPosition |
Boost signal triggered when seekPosition responses are received.
1st | [uint8_t] motor ID |
2nd | [MksMoveResponse] current movement status |
boost::signals2::signal<void(uint16_t, MksMoveResponse)> MksStepperController::ESendStep |
Boost signal triggered when sendStep responses are received.
1st | [uint8_t] motor ID |
2nd | [MksMoveResponse] current movement status |
boost::signals2::signal<void(uint16_t, bool)> MksStepperController::ESetSpeed |
Boost signal triggered when setSpeed responses are received.
1st | [uint8_t] motor ID |
2nd | [bool] 1 if movement succeeded |