|
|
|
@@ -110,7 +110,7 @@ void Encoder::set_linear_count(int32_t count) {
|
|
|
|
|
|
|
|
|
|
// Update states
|
|
|
|
|
shadow_count_ = count;
|
|
|
|
|
pos_estimate_ = static_cast<float>(count);
|
|
|
|
|
pos_estimate_ = (float)count;
|
|
|
|
|
tim_cnt_sample_ = count;
|
|
|
|
|
|
|
|
|
|
//Write hardware last
|
|
|
|
@@ -132,7 +132,7 @@ void Encoder::set_circular_count(int32_t count, bool update_offset) {
|
|
|
|
|
|
|
|
|
|
// Update states
|
|
|
|
|
count_in_cpr_ = mod(count, config_.cpr);
|
|
|
|
|
pos_cpr_ = static_cast<float>(count_in_cpr_);
|
|
|
|
|
pos_cpr_ = (float)count_in_cpr_;
|
|
|
|
|
|
|
|
|
|
cpu_exit_critical(prim);
|
|
|
|
|
}
|
|
|
|
@@ -182,7 +182,7 @@ bool Encoder::run_direction_find() {
|
|
|
|
|
// TODO: Do the scan with current, not voltage!
|
|
|
|
|
bool Encoder::run_offset_calibration() {
|
|
|
|
|
static const float start_lock_duration = 1.0f;
|
|
|
|
|
static const int num_steps = (int)(config_.calib_scan_distance / config_.calib_scan_omega * static_cast<float>(current_meas_hz));
|
|
|
|
|
static const int num_steps = (int)(config_.calib_scan_distance / config_.calib_scan_omega * (float)current_meas_hz);
|
|
|
|
|
|
|
|
|
|
// Require index found if enabled
|
|
|
|
|
if (config_.use_index && !index_found_) {
|
|
|
|
@@ -219,7 +219,7 @@ bool Encoder::run_offset_calibration() {
|
|
|
|
|
// scan forward
|
|
|
|
|
i = 0;
|
|
|
|
|
axis_->run_control_loop([&]() {
|
|
|
|
|
float phase = wrap_pm_pi(config_.calib_scan_distance * static_cast<float>(i) / static_cast<float>(num_steps) - config_.calib_scan_distance / 2.0f);
|
|
|
|
|
float phase = wrap_pm_pi(config_.calib_scan_distance * (float)i / (float)num_steps - config_.calib_scan_distance / 2.0f);
|
|
|
|
|
float v_alpha = voltage_magnitude * our_arm_cos_f32(phase);
|
|
|
|
|
float v_beta = voltage_magnitude * our_arm_sin_f32(phase);
|
|
|
|
|
if (!axis_->motor_.enqueue_voltage_timings(v_alpha, v_beta))
|
|
|
|
@@ -248,7 +248,7 @@ bool Encoder::run_offset_calibration() {
|
|
|
|
|
|
|
|
|
|
//TODO avoid recomputing elec_rad_per_enc every time
|
|
|
|
|
// Check CPR
|
|
|
|
|
float elec_rad_per_enc = axis_->motor_.config_.pole_pairs * 2 * M_PI * (1.0f / static_cast<float>(config_.cpr));
|
|
|
|
|
float elec_rad_per_enc = axis_->motor_.config_.pole_pairs * 2 * M_PI * (1.0f / (float)(config_.cpr));
|
|
|
|
|
float expected_encoder_delta = config_.calib_scan_distance / elec_rad_per_enc;
|
|
|
|
|
calib_scan_response_ = std::abs(shadow_count_ - init_enc_val);
|
|
|
|
|
if (std::abs(calib_scan_response_ - expected_encoder_delta) / expected_encoder_delta > config_.calib_range) {
|
|
|
|
@@ -259,7 +259,7 @@ bool Encoder::run_offset_calibration() {
|
|
|
|
|
// scan backwards
|
|
|
|
|
i = 0;
|
|
|
|
|
axis_->run_control_loop([&]() {
|
|
|
|
|
float phase = wrap_pm_pi(-config_.calib_scan_distance * static_cast<float>(i) / static_cast<float>(num_steps) + config_.calib_scan_distance / 2.0f);
|
|
|
|
|
float phase = wrap_pm_pi(-config_.calib_scan_distance * (float)i / (float)num_steps + config_.calib_scan_distance / 2.0f);
|
|
|
|
|
float v_alpha = voltage_magnitude * our_arm_cos_f32(phase);
|
|
|
|
|
float v_beta = voltage_magnitude * our_arm_sin_f32(phase);
|
|
|
|
|
if (!axis_->motor_.enqueue_voltage_timings(v_alpha, v_beta))
|
|
|
|
@@ -275,7 +275,7 @@ bool Encoder::run_offset_calibration() {
|
|
|
|
|
|
|
|
|
|
config_.offset = encvaluesum / (num_steps * 2);
|
|
|
|
|
int32_t residual = encvaluesum - ((int64_t)config_.offset * (int64_t)(num_steps * 2));
|
|
|
|
|
config_.offset_float = static_cast<float>(residual) / static_cast<float>(num_steps * 2) + 0.5f; // add 0.5 to center-align state to phase
|
|
|
|
|
config_.offset_float = (float)residual / (float)(num_steps * 2) + 0.5f; // add 0.5 to center-align state to phase
|
|
|
|
|
|
|
|
|
|
is_ready_ = true;
|
|
|
|
|
return true;
|
|
|
|
@@ -508,13 +508,13 @@ bool Encoder::update() {
|
|
|
|
|
pos_estimate_ += current_meas_period * vel_estimate_;
|
|
|
|
|
pos_cpr_ += current_meas_period * vel_estimate_;
|
|
|
|
|
// discrete phase detector
|
|
|
|
|
float delta_pos = static_cast<float>(shadow_count_) - static_cast<int32_t>(std::floor(pos_estimate_));
|
|
|
|
|
float delta_pos_cpr = static_cast<float>(count_in_cpr_) - static_cast<int32_t>(std::floor(pos_cpr_));
|
|
|
|
|
delta_pos_cpr = wrap_pm(delta_pos_cpr, 0.5f * static_cast<float>(config_.cpr));
|
|
|
|
|
float delta_pos = (float)(shadow_count_ - (int32_t)std::floor(pos_estimate_));
|
|
|
|
|
float delta_pos_cpr = (float)(count_in_cpr_ - (int32_t)std::floor(pos_cpr_));
|
|
|
|
|
delta_pos_cpr = wrap_pm(delta_pos_cpr, 0.5f * (float)(config_.cpr));
|
|
|
|
|
// pll feedback
|
|
|
|
|
pos_estimate_ += current_meas_period * pll_kp_ * delta_pos;
|
|
|
|
|
pos_cpr_ += current_meas_period * pll_kp_ * delta_pos_cpr;
|
|
|
|
|
pos_cpr_ = fmodf_pos(pos_cpr_, static_cast<float>(config_.cpr));
|
|
|
|
|
pos_cpr_ = fmodf_pos(pos_cpr_, (float)(config_.cpr));
|
|
|
|
|
vel_estimate_ += current_meas_period * pll_ki_ * delta_pos_cpr;
|
|
|
|
|
bool snap_to_zero_vel = false;
|
|
|
|
|
if (std::abs(vel_estimate_) < 0.5f * current_meas_period * pll_ki_) {
|
|
|
|
@@ -543,7 +543,7 @@ bool Encoder::update() {
|
|
|
|
|
|
|
|
|
|
//// compute electrical phase
|
|
|
|
|
//TODO avoid recomputing elec_rad_per_enc every time
|
|
|
|
|
float elec_rad_per_enc = axis_->motor_.config_.pole_pairs * 2 * M_PI * (1.0f / static_cast<float>(config_.cpr));
|
|
|
|
|
float elec_rad_per_enc = axis_->motor_.config_.pole_pairs * 2 * M_PI * (1.0f / (float)(config_.cpr));
|
|
|
|
|
float ph = elec_rad_per_enc * (interpolated_enc - config_.offset_float);
|
|
|
|
|
// ph = fmodf(ph, 2*M_PI);
|
|
|
|
|
phase_ = wrap_pm_pi(ph);
|
|
|
|
|