motor_param.cpp
Go to the documentation of this file.
1 
32 
34  : nh_(nh)
35  , mSerial(serial)
36 {
37  // Find path param
38  mName = nh_.getNamespace() + "/" + name;
39  // Roboteq motor number
40  mNumber = number;
41  // Set false on first run
42  setup_param = false;
43 }
44 
45 void MotorParamConfigurator::initConfigurator(bool load_from_board)
46 {
47  double ratio;
48  // Check if exist ratio variable
49  if(nh_.hasParam(mName + "/ratio"))
50  {
51  double temp_double;
52  nh_.getParam(mName + "/ratio", temp_double);
53  // Set Ratio
54  ratio = temp_double;
55  } else
56  {
57  nh_.setParam(mName + "/ratio", 1.0);
58  // Set Ratio
59  ratio = 1.0;
60  // Send alter ratio value
61  //ROS_WARN_STREAM("Default Ratio is " << ratio);
62  }
63 
64  // Check if is required load paramers
65  if(load_from_board)
66  {
67  // Load parameters from roboteq
69  }
70 
71  // Initialize parameter dynamic reconfigure
72  ds_param = new dynamic_reconfigure::Server<roboteq_control::RoboteqParameterConfig>(ros::NodeHandle(mName));
73  dynamic_reconfigure::Server<roboteq_control::RoboteqParameterConfig>::CallbackType cb_param = boost::bind(&MotorParamConfigurator::reconfigureCBParam, this, _1, _2);
74  ds_param->setCallback(cb_param);
75 
76  // Initialize pid type dynamic reconfigure
77  ds_pid_type = new dynamic_reconfigure::Server<roboteq_control::RoboteqPIDtypeConfig>(ros::NodeHandle(mName + "/pid"));
78  dynamic_reconfigure::Server<roboteq_control::RoboteqPIDtypeConfig>::CallbackType cb_pid_type = boost::bind(&MotorParamConfigurator::reconfigureCBPIDtype, this, _1, _2);
79  ds_pid_type->setCallback(cb_pid_type);
80 }
81 
83 {
84  // Update operative mode
85  mSerial->setParam("MMOD", std::to_string(mNumber) + " " + std::to_string(type));
86 }
87 
89 {
90  // Operative mode reference in [pag 321]
91  string str_mode = mSerial->getParam("MMOD", std::to_string(mNumber));
92  // Get sign from roboteq board
93  int mode = boost::lexical_cast<int>(str_mode);
94  // Set parameter
95  // nh_.setParam(mName + "/operating_mode", mode);
96 
97  return mode;
98 }
99 
100 void MotorParamConfigurator::reconfigureCBPIDtype(roboteq_control::RoboteqPIDtypeConfig &config, uint32_t level)
101 {
102  //The first time we're called, we just want to make sure we have the
103  //original configuration
104  if(!setup_pid_type)
105  {
106  _last_pid_type_config = config;
108  setup_pid_type = true;
109  return;
110  }
111 
112  if(config.restore_defaults)
113  {
114  //if someone sets restore defaults on the parameter server, prevent looping
115  config.restore_defaults = false;
116  // Overload config with default
117  config = default_pid_type_config;
118  }
119 }
120 
122 {
123  try
124  {
125  // Load Ratio
126  double ratio;
127  nh_.getParam(mName + "/ratio", ratio);
128 
129  // Motor direction {1 (Clockwise), -1 (Underclockwise)}
130  string str_mdir = mSerial->getParam("MDIR", std::to_string(mNumber));
131  // Get sign from roboteq board
132  int sign = boost::lexical_cast<int>(str_mdir) ? -1 : 1;
133  // Set parameter
134  nh_.setParam(mName + "/rotation", sign);
135 
136  // Stall detection [pag. 310]
137  string str_stall = mSerial->getParam("BLSTD", std::to_string(mNumber));
138  int stall = boost::lexical_cast<int>(str_stall);
139  // Set params
140  nh_.setParam(mName + "/stall_detection", stall);
141 
142  // Get Max Amper limit = alim / 10 [pag 306]
143  string str_alim = mSerial->getParam("ALIM", std::to_string(mNumber));
144  unsigned int tmp = boost::lexical_cast<unsigned int>(str_alim);
145  double alim = ((double) tmp) / 10.0;
146  // Set params
147  nh_.setParam(mName + "/amper_limit", alim);
148 
149  // Max power forward [pag. 323]
150  string str_max_fw = mSerial->getParam("MXPF", std::to_string(mNumber));
151  // Get max forward
152  int max_forward = boost::lexical_cast<unsigned int>(str_max_fw);
153  // Set parameter
154  nh_.setParam(mName + "/max_acceleration", max_forward);
155 
156  // Max power forward reverse [pag. 324]
157  string str_max_re = mSerial->getParam("MXPR", std::to_string(mNumber));
158  // Get max reverse
159  int max_reverse = boost::lexical_cast<unsigned int>(str_max_re);
160  // Set parameter
161  nh_.setParam(mName + "/max_deceleration", max_reverse);
162 
163  // Get Max RPM motor
164  string str_rpm_motor = mSerial->getParam("MXRPM", std::to_string(mNumber));
165  // Get RPM from board
166  unsigned int rpm_motor = boost::lexical_cast<unsigned int>(str_rpm_motor);
167  // Convert in max RPM
168  double max_rpm = ((double) rpm_motor) / ratio;
169  // Set parameter
170  nh_.setParam(mName + "/max_speed", max_rpm);
171  //ROS_ERROR("MAX SPEED !! %f", max_rpm);
172 
173  // Get Max RPM acceleration rate
174  string str_rpm_acceleration_motor = mSerial->getParam("MAC", std::to_string(mNumber));
175  // Get RPM from board
176  unsigned int rpm_acceleration_motor = boost::lexical_cast<unsigned int>(str_rpm_acceleration_motor);
177  // Convert in max RPM
178  double rpm_acceleration = ((double) rpm_acceleration_motor) / ratio;
179  // Set parameter
180  nh_.setParam(mName + "/max_acceleration", rpm_acceleration);
181 
182  // Get Max RPM deceleration rate
183  string str_rpm_deceleration_motor = mSerial->getParam("MDEC", std::to_string(mNumber));
184  // Get RPM from board
185  unsigned int rpm_deceleration_motor = boost::lexical_cast<unsigned int>(str_rpm_deceleration_motor);
186  // Convert in max RPM
187  double rpm_deceleration = ((double) rpm_deceleration_motor) / ratio;
188  // Set parameter
189  nh_.setParam(mName + "/max_deceleration", rpm_deceleration);
190 
191  } catch (std::bad_cast& e)
192  {
193  ROS_WARN_STREAM("Failure parsing feedback data. Dropping message." << e.what());
194  }
195 }
196 
197 void MotorParamConfigurator::reconfigureCBParam(roboteq_control::RoboteqParameterConfig &config, uint32_t level) {
198 
199  //The first time we're called, we just want to make sure we have the
200  //original configuration
201  if(!setup_param)
202  {
203  _last_param_config = config;
205  setup_param = true;
206  return;
207  }
208 
209  if(config.restore_defaults)
210  {
211  //if someone sets restore defaults on the parameter server, prevent looping
212  config.restore_defaults = false;
213  // Overload config with default
214  config = default_param_config;
215  }
216 
217  if(config.load_roboteq)
218  {
219  //if someone sets again the request on the parameter server, prevent looping
220  config.load_roboteq = false;
221  // Launch param load
223  // Skip other read
224  return;
225  }
226 
227  // Update ratio
228  // Get old max speed, acceleration and deceleartion and evaluate new equivalent value
229  if(_last_param_config.ratio != config.ratio)
230  {
231  // Get Max RPM motor
232  string str_rpm_motor = mSerial->getParam("MXRPM", std::to_string(mNumber));
233  // Get RPM from board
234  unsigned int rpm_motor = boost::lexical_cast<unsigned int>(str_rpm_motor);
235  // Update with new max speed
236  config.max_speed = ((double) rpm_motor) / config.ratio;
237 
238  // Get Max RPM acceleration rate
239  string str_rpm_acceleration_motor = mSerial->getParam("MAC", std::to_string(mNumber));
240  // Get RPM from board
241  unsigned int rpm_acceleration_motor = boost::lexical_cast<unsigned int>(str_rpm_acceleration_motor);
242  // Convert in max RPM
243  config.max_acceleration = ((double) rpm_acceleration_motor) / config.ratio;
244 
245  // Get Max RPM deceleration rate
246  string str_rpm_deceleration_motor = mSerial->getParam("MDEC", std::to_string(mNumber));
247  // Get RPM from board
248  unsigned int rpm_deceleration_motor = boost::lexical_cast<unsigned int>(str_rpm_deceleration_motor);
249  // Convert in max RPM
250  config.max_deceleration = ((double) rpm_deceleration_motor) / config.ratio;
251  }
252 
253  if(_last_param_config.rotation != config.rotation)
254  {
255  // Update direction
256  int direction = (config.rotation == -1) ? 1 : 0;
257  mSerial->setParam("MDIR", std::to_string(mNumber) + " " + std::to_string(direction));
258  }
259  // Stall detection [pag. 310]
260  if(_last_param_config.stall_detection != config.stall_detection)
261  {
262  // Update stall detection value
263  mSerial->setParam("BLSTD", std::to_string(mNumber) + " " + std::to_string(config.stall_detection));
264  }
265  // Get Max Amper limit = alim * 10 [pag 306]
266  if(_last_param_config.amper_limit != config.amper_limit)
267  {
268  // Update stall detection value
269  int alim = config.amper_limit * 10;
270  mSerial->setParam("ALIM", std::to_string(mNumber) + " " + std::to_string(alim));
271  }
272  // Max power forward [pag. 323]
273  if(_last_param_config.max_forward != config.max_forward)
274  {
275  // Update max forward
276  mSerial->setParam("MXPF", std::to_string(mNumber) + " " + std::to_string(config.max_forward));
277  }
278  // Max power forward reverse [pag. 324]
279  if(_last_param_config.max_forward != config.max_reverse)
280  {
281  // Update max forward reverse
282  mSerial->setParam("MXPR", std::to_string(mNumber) + " " + std::to_string(config.max_reverse));
283  }
284 
285  // Set Max RPM motor
286  if(_last_param_config.max_speed != config.max_speed)
287  {
288  // Update max RPM motor
289  long int max_speed_motor = config.ratio * config.max_speed;
290  mSerial->setParam("MXRPM", std::to_string(mNumber) + " " + std::to_string(max_speed_motor));
291  }
292 
293  // Set Max RPM acceleration rate
294  if(_last_param_config.max_acceleration != config.max_acceleration)
295  {
296  // Update max acceleration RPM/s motor
297  long int max_acceleration_motor = config.ratio * config.max_acceleration;
298  mSerial->setParam("MAC", std::to_string(mNumber) + " " + std::to_string(max_acceleration_motor));
299  }
300  // Set Max RPM deceleration rate
301  if(_last_param_config.max_deceleration != config.max_deceleration)
302  {
303  // Update max deceleration RPM/s motor
304  long int max_deceleration_motor = config.ratio * config.max_deceleration;
305  mSerial->setParam("MDEC", std::to_string(mNumber) + " " + std::to_string(max_deceleration_motor));
306  }
307 
308  // Update last configuration
309  _last_param_config = config;
310 }
bool setParam(string msg, string params="")
void reconfigureCBParam(roboteq_control::RoboteqParameterConfig &config, uint32_t level)
reconfigureCBParam when the dynamic reconfigurator change some values start this method ...
void setOperativeMode(int type)
setOperativeMode Reference in page 321 - MMOD
Definition: motor_param.cpp:82
void reconfigureCBPIDtype(roboteq_control::RoboteqPIDtypeConfig &config, uint32_t level)
reconfigureCBEncoder when the dynamic reconfigurator change some values start this method ...
void initConfigurator(bool load_from_board)
initConfigurator Initialize all parameter and syncronize parameters between ros and roboteq board ...
Definition: motor_param.cpp:45
roboteq_control::RoboteqParameterConfig default_param_config
Definition: motor_param.h:106
unsigned int mNumber
Number motor.
Definition: motor_param.h:81
MotorParamConfigurator(const ros::NodeHandle &nh, roboteq::serial_controller *serial, std::string name, unsigned int number)
MotorParamConfigurator.
Definition: motor_param.cpp:33
roboteq::serial_controller * mSerial
Serial port.
Definition: motor_param.h:85
void getParamFromRoboteq()
getParamFromRoboteq Load parameters from Roboteq board
dynamic_reconfigure::Server< roboteq_control::RoboteqPIDtypeConfig > * ds_pid_type
Dynamic reconfigure PID.
Definition: motor_param.h:97
const std::string & getNamespace() const
string mName
Associate name space.
Definition: motor_param.h:79
#define ROS_WARN_STREAM(args)
dynamic_reconfigure::Server< roboteq_control::RoboteqParameterConfig > * ds_param
Dynamic reconfigure parameters.
Definition: motor_param.h:88
roboteq_control::RoboteqPIDtypeConfig _last_pid_type_config
Definition: motor_param.h:107
string getParam(string msg, string params="")
bool getParam(const std::string &key, std::string &s) const
roboteq_control::RoboteqParameterConfig _last_param_config
Definition: motor_param.h:106
bool hasParam(const std::string &key) const
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
bool setup_param
Setup variable.
Definition: motor_param.h:76
roboteq_control::RoboteqPIDtypeConfig default_pid_type_config
Definition: motor_param.h:107
ros::NodeHandle nh_
Private namespace.
Definition: motor_param.h:83


roboteq_control
Author(s): Raffaello Bonghi
autogenerated on Wed Jan 3 2018 03:48:23