tmc_coe_stepper_motor.cpp
Go to the documentation of this file.
1 
7 
9 
10 /* Constructor */
12  uint8_t slave_number, uint8_t motor_number) :
13 TmcCoeMotor(p_nh, p_tmc_coe_interpreter, slave_number, motor_number)
14 {
15  ROS_DEBUG_STREAM("[TmcCoeStepperMotor::" << __func__ << "] called");
16 }
17 
18 /* Destructor */
20 {
21  ROS_DEBUG_STREAM("[TmcCoeStepperMotor::" << __func__ << "] called");
22 }
23 
25 
27 {
28  const std::string s_commutation_mode = "Commutation Mode";
29  const std::string s_position_scaler = "Position Scaler";
30  const std::string s_encoder_steps = "Encoder Settings - Steps";
31  std::string val = "";
32  position_scaler_ = 0;
33  encoder_steps_ = 0;
34 
35  ROS_INFO_STREAM("[TmcCoeStepperMotor::" << __func__ << "] called");
36 
37  if(p_tmc_coe_interpreter_->readSDO(slave_number_, s_commutation_mode, &val))
38  {
39  commutation_mode_ = (stepper_comm_mode_t) std::stoi(val);
40  val = "";
41  }
42  else
43  {
45  ROS_ERROR_STREAM("[" << __func__ << "] Object Name for Commutation Mode is not Available");
46  }
47 
49  {
50  if(p_tmc_coe_interpreter_->readSDO(slave_number_, s_position_scaler, &val))
51  {
52  position_scaler_ = std::stoi(val);
53  val = "";
54  }
55  else
56  {
57  ROS_ERROR_STREAM("[" << __func__ << "] Object Name for Position Scaler not Available");
58  }
59  }
60 
62  {
63  ROS_WARN_STREAM("[" << __func__ << "] Checking Encoder Steps");
64 
65  if(p_tmc_coe_interpreter_->readSDO(slave_number_, s_encoder_steps, &val))
66  {
67  encoder_steps_ = std::stoi(val);
68  }
69  else
70  {
71  ROS_ERROR_STREAM("[" << __func__ << "] Object Name for Encoder Steps not Available");
72  }
73  }
74 
75 
76  if(param_wheel_diameter_ == 0)
77  {
78  ROS_INFO_STREAM("[" << __func__ << "] Velocity unit: rpm");
79  }
80  else
81  {
82  ROS_INFO_STREAM("[" << __func__ << "] Velocity unit: m/s");
83  }
84 
85  if(position_scaler_ == 0 && encoder_steps_ == 0)
86  {
87  ROS_INFO_STREAM("[" << __func__ << "] Position unit: pulses");
88  }
89  else
90  {
91  ROS_INFO_STREAM("[" << __func__ << "] Position unit: angular degrees");
92  }
93 
94  ROS_INFO_STREAM("[" << __func__ << "] Torque unit: mA");
95 
96  initPublisher();
97  this->initSubscriber();
98 
99  ROS_INFO_STREAM("[" << __func__ << "] Motor" << static_cast<int>(motor_number_) << " Initialized!\n");
100 }
101 
102 /* Publisher Callback */
104 {
105  std::string mode_of_operation = "";
106 
107  tmc_coe_info_msg_.header.stamp = ros::Time::now();
108  tmc_coe_info_msg_.header.seq = seq_ctr_;
109  tmc_coe_info_msg_.header.frame_id = frame_id_;
110  tmc_coe_info_msg_.interface_name = param_interface_name_;
111  tmc_coe_info_msg_.slave_number = slave_number_;
112  tmc_coe_info_msg_.motor_number = motor_number_;
113 
114  /* Initialize messages to 0 first */
115  tmc_coe_info_msg_.velocity = 0;
116  tmc_coe_info_msg_.position = 0;
117  tmc_coe_info_msg_.torque = 0;
118 
119  switch (p_tmc_coe_interpreter_->input_pdo_[slave_number_]->modes_of_operation_display)
120  {
121  case NONE: mode_of_operation = "None"; break;
122  case PROFILE_POSITION: mode_of_operation = "Profile Position"; break;
123  case PROFILE_VELOCITY: mode_of_operation = "Profile Velocity"; break;
124  case HOMING_MODE: mode_of_operation = "Homing Mode"; break;
125  case CYCLIC_SYNC_POS: mode_of_operation = "Cyclic Synchronous Position Mode"; break;
126  case CYCLIC_SYNC_VEL: mode_of_operation = "Cyclic Synchronous Velocity Mode"; break;
127  case CYCLIC_SYNC_TRQ: mode_of_operation = "Cyclic Synchronous Torque Mode"; break;
128  default : mode_of_operation = "NONE"; break;
129  }
130  tmc_coe_info_msg_.mode_of_operation = mode_of_operation;
132 
134  {
135  tmc_coe_info_msg_.velocity = p_tmc_coe_interpreter_->input_pdo_[slave_number_]->velocity_actual_value;
136 
137  // Convert rpm to linear velocity
138  if(param_wheel_diameter_ == 0)
139  {
140  tmc_coe_info_msg_.velocity = p_tmc_coe_interpreter_->input_pdo_[slave_number_]->velocity_actual_value *
142  }
143  else
144  {
145  tmc_coe_info_msg_.velocity = p_tmc_coe_interpreter_->input_pdo_[slave_number_]->velocity_actual_value *
147  }
148  }
149 
151  {
152  tmc_coe_info_msg_.position = p_tmc_coe_interpreter_->input_pdo_[slave_number_]->position_actual_value;
153 
154  // Convert steps to degrees
155  if(position_scaler_ > 0)
156  {
157  tmc_coe_info_msg_.position = p_tmc_coe_interpreter_->input_pdo_[slave_number_]->position_actual_value *
159  }
160  else if(encoder_steps_ > 0)
161  {
162  tmc_coe_info_msg_.position = p_tmc_coe_interpreter_->input_pdo_[slave_number_]->position_actual_value *
164  }
165  else
166  {
167  tmc_coe_info_msg_.position = p_tmc_coe_interpreter_->input_pdo_[slave_number_]->position_actual_value *
169  }
170  }
171 
173  {
174  tmc_coe_info_msg_.torque = p_tmc_coe_interpreter_->input_pdo_[slave_number_]->torque_actual_value *
176  }
177 
179  seq_ctr_++;
180 }
181 
182 /* Initialize Subscriber */
184 {
185  ROS_INFO_STREAM("[TmcCoeStepperMotor::" << __func__ << "] called");
186 
187  switch (commutation_mode_)
188  {
190  ROS_INFO_STREAM("[" << __func__ << "] Commutation Mode : DISABLED");
191  break;
192 
194  ROS_INFO_STREAM("[" << __func__ << "] Commutation Mode : OPEN LOOP");
196  break;
197 
198  default:
199  ROS_INFO_STREAM("[" << __func__ << "] Commutation Mode : CLOSED LOOP");
201  break;
202  }
203 }
204 
205 /* Subscriber Callback */
206 void TmcCoeStepperMotor::cmdVelCallback(const geometry_msgs::Twist& msg)
207 {
208  float val = msg.linear.x;
209  int32_t board_val = 0;
210  uint8_t prev_cycle_count = 0;
211  uint8_t retries = 0;
212 
213  //If wheel diameter is set to 0 (or no wheels connected), the input value for linearX is equal to motors rpm
214  if(param_wheel_diameter_ == 0)
215  {
216  board_val = val / param_add_ratio_vel_;
217  }
218  else
219  {
220  //Formula to convert linear value to rpm (unit that the board accepts)
221  board_val = val * (SECS_TO_MIN / (PI * param_wheel_diameter_)) * (1 / param_add_ratio_vel_);
222  }
223 
224  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << val << " board value: "
225  << board_val);
227 
228  while(retries <= param_SDO_PDO_retries_)
229  {
231  {
232  if(p_tmc_coe_interpreter_->input_pdo_[slave_number_]->modes_of_operation_display != PROFILE_VELOCITY)
233  {
235  }
236  p_tmc_coe_interpreter_->output_pdo_[slave_number_]->target_velocity = board_val;
237 
238  while((p_tmc_coe_interpreter_->getCycleCounter() - prev_cycle_count) < 1)
239  {
240  // Wait until it reaches next cycle
241  }
242 
243  if(p_tmc_coe_interpreter_->output_pdo_[slave_number_]->target_velocity == board_val)
244  {
245  ROS_DEBUG_STREAM("["<< __func__ << "] Subscriber callback exited successfully");
246  break;
247  }
248  prev_cycle_count = p_tmc_coe_interpreter_->getCycleCounter();
249  retries++;
250  }
251  }
253 
254  if(p_tmc_coe_interpreter_->output_pdo_[slave_number_]->target_velocity != board_val)
255  {
256  ROS_ERROR_STREAM("["<< __func__ << "] Failed to set Velocity");
257  }
258 }
259 
260 void TmcCoeStepperMotor::cmdAbsPosCallback(const std_msgs::Int32 msg)
261 {
262  float convert_const_deg = 0.00;
263  int32_t unit_val = 0;
264  int32_t val = msg.data;
265 
266  //convert input(degrees) to unit
267  if(position_scaler_ > 0)
268  {
269  convert_const_deg = ((float)position_scaler_ / ANGULAR_FULL_ROTATION) * (1 / param_add_ratio_pos_);
270  }
271  else if(encoder_steps_ > 0)
272  {
273  convert_const_deg = ((float)encoder_steps_ / ANGULAR_FULL_ROTATION) * (1 / param_add_ratio_pos_);
274  }
275  else
276  {
277  //inverting position additional ratio
278  convert_const_deg = 1 / param_add_ratio_pos_;
279  }
280 
281  unit_val = val * convert_const_deg;
282 
283  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << msg.data << " board value: "
284  << unit_val);
286 
288  {
290  {
291  if(p_tmc_coe_interpreter_->input_pdo_[slave_number_]->modes_of_operation_display != PROFILE_POSITION)
292  {
294  }
295  p_tmc_coe_interpreter_->output_pdo_[slave_number_]->target_position = unit_val;
297  }
298 
300  {
301  break;
302  }
303  }
306 
308  {
310  {
312  }
314  {
315  break;
316  }
317  }
319 
320  if(p_tmc_coe_interpreter_->output_pdo_[slave_number_]->target_position == unit_val)
321  {
322  ROS_DEBUG_STREAM("["<< __func__ << "] Subscriber callback exited successfully");
323  }
324  else
325  {
326  ROS_ERROR_STREAM("["<< __func__ << "] Failed to set Absolute Position");
327  }
328 }
329 
330 void TmcCoeStepperMotor::cmdRelPosCallback(const std_msgs::Int32 msg)
331 {
332  float convert_const_deg = 0.00;
333  int32_t unit_val = 0;
334  int32_t val = msg.data;
335 
336  //convert input(degrees) to unit
337  if(position_scaler_ > 0)
338  {
339  convert_const_deg = ((float)position_scaler_ / ANGULAR_FULL_ROTATION) * (1 / param_add_ratio_pos_);
340  }
341  else if(encoder_steps_ > 0)
342  {
343  convert_const_deg = ((float)encoder_steps_ / ANGULAR_FULL_ROTATION) * (1 / param_add_ratio_pos_);
344  }
345  else
346  {
347  //inverting position additional ratio
348  convert_const_deg = 1 / param_add_ratio_pos_;
349  }
350 
351  unit_val = val * convert_const_deg;
352 
353  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << msg.data << " board value: "
354  << unit_val);
356 
358  {
360  {
361  if(p_tmc_coe_interpreter_->input_pdo_[slave_number_]->modes_of_operation_display != PROFILE_POSITION)
362  {
364  }
365  p_tmc_coe_interpreter_->output_pdo_[slave_number_]->target_position = unit_val;
367  }
368 
370  {
371  break;
372  }
373  }
376 
378  {
380  {
382  }
384  {
385  break;
386  }
387  }
389 
390  if(p_tmc_coe_interpreter_->output_pdo_[slave_number_]->target_position == unit_val)
391  {
392  ROS_DEBUG_STREAM("["<< __func__ << "] Subscriber callback exited successfully");
393  }
394  else
395  {
396  ROS_ERROR_STREAM("["<< __func__ << "] Failed to set Relative Position");
397  }
398 }
stepper_comm_mode_t
stepper_comm_mode_t
Definition: tmc_coe_stepper_motor.h:12
SET_POINT_ACK_IN_PROCESS
@ SET_POINT_ACK_IN_PROCESS
Definition: tmc_coe_interpreter.h:60
uint8_t
unsigned char uint8_t
TmcCoeStepperMotor::~TmcCoeStepperMotor
~TmcCoeStepperMotor()
Definition: tmc_coe_stepper_motor.cpp:19
TmcCoeMotor::slave_number_
uint8_t slave_number_
Definition: tmc_coe_motor.h:74
PI
const double PI
Definition: tmc_coe_motor.h:41
TmcCoeMotor::tmc_coe_info_msg_
adi_tmc_coe::TmcCoeInfo tmc_coe_info_msg_
Definition: tmc_coe_motor.h:58
TmcCoeInterpreter::input_pdo_
std::vector< input_pdo_t * > input_pdo_
Definition: tmc_coe_interpreter.h:200
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
PROFILE_VELOCITY
@ PROFILE_VELOCITY
Definition: tmc_coe_motor.h:20
PROFILE_POSITION
@ PROFILE_POSITION
Definition: tmc_coe_motor.h:19
TmcCoeMotor::param_interface_name_
std::string param_interface_name_
Definition: tmc_coe_motor.h:89
TmcCoeStepperMotor::commutation_mode_
stepper_comm_mode_t commutation_mode_
Definition: tmc_coe_stepper_motor.h:34
TmcCoeMotor::param_wheel_diameter_
float param_wheel_diameter_
Definition: tmc_coe_motor.h:85
ANGULAR_FULL_ROTATION
const uint16_t ANGULAR_FULL_ROTATION
Definition: tmc_coe_motor.h:45
TmcCoeInterpreter::readSDO
std::string readSDO(uint8_t slave_number, uint16_t index_number, uint16_t subindex_number, T value)
Definition: tmc_coe_interpreter.cpp:860
SECS_TO_MIN
const uint8_t SECS_TO_MIN
Definition: tmc_coe_motor.h:42
TmcCoeMotor::param_add_ratio_pos_
float param_add_ratio_pos_
Definition: tmc_coe_motor.h:87
ENABLE_OPERATION
@ ENABLE_OPERATION
Definition: tmc_coe_interpreter.h:32
STEPPER_OPENLOOP_MOTOR
@ STEPPER_OPENLOOP_MOTOR
Definition: tmc_coe_stepper_motor.h:15
TmcCoeMotor::param_SDO_PDO_retries_
int param_SDO_PDO_retries_
Definition: tmc_coe_motor.h:75
TmcCoeMotor::param_add_ratio_trq_
float param_add_ratio_trq_
Definition: tmc_coe_motor.h:88
HOMING_MODE
@ HOMING_MODE
Definition: tmc_coe_motor.h:21
TmcCoeInterpreter::statusWordState
bool statusWordState(uint8_t slave_number, fsa_state_t state)
Definition: tmc_coe_interpreter.cpp:355
ROS_DEBUG_STREAM
#define ROS_DEBUG_STREAM(args)
TmcCoeMotor::param_pub_actual_trq_
bool param_pub_actual_trq_
Definition: tmc_coe_motor.h:79
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
CYCLIC_SYNC_VEL
@ CYCLIC_SYNC_VEL
Definition: tmc_coe_motor.h:23
ROS_WARN_STREAM
#define ROS_WARN_STREAM(args)
TmcCoeStepperMotor::init
void init() override
Definition: tmc_coe_stepper_motor.cpp:26
TmcCoeStepperMotor::position_scaler_
int32_t position_scaler_
Definition: tmc_coe_stepper_motor.h:33
TmcCoeStepperMotor::cmdVelCallback
void cmdVelCallback(const geometry_msgs::Twist &msg) override
Definition: tmc_coe_stepper_motor.cpp:206
TmcCoeStepperMotor::initSubscriber
void initSubscriber() override
Definition: tmc_coe_stepper_motor.cpp:183
STEPPER_DISABLED_MOTOR
@ STEPPER_DISABLED_MOTOR
Definition: tmc_coe_stepper_motor.h:14
TmcCoeMotor::initSubscriber
virtual void initSubscriber()
Definition: tmc_coe_motor.cpp:329
TmcCoeMotor::seq_ctr_
uint32_t seq_ctr_
Definition: tmc_coe_motor.h:60
TmcCoeMotor::initPublisher
void initPublisher()
Definition: tmc_coe_motor.cpp:237
TmcCoeInterpreter::isCycleFinished
bool isCycleFinished()
Definition: tmc_coe_interpreter.cpp:679
TmcCoeInterpreter::stopCycleCounter
void stopCycleCounter()
Definition: tmc_coe_interpreter.cpp:707
ros::TimerEvent
TmcCoeMotor::frame_id_
std::string frame_id_
Definition: tmc_coe_motor.h:59
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
CYCLIC_SYNC_POS
@ CYCLIC_SYNC_POS
Definition: tmc_coe_motor.h:22
TmcCoeStepperMotor::encoder_steps_
uint32_t encoder_steps_
Definition: tmc_coe_stepper_motor.h:32
TmcCoeMotor
Definition: tmc_coe_motor.h:47
RELATIVE_POSITION
@ RELATIVE_POSITION
Definition: tmc_coe_motor.h:31
TmcCoeStepperMotor::cmdRelPosCallback
void cmdRelPosCallback(const std_msgs::Int32 msg) override
Definition: tmc_coe_stepper_motor.cpp:330
TmcCoeInterpreter::getCycleCounter
uint8_t getCycleCounter()
Definition: tmc_coe_interpreter.cpp:689
TmcCoeMotor::tmc_coe_info_pub_
ros::Publisher tmc_coe_info_pub_
Definition: tmc_coe_motor.h:57
int32_t
signed int int32_t
ABSOLUTE_POSITION
@ ABSOLUTE_POSITION
Definition: tmc_coe_motor.h:30
TmcCoeInterpreter
Definition: tmc_coe_interpreter.h:148
NONE
@ NONE
Definition: tmc_coe_motor.h:18
TmcCoeInterpreter::output_pdo_
std::vector< output_pdo_t * > output_pdo_
Definition: tmc_coe_interpreter.h:201
TmcCoeInterpreter::startCycleCounter
void startCycleCounter()
Definition: tmc_coe_interpreter.cpp:698
TmcCoeMotor::p_tmc_coe_interpreter_
TmcCoeInterpreter * p_tmc_coe_interpreter_
Definition: tmc_coe_motor.h:93
TmcCoeMotor::param_add_ratio_vel_
float param_add_ratio_vel_
Definition: tmc_coe_motor.h:86
TmcCoeStepperMotor::cmdAbsPosCallback
void cmdAbsPosCallback(const std_msgs::Int32 msg) override
Definition: tmc_coe_stepper_motor.cpp:260
tmc_coe_stepper_motor.h
TmcCoeMotor::param_pub_actual_pos_
bool param_pub_actual_pos_
Definition: tmc_coe_motor.h:78
CYCLIC_SYNC_TRQ
@ CYCLIC_SYNC_TRQ
Definition: tmc_coe_motor.h:24
TmcCoeStepperMotor::TmcCoeStepperMotor
TmcCoeStepperMotor(ros::NodeHandle *p_nh, TmcCoeInterpreter *p_tmc_coe_interpreter, uint8_t slave_number, uint8_t motor_number)
Definition: tmc_coe_stepper_motor.cpp:11
TmcCoeMotor::motor_number_
uint8_t motor_number_
Definition: tmc_coe_motor.h:73
TmcCoeStepperMotor::rosPublishTmcCoeInfo
void rosPublishTmcCoeInfo(const ros::TimerEvent &event) override
Definition: tmc_coe_stepper_motor.cpp:103
TmcCoeMotor::param_pub_actual_vel_
bool param_pub_actual_vel_
Definition: tmc_coe_motor.h:77
ros::NodeHandle
ros::Time::now
static Time now()


adi_tmc_coe
Author(s):
autogenerated on Sun Feb 2 2025 03:07:24