Skip to content

Commit fd48dc2

Browse files
committed
Make PID position control direction consistent with other control modes when encoder is inverted
1 parent fff18eb commit fd48dc2

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

conf_general.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define FW_VERSION_MAJOR 5
2525
#define FW_VERSION_MINOR 03
2626
// Set to 0 for building a release and iterate during beta test builds
27-
#define FW_TEST_VERSION_NUMBER 64
27+
#define FW_TEST_VERSION_NUMBER 65
2828

2929
#include "datatypes.h"
3030

mc_interface.c

+19-2
Original file line numberDiff line numberDiff line change
@@ -672,13 +672,22 @@ void mc_interface_set_pid_pos(float pos) {
672672
return;
673673
}
674674

675+
volatile mc_configuration *conf = &motor_now()->m_conf;
676+
675677
motor_now()->m_position_set = pos;
676678

677679
pos += motor_now()->m_conf.p_pid_offset;
678680
pos *= DIR_MULT;
681+
682+
if (encoder_is_configured()) {
683+
if (conf->foc_encoder_inverted) {
684+
pos *= -1.0;
685+
}
686+
}
687+
679688
utils_norm_angle(&pos);
680689

681-
switch (motor_now()->m_conf.motor_type) {
690+
switch (conf->motor_type) {
682691
case MOTOR_TYPE_BLDC:
683692
case MOTOR_TYPE_DC:
684693
mcpwm_set_pid_pos(pos);
@@ -1364,7 +1373,9 @@ float mc_interface_get_pid_pos_set(void) {
13641373
float mc_interface_get_pid_pos_now(void) {
13651374
float ret = 0.0;
13661375

1367-
switch (motor_now()->m_conf.motor_type) {
1376+
volatile mc_configuration *conf = &motor_now()->m_conf;
1377+
1378+
switch (conf->motor_type) {
13681379
case MOTOR_TYPE_BLDC:
13691380
case MOTOR_TYPE_DC:
13701381
ret = encoder_read_deg();
@@ -1378,6 +1389,12 @@ float mc_interface_get_pid_pos_now(void) {
13781389
break;
13791390
}
13801391

1392+
if (encoder_is_configured()) {
1393+
if (conf->foc_encoder_inverted) {
1394+
ret *= -1.0;
1395+
}
1396+
}
1397+
13811398
ret *= DIR_MULT;
13821399
ret -= motor_now()->m_conf.p_pid_offset;
13831400
utils_norm_angle(&ret);

0 commit comments

Comments
 (0)