Skip to content

Commit c74c3d0

Browse files
authored
fix(AS5600): convert static variables to instance members (#571)
* fix: convert static variables to instance members to enable supporting sampling multiple sensors Replace static variables `prev_count` and `prev_time` with instance member variables to support multiple AS5600 sensor instances running simultaneously. Previously, all instances would share the same static variables, causing incorrect position and velocity calculations in multi-sensor configurations. - Add `prev_count_` and `prev_time_` as class member variables - Initialize `prev_count_` to 0 and `prev_time_` to current time - Remove static keyword from variables in `update()` method - Ensures each sensor instance maintains independent state * fix(as5600): initialize prev_count_ to prevent incorrect first diff Initialize prev_count_ to the first sensor reading in init() to avoid calculating an incorrect diff on the first update() call. Previously, prev_count_ defaulted to 0 while count_ was set to the actual sensor reading, causing a large erroneous jump in the accumulator on startup. - Set prev_count_ = count after reading initial angle - Ensures diff calculation is correct from the first update - Prevents accumulator corruption on initialization * refactor(as5600): align implementation with MT6701 encoder - Replace std::chrono::high_resolution_clock with esp_timer_get_time() - Convert prev_time_ to uint64_t prev_time_us_ for microsecond precision - Store prev_count as local variable instead of class member * Fix (AS5600): Used read_counts() instead of non-existant read() method and cleaned the code. - Used read_counts() method instead of the wrongly used non-existed read() method - Removed unnecessary trailing white spaces
1 parent c7e68b2 commit c74c3d0

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

components/as5600/include/as5600.hpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,21 @@ class As5600 : public BasePeripheral<> {
177177
void update(std::error_code &ec) {
178178
logger_.info("update");
179179
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
180+
// measure update timing
181+
uint64_t now_us = esp_timer_get_time();
182+
auto dt = now_us - prev_time_us_;
183+
float seconds = dt / 1e6f;
184+
prev_time_us_ = now_us;
185+
// store the previous count
186+
int prev_count = count_.load();
180187
// update raw count
181188
auto count = read_count(ec);
182189
if (ec) {
183190
return;
184191
}
185192
count_.store(count);
186-
static int prev_count = count_;
187193
// compute diff
188194
int diff = count_ - prev_count;
189-
// update prev_count
190-
prev_count = count_;
191195
// check for zero crossing
192196
if (diff > COUNTS_PER_REVOLUTION / 2) {
193197
// we crossed zero going clockwise (1 -> 359)
@@ -200,18 +204,15 @@ class As5600 : public BasePeripheral<> {
200204
accumulator_ += diff;
201205
logger_.debug("CDA: {}, {}, {}", count_, diff, accumulator_);
202206
// update velocity (filtering it)
203-
static auto prev_time = std::chrono::high_resolution_clock::now();
204-
auto now = std::chrono::high_resolution_clock::now();
205-
float elapsed = std::chrono::duration<float>(now - prev_time).count();
206-
prev_time = now;
207-
float seconds = elapsed ? elapsed : update_period_.count();
208-
float raw_velocity = (float)(diff) / COUNTS_PER_REVOLUTION_F / seconds * SECONDS_PER_MINUTE;
207+
float raw_velocity = (dt > 0) ? (float)(diff) / COUNTS_PER_REVOLUTION_F / seconds * SECONDS_PER_MINUTE : 0.0f;
209208
velocity_rpm_ = velocity_filter_ ? velocity_filter_(raw_velocity) : raw_velocity;
210-
static float max_velocity = 0.5f / update_period_.count() * SECONDS_PER_MINUTE;
211-
if (raw_velocity >= max_velocity) {
212-
logger_.warn("Velocity nearing measurement limit ({:.3f} RPM), consider decreasing your "
213-
"update period!",
214-
max_velocity);
209+
if (dt > 0) {
210+
float max_velocity = 0.5f / seconds * SECONDS_PER_MINUTE;
211+
if (raw_velocity >= max_velocity) {
212+
logger_.warn("Velocity nearing measurement limit ({:.3f} RPM), consider decreasing your "
213+
"update period!",
214+
max_velocity);
215+
}
215216
}
216217
}
217218

@@ -234,11 +235,13 @@ class As5600 : public BasePeripheral<> {
234235
void init(std::error_code &ec) {
235236
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
236237
// initialize the accumulator to have the current angle
237-
auto count = read_count(ec);
238+
read_count(ec);
238239
if (ec) {
239240
return;
240241
}
241-
accumulator_ = count;
242+
accumulator_ = count_.load();
243+
// initialize timing
244+
prev_time_us_ = esp_timer_get_time();
242245
// start the task
243246
using namespace std::placeholders;
244247
task_ = Task::make_unique({
@@ -298,5 +301,6 @@ class As5600 : public BasePeripheral<> {
298301
std::atomic<int> accumulator_{0};
299302
std::atomic<float> velocity_rpm_{0};
300303
std::unique_ptr<Task> task_;
304+
uint64_t prev_time_us_{0};
301305
};
302306
} // namespace espp

0 commit comments

Comments
 (0)