Skip to content

Reduce calculation cost in PWM interrupt handler #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8be1c7c
Add hall signal delay compensation
at-wat Sep 4, 2019
27d9dc4
Fix param receive
at-wat Sep 4, 2019
26292e3
Fix param init
at-wat Sep 4, 2019
78760c7
Add LR delay compensation
at-wat Sep 4, 2019
891e149
Fix atan
at-wat Sep 4, 2019
d96eb36
Calculate rely_hall threshold from parameters
at-wat Sep 5, 2019
038bd4f
Merge branch 'add-hall-signal-delay-compensation' into add-lr-delay-c…
at-wat Sep 5, 2019
607af59
Fix phase offset direction
at-wat Sep 5, 2019
0382bf7
Tweak parameter
at-wat Sep 5, 2019
0ad0112
Merge branch 'add-hall-signal-delay-compensation' into add-lr-delay-c…
at-wat Sep 5, 2019
51a9b8a
Fix rely hall condition
at-wat Sep 5, 2019
516faa2
Merge branch 'add-hall-signal-delay-compensation' into add-lr-delay-c…
at-wat Sep 5, 2019
118d003
Fix delay compensation
at-wat Sep 5, 2019
cb70926
Use reference velocity in LR delay calculation
at-wat Sep 5, 2019
7078e09
Remove value for debug
at-wat Sep 5, 2019
36bdac4
Add LPF to encoder origin
at-wat Sep 5, 2019
99cec3a
Fix LPF apply condition
at-wat Sep 5, 2019
3100ac4
Reduce calculation cost in PWM interrupt handler
at-wat Sep 6, 2019
6f45d29
Remove unused code
at-wat Sep 6, 2019
946583f
Update comment
at-wat Sep 6, 2019
d3440f9
Merge branch 'add-lpf-to-encoder-origin' into reduce-calculation-cost…
at-wat Sep 6, 2019
398d390
Normalize initial origin
at-wat Sep 7, 2019
7756e7b
Merge branch 'add-lpf-to-encoder-origin' into reduce-calculation-cost…
at-wat Sep 7, 2019
4faf7ea
Fix cyclic filter
at-wat Sep 7, 2019
de844b5
LPF in velocity control loop
at-wat Sep 7, 2019
c6add5f
Merge branch 'add-lpf-to-encoder-origin' into reduce-calculation-cost…
at-wat Sep 7, 2019
c4b4d58
Fix variable name
at-wat Sep 11, 2019
3f4c44c
Merge branch 'add-lr-delay-compensation' into add-lpf-to-encoder-origin
at-wat Sep 11, 2019
932386f
Merge branch 'add-lpf-to-encoder-origin' into reduce-calculation-cost…
at-wat Sep 11, 2019
f08a348
Add static assert to ensure ENC0_BUF_MAX is power of 2
at-wat Sep 11, 2019
454ce0a
Merge branch 'master' into reduce-calculation-cost-in-pwm-interrupt
at-wat Sep 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions tfrog-motordriver/controlPWM.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ void controlPWM_config(int i)

// initialize encoder zero position buffer
motor[i].enc0_buf_len =
motor_param[i].enc_denominator < ENC0_BUF_MAX_DENOMINATOR ?
motor_param[i].enc_denominator < ENC0_DENOMINATOR_MAX ?
motor_param[i].enc_denominator * 6 :
ENC0_BUF_MAX;
int j;
for (j = 0; j < motor[i].enc0_buf_len; ++j)
motor[i].enc0_buf[j] = ENC0_BUF_UNKNOWN;
motor[i].enc0_buf_updated = 0;

motor_param[i].enc_drev[0] = motor_param[i].enc_rev * 1 / 6;
motor_param[i].enc_drev[1] = motor_param[i].enc_rev * 2 / 6;
Expand Down Expand Up @@ -629,26 +630,9 @@ void FIQ_PWMPeriod()
enc0 -= motor[i].vel1 * motor_param[i].hall_delay_factor / 32768;

