From e39754ba105213c133407e2dacb5fd31f3873e4c Mon Sep 17 00:00:00 2001 From: Robin Howard Date: Fri, 19 Apr 2024 16:00:57 +1000 Subject: Fix DRV2065L register usage: we were accidentally setting control2 when we meant control3. --- src/drivers/haptics.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/drivers/haptics.cpp') diff --git a/src/drivers/haptics.cpp b/src/drivers/haptics.cpp index 46136e62..ccc9e599 100644 --- a/src/drivers/haptics.cpp +++ b/src/drivers/haptics.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "assert.h" #include "driver/gpio.h" -- cgit v1.2.3 From 7c075cf5b776feaed2065f936dff0c176635b89d Mon Sep 17 00:00:00 2001 From: Robin Howard Date: Fri, 19 Apr 2024 16:03:35 +1000 Subject: Adds LRA haptic support (open-loop only for now). --- src/drivers/haptics.cpp | 67 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 18 deletions(-) (limited to 'src/drivers/haptics.cpp') diff --git a/src/drivers/haptics.cpp b/src/drivers/haptics.cpp index ccc9e599..f7b18086 100644 --- a/src/drivers/haptics.cpp +++ b/src/drivers/haptics.cpp @@ -28,24 +28,55 @@ namespace drivers { static constexpr char kTag[] = "haptics"; static constexpr uint8_t kHapticsAddress = 0x5A; -Haptics::Haptics() { +Haptics::Haptics(const std::variant& motor) { PowerUp(); - // Put into ERM Open Loop: - // (§8.5.4.1 Programming for ERM Open-Loop Operation) - // - Turn off N_ERM_LRA first - WriteRegister(Register::kControl1, - static_cast(RegisterDefaults::kControl1) & - (~ControlMask::kNErmLra)); - // - Turn on ERM_OPEN_LOOP - WriteRegister(Register::kControl3, - static_cast(RegisterDefaults::kControl3) | - ControlMask::kErmOpenLoop); - - // Set library - // TODO(robin): try the other libraries and test response. C is marginal, D - // too much? - WriteRegister(Register::kWaveformLibrary, static_cast(kDefaultLibrary)); + // TODO: Break out into helper functions + // TODO: Use LRA closed loop instead, loading in calibration data from NVS, or + // running calibration + storing the result first if necessary. + + if (std::holds_alternative(motor)) { + ESP_LOGI(kTag, "Setting up ERM motor..."); + + // Put into ERM Open Loop: + // (§8.5.4.1 Programming for ERM Open-Loop Operation) + + // - Turn off N_ERM_LRA first + WriteRegister(Register::kFeedbackControl, + static_cast(RegisterDefaults::kFeedbackControl) & + (~ControlMask::kNErmLra)); + + // - Turn on ERM_OPEN_LOOP + WriteRegister(Register::kControl3, + static_cast(RegisterDefaults::kControl3) | + ControlMask::kErmOpenLoop); + + // Set library + // TODO(robin): try the other libraries and test response. C is marginal, D + // too much? + WriteRegister(Register::kWaveformLibrary, static_cast(kDefaultErmLibrary)); + + } else if (std::holds_alternative(motor)) { + ESP_LOGI(kTag, "Setting up LRA motor..."); + // TODO: + // auto lraInit = std::get(motor); + + // Put into LRA Open Loop: + // (§8.5.4.1 Programming for LRA Open-Loop Operation) + + // - Turn on N_ERM_LRA first + WriteRegister(Register::kFeedbackControl, + static_cast(RegisterDefaults::kFeedbackControl) & + (ControlMask::kNErmLra)); + + // - Turn on LRA_OPEN_LOOP + WriteRegister(Register::kControl3, + static_cast(RegisterDefaults::kControl3) | + ControlMask::kLraOpenLoop); + + // Set library; only option is the LRA one for, well, LRA motors. + WriteRegister(Register::kWaveformLibrary, static_cast(Library::LRA)); + } // Set mode (internal trigger, on writing 1 to Go register) WriteRegister(Register::kMode, static_cast(Mode::kInternalTrigger)); @@ -94,13 +125,13 @@ auto Haptics::SetWaveformEffect(Effect effect) -> void { auto Haptics::TourEffects() -> void { - TourEffects(Effect::kFirst, Effect::kLast, kDefaultLibrary); + TourEffects(Effect::kFirst, Effect::kLast, kDefaultErmLibrary); } auto Haptics::TourEffects(Library lib) -> void { TourEffects(Effect::kFirst, Effect::kLast, lib); } auto Haptics::TourEffects(Effect from, Effect to) -> void { - TourEffects(from, to, kDefaultLibrary); + TourEffects(from, to, kDefaultErmLibrary); } auto Haptics::TourEffects(Effect from, Effect to, Library lib) -> void { ESP_LOGI(kTag, "With library #%u...", static_cast(lib)); -- cgit v1.2.3