tmcl_motor.cpp
Go to the documentation of this file.
1 
6 #include "tmcl_motor.h"
7 
9 
10 /* Constructor */
11 Motor::Motor(ros::NodeHandle *p_nh, TmclInterpreter* p_tmcl_interpreter,
12  uint16_t module_number, uint8_t motor_number) :
13  p_nh_ (p_nh),
14  p_tmcl_interpreter_(p_tmcl_interpreter),
15  module_number_(module_number),
16  motor_number_(motor_number)
17 {
18  ROS_DEBUG_STREAM("[Motor::" << __func__ << "] called");
19  initParams();
20 }
21 
22 /* Destructor */
24 {
25  ROS_DEBUG_STREAM("[Motor::" << __func__ << "] called");
26  p_tmcl_interpreter_ = nullptr;
27  p_nh_ = nullptr;
28 }
29 
31 {
32  ROS_INFO_STREAM("[Motor::" << __func__ << "] called");
33 
34  initPublisher();
36  ROS_INFO("[%s] Motor %d Initialized!\n", __func__, motor_number_);
37 }
38 
39 /* Initialize Parameters */
41 {
42 
43  ROS_INFO_STREAM("[Motor::" << __func__ << "] called");
44 
46 
47  const std::string s_en_pub_tmc_info = s_node_name_ + "/motor"+ std::to_string(motor_number_) + "/en_pub_tmc_info";
48  if(!p_nh_->getParam(s_en_pub_tmc_info, param_en_pub_tmc_info_))
49  {
50  param_en_pub_tmc_info_ = false;
51  p_nh_->setParam(s_en_pub_tmc_info, param_en_pub_tmc_info_);
52  ROS_WARN_STREAM("[" << __func__ << "] Failed to get en_pub_tmc_info for motor" << std::to_string(motor_number_)
53  << ", setting to default value: " << std::boolalpha << param_en_pub_tmc_info_);
54  }
55  const std::string s_pub_actual_vel = s_node_name_ + "/motor"+ std::to_string(motor_number_) + "/pub_actual_vel";
56  if(!p_nh_->getParam(s_pub_actual_vel, param_pub_actual_vel_))
57  {
58  param_pub_actual_vel_ = false;
59  p_nh_->setParam(s_pub_actual_vel, param_pub_actual_vel_);
60  ROS_WARN_STREAM("[" << __func__ << "] Failed to get pub_actual_vel for motor" << std::to_string(motor_number_)
61  << ", setting to default value: " << std::boolalpha << param_pub_actual_vel_);
62  }
63  const std::string s_pub_actual_pos = s_node_name_ + "/motor"+ std::to_string(motor_number_) + "/pub_actual_pos";
64  if(!p_nh_->getParam(s_pub_actual_pos, param_pub_actual_pos_))
65  {
66  param_pub_actual_pos_ = false;
67  p_nh_->setParam(s_pub_actual_pos, param_pub_actual_pos_);
68  ROS_WARN_STREAM("[" << __func__ << "] Failed to get pub_actual_pos for motor" << std::to_string(motor_number_)
69  << ", setting to default value: " << std::boolalpha << param_pub_actual_pos_);
70  }
71  const std::string s_pub_actual_trq = s_node_name_ + "/motor"+ std::to_string(motor_number_) + "/pub_actual_trq";
72  if(!p_nh_->getParam(s_pub_actual_trq, param_pub_actual_trq_))
73  {
74  param_pub_actual_trq_ = false;
75  p_nh_->setParam(s_pub_actual_trq, param_pub_actual_trq_);
76  ROS_WARN_STREAM("[" << __func__ << "] Failed to get pub_actual_trq for motor" << std::to_string(motor_number_)
77  << ", setting to default value: " << std::boolalpha << param_pub_actual_trq_);
78  }
79  const std::string s_tmc_info_topic = s_node_name_ + "/motor"+ std::to_string(motor_number_) + "/tmc_info_topic";
80  if(!p_nh_->getParam(s_tmc_info_topic, param_tmc_info_topic_))
81  {
82  param_tmc_info_topic_ = "/tmc_info_" + std::to_string(motor_number_);
83  p_nh_->setParam(s_tmc_info_topic, param_tmc_info_topic_);
84  ROS_WARN_STREAM("[" << __func__ << "] Failed to get tmc_info_topic for motor" << std::to_string(motor_number_)
85  << ", setting to default value: " << param_tmc_info_topic_);
86  }
87  const std::string s_tmc_cmd_vel_topic = s_node_name_ + "/motor"+ std::to_string(motor_number_) +
88  "/tmc_cmd_vel_topic";
89  if(!p_nh_->getParam(s_tmc_cmd_vel_topic, param_tmc_cmd_vel_topic_))
90  {
91  param_tmc_cmd_vel_topic_ = "/cmd_vel_" + std::to_string(motor_number_);
92  p_nh_->setParam(s_tmc_cmd_vel_topic, param_tmc_cmd_vel_topic_);
93  ROS_WARN_STREAM("[" << __func__ << "] Failed to get tmc_cmd_vel_topic for motor" << std::to_string(motor_number_)
94  << ", setting to default value: " << param_tmc_cmd_vel_topic_);
95  }
96  const std::string s_tmc_cmd_abspos_topic = s_node_name_ + "/motor"+ std::to_string(motor_number_) +
97  "/tmc_cmd_abspos_topic";
98  if(!p_nh_->getParam(s_tmc_cmd_abspos_topic, param_tmc_cmd_abspos_topic_))
99  {
100  param_tmc_cmd_abspos_topic_ = "/cmd_abspos_" + std::to_string(motor_number_);
101  p_nh_->setParam(s_tmc_cmd_abspos_topic, param_tmc_cmd_abspos_topic_);
102  ROS_WARN_STREAM("[" << __func__ << "] Failed to get tmc_cmd_abspos_topic for motor"
103  << std::to_string(motor_number_) << ", setting to default value: " << param_tmc_cmd_abspos_topic_);
104  }
105  const std::string s_tmc_cmd_relpos_topic = s_node_name_ + "/motor"+ std::to_string(motor_number_) +
106  "/tmc_cmd_relpos_topic";
107  if(!p_nh_->getParam(s_tmc_cmd_relpos_topic, param_tmc_cmd_relpos_topic_))
108  {
109  param_tmc_cmd_relpos_topic_ = "/cmd_relpos_" + std::to_string(motor_number_);
110  p_nh_->setParam(s_tmc_cmd_relpos_topic, param_tmc_cmd_relpos_topic_);
111  ROS_WARN_STREAM("[" << __func__ << "] Failed to get tmc_cmd_relpos_topic for motor"
112  << std::to_string(motor_number_) << ", setting to default value: " << param_tmc_cmd_relpos_topic_);
113  }
114  const std::string s_tmc_cmd_trq_topic = s_node_name_ + "/motor"+ std::to_string(motor_number_) +
115  "/tmc_cmd_trq_topic";
116  if(!p_nh_->getParam(s_tmc_cmd_trq_topic, param_tmc_cmd_trq_topic_))
117  {
118  param_tmc_cmd_trq_topic_ = "/cmd_trq_" + std::to_string(motor_number_);
119  p_nh_->setParam(s_tmc_cmd_trq_topic, param_tmc_cmd_trq_topic_);
120  ROS_WARN_STREAM("[" << __func__ << "] Failed to get tmc_cmd_trq_topic for motor" << std::to_string(motor_number_)
121  << ", setting to default value: " << param_tmc_cmd_trq_topic_);
122  }
123  const std::string s_wheel_diameter = s_node_name_ + "/motor"+ std::to_string(motor_number_) + "/wheel_diameter";
124  if(!p_nh_->getParam(s_wheel_diameter, param_wheel_diameter_))
125  {
127  p_nh_->setParam(s_wheel_diameter, param_wheel_diameter_);
128  ROS_WARN_STREAM("[" << __func__ << "] Failed to get wheel_diameter for motor" << std::to_string(motor_number_)
129  << ", setting to default value: " << param_wheel_diameter_);
130  }
131  const std::string s_additional_ratio_vel = s_node_name_ + "/motor"+ std::to_string(motor_number_) +
132  "/additional_ratio_vel";
133  if(!p_nh_->getParam(s_additional_ratio_vel, param_add_ratio_vel_))
134  {
136  p_nh_->setParam(s_additional_ratio_vel, param_add_ratio_vel_);
137  ROS_WARN_STREAM("[" << __func__ << "] Failed to get additional_ratio_vel for motor"
138  << std::to_string(motor_number_) << ", setting to default value: " << param_add_ratio_vel_);
139  }
140  const std::string s_additional_ratio_pos = s_node_name_ + "/motor"+ std::to_string(motor_number_) +
141  "/additional_ratio_pos";
142  if(!p_nh_->getParam(s_additional_ratio_pos, param_add_ratio_pos_))
143  {
145  p_nh_->setParam(s_additional_ratio_pos, param_add_ratio_pos_);
146  ROS_WARN_STREAM("[" << __func__ << "] Failed to get additional_ratio_pos for motor"
147  << std::to_string(motor_number_) << ", setting to default value: " << param_add_ratio_pos_);
148  }
149  const std::string s_additional_ratio_trq = s_node_name_ + "/motor"+ std::to_string(motor_number_) +
150  "/additional_ratio_trq";
151  if(!p_nh_->getParam(s_additional_ratio_trq, param_add_ratio_trq_))
152  {
154  p_nh_->setParam(s_additional_ratio_trq, param_add_ratio_trq_);
155  ROS_WARN_STREAM("[" << __func__ << "] Failed to get additional_ratio_trq for motor"
156  << std::to_string(motor_number_) << ", setting to default value: " << param_add_ratio_trq_);
157  }
158 
159  /* Parameters that are already validated in tmcl_ros.cpp. Only need to get the param value */
160  const std::string s_pub_rate_tmc_info= s_node_name_ + "/pub_rate_tmc_info";
161  (void)p_nh_->getParam(s_pub_rate_tmc_info, param_pub_rate_tmc_info_);
162  const std::string s_comm_interface_name= s_node_name_ + "/comm_interface_name";
163  (void)p_nh_->getParam(s_comm_interface_name, param_comm_interface_name_);
164 }
165 
166 /* Initialize Publisher */
168 {
169  ROS_INFO_STREAM("[Motor::" << __func__ << "] called");
170 
171  // Set frame ID
173  /* Checks if namespace is empty if not, remove "/" */
174  if(s_namespace_.compare("/") == 0)
175  {
176  frame_id_ = "/tmcm" + std::to_string(module_number_) + "_mtr" + std::to_string(motor_number_) + "_frame";
177  }
178  else
179  {
180  s_namespace_.erase(s_namespace_.begin());
181  frame_id_ = s_namespace_ + "/tmcm" + std::to_string(module_number_) + "_mtr" + std::to_string(motor_number_) +
182  "_frame";
183  }
184 
186  {
187  tmc_info_pub_ = p_nh_->advertise<adi_tmcl::TmcInfo>(param_tmc_info_topic_, 100);
188  seq_ctr_ = 0;
191 
193  {
194  ROS_INFO_STREAM("[" << __func__ << "] Publisher TimerCallback is created successfully!");
195  }
196  else
197  {
198  ROS_ERROR_STREAM("[" << __func__ << "] Publisher TimerCallback not created!");
199  }
200  }
201 }
202 
203 /* Publisher Callback */
205 {
206  int32_t val = 0;
207 
208  tmc_info_msg_.header.stamp = ros::Time::now();
209  tmc_info_msg_.header.seq = seq_ctr_;
210  tmc_info_msg_.header.frame_id = frame_id_;
211  tmc_info_msg_.interface_name = param_comm_interface_name_;
212  tmc_info_msg_.motor_number = motor_number_;
213 
214  /* Initialize messages to 0 first */
215  tmc_info_msg_.board_voltage = 0;
216  tmc_info_msg_.status_flag = 0;
217  tmc_info_msg_.velocity = 0;
218  tmc_info_msg_.position = 0;
219  tmc_info_msg_.torque = 0;
220 
221  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "SupplyVoltage", motor_number_, &val))
222  {
223  tmc_info_msg_.board_voltage = val / 10; //converts mV to V
224  }
225  else
226  {
227  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get SupplyVoltage");
228  }
229 
230  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "StatusFlags", motor_number_, &val))
231  {
232  tmc_info_msg_.status_flag = val;
233  }
234  else
235  {
236  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get StatusFlags");
237  }
238 
239  /* Velocity, Position, Torque */
241  {
242  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "ActualVelocity", motor_number_, &val))
243  {
244  tmc_info_msg_.velocity = val * param_add_ratio_vel_;
245  }
246  else
247  {
248  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get ActualVelocity");
249  }
250  }
251 
253  {
254  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "ActualPosition", motor_number_, &val))
255  {
256  tmc_info_msg_.position = val * param_add_ratio_pos_;
257  }
258  else
259  {
260  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get ActualPosition");
261  }
262  }
263 
265  {
266  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_GAP, "ActualTorque", motor_number_, &val))
267  {
268  tmc_info_msg_.torque = val * param_add_ratio_trq_;
269  }
270  else
271  {
272  ROS_DEBUG_STREAM("[" << __func__ << "] Failed to get ActualTorque");
273  }
274  }
275 
277  seq_ctr_++;
278 }
279 
280 /* Initialize Subscriber */
282 {
283  ROS_INFO_STREAM("[Motor::" << __func__ << "] called");
284 
289 }
290 
291 /* Subscriber Callback */
292 void Motor::cmdVelCallback(const geometry_msgs::Twist& msg)
293 {
294  float val = msg.linear.x;
295  int32_t board_val = 0;
296 
297  board_val = val / param_add_ratio_vel_;
298 
299  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << val << " board value: "
300  << board_val);
301 
302  if(board_val >= 0)
303  {
305  {
306  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
307  }
308  else
309  {
310  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Velocity");
311  }
312  }
313  else
314  {
315  board_val = abs(board_val);
317  {
318  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
319  }
320  else
321  {
322  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Velocity");
323  }
324  }
325 }
326 void Motor::cmdAbsPosCallback(const std_msgs::Int32 msg)
327 {
328  int32_t val = msg.data;
329 
330  val /= param_add_ratio_pos_;
331 
332  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << msg.data << " board value: "
333  << val);
334 
336  {
337  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
338  }
339  else
340  {
341  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Absolute Position");
342  }
343 }
344 void Motor::cmdRelPosCallback(const std_msgs::Int32 msg)
345 {
346  int32_t val = msg.data;
347 
348  val /= param_add_ratio_pos_;
349 
350  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << msg.data << " board value: "
351  << val);
352 
354  {
355  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
356  }
357  else
358  {
359  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Relative Position");
360  }
361 }
362 void Motor::cmdTrqCallback(const std_msgs::Int32 msg)
363 {
364  int32_t val = msg.data;
365 
366  val /= param_add_ratio_trq_;
367 
368  ROS_DEBUG_STREAM("[" << __func__ << "] Subscriber callback entered, received: " << val);
369 
370  if(p_tmcl_interpreter_->executeCmd(TMCL_CMD_SAP, "TargetTorque", motor_number_, &val))
371  {
372  ROS_DEBUG_STREAM("\n[" << __func__ << "] Subscriber callback exited successfully");
373  }
374  else
375  {
376  ROS_ERROR_STREAM("[" << __func__ << "] Failed to set Torque");
377  }
378 }
Motor::timer_callback_
ros::Timer timer_callback_
Definition: tmcl_motor.h:43
tmcl_motor.h
ros::NodeHandle::setParam
void setParam(const std::string &key, bool b) const
ros::this_node::getNamespace
const ROSCPP_DECL std::string & getNamespace()
Motor::param_wheel_diameter_
float param_wheel_diameter_
Definition: tmcl_motor.h:82
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
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
Motor::cmdTrqCallback
virtual void cmdTrqCallback(const std_msgs::Int32 msg)
Definition: tmcl_motor.cpp:362
TMCL_CMD_GAP
@ TMCL_CMD_GAP
Definition: tmcl_interpreter.h:64
Motor::param_tmc_info_topic_
std::string param_tmc_info_topic_
Definition: tmcl_motor.h:47
Motor::s_node_name_
std::string s_node_name_
Definition: tmcl_motor.h:68
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
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
Motor::cmdAbsPosCallback
virtual void cmdAbsPosCallback(const std_msgs::Int32 msg)
Definition: tmcl_motor.cpp:326
Motor::param_pub_rate_tmc_info_
float param_pub_rate_tmc_info_
Definition: tmcl_motor.h:46
ros::Timer::isValid
bool isValid()
Motor::param_tmc_cmd_vel_topic_
std::string param_tmc_cmd_vel_topic_
Definition: tmcl_motor.h:75
Motor::s_namespace_
std::string s_namespace_
Definition: tmcl_motor.h:69
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)
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
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
Motor::~Motor
virtual ~Motor()
Definition: tmcl_motor.cpp:23
Motor::Motor
Motor(ros::NodeHandle *p_nh, TmclInterpreter *p_tmcl_interpreter, uint16_t module_number, uint8_t motor_number)
Definition: tmcl_motor.cpp:11
TMCL_CMD_ROL
@ TMCL_CMD_ROL
Definition: tmcl_interpreter.h:60
ROS_WARN_STREAM
#define ROS_WARN_STREAM(args)
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())
Motor::module_number_
uint16_t module_number_
Definition: tmcl_motor.h:71
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
ros::this_node::getName
const ROSCPP_DECL std::string & getName()
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::cmdVelCallback
virtual void cmdVelCallback(const geometry_msgs::Twist &msg)
Definition: tmcl_motor.cpp:292
Motor::param_tmc_cmd_relpos_topic_
std::string param_tmc_cmd_relpos_topic_
Definition: tmcl_motor.h:77
Motor::initSubscriber
virtual void initSubscriber()
Definition: tmcl_motor.cpp:281
Motor::initParams
void initParams()
Definition: tmcl_motor.cpp:40
Motor::param_add_ratio_trq_
float param_add_ratio_trq_
Definition: tmcl_motor.h:85
Motor::cmdRelPosCallback
virtual void cmdRelPosCallback(const std_msgs::Int32 msg)
Definition: tmcl_motor.cpp:344
TMCL_CMD_MVP
@ TMCL_CMD_MVP
Definition: tmcl_interpreter.h:62
Motor::param_en_pub_tmc_info_
bool param_en_pub_tmc_info_
Definition: tmcl_motor.h:48
TMCL_CMD_ROR
@ TMCL_CMD_ROR
Definition: tmcl_interpreter.h:59
ros::Rate
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(...)
ros::NodeHandle::createTimer
Timer createTimer(Duration period, const TimerCallback &callback, bool oneshot=false, bool autostart=true) const
Motor::rosPublishTmcInfo
virtual void rosPublishTmcInfo(const ros::TimerEvent &event)
Definition: tmcl_motor.cpp:204
ros::Duration
Motor::init
virtual void init()
Definition: tmcl_motor.cpp:30
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