Skip to content
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

Use potentiometer for angle offset calib #760

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions confgenerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ int32_t confgenerator_serialize_mcconf(uint8_t *buffer, const mc_configuration *
buffer_append_float32_auto(buffer, conf->p_pid_ang_div, &ind);
buffer_append_float16(buffer, conf->p_pid_gain_dec_angle, 10, &ind);
buffer_append_float32_auto(buffer, conf->p_pid_offset, &ind);
buffer[ind++] = conf->p_pid_offset_pot_calib;
buffer_append_float16(buffer, conf->cc_startup_boost_duty, 10000, &ind);
buffer_append_float32_auto(buffer, conf->cc_min_current, &ind);
buffer_append_float32_auto(buffer, conf->cc_gain, &ind);
Expand Down Expand Up @@ -495,6 +496,7 @@ bool confgenerator_deserialize_mcconf(const uint8_t *buffer, mc_configuration *c
conf->p_pid_ang_div = buffer_get_float32_auto(buffer, &ind);
conf->p_pid_gain_dec_angle = buffer_get_float16(buffer, 10, &ind);
conf->p_pid_offset = buffer_get_float32_auto(buffer, &ind);
conf->p_pid_offset_pot_calib = buffer[ind++];
conf->cc_startup_boost_duty = buffer_get_float16(buffer, 10000, &ind);
conf->cc_min_current = buffer_get_float32_auto(buffer, &ind);
conf->cc_gain = buffer_get_float32_auto(buffer, &ind);
Expand Down Expand Up @@ -827,6 +829,7 @@ void confgenerator_set_defaults_mcconf(mc_configuration *conf) {
conf->p_pid_ang_div = MCCONF_P_PID_ANG_DIV;
conf->p_pid_gain_dec_angle = MCCONF_P_PID_GAIN_DEC_ANGLE;
conf->p_pid_offset = MCCONF_P_PID_OFFSET;
conf->p_pid_offset_pot_calib = MCCONF_P_PID_OFFSET_POT_CALIB;
conf->cc_startup_boost_duty = MCCONF_CC_STARTUP_BOOST_DUTY;
conf->cc_min_current = MCCONF_CC_MIN_CURRENT;
conf->cc_gain = MCCONF_CC_GAIN;
Expand Down
2 changes: 1 addition & 1 deletion confgenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stdbool.h>

// Constants
#define MCCONF_SIGNATURE 1065524471
#define MCCONF_SIGNATURE 1439595948
#define APPCONF_SIGNATURE 2099347128

// Functions
Expand Down
1 change: 1 addition & 0 deletions datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ typedef struct {
float p_pid_ang_div;
float p_pid_gain_dec_angle;
float p_pid_offset;
bool p_pid_offset_pot_calib;

// Current controller
float cc_startup_boost_duty;
Expand Down
3 changes: 3 additions & 0 deletions hwconf/luna/m600/mcconf_luna_m600_48V.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@
// Position PID Offset Angle
#define MCCONF_P_PID_OFFSET 0

// Position PID Use Potentiometer for Offset Angle Calibration
#define MCCONF_P_PID_POT_OFFSET_CALIB 0

// Startup boost
#define MCCONF_CC_STARTUP_BOOST_DUTY 0.01

Expand Down
3 changes: 3 additions & 0 deletions hwconf/luna/m600/mcconf_luna_m600_60V.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@
// Position PID Offset Angle
#define MCCONF_P_PID_OFFSET 0

// Position PID Use Potentiometer for Offset Angle Calibration
#define MCCONF_P_PID_POT_OFFSET_CALIB 0

// Startup boost
#define MCCONF_CC_STARTUP_BOOST_DUTY 0.01

Expand Down
21 changes: 20 additions & 1 deletion motor/mc_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,27 @@ float mc_interface_get_pid_pos_now(void) {
void mc_interface_update_pid_pos_offset(float angle_now, bool store) {
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();

// Use potentiometer to calibrate the offset if enabled
if (mcconf->p_pid_offset_pot_calib) {
const app_configuration *appconf = app_get_configuration();
const int adc_avg_window_size = 100;
float angle_potentiometer_sum = 0.0;
// moving average as a low pass filter
for (int i = 0; i < adc_avg_window_size; i++) {
float pwr = ADC_VOLTS(ADC_IND_EXT);
angle_potentiometer_sum +=
utils_map(pwr, appconf->app_adc_conf.voltage_start,
appconf->app_adc_conf.voltage_end, 0.0, 360.0);
chThdSleepMilliseconds(10);
}
float angle_potentiometer =
angle_potentiometer_sum / adc_avg_window_size;
mcconf->p_pid_offset = angle_potentiometer-angle_now;
}else{
mcconf->p_pid_offset += mc_interface_get_pid_pos_now() - angle_now;
}

mcconf->p_pid_offset += mc_interface_get_pid_pos_now() - angle_now;
utils_norm_angle(&mcconf->p_pid_offset);

if (store) {
Expand Down
3 changes: 3 additions & 0 deletions motor/mcconf_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@
#ifndef MCCONF_P_PID_OFFSET
#define MCCONF_P_PID_OFFSET 0.0 // Angle offset
#endif
#ifndef MCCONF_P_PID_OFFSET_POT_CALIB
#define MCCONF_P_PID_OFFSET_POT_CALIB false
#endif

// Current control parameters
#ifndef MCCONF_CC_GAIN
Expand Down