Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions components/as5600/include/as5600.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,10 @@ class As5600 : public BasePeripheral<> {
return;
}
count_.store(count);
static int prev_count = count_;
// compute diff
int diff = count_ - prev_count;
// compute diff
int diff = count_ - prev_count_;
// update prev_count
prev_count = count_;
prev_count_ = count_;
// check for zero crossing
if (diff > COUNTS_PER_REVOLUTION / 2) {
// we crossed zero going clockwise (1 -> 359)
Expand All @@ -199,11 +198,10 @@ class As5600 : public BasePeripheral<> {
// update accumulator
accumulator_ += diff;
logger_.debug("CDA: {}, {}, {}", count_, diff, accumulator_);
// update velocity (filtering it)
static auto prev_time = std::chrono::high_resolution_clock::now();
// update velocity (filtering it)
auto now = std::chrono::high_resolution_clock::now();
float elapsed = std::chrono::duration<float>(now - prev_time).count();
prev_time = now;
float elapsed = std::chrono::duration<float>(now - prev_time_).count();
prev_time_ = now;
float seconds = elapsed ? elapsed : update_period_.count();
float raw_velocity = (float)(diff) / COUNTS_PER_REVOLUTION_F / seconds * SECONDS_PER_MINUTE;
velocity_rpm_ = velocity_filter_ ? velocity_filter_(raw_velocity) : raw_velocity;
Expand Down Expand Up @@ -235,6 +233,7 @@ class As5600 : public BasePeripheral<> {
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
// initialize the accumulator to have the current angle
auto count = read_count(ec);
prev_count_ = count;
if (ec) {
return;
}
Expand Down Expand Up @@ -298,5 +297,8 @@ class As5600 : public BasePeripheral<> {
std::atomic<int> accumulator_{0};
std::atomic<float> velocity_rpm_{0};
std::unique_ptr<Task> task_;
// Instance-specific variables (not static) to support multiple AS5600 sensors
int prev_count_{0};
std::chrono::high_resolution_clock::time_point prev_time_{std::chrono::high_resolution_clock::now()};
};
} // namespace espp
Loading