-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservos.cpp
434 lines (386 loc) · 12.9 KB
/
servos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "servos.hpp"
#include "gazebo/common/Assert.hh"
#include <cmath>
namespace igm = ignition::math;
namespace gzc = gazebo::common;
namespace gzt = gazebo::transport;
namespace gzm = gazebo::msgs;
double clamp(double val, double low, double high)
{
return std::max(low, std::min(val, high));
}
DCModel::DCModel()
{
Voltage = 0;
Ra = 10; //10 Ohm
La = 0.0001; //0.1 mH
Ki = 0.005;
Kv = 0.05;
reduction = 254.0;
counterElF = 0;
current = 0;
didt = 0;
prevcurrent = 0;
torque = 0;
speed = 0;
}
void DCModel::reset()
{
Voltage = 0;
counterElF = 0;
current = 0;
didt = 0;
prevcurrent = 0;
torque = 0;
speed = 0;
}
void DCModel::step(double dt)
{
//Stall Torque : imax = 0.9, Torque = 1.5 N.m => K = 1,66 * red
//Max speed : Rpm max = 1.2 Rps, U = 9.6 v => K = 8 / red
//red = 2.2
//std::cout << "[" << Voltage << "]" << std::endl;
counterElF = Kv * speed * reduction;//speed is in rad/sec => Hz
if((counterElF < -20)||(counterElF>20))
{
//std::cout << "counterElf Too High>counterElF: " << counterElF << " Kv " << Kv << " speed " << speed << " dt " << dt <<" reduction " << reduction << std::endl;
}
counterElF = clamp(counterElF,-8,8);
//current = (Voltage - counterElF - La * didt) / Ra;//didt is not filtred and is too high
current = (Voltage - counterElF) / Ra;
if((current < -6)||(current>6))
{
std::cout << "current Too High(no didt)>current: "<< current <<" Voltage " << Voltage << " counterElF " << counterElF << " La " << La << " didt " << didt << " Ra " << Ra << std::endl;
}
current = clamp(current,-1,1);
//GZ_ASSERT(((current < -2)||(current>2)), "Current Too High");
//char f_val[30]; sprintf(f_val,"i=%0.2f ",current); std::cout << f_val;
torque = Ki * current * reduction;
if((torque < -6)||(torque>6))
{
std::cout << "torque too High>torque: " << torque <<" Ki " << Ki << " current " << current << " reduction " << reduction << std::endl;
}
torque = clamp(torque,-1,1);
//GZ_ASSERT(((torque < -2)||(torque>2)), "Torque Too High");
//if(torque != 0){char f_val[30]; sprintf(f_val,"T=%0.2f ",torque); std::cout << f_val << std::endl;}
didt = (current - prevcurrent) / dt;
//store values for next cycle
prevcurrent = current;
}
void DCModel::setParams(double r,double l, double ki, double kv)
{
Ra = r;
La = l;
Ki = ki;
Kv = kv;
}
void DCModel::control(double v)
{
Voltage = v;
}
Servo::Servo():speedFilter(6),positionFilter(6),jitterMeasure(6)
{
isPID_Pos = false;
isPID_Speed = false;
isPID_Torque = false;
isPublishing = false;
isLogging = false;
prevtime = 0;
jitter = 0;
}
bool Servo::safe_dt(double simtime, double &dt)
{
double res = false;
if(simtime != 0.0) //making sure it is a valid time and not the default value
if(simtime > prevtime) //making sure the time has advanced so that dt is not 0
{
double tdiff = simtime - prevtime;
if(tdiff >= min_dt) //plausibilisation, small values are more likely to be errors
if(tdiff <= max_dt) //plausibilisation, close to time constants, and eliminates gaps
{
dt = tdiff;
res = true;
}
}
return res;
}
int valcount = 0;
void Servo::run_step(double simtime, double batVoltage)
{
if(type == ServoType::DC)
{
double dt = 0;
if(safe_dt(simtime,dt))
{
//PIDs regulation
if(isPID_Pos)
{
//calculate the error diff
igm::Angle pid_Target = target;
igm::Angle errAngle = position - pid_Target;
//errAngle.Normalize();
double e = errAngle.Radian() / 3.141592654;//normalized error ~ [-1 , 1]
pos_pid->Update(e,gzc::Time(dt));
//inputs
dc->control(batVoltage * pos_pid->GetCmd());//[-Vbat , Vbat]
}
else if(isPID_Speed)
{
}
else if(isPID_Torque)
{
}
//Electrical Motor simulation
dc->step(dt);
}
else
{
char f_val[60]; sprintf(f_val," min_dt=%0.8f ",min_dt); std::cout << f_val;
sprintf(f_val," max_dt=%0.8f ",max_dt); std::cout << f_val;
sprintf(f_val," dt=%0.8f ",dt); std::cout << f_val;
std::cout << " Simulation Time Not Plausible. simtime: "<< simtime << std::endl;
//clear accumulated energy
pos_pid->Reset();
}
if(isPublishing)
{
pub_pos_target->Publish(gzm::ConvertAny(target));
pub_torque->Publish(gzm::ConvertAny(getTorque()));
pub_current->Publish(gzm::ConvertAny(getCurrent()));
pub_posf->Publish(gzm::ConvertAny(position.Radian()));
pub_speedf->Publish(gzm::ConvertAny(dc->speed));
}
if(isLogging)
{
//gzlog << "Torque" << "\t" << "Current" << "\t" << "PosRef" << "\t" << "Positionf" << "\t" << "speedf" << std::endl;
gzlog << "\t" << name << "\t" << getTorque() << "\t" << getCurrent() << "\t" << target << "\t" << position.Radian() << "\t" << dc->speed << std::endl;
}
prevtime = simtime;
}
}
void Servo::updatePosition(double pos)
{
//This position is used for the Servo PID, when jittering, the jitter is amplified and applied as torque
position = positionFilter.add_read(pos);//math::Angle <= double
//std::cout << "pos: " << pos << " : " << position.Radian();
}
void Servo::updateSpeed(double o)
{
jitterMeasure.addMeasure(o);
//the speed is used to create the Counter Electromotive Force, so when jittering with an unrealistic high up and down
//it results in multiple jittering DC model parameters
if(type == ServoType::DC)
{
dc->speed = speedFilter.add_read(o);
//std::cout << "speed: " << o << " : " << dc->speed << " : " << std::endl;
}
}
double Servo::getJitter()
{
return jitterMeasure.read();
}
double Servo::getCurrent()
{
double res = 0.0;
if(type == ServoType::DC)
{
//the Servo handles the voltage inversion, so the consumed current is always positive
res = fabs(dc->current);
}
return res;
}
double Servo::getTorque()
{
double res = 0.0;
if(type == ServoType::DC)
{
res = dc->torque;
}
return res;
}
void Servo::Set_ax12a()
{
type = ServoType::DC;
dc = new DCModel();
//@9.6V => 900 mA; R = 10 Ohm
//L ~ 0.1 mH
//K ; @9.6V, 0 Load => U = K . Omega_rps => K = 9.6 / (0.196 sec / 60 deg)
//0.196 sec / 60 deg => 0.85 Rpsec / reduction => 216 rps
//Kv = 9.6 / (rps * 254) = 9.6 / 216 = ~ 0.044 - speed coming from phyisics too high
//Ki = (Stall_Torque/254) / i_max = ~ 0.0064
dc->setParams(10,0.0001,0.0064,0.008);//r, l, ki, kv
//--------------------------------------------------------
//position => [-Pi , Pi]
double iMAX = 0.8;
double cmdMAX = 1;//the regulator will multiply by battery voltage
//3,2,1, 0.5 1
pos_pid = new gazebo::common::PID( 3, // 1/p : Error that brings cmd to max ~ pi/10
1,
0.5,
iMAX,-iMAX,
cmdMAX,-cmdMAX);
pos_pid->Reset();
isLogging = true;
}
void Servo::Advertise_ax12a(const std::string &servo_topic_path)
{
isPublishing = false;
node = gzt::NodePtr(new gzt::Node());
node->Init();
pub_pos_target = node->Advertise<gazebo::msgs::Any>(servo_topic_path+"/posref",100*10000,10000);//100*10000,10000
pub_torque = node->Advertise<gazebo::msgs::Any>(servo_topic_path+"/torque",100*10000,10000);
pub_current = node->Advertise<gazebo::msgs::Any>(servo_topic_path+"/current",100*10000,10000);
pub_posf = node->Advertise<gazebo::msgs::Any>(servo_topic_path+"/posf",100*10000,10000);
pub_speedf = node->Advertise<gazebo::msgs::Any>(servo_topic_path+"/speedf",100*10000,10000);
}
void Servo::SetPositionTarget(double v_target)
{
isPID_Pos = true;
isPID_Speed = false;
isPID_Torque = false;
target = v_target;
}
void Servo::SetSpeedTarget(double v_target)
{
isPID_Pos = false;
isPID_Speed = true;
isPID_Torque = false;
target = v_target;
}
void Servo::SetTorqueTarget(double v_target)
{
isPID_Pos = false;
isPID_Speed = false;
isPID_Torque = true;
target = v_target;
}
ServosController::ServosController()
{
isPID = false;
isDC = false;
isBLDC = false;
}
void ServosController::SetModel(gazebo::physics::ModelPtr l_model)
{
model = l_model;
}
void ServosController::SetBattery(gazebo::common::BatteryPtr v_bat)
{
bat = v_bat;
}
void ServosController::SetPid(const std::string &jointName)
{
servos.insert(std::make_pair(jointName,new Servo()));
servos[jointName]->type = ServoType::PID;
// P,I,D, IMAX, Imin, cmdMAX, cmdmin
//servos[jointName]->pid = new gazebo::common::PID(20,10,5,10,-10,50,-50);
double cmdMAX = 1.47;//15 Kg cm => 1.47 N.m
servos[jointName]->pid = new gazebo::common::PID(3,2,1,0.5,-0.5,cmdMAX,-cmdMAX);
servos[jointName]->pid->Reset();
gazebo::physics::JointControllerPtr pj1 = model->GetJointController();
pj1->SetPositionPID(jointName,*servos[jointName]->pid);
isPID = true;
}
void ServosController::Set_ax12a(const std::string &jointName)
{
servos.insert(std::make_pair(jointName,new Servo()));
servos[jointName]->Set_ax12a();
servos[jointName]->name = jointName;
std::string servo_topic_path = "/gazebo/default/"+model->GetName()+"/servos/"+jointName;
servos[jointName]->Advertise_ax12a(servo_topic_path);
isDC = true;
gzlog << "\t" << "Name" << "\t" << "Torque" << "\t" << "Current" << "\t" << "PosRef" << "\t" << "Positionf" << "\t" << "speedf" << std::endl;
}
void ServosController::SetServo(const std::string &jointName, const std::string &servoName)
{
if(servoName.compare("PID") == 0)
{
SetPid(jointName);
}
else if(servoName.compare("AX12A") == 0)
{
Set_ax12a(jointName);
}
else if(servoName.compare("AX-GM2212-72Kv") == 0)
{
Set_bldc_72(jointName);
isBLDC = true;
}
}
void ServosController::SetPositionTarget(const std::string &jointName, double target)
{
Servo &serv = *servos[jointName];
if(serv.type == ServoType::PID)
{
gazebo::physics::JointControllerPtr pj1 = model->GetJointController();
pj1->SetPositionTarget(jointName,target);
}
if(serv.type == ServoType::DC)
{
serv.SetPositionTarget(target);
//std::cout << "PosTarget " << target;
}
}
void ServosController::SetSpeedTarget(const std::string &jointName, double target)
{
servos[jointName]->SetSpeedTarget(target);
}
void ServosController::SetTorqueTarget(const std::string &jointName, double target)
{
servos[jointName]->SetTorqueTarget(target);
}
bool ServosController::isTimeTicked(double period)
{
static double lastTick = 0;
double timeNow = model->GetWorld()->RealTime().Double();
bool isRes = false;
if(lastTick == 0)
{
lastTick = timeNow;
isRes = true;
}
else
{
if((timeNow-lastTick) > period)
{
lastTick = timeNow;
isRes = true;
}
}
return isRes;
}
void ServosController::update(double simtime)
{
static int count = 0;
count++;
double sumJitter = 0;
int nb_serv = 0;
for(auto& servo : servos)
{
//std::cout << servo.first << " " << << std::endl;
if(servo.second->type == ServoType::PID)
{
model->GetJointController()->Update();
}
else if(servo.second->type == ServoType::DC)
{
gazebo::physics::JointPtr joint = model->GetJoint(servo.first);
//------------update the motors physics from the simulation------------
servo.second->updateSpeed(joint->GetVelocity(0));
//------------update the servo encoder for position regulation------------
servo.second->updatePosition(joint->Position());
//---------process the Servo control loop + electrical simulation---------
//every step needs the new battery level value
servo.second->run_step(simtime,bat->Voltage());
//------------inject the generated Force into the simulation--------------
joint->SetForce(0,servo.second->getTorque());
//TODO consume the current from the battery
sumJitter+=servo.second->getJitter();
nb_serv++;
}
}
if(isTimeTicked(3.0))
{
std::cout << "@ " << count << " ; avg jitter: "<< sumJitter/nb_serv << std::endl;
}
}