motor_pid.cpp
Go to the documentation of this file.
1 
31 #include "configurator/motor_pid.h"
32 
33 MotorPIDConfigurator::MotorPIDConfigurator(const ros::NodeHandle& nh, roboteq::serial_controller *serial, string path, string name, unsigned int number)
34  : nh_(nh)
35  , mSerial(serial)
36 {
37  // Find path param
38  mName = nh_.getNamespace() + "/" + path + "/pid/" + name;
39  mType = name;
40  ROS_DEBUG_STREAM("Param " << path + "/pid/" + name << " has " << mName << " N:" << number);
41  // Roboteq motor number
42  mNumber = number;
43  // Set false on first run
44  setup_pid = false;
45 
46 }
47 
48 void MotorPIDConfigurator::initConfigurator(bool load_from_board)
49 {
50  // Check if is required load paramers
51  if(load_from_board)
52  {
53  // Load parameters from roboteq
55  }
56 
57  // Initialize parameter dynamic reconfigure
58  ds_pid = new dynamic_reconfigure::Server<roboteq_control::RoboteqPIDConfig>(ros::NodeHandle(mName));
59  dynamic_reconfigure::Server<roboteq_control::RoboteqPIDConfig>::CallbackType cb_pid = boost::bind(&MotorPIDConfigurator::reconfigureCBPID, this, _1, _2);
60  ds_pid->setCallback(cb_pid);
61 }
62 
64 {
65  try
66  {
67  // Get Position velocity [pag. 322]
68  string str_pos_vel = mSerial->getParam("MVEL", std::to_string(mNumber));
69  int pos_vel = boost::lexical_cast<unsigned int>(str_pos_vel);
70  // Set params
71  nh_.setParam(mName + "/position_mode_velocity", pos_vel);
72 
73  // Get number of turn between limits [pag. 325]
74  string str_mxtrn = mSerial->getParam("MXTRN", std::to_string(mNumber));
75  unsigned int tmp_mxtrn = boost::lexical_cast<unsigned int>(str_mxtrn);
76  double mxtrn = ((double) tmp_mxtrn) / 100.0;
77  // Set params
78  nh_.setParam(mName + "/turn_min_to_max", mxtrn);
79 
80  // Get KP gain = kp / 10 [pag 319]
81  string str_kp = mSerial->getParam("KP", std::to_string(mNumber));
82  unsigned int tmp_kp = boost::lexical_cast<unsigned int>(str_kp);
83  double kp = ((double) tmp_kp) / 10.0;
84  // Set params
85  nh_.setParam(mName + "/Kp", kp);
86 
87  // Get KI gain = ki / 10 [pag 318]
88  string str_ki = mSerial->getParam("KI", std::to_string(mNumber));
89  unsigned int tmp_ki = boost::lexical_cast<unsigned int>(str_ki);
90  double ki = ((double) tmp_ki) / 10.0;
91  // Set params
92  nh_.setParam(mName + "/Ki", ki);
93 
94  // Get KD gain = kd / 10 [pag 317]
95  string str_kd = mSerial->getParam("KD", std::to_string(mNumber));
96  unsigned int tmp_kd = boost::lexical_cast<unsigned int>(str_kd);
97  double kd = ((double) tmp_kd) / 10.0;
98  // Set params
99  nh_.setParam(mName + "/Kd", kd);
100 
101  // Get Integral cap [pag. 317]
102  string str_icap = mSerial->getParam("ICAP", std::to_string(mNumber));
103  int icap = boost::lexical_cast<unsigned int>(str_icap);
104  // Set params
105  nh_.setParam(mName + "/integrator_limit", icap);
106 
107  // Get closed loop error detection [pag. 311]
108  string str_clred = mSerial->getParam("CLERD", std::to_string(mNumber));
109  int clerd = boost::lexical_cast<unsigned int>(str_clred);
110  // Set params
111  nh_.setParam(mName + "/loop_error_detection", clerd);
112 
113  } catch (std::bad_cast& e)
114  {
115  ROS_WARN_STREAM("Failure parsing feedback data. Dropping message." << e.what());
116  }
117 
118 }
119 
121 {
122  // Set Position velocity [pag. 322]
123  int pos_vel;
124  // Set params
125  nh_.getParam(mName + "/position_mode_velocity", pos_vel);
126  // Update position velocity
127  mSerial->setParam("MVEL", std::to_string(mNumber) + " " + std::to_string(pos_vel));
128 
129  // Set number of turn between limits [pag. 325]
130  double mxtrn;
131  // Set params
132  nh_.getParam(mName + "/turn_min_to_max", mxtrn);
133  // Update position velocity
134  mSerial->setParam("MXTRN", std::to_string(mNumber) + " " + std::to_string(mxtrn * 100));
135 
136  // Set KP gain = kp * 10 [pag 319]
137  double kp;
138  // Set params
139  nh_.getParam(mName + "/Kp", kp);
140  // Update gain position
141  mSerial->setParam("KP", std::to_string(mNumber) + " " + std::to_string(kp * 10));
142 
143  // Set KI gain = ki * 10 [pag 318]
144  double ki;
145  // Set params
146  nh_.getParam(mName + "/Ki", ki);
147  // Set KI parameter
148  mSerial->setParam("KI", std::to_string(mNumber) + " " + std::to_string(ki * 10));
149 
150  // Set KD gain = kd * 10 [pag 317]
151  double kd;
152  // Set params
153  nh_.getParam(mName + "/Kd", kd);
154  // Set KD parameter
155  mSerial->setParam("KD", std::to_string(mNumber) + " " + std::to_string(kd * 10));
156 
157  // Set Integral cap [pag. 317]
158  int icap;
159  // Set params
160  nh_.getParam(mName + "/integrator_limit", icap);
161  // Update integral cap
162  mSerial->setParam("ICAP", std::to_string(mNumber) + " " + std::to_string(icap));
163 
164  // Set closed loop error detection [pag. 311]
165  int clerd;
166  // Set params
167  nh_.getParam(mName + "/loop_error_detection", clerd);
168  // Update integral cap
169  mSerial->setParam("CLERD", std::to_string(mNumber) + " " + std::to_string(clerd));
170 }
171 
172 void MotorPIDConfigurator::reconfigureCBPID(roboteq_control::RoboteqPIDConfig &config, uint32_t level)
173 {
174  //The first time we're called, we just want to make sure we have the
175  //original configuration
176  if(!setup_pid)
177  {
178  _last_pid_config = config;
180  setup_pid = true;
181  return;
182  }
183 
184  // Check type if the PID selected is right
185  // Operative mode reference in [pag 321]
186  string str_mode = mSerial->getParam("MMOD", std::to_string(mNumber));
187  // Get sign from roboteq board
188  int mode = boost::lexical_cast<int>(str_mode);
189  // Check if the roboteq board is set in right configuration for this PID controller
190  bool check1 = (mType.compare("velocity") == 0 && ((mode == 1) || (mode == 6)));
191  bool check2 = (mType.compare("position") == 0 && ((mode == 2) || (mode == 3) || (mode == 4)));
192  bool check3 = (mType.compare("torque") == 0 && ((mode == 5)));
193  bool status = check1 | check2 | check3;
194  // ROS_INFO_STREAM("[" << mode << "]" << mType << " ck1:" << check1 << " ck2:" << check2 << " ck3:" << check3 << " status:" << (status ? "true" : "false"));
195  if(!status)
196  {
197  // Restore old configuration
198  config = _last_pid_config;
199  return;
200  }
201 
202  if(config.restore_defaults)
203  {
204  //if someone sets restore defaults on the parameter server, prevent looping
205  config.restore_defaults = false;
206  // Overload config with default
207  config = default_pid_config;
208  }
209 
210  if(config.load_roboteq)
211  {
212  //if someone sets again the request on the parameter server, prevent looping
213  config.load_roboteq = false;
214  // Launch param load
216  // Skip other read
217  return;
218  }
219 
220  // Set Position velocity [pag. 322]
221  if(_last_pid_config.position_mode_velocity != config.position_mode_velocity)
222  {
223  // Update position velocity
224  mSerial->setParam("MVEL", std::to_string(mNumber) + " " + std::to_string(config.position_mode_velocity));
225  }
226  // Set number of turn between limits [pag. 325]
227  if(_last_pid_config.turn_min_to_max != config.turn_min_to_max)
228  {
229  // Update position velocity
230  int gain = config.turn_min_to_max * 100;
231  mSerial->setParam("MXTRN", std::to_string(mNumber) + " " + std::to_string(gain));
232  }
233  // Set KP gain = kp * 10 [pag 319]
234  if(_last_pid_config.Kp != config.Kp)
235  {
236  // Update gain
237  int gain = config.Kp * 10;
238  mSerial->setParam("KP", std::to_string(mNumber) + " " + std::to_string(gain));
239  }
240  // Set KI gain = ki * 10 [pag 318]
241  if(_last_pid_config.Ki != config.Ki)
242  {
243  // Update gain
244  int gain = config.Ki * 10;
245  mSerial->setParam("KI", std::to_string(mNumber) + " " + std::to_string(gain));
246  }
247  // Set KD gain = kd * 10 [pag 317]
248  if(_last_pid_config.Kd != config.Kd)
249  {
250  // Update gain
251  int gain = config.Kd * 10;
252  mSerial->setParam("KD", std::to_string(mNumber) + " " + std::to_string(gain));
253  }
254 
255  // Set Integral cap [pag. 317]
256  if(_last_pid_config.integrator_limit != config.integrator_limit)
257  {
258  // Update integral cap
259  mSerial->setParam("ICAP", std::to_string(mNumber) + " " + std::to_string(config.integrator_limit));
260  }
261  // Set closed loop error detection [pag. 311]
262  if(_last_pid_config.loop_error_detection != config.loop_error_detection)
263  {
264  // Update integral cap
265  mSerial->setParam("CLERD", std::to_string(mNumber) + " " + std::to_string(config.loop_error_detection));
266  }
267 
268 
269  // Update last configuration
270  _last_pid_config = config;
271 }
void reconfigureCBPID(roboteq_control::RoboteqPIDConfig &config, uint32_t level)
reconfigureCBEncoder when the dynamic reconfigurator change some values start this method ...
Definition: motor_pid.cpp:172
dynamic_reconfigure::Server< roboteq_control::RoboteqPIDConfig > * ds_pid
Dynamic reconfigure PID.
Definition: motor_pid.h:73
bool setParam(string msg, string params="")
status
ros::NodeHandle nh_
Private namespace.
Definition: motor_pid.h:68
const std::string & getNamespace() const
roboteq_control::RoboteqPIDConfig _last_pid_config
Definition: motor_pid.h:82
roboteq::serial_controller * mSerial
Serial port.
Definition: motor_pid.h:70
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
string getParam(string msg, string params="")
string mName
Associate name space.
Definition: motor_pid.h:63
bool getParam(const std::string &key, std::string &s) const
void getPIDFromRoboteq()
getPIDFromRoboteq Load PID parameters from Roboteq board
Definition: motor_pid.cpp:63
roboteq_control::RoboteqPIDConfig default_pid_config
Definition: motor_pid.h:82
unsigned int mNumber
Number motor.
Definition: motor_pid.h:66
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
MotorPIDConfigurator(const ros::NodeHandle &nh, roboteq::serial_controller *serial, string path, string name, unsigned int number)
MotorPIDConfigurator Initialize the dynamic reconfigurator.
Definition: motor_pid.cpp:33
bool setup_pid
Setup variable.
Definition: motor_pid.h:60
void initConfigurator(bool load_from_board)
Definition: motor_pid.cpp:48


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