// Fill enc0_buf and calculate average
if (hall_pos >= ENC0_BUF_MAX)
hall_pos = hall_pos % ENC0_BUF_MAX;

hall_pos = hall_pos % ENC0_BUF_MAX;
motor[i].enc0_buf[hall_pos] = enc0;
int j;
int sum_enc0_err = 0, num_enc0 = 0;
for (j = 0; j < motor[i].enc0_buf_len; ++j)
{
if (motor[i].enc0_buf[j] != ENC0_BUF_UNKNOWN)
{
int err = motor[i].enc0_buf[j] - enc0;
normalize(&err, -motor_param[i].enc_rev_h, motor_param[i].enc_rev);

sum_enc0_err += err;
num_enc0++;
}
}
enc0 += (sum_enc0_err / num_enc0);
normalize(&enc0, 0, motor_param[i].enc_rev);
motor_param[i].enc0 = enc0;
motor[i].enc0_buf_updated = 1;
}
}

Expand Down
24 changes: 24 additions & 0 deletions tfrog-motordriver/controlVelocity.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,30 @@ void timer0_vel_calc()
FilterExp_FilterAngle(
&motor[i].enc0_lpf, motor_param[i].enc0,
motor_param[i].enc_rev, motor_param[i].enc_rev_raw);

// Calculate 1 rev average of encoder origin
if (!motor[i].enc0_buf_updated)
continue;

motor[i].enc0_buf_updated = 0;

int enc0 = motor_param[i].enc0;
int sum_enc0_err = 0, num_enc0 = 0;
for (int j = 0; j < motor[i].enc0_buf_len; ++j)
{
const int e0 = motor[i].enc0_buf[j];
if (e0 != ENC0_BUF_UNKNOWN)
{
int err = e0 - enc0;
normalize(&err, -motor_param[i].enc_rev_h, motor_param[i].enc_rev);

sum_enc0_err += err;
num_enc0++;
}
}
enc0 += sum_enc0_err / num_enc0;
normalize(&enc0, 0, motor_param[i].enc_rev_raw);
motor_param[i].enc0 = enc0;
}

ISR_VelocityControl();
Expand Down
16 changes: 12 additions & 4 deletions tfrog-motordriver/controlVelocity.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ typedef enum _ErrorID
} ErrorID;
#define ERROR_NUM 6

#define ENC0_BUF_MAX_DENOMINATOR 16 // up to 16 electric revolution
#define ENC0_BUF_MAX (ENC0_BUF_MAX_DENOMINATOR * 6)
// ENC0_BUF_MAX must be 2^n to reduce computation cost.
#define ENC0_BUF_MAX 64
#define ENC0_DENOMINATOR_MAX ((int)(ENC0_BUF_MAX / 6))
#define ENC0_BUF_UNKNOWN 0x7FFFFFF

static_assert(
ENC0_BUF_MAX && ((ENC0_BUF_MAX & (ENC0_BUF_MAX - 1)) == 0),
"ENC0_BUF_MAX must be 2^n");

#define ACCEL_FILTER_TIME 15 // Timeconstant in velocity control steps
#define ENC0_FILTER_TIME 32 // Timeconstant in velocity control steps

Expand All @@ -60,8 +65,6 @@ typedef struct _MotorState
unsigned int spd_cnt;
unsigned short enc;
short dir;
int enc0_buf[ENC0_BUF_MAX];
unsigned short enc0_buf_len;

struct
{
Expand All @@ -80,6 +83,11 @@ typedef struct _MotorState
int error;
int64_t error_integ;
char control_init;

char enc0_buf_updated;
int enc0_buf[ENC0_BUF_MAX];
unsigned short enc0_buf_len;

YPSpur_servo_level servo_level;
ErrorID error_state;
FilterExp enc0_lpf;
Expand Down