Skip to content

Commit

Permalink
fix(OpenGloves): tick input sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Feb 10, 2024
1 parent 825e6f0 commit db18a33
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 16 deletions.
3 changes: 2 additions & 1 deletion firmware/mode_configs/opengloves/opengloves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ void setupMode()
auto* pinch = new BUTTON_CLASS(PIN_BUTTON_PINCH, BUTTON_PINCH_INVERT);
#endif

OpenGlovesTrackingComponent::Config const tracking_config(2000, false);
Serial.begin(115200);
OpenGlovesTrackingComponent::Config tracking_config(2000, false);
auto* opengloves_tracking =
new OpenGlovesTrackingComponent(tracking_config, input_sensors, new StreamTransport(Serial));

Check warning on line 159 in firmware/mode_configs/opengloves/opengloves.cpp

View check run for this annotation

Codecov / codecov/patch

firmware/mode_configs/opengloves/opengloves.cpp#L156-L159

Added lines #L156 - L159 were not covered by tests

Expand Down
2 changes: 0 additions & 2 deletions ini/opengloves-lucidgloves.ini
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ build_flags = ${opengloves.build_flags} -D OG_ENCODE_FAST
; -D PIN_BUTTON_GRAB=13 ; unused if GESTURE_GRAB is true
; -D PIN_BUTTON_PINCH=23 ; unused if GESTURE_PINCH is true

; todo: add servo pins

build_unflags = ${opengloves.build_unflags}
build_src_filter = ${opengloves.build_src_filter}
+<mode_configs/opengloves/opengloves.cpp>
Expand Down
6 changes: 4 additions & 2 deletions lib/arduino/senseshift/arduino/input/sensor/digital.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ namespace SenseShift::Arduino::Input {
};

