2022-02-19 06:37:12 -08:00
|
|
|
/*
|
|
|
|
Copyright 2016 - 2022 Benjamin Vedder benjamin@vedder.se
|
|
|
|
Copyright 2022 Marcos Chaparro mchaparro@powerdesigns.ca
|
|
|
|
Copyright 2022 Jakub Tomczak
|
|
|
|
|
|
|
|
This file is part of the VESC firmware.
|
|
|
|
|
|
|
|
The VESC firmware is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
The VESC firmware is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-12-15 07:48:49 -08:00
|
|
|
|
2022-02-19 12:00:52 -08:00
|
|
|
#include "enc_sincos.h"
|
2021-12-15 07:26:36 -08:00
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "stm32f4xx_conf.h"
|
|
|
|
#include "mc_interface.h"
|
2022-03-16 10:40:51 -07:00
|
|
|
#include "utils_math.h"
|
2022-01-20 12:23:37 -08:00
|
|
|
#include "hw.h"
|
2022-02-20 03:12:40 -08:00
|
|
|
#include "timer.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2022-01-02 08:40:30 -08:00
|
|
|
|
2022-11-16 04:48:50 -08:00
|
|
|
#define SINCOS_MIN_AMPLITUDE 0.7 // sqrt(sin^2 + cos^2) has to be larger than this
|
|
|
|
#define SINCOS_MAX_AMPLITUDE 1.3 // sqrt(sin^2 + cos^2) has to be smaller than this
|
2021-12-15 07:26:36 -08:00
|
|
|
|
2022-02-20 07:22:38 -08:00
|
|
|
bool enc_sincos_init(ENCSINCOS_config_t *cfg) {
|
2022-02-20 03:12:40 -08:00
|
|
|
memset(&cfg->state, 0, sizeof(ENCSINCOS_state));
|
2022-02-20 07:22:38 -08:00
|
|
|
return true;
|
2021-12-15 07:26:36 -08:00
|
|
|
}
|
|
|
|
|
2022-02-20 03:12:40 -08:00
|
|
|
void enc_sincos_deinit(ENCSINCOS_config_t *cfg) {
|
|
|
|
memset(&cfg->state, 0, sizeof(ENCSINCOS_state));
|
2021-12-15 07:26:36 -08:00
|
|
|
}
|
2022-02-19 10:40:42 -08:00
|
|
|
|
2022-02-20 03:12:40 -08:00
|
|
|
float enc_sincos_read_deg(ENCSINCOS_config_t *cfg) {
|
2022-10-11 08:52:17 -07:00
|
|
|
float sin = (ENCODER_SIN_VOLTS - cfg->s_offset) * cfg->s_gain;
|
|
|
|
float cos = (ENCODER_COS_VOLTS - cfg->c_offset) * cfg->c_gain;
|
2021-12-15 07:26:36 -08:00
|
|
|
|
2022-10-11 07:54:54 -07:00
|
|
|
UTILS_LP_FAST(cfg->state.sin_filter, sin, cfg->filter_constant);
|
|
|
|
UTILS_LP_FAST(cfg->state.cos_filter, cos, cfg->filter_constant);
|
|
|
|
sin = cfg->state.sin_filter;
|
|
|
|
cos = cfg->state.cos_filter;
|
|
|
|
|
2022-12-01 06:50:27 -08:00
|
|
|
//phase error compensation
|
2022-12-01 07:17:24 -08:00
|
|
|
cos = (cos + sin*cfg->sph)/cfg->cph;
|
2022-12-01 06:50:27 -08:00
|
|
|
|
|
|
|
|
2021-12-15 07:26:36 -08:00
|
|
|
float module = SQ(sin) + SQ(cos);
|
|
|
|
|
2022-02-20 03:12:40 -08:00
|
|
|
float timestep = timer_seconds_elapsed_since(cfg->state.last_update_time);
|
|
|
|
if (timestep > 1.0) {
|
|
|
|
timestep = 1.0;
|
|
|
|
}
|
|
|
|
cfg->state.last_update_time = timer_time_now();
|
|
|
|
|
2021-12-15 07:26:36 -08:00
|
|
|
if (module > SQ(SINCOS_MAX_AMPLITUDE) ) {
|
|
|
|
// signals vector outside of the valid area. Increase error count and discard measurement
|
2022-02-20 03:12:40 -08:00
|
|
|
++cfg->state.signal_above_max_error_cnt;
|
|
|
|
UTILS_LP_FAST(cfg->state.signal_above_max_error_rate, 1.0, timestep);
|
2022-10-11 07:54:54 -07:00
|
|
|
} else if (module < SQ(SINCOS_MIN_AMPLITUDE)) {
|
|
|
|
++cfg->state.signal_below_min_error_cnt;
|
|
|
|
UTILS_LP_FAST(cfg->state.signal_low_error_rate, 1.0, timestep);
|
2021-12-15 07:26:36 -08:00
|
|
|
} else {
|
2022-10-11 07:54:54 -07:00
|
|
|
UTILS_LP_FAST(cfg->state.signal_above_max_error_rate, 0.0, timestep);
|
|
|
|
UTILS_LP_FAST(cfg->state.signal_low_error_rate, 0.0, timestep);
|
2022-10-11 08:52:17 -07:00
|
|
|
cfg->state.last_enc_angle = RAD2DEG_f(utils_fast_atan2(sin, cos)) + 180.0;
|
2021-12-15 07:26:36 -08:00
|
|
|
}
|
2022-02-19 10:40:42 -08:00
|
|
|
|
2022-10-11 07:54:54 -07:00
|
|
|
return cfg->state.last_enc_angle;
|
2021-12-20 05:18:51 -08:00
|
|
|
}
|