Skip to content

Commit

Permalink
Added VBUS-limited torque sweep with adaptive field weakening to comp…
Browse files Browse the repository at this point in the history
…arison tool
  • Loading branch information
vedderb committed Nov 28, 2023
1 parent 0154d62 commit ecc25c4
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 9 deletions.
55 changes: 52 additions & 3 deletions pages/pagemotorcomparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ PageMotorComparison::PageMotorComparison(QWidget *parent) :
[this]() { settingChanged(); });
connect(ui->testModeVbusButton, &QRadioButton::toggled,
[this]() { settingChanged(); });
connect(ui->testModeVBFWButton, &QRadioButton::toggled,
[this]() { settingChanged(); });

connect(ui->testLiveUpdateBox, &QCheckBox::toggled,
[this](bool checked) { (void)checked; settingChanged(); });
Expand Down Expand Up @@ -332,18 +334,21 @@ PageMotorComparison::PageMotorComparison(QWidget *parent) :
if (mSettingUpdateRequired) {
ui->testTorqueBox->setEnabled(ui->testModeTorqueButton->isChecked() ||
ui->testModeRpmButton->isChecked() ||
ui->testModeVbusButton->isChecked());
ui->testModeVbusButton->isChecked() ||
ui->testModeVBFWButton->isChecked());
ui->testPowerBox->setEnabled(ui->testModeRpmPowerButton->isChecked() ||
ui->testModeExpButton->isChecked());
ui->testRpmStartBox->setEnabled(ui->testModeRpmPowerButton->isChecked() ||
ui->testModeExpButton->isChecked());
ui->testRpmBox->setEnabled(ui->testModeRpmPowerButton->isChecked() ||
ui->testModeExpButton->isChecked() ||
ui->testModeRpmButton->isChecked() ||
ui->testModeTorqueButton->isChecked());
ui->testModeTorqueButton->isChecked() ||
ui->testModeVBFWButton->isChecked());
ui->testExpBox->setEnabled(ui->testModeExpButton->isChecked());
ui->testExpBaseTorqueBox->setEnabled(ui->testModeExpButton->isChecked());
ui->testVbusBox->setEnabled(ui->testModeVbusButton->isChecked());
ui->testVbusBox->setEnabled(ui->testModeVbusButton->isChecked() ||
ui->testModeVBFWButton->isChecked());

if (ui->tabWidget->currentIndex() == 1) {
setQmlMotorParams();
Expand Down Expand Up @@ -595,6 +600,14 @@ void PageMotorComparison::updateDataAndPlot(double posx, double yMin, double yMa
md.configure(&mM2Config, getParamsUi(2));
md.updateTorqueVBus(posx, ui->testVbusBox->value());
updateTable(md, ui->m2PlotTable);
} else if (ui->testModeVBFWButton->isChecked()) {
MotorData md;
md.configure(&mM1Config, getParamsUi(1));
md.updateTorqueVBusFW(posx, ui->testRpmBox->value(), ui->testVbusBox->value());
updateTable(md, ui->m1PlotTable);
md.configure(&mM2Config, getParamsUi(2));
md.updateTorqueVBusFW(posx, ui->testRpmBox->value(), ui->testVbusBox->value());
updateTable(md, ui->m2PlotTable);
}
}
}
Expand Down Expand Up @@ -998,6 +1011,38 @@ void PageMotorComparison::on_testRunButton_clicked()
updateGraphs(xAxis, yAxes, names);
};

auto plotVBFWSweep = [this, updateData, updateGraphs, plotPoints](QTableWidget *table,
ConfigParams &config, MotorDataParams param) {
double torque = fabs(ui->testTorqueBox->value());
double vbus = ui->testVbusBox->value();
double rpm = ui->testRpmBox->value();

QVector<double> xAxis;
QVector<QVector<double> > yAxes;
QVector<QString> names;

double torque_start = -torque;
if (!ui->testNegativeBox->isChecked()) {
torque_start = torque / plotPoints;
}

for (double t = torque_start;t < torque;t += (torque / plotPoints)) {
MotorData md;
md.configure(&config, param);
md.updateTorqueVBusFW(t, rpm, vbus);
xAxis.append(t);
updateData(md, table, yAxes, names);

if (md.rpm_motor_shaft >= param.maxRpm) {
mVesc->emitMessageDialog("Max RPM", "Maximum motor shaft RPM exceeded", false);
break;
}
}

ui->plot->xAxis->setLabel("Torque (Nm)");
updateGraphs(xAxis, yAxes, names);
};

