UMRT Arm Firmware Library
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
MksStepperController Class Reference

Abstracts CAN bus communication to MKS SERVO57D/42D/35D/28D stepper motor driver modules. More...

#include <mks_stepper_controller.hpp>

Collaboration diagram for MksStepperController:
Collaboration graph
[legend]

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.

Parameters
messagethe de-firmatified Sysex payload
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)
 

Detailed Description

Abstracts CAN bus communication to MKS SERVO57D/42D/35D/28D stepper motor driver modules.

Responses are conveyed through Boost signals.

Interpolated Normalisation

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.

Constructor & Destructor Documentation

◆ MksStepperController()

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.

Parameters
can_interfaceSocketCAN network interface corresponding to the CAN bus
motor_idsCAN IDs for the motor controllers, used to filter CAN messages so other devices' messages aren't attempted to be decoded
norm_factorinterpolated normalisation factor to use, see Interpolated Normalisation; defaults to off

Member Function Documentation

◆ getPosition()

bool MksStepperController::getPosition ( const uint16_t  motor)

Sends a MksCommands::CURRENT_POS command to query the current position of a motor in steps.

Parameters
motorthe ID of the motor to query
Returns
true if transmitted over the CAN bus

◆ handleCanMessage()

void MksStepperController::handleCanMessage ( const std::vector< uint8_t > &  message,
drivers::socketcan::CanId &  info 
)
protected

Handles received CAN messages and sends out signals as appropriate.

Parameters
messagethe message payload
infoauxiliary information associated with the message, e.g. driver ID, bus time

◆ isSetup()

bool MksStepperController::isSetup ( ) const

Returns whether the CAN bus connection has been fully established.

Returns
true if so

◆ seekPosition()

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.

Parameters
motorthe ID of the motor to move
positionthe target position in number of steps from the motor's zero point, maximum of 2^23 - 1
speedthe signed target speed to set the motor to, in RPM; note that the absolute value is taken
accelerationthe speed ramp profile, see MksTest.Constants.MAX_ACCEL; defaults to instantaneous
Returns
true if transmitted over the CAN bus

◆ sendStep()

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.

Parameters
motorthe ID of the motor to move
num_stepsthe number of steps to move, maximum of 2^24 - 1
speedthe signed target speed to set the motor to, in RPM
accelerationthe speed ramp profile, see MksTest.Constants.MAX_ACCEL; defaults to instantaneous
Returns
true if transmitted over the CAN bus

◆ setSpeed()

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.

Parameters
motorthe ID of the motor to control
speedthe signed target speed to set the motor to, in RPM
accelerationthe speed ramp profile, see MksTest.Constants.MAX_ACCEL; defaults to instantaneous
Returns
true if transmitted over the CAN bus

◆ update()

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.

Parameters
timeoutmaximum time to wait for a message to appear on the bus

Member Data Documentation

◆ EGetPosition

boost::signals2::signal<void(uint16_t, int32_t)> MksStepperController::EGetPosition

Boost signal triggered when getPosition responses are received.

Parameters
1st[uint8_t] motor ID
2nd[int32_t] motor position in steps

◆ ESeekPosition

boost::signals2::signal<void(uint16_t, MksMoveResponse)> MksStepperController::ESeekPosition

Boost signal triggered when seekPosition responses are received.

Parameters
1st[uint8_t] motor ID
2nd[MksMoveResponse] current movement status

◆ ESendStep

boost::signals2::signal<void(uint16_t, MksMoveResponse)> MksStepperController::ESendStep

Boost signal triggered when sendStep responses are received.

Parameters
1st[uint8_t] motor ID
2nd[MksMoveResponse] current movement status

◆ ESetSpeed

boost::signals2::signal<void(uint16_t, bool)> MksStepperController::ESetSpeed

Boost signal triggered when setSpeed responses are received.

Parameters
1st[uint8_t] motor ID
2nd[bool] 1 if movement succeeded

The documentation for this class was generated from the following files: