Skip to content

Commit fceaa1d

Browse files
committed
MotorPlugin: add torque_constant
- Rename some parameters. Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com>
1 parent 68711c7 commit fceaa1d

2 files changed

Lines changed: 41 additions & 21 deletions

File tree

include/MotorPlugin.hh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ namespace systems {
3838
///
3939
/// ### Electro-mechanical properties
4040
///
41-
/// - `<voltage_max>` The maximum terminal voltage.
41+
/// - `<voltage_bat>` The battery voltage (terminal voltage).
4242
///
4343
/// - `<speed_constant>` K_V the motor speed constant, relates the back-emf to
44-
/// the motor angular velocity omega by the relation V_m = omega / K_V.
44+
/// the motor angular velocity omega by the relation v_m = omega / K_V.
4545
///
46-
/// - `<coil_resistance>` R the motor internal resistance (Ohms)
46+
/// - `<torque_constant>` K_Q the motor torque constant, relates the shaft
47+
/// torque to the current by the relation Q_m = (i - i_0) / K_Q.
48+
///
49+
/// - `<resistance>` R the motor internal resistance (Ohms)
4750
///
4851
/// - `<no_load_current>` i_0 the current draw when the motor is run at its
4952
/// specificed voltage and operational RPM with no load.

src/MotorPlugin.cc

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,17 @@ class MotorPlugin::Impl
190190
/// \brief True if braking is disabled.
191191
public: bool disableBraking{false};
192192

193-
/// \brief The maximum terminal voltage (V).
194-
public: double voltageMax;
193+
/// \brief The battery voltage (terminal voltage) (V).
194+
public: double voltageBat;
195195

196-
/// \brief The motor speed constant K_V.
196+
/// \brief The motor speed constant K_V (rad/s/V).
197197
public: double speedConstant;
198198

199+
/// \brief The motor torque constant K_Q (A/N.m).
200+
public: double torqueConstant;
201+
199202
/// \brief R the motor internal resistance (Ohms).
200-
public: double coilResistance;
203+
public: double resistance;
201204

202205
/// \brief i_0 the current draw when the motor is run at its
203206
/// specificed voltage and operational RPM with no load (A).
@@ -412,13 +415,13 @@ void MotorPlugin::Configure(
412415
}
413416

414417
// Electro-mechanical parameters
415-
if (_sdf->HasElement("voltage_max"))
418+
if (_sdf->HasElement("voltage_bat"))
416419
{
417-
this->impl->voltageMax = _sdf->Get<double>("voltage_max");
420+
this->impl->voltageBat = _sdf->Get<double>("voltage_bat");
418421
}
419422
else
420423
{
421-
gzerr << "MotorPlugin: must set `<voltage_max>`." << std::endl;
424+
gzerr << "MotorPlugin: must set `<voltage_bat>`." << std::endl;
422425
return;
423426
}
424427

@@ -432,13 +435,23 @@ void MotorPlugin::Configure(
432435
return;
433436
}
434437

435-
if (_sdf->HasElement("coil_resistance"))
438+
if (_sdf->HasElement("torque_constant"))
439+
{
440+
this->impl->torqueConstant = _sdf->Get<double>("torque_constant");
441+
}
442+
else
443+
{
444+
gzerr << "MotorPlugin: must set `<torque_constant>`." << std::endl;
445+
return;
446+
}
447+
448+
if (_sdf->HasElement("resistance"))
436449
{
437-
this->impl->coilResistance = _sdf->Get<double>("coil_resistance");
450+
this->impl->resistance = _sdf->Get<double>("resistance");
438451
}
439452
else
440453
{
441-
gzerr << "MotorPlugin: must set `<coil_resistance>`." << std::endl;
454+
gzerr << "MotorPlugin: must set `<resistance>`." << std::endl;
442455
return;
443456
}
444457

@@ -454,9 +467,10 @@ void MotorPlugin::Configure(
454467

455468
{
456469
gzdbg << "MotorPlugin: system parameters:" << std::endl;
457-
gzdbg << "voltage_max: [" << this->impl->voltageMax << "]" << std::endl;
470+
gzdbg << "voltage_bat: [" << this->impl->voltageBat << "]" << std::endl;
458471
gzdbg << "speed_constant: [" << this->impl->speedConstant << "]" << std::endl;
459-
gzdbg << "coil_resistance: [" << this->impl->coilResistance << "]" << std::endl;
472+
gzdbg << "torque_constant: [" << this->impl->torqueConstant << "]" << std::endl;
473+
gzdbg << "resistance: [" << this->impl->resistance << "]" << std::endl;
460474
gzdbg << "no_load_current: [" << this->impl->noLoadCurrent << "]" << std::endl;
461475
}
462476

@@ -636,19 +650,22 @@ void MotorPlugin::PreUpdate(
636650
// Mark Drela, MIT Aero & Astro
637651
// February 2007
638652
double Omega = actualVel;
653+
double Q_m = force;
639654
double K_V = this->impl->speedConstant;
655+
double K_Q = this->impl->torqueConstant;
640656
double i_0 = this->impl->noLoadCurrent;
641-
double R = this->impl->coilResistance;
642-
double v_bat = this->impl->voltageMax;
643-
double K_Q = K_V;
657+
double R = this->impl->resistance;
658+
double v_bat = this->impl->voltageBat;
644659
double v_m = Omega / K_V;
645-
double Q_m = force;
646660
double i = Q_m * K_Q + i_0;
647661
double v = v_m + i * R;
648662

649663
{
650664
gzdbg << "--------------------------------" << std::endl;
651665
gzdbg << "MotorPlugin: electic motor calcs" << std::endl;
666+
gzdbg << "actualVel: " << actualVel << std::endl;
667+
gzdbg << "targetVel: " << targetVel << std::endl;
668+
gzdbg << "error: " << error << std::endl;
652669
gzdbg << "Omega: " << Omega << std::endl;
653670
gzdbg << "K_V: " << K_V << std::endl;
654671
gzdbg << "K_Q: " << K_Q << std::endl;
@@ -740,13 +757,13 @@ void MotorPlugin::PreUpdate(
740757
// Note: velocityConstant should be KV in rad/s/Volt
741758

742759
double reqV = (desOmega / control.velocityConstant) +
743-
(control.noLoadCurrent * control.coilResistance);
760+
(control.noLoadCurrent * control.resistance);
744761

745762
// Clamp voltage to available range
746763
double terminalV = std::max(-control.maxVolts, std::min(control.maxVolts, reqV));
747764

748765
double backEmfV = currOmega / control.velocityConstant; // Ω/KV
749-
double current = (terminalV - backEmfV) / control.coilResistance;
766+
double current = (terminalV - backEmfV) / control.resistance;
750767

751768
// Equation (5): Qm = (i - io) / KQ
752769
double torque = (current - control.noLoadCurrent) * control.velocityConstant;

0 commit comments

Comments
 (0)