Skip to content

Commit e16c745

Browse files
authored
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
1 parent 2d65174 commit e16c745

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

components/as5600/include/as5600.hpp

Lines changed: 26 additions & 21 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,19 +176,25 @@ 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_);
184+
// measure update timing
185+
uint64_t now_us = esp_timer_get_time();
186+
auto dt = now_us - prev_time_us_;
187+
float seconds = dt / 1e6f;
188+
prev_time_us_ = now_us;
189+
// store the previous count
190+
int prev_count = count_.load();
180191
// update raw count
181-
auto count = read_count(ec);
192+
read(ec);
182193
if (ec) {
183194
return;
184195
}
185-
count_.store(count);
186196
// compute diff
187-
int diff = count_ - prev_count_;
188-
// update prev_count
189-
prev_count_ = count_;
197+
int diff = count_ - prev_count;
190198
// check for zero crossing
191199
if (diff > COUNTS_PER_REVOLUTION / 2) {
192200
// we crossed zero going clockwise (1 -> 359)
@@ -199,17 +207,15 @@ class As5600 : public BasePeripheral<> {
199207
accumulator_ += diff;
200208
logger_.debug("CDA: {}, {}, {}", count_, diff, accumulator_);
201209
// update velocity (filtering it)
202-
auto now = std::chrono::high_resolution_clock::now();
203-
float elapsed = std::chrono::duration<float>(now - prev_time_).count();
204-
prev_time_ = now;
205-
float seconds = elapsed ? elapsed : update_period_.count();
206-
float raw_velocity = (float)(diff) / COUNTS_PER_REVOLUTION_F / seconds * SECONDS_PER_MINUTE;
210+
float raw_velocity = (dt > 0) ? (float)(diff) / COUNTS_PER_REVOLUTION_F / seconds * SECONDS_PER_MINUTE : 0.0f;
207211
velocity_rpm_ = velocity_filter_ ? velocity_filter_(raw_velocity) : raw_velocity;
208-
static float max_velocity = 0.5f / update_period_.count() * SECONDS_PER_MINUTE;
209-
if (raw_velocity >= max_velocity) {
210-
logger_.warn("Velocity nearing measurement limit ({:.3f} RPM), consider decreasing your "
211-
"update period!",
212-
max_velocity);
212+
if (dt > 0) {
213+
float max_velocity = 0.5f / seconds * SECONDS_PER_MINUTE;
214+
if (raw_velocity >= max_velocity) {
215+
logger_.warn("Velocity nearing measurement limit ({:.3f} RPM), consider decreasing your "
216+
"update period!",
217+
max_velocity);
218+
}
213219
}
214220
}
215221

@@ -232,12 +238,13 @@ class As5600 : public BasePeripheral<> {
232238
void init(std::error_code &ec) {
233239
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
234240
// initialize the accumulator to have the current angle
235-
auto count = read_count(ec);
236-
prev_count_ = count;
241+
read_count(ec);
237242
if (ec) {
238243
return;
239244
}
240-
accumulator_ = count;
245+
accumulator_ = count_.load();
246+
// initialize timing
247+
prev_time_us_ = esp_timer_get_time();
241248
// start the task
242249
using namespace std::placeholders;
243250
task_ = Task::make_unique({
@@ -297,8 +304,6 @@ class As5600 : public BasePeripheral<> {
297304
std::atomic<int> accumulator_{0};
298305
std::atomic<float> velocity_rpm_{0};
299306
std::unique_ptr<Task> task_;
300-
// Instance-specific variables (not static) to support multiple AS5600 sensors
301-
int prev_count_{0};
302-
std::chrono::high_resolution_clock::time_point prev_time_{std::chrono::high_resolution_clock::now()};
307+
uint64_t prev_time_us_{0};
303308
};
304309
} // namespace espp

0 commit comments

Comments
 (0)