Skip to content

Commit 4d9a7ec

Browse files
authored
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
1 parent c7e68b2 commit 4d9a7ec

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

components/as5600/include/as5600.hpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class As5600 : public BasePeripheral<> {
9595
*/
9696
void initialize(std::error_code &ec) { init(ec); }
9797

98+
99+
98100
/**
99101
* @brief Return whether the sensor has found absolute 0 yet.
100102
* @note The AS5600 (using I2C/SPI) does not need to search for absolute 0
@@ -174,6 +176,8 @@ class As5600 : public BasePeripheral<> {
174176
return (int)((angle_h << 6) | angle_l);
175177
}
176178

179+
180+
177181
void update(std::error_code &ec) {
178182
logger_.info("update");
179183
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
@@ -183,11 +187,10 @@ class As5600 : public BasePeripheral<> {
183187
return;
184188
}
185189
count_.store(count);
186-
static int prev_count = count_;
187-
// compute diff
188-
int diff = count_ - prev_count;
190+
// compute diff
191+
int diff = count_ - prev_count_;
189192
// update prev_count
190-
prev_count = count_;
193+
prev_count_ = count_;
191194
// check for zero crossing
192195
if (diff > COUNTS_PER_REVOLUTION / 2) {
193196
// we crossed zero going clockwise (1 -> 359)
@@ -199,11 +202,10 @@ class As5600 : public BasePeripheral<> {
199202
// update accumulator
200203
accumulator_ += diff;
201204
logger_.debug("CDA: {}, {}, {}", count_, diff, accumulator_);
202-
// update velocity (filtering it)
203-
static auto prev_time = std::chrono::high_resolution_clock::now();
205+
// update velocity (filtering it)
204206
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 elapsed = std::chrono::duration<float>(now - prev_time_).count();
208+
prev_time_ = now;
207209
float seconds = elapsed ? elapsed : update_period_.count();
208210
float raw_velocity = (float)(diff) / COUNTS_PER_REVOLUTION_F / seconds * SECONDS_PER_MINUTE;
209211
velocity_rpm_ = velocity_filter_ ? velocity_filter_(raw_velocity) : raw_velocity;
@@ -298,5 +300,8 @@ class As5600 : public BasePeripheral<> {
298300
std::atomic<int> accumulator_{0};
299301
std::atomic<float> velocity_rpm_{0};
300302
std::unique_ptr<Task> task_;
303+
// Instance-specific variables (not static) to support multiple AS5600 sensors
304+
int prev_count_{0};
305+
std::chrono::high_resolution_clock::time_point prev_time_{std::chrono::high_resolution_clock::now()};
301306
};
302307
} // namespace espp

0 commit comments

Comments
 (0)