template<>
[[nodiscard]] inline auto DigitalSimpleSensor<false>::getValue() -> bool {
[[nodiscard]] inline auto DigitalSimpleSensor<false>::getValue() -> bool

Check warning on line 21 in lib/arduino/senseshift/arduino/input/sensor/digital.hpp

View check run for this annotation

Codecov / codecov/patch

lib/arduino/senseshift/arduino/input/sensor/digital.hpp#L21

Added line #L21 was not covered by tests
{
return digitalRead(this->pin_) == LOW;

Check warning on line 23 in lib/arduino/senseshift/arduino/input/sensor/digital.hpp

View check run for this annotation

Codecov / codecov/patch

lib/arduino/senseshift/arduino/input/sensor/digital.hpp#L23

Added line #L23 was not covered by tests
}

template<>
[[nodiscard]] inline auto DigitalSimpleSensor<true>::getValue() -> bool {
[[nodiscard]] inline auto DigitalSimpleSensor<true>::getValue() -> bool

Check warning on line 27 in lib/arduino/senseshift/arduino/input/sensor/digital.hpp

View check run for this annotation

Codecov / codecov/patch

lib/arduino/senseshift/arduino/input/sensor/digital.hpp#L27

Added line #L27 was not covered by tests
{
return digitalRead(this->pin_) == HIGH;

Check warning on line 29 in lib/arduino/senseshift/arduino/input/sensor/digital.hpp

View check run for this annotation

Codecov / codecov/patch

lib/arduino/senseshift/arduino/input/sensor/digital.hpp#L29

Added line #L29 was not covered by tests
}
} // namespace SenseShift::Arduino::Input
3 changes: 0 additions & 3 deletions lib/io/senseshift/input/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,6 @@ namespace SenseShift::Input::Filter {
public:
explicit ExponentialMovingAverageFilter(float alpha) : alpha_(alpha){};

template<typename U = Tp, std::enable_if_t<std::is_same_v<U, float>, int> = 0>
explicit ExponentialMovingAverageFilter(float alpha) : alpha_(alpha), acc_(std::nanf){};

auto filter(ISimpleSensor<Tp>* /*sensor*/, Tp value) -> Tp override
{
if (this->is_first_) {
Expand Down
6 changes: 6 additions & 0 deletions lib/io/senseshift/input/sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ namespace SenseShift::Input {

explicit Sensor() = default;

template<typename U = Tp, std::enable_if_t<std::is_same_v<U, float>, int> = 0>
explicit Sensor(float value = 0.0f) : raw_value_(value)
{
this->value_ = this->applyFilters(value);
}

/// Appends a filter to the sensor's filter chain.
///
/// \param filter The filter to add.
Expand Down
2 changes: 1 addition & 1 deletion lib/opengloves/opengloves/opengloves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace og {
length - written,
"%c%u",
'A' + i,
ifloor<std::uint8_t, float>(finger_curl * MAX_ANALOG_VALUE)
ifloor<std::uint16_t, float>(finger_curl * MAX_ANALOG_VALUE)
);
}
#else
Expand Down
5 changes: 5 additions & 0 deletions lib/opengloves/opengloves/opengloves.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ namespace og {

template<typename Tf = float>
union InputFingerCurl {
/// Access the curl as an array.
std::array<Tf, 4> curl; // NOLINT(*-magic-numbers): I'm sure our finger aren't changing anytime soon

union {
/// The total curl of the finger.
/// Only use it if you do not use per-joint tracking.
Tf curl_total;

/// Access the individual curl joints.
struct {
Tf curl_joint0;
Tf curl_joint1;
Expand Down
2 changes: 1 addition & 1 deletion lib/opengloves/senseshift/opengloves/opengloves.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace SenseShift::OpenGloves {

auto collectData() -> og::InputPeripheralData

Check warning on line 76 in lib/opengloves/senseshift/opengloves/opengloves.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves/senseshift/opengloves/opengloves.hpp#L76

Added line #L76 was not covered by tests
{
og::InputPeripheralData data;
og::InputPeripheralData data{};

Check warning on line 78 in lib/opengloves/senseshift/opengloves/opengloves.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves/senseshift/opengloves/opengloves.hpp#L78

Added line #L78 was not covered by tests

const auto& curls = this->curl.fingers;
const auto& splays = this->splay.fingers;
Expand Down
50 changes: 44 additions & 6 deletions lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <Arduino.h>

#include <array>
#include <cstddef>
#include <optional>

Expand All @@ -15,6 +16,11 @@
#include <opengloves/opengloves.hpp>
#include <utility>

#define SS_START_CALIBRATION_NOT_NULL(ptr) \
if ((ptr) != nullptr) { \
(ptr)->startCalibration(); \
}

namespace SenseShift::OpenGloves {
class OpenGlovesTrackingComponent : public SenseShift::Component {
public:
Expand All @@ -32,8 +38,8 @@ namespace SenseShift::OpenGloves {
}
};

OpenGlovesTrackingComponent(Config config, InputSensors& input_sensors, ITransport* communication) :
config(config), input_sensors_(std::move(input_sensors)), communication_(communication)
OpenGlovesTrackingComponent(Config& config, InputSensors& input_sensors, ITransport* communication) :
config_(config), input_sensors_(std::move(input_sensors)), communication_(communication)

Check warning on line 42 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L41-L42

Added lines #L41 - L42 were not covered by tests
{
this->encoder_ = new og::AlphaEncoder();
}

Check warning on line 45 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L44-L45

Added lines #L44 - L45 were not covered by tests
Expand All @@ -42,19 +48,51 @@ namespace SenseShift::OpenGloves {
{
this->communication_->init();
this->input_sensors_.init();

Check warning on line 50 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L49-L50

Added lines #L49 - L50 were not covered by tests

if (this->config_.always_calibrate_) {
this->startCalibration();

Check warning on line 53 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L52-L53

Added lines #L52 - L53 were not covered by tests
}
}

Check warning on line 55 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L55

Added line #L55 was not covered by tests

void tick() override

Check warning on line 57 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L57

Added line #L57 was not covered by tests
{
this->input_sensors_.tick();
const auto data = this->input_sensors_.collectData();
const auto length = this->encoder_->encode_input(data, buffer.data(), buffer.size());
this->communication_->send(buffer.data(), length);
}

Check warning on line 63 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L59-L63

Added lines #L59 - L63 were not covered by tests

protected:
void startCalibration()

Check warning on line 66 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L66

Added line #L66 was not covered by tests
{
for (auto& finger_curl : this->input_sensors_.curl.fingers) {
for (auto& joint_sensor : finger_curl.curl) {
SS_START_CALIBRATION_NOT_NULL(joint_sensor);

Check warning on line 70 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L68-L70

Added lines #L68 - L70 were not covered by tests
}
}

char buffer[256];
this->encoder_->encode_input(data, buffer, sizeof(buffer));
this->communication_->send(buffer, sizeof(buffer));
for (auto& finger_splay : this->input_sensors_.splay.fingers) {
SS_START_CALIBRATION_NOT_NULL(finger_splay);

Check warning on line 75 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L74-L75

Added lines #L74 - L75 were not covered by tests
}

SS_START_CALIBRATION_NOT_NULL(this->input_sensors_.joystick.x);
SS_START_CALIBRATION_NOT_NULL(this->input_sensors_.joystick.y);
SS_START_CALIBRATION_NOT_NULL(this->input_sensors_.joystick.press);

Check warning on line 80 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L78-L80

Added lines #L78 - L80 were not covered by tests

for (auto& button : this->input_sensors_.buttons) {
SS_START_CALIBRATION_NOT_NULL(button.press);

Check warning on line 83 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L82-L83

Added lines #L82 - L83 were not covered by tests
}

for (auto& analog_button : this->input_sensors_.analog_buttons) {
SS_START_CALIBRATION_NOT_NULL(analog_button.press);
SS_START_CALIBRATION_NOT_NULL(analog_button.value);

Check warning on line 88 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L86-L88

Added lines #L86 - L88 were not covered by tests
}
}

Check warning on line 90 in lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp

View check run for this annotation

Codecov / codecov/patch

lib/opengloves_freertos/senseshift/opengloves/opengloves_task.hpp#L90

Added line #L90 was not covered by tests

private:
Config& config;
std::array<char, 256> buffer;

Config& config_;
InputSensors input_sensors_;
ITransport* communication_;
og::IEncoder* encoder_;
Expand Down

0 comments on commit db18a33

Please sign in to comment.