|
|
@@ -97,6 +97,121 @@ int motor_map_torque_max_count(void) {
|
|
|
|
|
|
#ifdef MOT_HAVE_MAPS
|
|
|
|
|
|
+vol_rpm_torque_map vol_rpm_curr_map[CONFIG_MAX_VOL_COUNT];
|
|
|
+
|
|
|
+static void motor_vol_lookup(s16 vol, vol_rpm_torque_map *table, vol_rpm_torque_map **out_mapl, vol_rpm_torque_map **out_maph) {
|
|
|
+ if (vol <= table[0].vol) {
|
|
|
+ *out_mapl = table;
|
|
|
+ *out_maph = table + 1;
|
|
|
+ return;
|
|
|
+ }else if (vol >= table[CONFIG_MAX_VOL_COUNT-1].vol){
|
|
|
+ *out_mapl = table + CONFIG_MAX_VOL_COUNT - 2;
|
|
|
+ *out_maph = table + CONFIG_MAX_VOL_COUNT - 1;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (int i = 1; i < CONFIG_MAX_VOL_COUNT; i++) {
|
|
|
+ if ((vol <= table[i].vol)) {
|
|
|
+ *out_mapl = table + i - 1;
|
|
|
+ *out_maph = table + i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void motor_rpm_lookup(s16 rpm, vol_rpm_torque_map *map, rpm_torque_map **out_map1, rpm_torque_map **out_maph) {
|
|
|
+ rpm_torque_map *table = map->rpm_torque;
|
|
|
+ if (rpm <= table[0].rpm) {
|
|
|
+ *out_map1 = table;
|
|
|
+ *out_maph = table + 1;
|
|
|
+ return;
|
|
|
+ }else if (rpm >= table[CONFIG_MAX_VEL_COUNT-1].rpm) {
|
|
|
+ *out_map1 = *out_maph = table + CONFIG_MAX_VEL_COUNT - 1;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (int i = 1; i < CONFIG_MAX_VEL_COUNT; i++) {
|
|
|
+ if (rpm <= table[i].rpm) {
|
|
|
+ *out_map1 = table + i - 1;
|
|
|
+ *out_maph = table + i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void motor_current_lookup(s16 torque, rpm_torque_map *map, curr_pair_t **currl, curr_pair_t **currh) {
|
|
|
+ curr_pair_t *table = map->curr;
|
|
|
+ if (torque <= table[0].curr) {
|
|
|
+ *currl = table;
|
|
|
+ *currh = table + 1;
|
|
|
+ }else if (torque >= table[CONFIG_MAX_D_COUNT - 1].curr) {
|
|
|
+ *currl = table + CONFIG_MAX_D_COUNT - 2;
|
|
|
+ *currh = table + CONFIG_MAX_D_COUNT - 1;
|
|
|
+ }
|
|
|
+ for (int i = 1; i < CONFIG_MAX_D_COUNT; i++) {
|
|
|
+ if (torque <= table[i].curr) {
|
|
|
+ *currl = table + i - 1;
|
|
|
+ *currh = table + i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void motor_torque_lookup(s16 vel, s16 torque, rpm_torque_map *mapl, rpm_torque_map *maph, curr_pair_t *curr) {
|
|
|
+ curr_pair_t *currl, *currh;
|
|
|
+ motor_current_lookup(torque, mapl, &currl, &currh);
|
|
|
+ float dl = line_intp(torque, currl->curr, currh->curr, currl->id, currh->id);
|
|
|
+ _DEBUG("rpm %d -> curr %d : %d, id=%f\n", mapl->rpm, currl->curr, currh->curr, dl);
|
|
|
+ motor_current_lookup(torque, maph, &currl, &currh);
|
|
|
+ float dh = line_intp(torque, currl->curr, currh->curr, currl->id, currh->id);
|
|
|
+ _DEBUG("rpm %d -> curr %d : %d, id=%f\n", maph->rpm, currl->curr, currh->curr, dh);
|
|
|
+ float d = line_intp(vel, mapl->rpm, maph->rpm, dl, dh);
|
|
|
+ _DEBUG("id = %f\n", d);
|
|
|
+ curr->curr = torque;
|
|
|
+ curr->id = d;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+#define SCALE(X) (X/10)
|
|
|
+
|
|
|
+void motor_mpta_fw_lookup2(float rpm, float torque, dq_t *dq_out) {
|
|
|
+ vol_rpm_torque_map *vmapl, *vmaph;
|
|
|
+ rpm_torque_map *rmapl, *rmaph;
|
|
|
+ curr_pair_t currl, currh;
|
|
|
+ s16 vel = ABS(((s16)rpm));
|
|
|
+ s16 bus_vol = get_vbus_int();
|
|
|
+ torque = torque * 10;
|
|
|
+ /* 通过当前电压查找 对应的转速扭矩 */
|
|
|
+ motor_vol_lookup(bus_vol, vol_rpm_curr_map, &vmapl, &vmaph);
|
|
|
+ _DEBUG("vol %d : %d\n", vmapl->vol, vmaph->vol);
|
|
|
+ /* 通过当前转速查找对应的扭矩 */
|
|
|
+ motor_rpm_lookup(vel, vmapl, &rmapl, &rmaph);
|
|
|
+ _DEBUG("vol %d -> rpm %d : %d\n", vmapl->vol, rmapl->rpm, rmaph->rpm);
|
|
|
+ /* 通过当前扭矩需求插值计算dq电流 */
|
|
|
+ motor_torque_lookup(vel, ABS(torque), rmapl, rmaph, &currl);
|
|
|
+
|
|
|
+ motor_rpm_lookup(vel, vmaph, &rmapl, &rmaph);
|
|
|
+ _DEBUG("vol %d -> rpm %d : %d\n", vmaph->vol, rmapl->rpm, rmaph->rpm);
|
|
|
+
|
|
|
+ motor_torque_lookup(vel, ABS(torque), rmapl, rmaph, &currh);
|
|
|
+
|
|
|
+ float d = line_intp(bus_vol, vmapl->vol, vmaph->vol, currl.id, currh.id);
|
|
|
+ float q;
|
|
|
+ if (d >= torque) {
|
|
|
+ d = ABS(torque) * SIGN(torque);
|
|
|
+ q = 0;
|
|
|
+ }else {
|
|
|
+ q = sqrtf(torque * torque - d * d);
|
|
|
+ }
|
|
|
+ if (torque != 0) {
|
|
|
+ dq_out->d = SCALE(d);
|
|
|
+ dq_out->q = torque > 0 ? SCALE(q):SCALE(-q);
|
|
|
+ }else {
|
|
|
+ step_towards(&dq_out->d, SCALE(d), 0.5f);
|
|
|
+ step_towards(&dq_out->q, SCALE(q), 0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
//x -> rpm
|
|
|
//z -> torque
|
|
|
static void intp_line2(float frac_x, s16 z, torque_map_t **map, float *d, float *q) {
|