auto plotQmlSweep = [this, updateData, updateGraphs, plotPoints](QTableWidget *table,
ConfigParams &config, MotorDataParams param, int motor) {

Expand Down Expand Up @@ -1064,6 +1109,10 @@ void PageMotorComparison::on_testRunButton_clicked()
ui->plot->clearGraphs();
plotVbusSweep(ui->m1PlotTable, mM1Config, getParamsUi(1));
plotVbusSweep(ui->m2PlotTable, mM2Config, getParamsUi(2));
} else if (ui->testModeVBFWButton->isChecked()) {
ui->plot->clearGraphs();
plotVBFWSweep(ui->m1PlotTable, mM1Config, getParamsUi(1));
plotVBFWSweep(ui->m2PlotTable, mM2Config, getParamsUi(2));
}
}

Expand Down
50 changes: 47 additions & 3 deletions pages/pagemotorcomparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,53 @@ struct MotorData {
params = prm;
}

Q_INVOKABLE bool updateTorqueVBus(double torque, double vbus) {
double rpm_guess = 1000.0;
Q_INVOKABLE bool updateTorqueVBusFW(double torque, double rpm, double vbus) {
double fw_max = params.fwCurrent;
params.fwCurrent = 0.0;

if (!update(rpm, torque)) {
params.fwCurrent = fw_max;
return false;
}

if (vbus_min < vbus) {
params.fwCurrent = fw_max;
return true;
}

double vbus_lower = vbus_min;
params.fwCurrent = fw_max;
update(rpm, torque);
double vbus_upper = vbus_min;

if (vbus_upper > vbus) {
return updateTorqueVBus(torque, vbus);
}

params.fwCurrent = Utility::map(vbus, vbus_lower, vbus_upper, 0.0, fw_max);

for (int j = 0;j < 20;j++) {
if (!update(rpm, torque)) {
params.fwCurrent = fw_max;
return false;
}

params.fwCurrent *= vbus_min / vbus;

if (params.fwCurrent > fw_max) {
params.fwCurrent = fw_max;
}

if (params.fwCurrent < 0.0) {
params.fwCurrent = 0.0;
}
}

params.fwCurrent = fw_max;
return true;
}

Q_INVOKABLE bool updateTorqueVBus(double torque, double vbus, double rpm_guess = 1000.0) {
for (int i = 0;i < 20;i++) {
if (!update(rpm_guess, torque)) {
return false;
Expand Down Expand Up @@ -187,7 +231,7 @@ struct MotorData {
// Iterate taking motor saliency into account to get the current that produces the desired torque
double torque_motor_shaft_updated = torque_motor_shaft;
double iq_adj = 0.0;
for (int i = 0;i < 50;i++) {
for (int i = 0;i < 30;i++) {
iq -= 0.2 * iq * (torque_motor_shaft_updated - torque_motor_shaft) /
(SIGN(torque_motor_shaft_updated) * fmax(fabs(torque_motor_shaft_updated), 1.0));
iq += iq_adj;
Expand Down
16 changes: 13 additions & 3 deletions pages/pagemotorcomparison.ui
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,17 @@
<string>Torque sweep while keeping the bus voltage constant.</string>
</property>
<property name="text">
<string>Vbus</string>
<string>VBus</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QRadioButton" name="testModeVBFWButton">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Torque-sweep at fixed RPM and limited bus voltage. Uses field weakening according to the duty in the configuration and the current in the FW-box.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>VB+FW</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -450,14 +460,14 @@
<item>
<widget class="QLineEdit" name="compAEdit">
<property name="text">
<string>A</string>
<string>Motor A</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="compBEdit">
<property name="text">
<string>B</string>
<string>Motor B</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit ecc25c4

Please sign in to comment.