Skip to content

Commit

Permalink
Merge branch 'dev_ver_3_01' of https://github.com/vedderb/vesc_tool i…
Browse files Browse the repository at this point in the history
…nto dev_ver_3_01
  • Loading branch information
Jfriesen222 committed Nov 22, 2021
2 parents d744608 + 69079ab commit 65b2b49
Show file tree
Hide file tree
Showing 11 changed files with 1,062 additions and 610 deletions.
6 changes: 5 additions & 1 deletion commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,11 @@ void Commands::processPacket(QByteArray data)
case COMM_DETECT_MOTOR_R_L: {
double r = vb.vbPopFrontDouble32(1e6);
double l = vb.vbPopFrontDouble32(1e3);
emit motorRLReceived(r, l);
double ld_lq_diff = 0.0;
if (vb.size() >= 4) {
ld_lq_diff = vb.vbPopFrontDouble32(1e3);
}
emit motorRLReceived(r, l, ld_lq_diff);
} break;

case COMM_DETECT_MOTOR_FLUX_LINKAGE: {
Expand Down
2 changes: 1 addition & 1 deletion commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Commands : public QObject
void decodedAdcReceived(double value, double voltage, double value2, double voltage2);
void decodedChukReceived(double value);
void decodedBalanceReceived(BALANCE_VALUES values);
void motorRLReceived(double r, double l);
void motorRLReceived(double r, double l, double ld_lq_diff);
void motorLinkageReceived(double flux_linkage);
void encoderParamReceived(double offset, double ratio, bool inverted);
void customAppDataReceived(QByteArray data);
Expand Down
226 changes: 169 additions & 57 deletions pages/pagemotorcomparison.cpp

Large diffs are not rendered by default.

111 changes: 93 additions & 18 deletions pages/pagemotorcomparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "vescinterface.h"
#include "configparams.h"
#include "widgets/qcustomplot.h"
#include "utility.h"

namespace Ui {
class PageMotorComparison;
Expand All @@ -45,11 +46,30 @@ private slots:
void on_testRunButton_clicked();

private:
struct TestParams {
TestParams() {
gearing = 1.0;
maxRpm = 50000.0;
gearingEfficiency = 1.0;
fwCurrent = 0.0;
motorNum = 1.0;
tempInc = 0.0;
}

double gearing;
double maxRpm;
double gearingEfficiency;
double fwCurrent;
double motorNum;
double tempInc;
};

Ui::PageMotorComparison *ui;
VescInterface *mVesc;
void settingChanged();
bool reloadConfigs();
void updateDataAndPlot(double posx, double yMin, double yMax);
TestParams getParamsUi(int motor);

ConfigParams mM1Config;
ConfigParams mM2Config;
Expand All @@ -68,6 +88,7 @@ private slots:
rpm_motor_shaft = 0.0;
erpm = 0.0;
iq = 0.0;
id = 0.0;
loss_motor_res = 0.0;
loss_motor_other = 0.0;
loss_motor_tot = 0.0;
Expand All @@ -79,41 +100,88 @@ private slots:
vq = 0.0;
vd = 0.0;
vbus_min = 0.0;
km_h = 0.0;
mph = 0.0;
wh_km = 0.0;
wh_mi = 0.0;
kv_bldc = 0.0;
kv_bldc_noload = 0.0;
}

void update(ConfigParams &config, double rpm, double torque,
double gearing, double gear_eff, double motors, double temp_inc) {
void update(ConfigParams &config, double rpm, double torque, TestParams params) {

// See https://www.mathworks.com/help/physmod/sps/ref/pmsm.html
// for the motor equations

double r = config.getParamDouble("foc_motor_r");
double l = config.getParamDouble("foc_motor_l");
double ld_lq_diff = config.getParamDouble("foc_motor_ld_lq_diff");
double lq = l + ld_lq_diff / 2.0;
double ld = l - ld_lq_diff / 2.0;
double lambda = config.getParamDouble("foc_motor_flux_linkage");
double i_nl = config.getParamDouble("si_motor_nl_current");
double poles_pairs = double(config.getParamInt("si_motor_poles")) / 2.0;
double pole_pairs = double(config.getParamInt("si_motor_poles")) / 2.0;
double wheel_diam = config.getParamDouble("si_wheel_diameter");
bool use_mpta = config.getParamEnum("foc_mtpa_mode");

r += r * 0.00386 * (temp_inc);
r += r * 0.00386 * (params.tempInc);

torque_out = torque;
rpm_out = rpm;
rpm_motor_shaft = rpm * gearing;
erpm = rpm_motor_shaft * poles_pairs;
torque_motor_shaft = torque / (gearing * motors * gear_eff);
rpm_motor_shaft = rpm * params.gearing;
erpm = rpm_motor_shaft * pole_pairs;
torque_motor_shaft = torque / (params.gearing * params.motorNum * params.gearingEfficiency);

double rps_out = rpm * 2.0 * M_PI / 60.0;
double rps_motor = rps_out * gearing;
double e_rps = rps_motor * poles_pairs;

iq = (torque_motor_shaft / (lambda * poles_pairs)) + i_nl;
loss_motor_res = iq * iq * r * 1.5 * motors;
loss_motor_other = e_rps * lambda * i_nl * motors;
double rps_motor = rps_out * params.gearing;
double e_rps = rps_motor * pole_pairs;
double t_nl = (3.0 / 2.0) * i_nl * lambda * pole_pairs; // No-load torque from core losses

iq = ((torque_motor_shaft + t_nl) * (2.0 / 3.0) / (lambda * pole_pairs));
id = -params.fwCurrent;

// 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++) {
iq -= 0.2 * iq * (torque_motor_shaft_updated - torque_motor_shaft) / fmax(fabs(torque_motor_shaft_updated), 1.0);
iq += iq_adj;

// See https://github.com/vedderb/bldc/pull/179
if (use_mpta && fabs(ld_lq_diff) > 1e-9) {
id = (lambda - sqrt(SQ(lambda) + 8.0 * SQ(ld_lq_diff) * SQ(iq))) / (4.0 * ld_lq_diff);
iq_adj = iq - SIGN(iq) * sqrt(SQ(iq) - SQ(id));
iq = SIGN(iq) * sqrt(SQ(iq) - SQ(id));
id -= params.fwCurrent;
}

torque_motor_shaft_updated = (3.0 / 2.0) * pole_pairs * (iq * lambda + iq * id * (ld - lq)) - t_nl;
}

torque_motor_shaft = torque_motor_shaft_updated;
torque_out = torque_motor_shaft * params.gearing * params.motorNum * params.gearingEfficiency;

double i_mag = sqrt(iq * iq + id * id);
loss_motor_res = i_mag * i_mag * r * (3.0 / 2.0) * params.motorNum;
loss_motor_other = rps_motor * t_nl * params.motorNum;
loss_motor_tot = loss_motor_res + loss_motor_other;
loss_gearing = torque_motor_shaft * (1.0 - gear_eff) * rps_motor;
loss_gearing = torque_motor_shaft * (1.0 - params.gearingEfficiency) * rps_motor;
loss_tot = loss_motor_tot + loss_gearing;
p_out = rps_out * torque_out;
p_in = rps_motor * torque_motor_shaft * motors + loss_motor_tot;
p_out = rps_motor * torque_motor_shaft * params.motorNum * params.gearingEfficiency;
p_in = rps_motor * torque_motor_shaft * params.motorNum + loss_motor_tot;
efficiency = p_out / p_in;
vq = r * iq + e_rps * lambda;
vd = -e_rps * l * iq;
vq = r * iq + e_rps * (lambda + id * ld);
vd = r * id - e_rps * lq * iq;
vbus_min = (3.0 / 2.0) * sqrt(vq * vq + vd * vd) / (sqrt(3.0) / 2.0) / 0.95;

km_h = 3.6 * M_PI * wheel_diam * rpm_out / 60.0;
mph = km_h * 0.621371192;

wh_km = p_in / km_h;
wh_mi = p_in / mph;

kv_bldc = rpm_motor_shaft / (vbus_min * (sqrt(3.0) / 2.0));
kv_bldc_noload = (60.0 * 0.95) / (lambda * (3.0 / 2.0) * M_PI * 2.0 * pole_pairs);
}

double torque_out;
Expand All @@ -122,6 +190,7 @@ private slots:
double rpm_motor_shaft;
double erpm;
double iq;
double id;
double loss_motor_res;
double loss_motor_other;
double loss_motor_tot;
Expand All @@ -133,6 +202,12 @@ private slots:
double vq;
double vd;
double vbus_min;
double km_h;
double mph;
double wh_km;
double wh_mi;
double kv_bldc;
double kv_bldc_noload;
};
};

Expand Down
Loading

0 comments on commit 65b2b49

Please sign in to comment.