Merge branch 'devel' into preroll

This commit is contained in:
Oskar Weigl
2019-02-10 20:11:18 -08:00
72 changed files with 1196 additions and 7410 deletions
+40 -5
View File
@@ -24,7 +24,7 @@ void Encoder::setup() {
enc_index_cb_wrapper, this);
}
void Encoder::set_error(Encoder::Error_t error) {
void Encoder::set_error(Error_t error) {
error_ |= error;
axis_->error_ |= Axis::ERROR_ENCODER_FAILED;
}
@@ -46,7 +46,8 @@ void Encoder::enc_index_cb() {
if (config_.find_idx_on_lockin && axis_->lockin_state_ != Axis::LOCKIN_STATE_CONST_VEL)
return;
set_circular_count(0, false);
set_linear_count(0); // Avoid position control transient after search
if (config_.zero_count_on_find_idx)
set_linear_count(0); // Avoid position control transient after search
if (config_.pre_calibrated) {
is_ready_ = true;
} else {
@@ -221,6 +222,27 @@ void Encoder::update_pll_gains() {
}
}
void Encoder::sample_now() {
switch (config_.mode) {
case MODE_INCREMENTAL: {
tim_cnt_sample_ = (int16_t)hw_config_.timer->Instance->CNT;
} break;
case MODE_HALL: {
// do nothing: samples already captured in general GPIO capture
} break;
case MODE_SINCOS: {
sincos_sample_s_ = get_adc_voltage(GPIO_3_GPIO_Port, GPIO_3_Pin) / 3.3f;
sincos_sample_c_ = get_adc_voltage(GPIO_4_GPIO_Port, GPIO_4_Pin) / 3.3f;
} break;
default: {
set_error(ERROR_UNSUPPORTED_ENCODER_MODE);
} break;
}
}
bool Encoder::update() {
// update internal encoder state.
int32_t delta_enc = 0;
@@ -228,7 +250,7 @@ bool Encoder::update() {
case MODE_INCREMENTAL: {
//TODO: use count_in_cpr_ instead as shadow_count_ can overflow
//or use 64 bit
int16_t delta_enc_16 = (int16_t)hw_config_.timer->Instance->CNT - (int16_t)shadow_count_;
int16_t delta_enc_16 = (int16_t)tim_cnt_sample_ - (int16_t)shadow_count_;
delta_enc = (int32_t)delta_enc_16; //sign extend
} break;
@@ -240,10 +262,23 @@ bool Encoder::update() {
if (delta_enc > 3)
delta_enc -= 6;
} else {
set_error(ERROR_ILLEGAL_HALL_STATE);
return false;
if (!config_.ignore_illegal_hall_state) {
set_error(ERROR_ILLEGAL_HALL_STATE);
return false;
}
}
} break;
case MODE_SINCOS: {
float phase = fast_atan2(sincos_sample_s_, sincos_sample_c_);
int fake_count = (int)(1000.0f * phase);
//CPR = 6283 = 2pi * 1k
delta_enc = fake_count - count_in_cpr_;
delta_enc = mod(delta_enc, 6283);
if (delta_enc > 6283/2)
delta_enc -= 6283;
} break;
default: {
set_error(ERROR_UNSUPPORTED_ENCODER_MODE);