tmcl_stepper_motor.cpp
Go to the documentation of this file.
1 
6 #include "tmcl_stepper_motor.h"
7 
9 
10 /* Constructor */
12  uint16_t module_number, uint8_t motor_number) :
13  Motor(p_nh, p_tmcl_interpreter, module_number, motor_number)
14 {
15  ROS_DEBUG_STREAM("[StepperMotor::" << __func__ << "] called");
16 }
17 
18 /* Destructor */
20 {
21  ROS_DEBUG_STREAM("[StepperMotor::" << __func__ << "] called");
22 }
23 
25 {
26  int32_t val = 0;
27 
28  ROS_INFO_STREAM("[StepperMotor::" << __func__ << "] called");
29 
30  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "closed loop", motor_number_, &val))
31  {
33  }
34  else
35  {
37  ROS_WARN_STREAM("[" << __func__ << "] \"closed loop\" is not available. Set Commutation Mode to Open loop");
38  }
39  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "MotorFullStepResolution", motor_number_, &val) ||
40  p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "motor full step resolution", motor_number_, &val) ||
41  p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "fullstep resolution", motor_number_, &val))
42  {
44  }
45  else
46  {
48  ROS_WARN_STREAM("[" << __func__ << "] \"motor full step resolution\" is not available");
49  }
50  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "MicrostepResolution", motor_number_, &val))
51  {
53  switch (microstep_resolution_)
54  {
55  case 0:
57  break;
58  case 1:
60  break;
61  case 2:
63  break;
64  case 3:
66  break;
67  case 4:
69  break;
70  case 5:
72  break;
73  case 6:
75  break;
76  case 7:
78  break;
79  default:
81  break;
82  }
83  }
84  else
85  {
87  ROS_WARN_STREAM("[" << __func__ << "] \"MicrostepResolution\" is not available");
88  }
89  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "relative positioning option", motor_number_, &val))
90  {
91  ROS_INFO_STREAM("[" << __func__ << "] Relative Positioning Option: " << val);
92  ROS_INFO_STREAM("[" << __func__ << "] NOTE: " << param_tmc_cmd_relpos_topic_ <<
93  " depends on the Relative Positioning Option Axis Parameter");
94  ROS_INFO_STREAM(" Refer to datasheet on how to configure.");
95  }
96  else
97  {
98  ROS_WARN_STREAM("[" << __func__ << "] \"relative positioning option\" is not available");
99  }
100 
101  /* Print units of each commands */
103  {
104  ROS_INFO_STREAM("[" << __func__ << "] Velocity unit: pps");
105  }
106  else
107  {
108  ROS_INFO_STREAM("[" << __func__ << "] Velocity unit: m/s");
109  }
111  {
112  ROS_INFO_STREAM("[" << __func__ << "] Position unit: pulses");
113  }
114  else
115  {
116  ROS_INFO_STREAM("[" << __func__ << "] Position unit: angular degrees");
117  }
118 
119  ROS_INFO_STREAM("[" << __func__ << "] Torque unit: mA");
120 
121  initPublisher();
122  this->initSubscriber();
123  ROS_INFO("[%s] Motor %d Initialized!\n", __func__, motor_number_);
124 }
125 
126 /* Publisher Callback */
128 {
129  int32_t val = 0;
130  bool b_drv_statusflag_available = false;
131 
132  tmc_info_msg_.header.stamp = ros::Time::now();
133  tmc_info_msg_.header.seq = seq_ctr_;
134  tmc_info_msg_.header.frame_id = frame_id_;
135  tmc_info_msg_.interface_name = param_comm_interface_name_;
136  tmc_info_msg_.motor_number = motor_number_;
137 
138  /* Initialize messages to 0 first */
139  tmc_info_msg_.board_voltage = 0;
140  tmc_info_msg_.status_flag = 0;
141  tmc_info_msg_.velocity = 0;
142  tmc_info_msg_.position = 0;
143  tmc_info_msg_.torque = 0;
144 
145  /* No Board Voltage */
146  tmc_info_msg_.board_voltage = NAN;
147 
148  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "driver error flags", motor_number_, &val) ||
149  p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "DrvStatusFlags", motor_number_, &val))
150  {
151  tmc_info_msg_.status = "driver error flags: "+ std::to_string(val);
152  b_drv_statusflag_available = true;
153  }
154  else
155  {
156  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get driver error flags");
157  }
158 
159  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "extended error flags", motor_number_, &val))
160  {
161  if(b_drv_statusflag_available)
162  {
163  tmc_info_msg_.status = tmc_info_msg_.status + " | extended error flags: "+ std::to_string(val);
164  }
165  else
166  {
167  tmc_info_msg_.status = "extended error flags: " + std::to_string(val);
168  }
169  }
170  else
171  {
172  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get extended error flags");
173  }
174 
175  /* Velocity, Position, Torque */
177  {
178  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "ActualVelocity", motor_number_, &val))
179  {
180  //converts pps to linear velocity
182  {
183  tmc_info_msg_.velocity = val * param_add_ratio_vel_;
184  }
185  else
186  {
187  tmc_info_msg_.velocity = val * ((PI * param_wheel_diameter_) /
189  }
190  }
191  else
192  {
193  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get ActualVelocity");
194  }
195  }
196 
198  {
199  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "ActualPosition", motor_number_, &val))
200  {
201  //converts pulses to degrees
203  {
204  tmc_info_msg_.position = val * param_add_ratio_pos_;
205  }
206  else
207  {
208  tmc_info_msg_.position = val * (ANGULAR_FULL_ROTATION /
210  }
211  }
212  else
213  {
214  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get ActualPosition");
215  }
216  }
217 
219  {
221  {
222  tmc_info_msg_.torque = val * param_add_ratio_trq_;
223  }
224  else
225  {
226  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get torque");
227  }
228  }
229 
231  seq_ctr_++;
232 }
233 
234 /* Initialize Subscriber */
236 {
237  ROS_INFO_STREAM("[StepperMotor::" << __func__ << "] called");
238 
240  {
241  ROS_INFO_STREAM("[" << __func__ << "] CommutationMode : OPENLOOP");
245  }
247  {
248  ROS_INFO_STREAM("[" << __func__ << "] CommutationMode : CLOSEDLOOP");
253  }
254 }
255 
256 /* Subscriber Callback */
257 void StepperMotor::cmdVelCallback(const geometry_msgs::Twist& msg)
258 {
259  float val = msg.linear.x;
260  int32_t board_val = 0;
261 
262  //If wheel diameter is set to 0 (or no wheels connected), the input value for linearX is equal to motors rpm
264  {
265  board_val = val / param_add_ratio_vel_;
266  }
267  else
268  {
269  //Formula to convert linear value to pps (unit that the board accepts)
270  board_val = val * ((microstep_resolution_ * (float)motor_fullstep_resolution_) / (PI * param_wheel_diameter_)) *
271  (1 / param_add_ratio_vel_);
272  }
273 
274  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << val << " board value: "
275  << board_val);
276 
277  if(board_val >= 0)
278  {
280  {
281  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
282  }
283  else
284  {
285  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Velocity");
286  }
287  }
288  else
289  {
290  board_val = abs(board_val);
292  {
293  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
294  }
295  else
296  {
297  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Velocity");
298  }
299  }
300 }
301 void StepperMotor::cmdAbsPosCallback(const std_msgs::Int32 msg)
302 {
303  float convert_const_deg = 0.00;
304  int32_t unit_val = 0;
305  int32_t val = msg.data;
306 
307  //convert input(degrees) to unit
309  {
310  convert_const_deg = ((microstep_resolution_ * (float)motor_fullstep_resolution_) / ANGULAR_FULL_ROTATION) *
311  (1 / param_add_ratio_pos_);
312  }
313  else
314  {
315  //inverting position additional ratio
316  convert_const_deg = 1 / param_add_ratio_pos_;
317  }
318 
319  unit_val = val * convert_const_deg;
320 
321  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << val << " board value: "
322  << unit_val);
323 
325  {
326  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
327  }
328  else
329  {
330  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Absolute Position");
331  }
332 }
333 void StepperMotor::cmdRelPosCallback(const std_msgs::Int32 msg)
334 {
335  float convert_const_deg = 0;
336  int32_t unit_val = 0;
337  int32_t val = msg.data;
338 
339  //convert input(degrees) to unit
341  {
342  convert_const_deg = ((microstep_resolution_ * (float)motor_fullstep_resolution_) / ANGULAR_FULL_ROTATION) *
343  (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: " << val << " board value: "
354  << unit_val);
355 
357  {
358  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
359  }
360  else
361  {
362  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Relative Position");
363  }
364 }
365 void StepperMotor::cmdTrqCallback(const std_msgs::Int32 msg)
366 {
367  int32_t val = msg.data;
368 
369  val /= param_add_ratio_trq_;
370 
371  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << val);
372 
374  {
375  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
376  }
377  else
378  {
379  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Torque");
380  }
381 }
tmcl_stepper_motor.h
Motor::param_wheel_diameter_
float param_wheel_diameter_
Definition: tmcl_motor.h:82
StepperMotor::cmdVelCallback
void cmdVelCallback(const geometry_msgs::Twist &msg) override
Definition: tmcl_stepper_motor.cpp:257
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
StepperMotor::microstep_resolution_
uint16_t microstep_resolution_
Definition: tmcl_stepper_motor.h:43
Motor::tmc_cmd_relpos_sub_
ros::Subscriber tmc_cmd_relpos_sub_
Definition: tmcl_motor.h:59
Motor::tmc_cmd_trq_sub_
ros::Subscriber tmc_cmd_trq_sub_
Definition: tmcl_motor.h:60
TMCL_CMD_GAP
@ TMCL_CMD_GAP
Definition: tmcl_interpreter.h:64
Motor::param_add_ratio_vel_
float param_add_ratio_vel_
Definition: tmcl_motor.h:83
Motor::frame_id_
std::string frame_id_
Definition: tmcl_motor.h:70
TmclInterpreter::executeCmd
bool executeCmd(tmcl_cmd_t cmd, uint8_t type, uint8_t motor, int32_t *val)
Definition: tmcl_interpreter.cpp:75
StepperMotor::init
void init() override
Definition: tmcl_stepper_motor.cpp:24
PI
const double PI
Definition: tmcl_motor.h:20
StepperMotor::rosPublishTmcInfo
void rosPublishTmcInfo(const ros::TimerEvent &event) override
Definition: tmcl_stepper_motor.cpp:127
StepperMotor::motor_fullstep_resolution_
uint32_t motor_fullstep_resolution_
Definition: tmcl_stepper_motor.h:44
ANGULAR_FULL_ROTATION
const uint16_t ANGULAR_FULL_ROTATION
Definition: tmcl_motor.h:24
Motor::param_tmc_cmd_vel_topic_
std::string param_tmc_cmd_vel_topic_
Definition: tmcl_motor.h:75
StepperMotor::StepperMotor
StepperMotor(ros::NodeHandle *p_nh, TmclInterpreter *p_tmcl_interpreter, uint16_t module_number, uint8_t motor_number)
Definition: tmcl_stepper_motor.cpp:11
StepperMotor::cmdTrqCallback
void cmdTrqCallback(const std_msgs::Int32 msg) override
Definition: tmcl_stepper_motor.cpp:365
Motor::param_tmc_cmd_trq_topic_
std::string param_tmc_cmd_trq_topic_
Definition: tmcl_motor.h:78
ROS_DEBUG_STREAM
#define ROS_DEBUG_STREAM(args)
StepperMotor::cmdRelPosCallback
void cmdRelPosCallback(const std_msgs::Int32 msg) override
Definition: tmcl_stepper_motor.cpp:333
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
StepperMotor::initSubscriber
void initSubscriber() override
Definition: tmcl_stepper_motor.cpp:235
StepperMotor::cmdAbsPosCallback
void cmdAbsPosCallback(const std_msgs::Int32 msg) override
Definition: tmcl_stepper_motor.cpp:301
Motor::tmc_cmd_abspos_sub_
ros::Subscriber tmc_cmd_abspos_sub_
Definition: tmcl_motor.h:58
Motor::initPublisher
void initPublisher()
Definition: tmcl_motor.cpp:167
Motor::tmc_cmd_vel_sub_
ros::Subscriber tmc_cmd_vel_sub_
Definition: tmcl_motor.h:57
TMCL_CMD_ROL
@ TMCL_CMD_ROL
Definition: tmcl_interpreter.h:60
ROS_WARN_STREAM
#define ROS_WARN_STREAM(args)
STEPPER_OPENLOOP_MOTOR
@ STEPPER_OPENLOOP_MOTOR
Definition: tmcl_stepper_motor.h:14
stepper_comm_mode_t
stepper_comm_mode_t
Definition: tmcl_stepper_motor.h:12
STEPPER_CLOSEDLOOP_MOTOR
@ STEPPER_CLOSEDLOOP_MOTOR
Definition: tmcl_stepper_motor.h:15
StepperMotor::comm_mode_
stepper_comm_mode_t comm_mode_
Definition: tmcl_stepper_motor.h:46
Motor::param_tmc_cmd_abspos_topic_
std::string param_tmc_cmd_abspos_topic_
Definition: tmcl_motor.h:76
Motor::tmc_info_pub_
ros::Publisher tmc_info_pub_
Definition: tmcl_motor.h:44
TmclInterpreter
Definition: tmcl_interpreter.h:98
Motor::p_tmcl_interpreter_
TmclInterpreter * p_tmcl_interpreter_
Definition: tmcl_motor.h:64
Motor::p_nh_
ros::NodeHandle * p_nh_
Definition: tmcl_motor.h:63
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
ros::TimerEvent
ROS_INFO_STREAM
#define ROS_INFO_STREAM(args)
Motor::param_pub_actual_pos_
bool param_pub_actual_pos_
Definition: tmcl_motor.h:80
Motor::param_pub_actual_trq_
bool param_pub_actual_trq_
Definition: tmcl_motor.h:81
Motor::seq_ctr_
uint32_t seq_ctr_
Definition: tmcl_motor.h:49
Motor::tmc_info_msg_
adi_tmcl::TmcInfo tmc_info_msg_
Definition: tmcl_motor.h:45
Motor::param_add_ratio_pos_
float param_add_ratio_pos_
Definition: tmcl_motor.h:84
Motor::motor_number_
uint8_t motor_number_
Definition: tmcl_motor.h:72
Motor::param_tmc_cmd_relpos_topic_
std::string param_tmc_cmd_relpos_topic_
Definition: tmcl_motor.h:77
Motor::param_add_ratio_trq_
float param_add_ratio_trq_
Definition: tmcl_motor.h:85
Motor
Definition: tmcl_motor.h:26
TMCL_CMD_MVP
@ TMCL_CMD_MVP
Definition: tmcl_interpreter.h:62
TMCL_CMD_ROR
@ TMCL_CMD_ROR
Definition: tmcl_interpreter.h:59
TMCL_CMD_SAP
@ TMCL_CMD_SAP
Definition: tmcl_interpreter.h:63
Motor::param_pub_actual_vel_
bool param_pub_actual_vel_
Definition: tmcl_motor.h:79
ROS_INFO
#define ROS_INFO(...)
StepperMotor::~StepperMotor
~StepperMotor() override
Definition: tmcl_stepper_motor.cpp:19
Motor::param_comm_interface_name_
std::string param_comm_interface_name_
Definition: tmcl_motor.h:67
ros::NodeHandle
ros::Time::now
static Time now()


adi_tmcl
Author(s):
autogenerated on Wed Apr 2 2025 02:43:01