Skip to content

Commit

Permalink
test: cover edge-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Feb 9, 2024
1 parent f43319a commit 0fc131a
Show file tree
Hide file tree
Showing 24 changed files with 262 additions and 120 deletions.
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "senseshift.h"

#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactglove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "senseshift.h"

#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactosy2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "senseshift.h"

#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactosyf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "senseshift.h"

#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactosyh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "senseshift.h"

#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactsuit_x16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "senseshift.h"

#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
3 changes: 2 additions & 1 deletion firmware/mode_configs/bhaptics/tactsuit_x16_pca9685.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "senseshift.h"

#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/pca9685.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand All @@ -21,6 +21,7 @@ using namespace SenseShift::Arduino::Output;
using namespace SenseShift::Arduino::Input;
using namespace SenseShift::FreeRTOS::Input;
using namespace SenseShift::Battery;
using namespace SenseShift::Battery::Input;
using namespace SenseShift::BH;
using namespace SenseShift::Body::Haptics;

Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactsuit_x40.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

#include "senseshift.h"

#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/arduino/output/pca9685.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
2 changes: 1 addition & 1 deletion firmware/mode_configs/bhaptics/tactvisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "senseshift.h"

#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/arduino/input/sensor/analog.hpp>
#include <senseshift/arduino/output/ledc.hpp>
#include <senseshift/battery/sensor.hpp>
#include <senseshift/bh/ble/connection.hpp>
#include <senseshift/bh/devices.hpp>
#include <senseshift/bh/encoding.hpp>
Expand Down
2 changes: 1 addition & 1 deletion include/senseshift.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "config/all.h"

#include <senseshift/battery/sensor.hpp>
#include "senseshift/battery/input/battery_sensor.hpp"
#include <senseshift/body/haptics/body.hpp>
#include <senseshift/events.hpp>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,21 @@

#include <cstdint>
#include <limits>
#include <algorithm>

#include <frozen/map.h>
#include "frozen/map.h"

#include <senseshift/events.hpp>
#include "senseshift/events.hpp"

namespace SenseShift::Battery {

namespace VoltageMap {
/// Lookup table for LiPO 1S 4.2V batteries
/// \see <a href="https://blog.ampow.com/lipo-voltage-chart/">Source</a>
[[maybe_unused]] inline static const auto LiPO_1S_42= frozen::make_map<float, float>({
{ 4.2, 1.0 },
{ 4.15, 0.95 },
{ 4.11, 0.9 },
{ 4.08, 0.85 },
{ 4.02, 0.8 },
{ 3.98, 0.75 },
{ 3.95, 0.7 },
{ 3.91, 0.65 },
{ 3.87, 0.6 },
{ 3.85, 0.55 },
{ 3.84, 0.5 },
{ 3.82, 0.45 },
{ 3.8, 0.4 },
{ 3.79, 0.35 },
{ 3.77, 0.3 },
{ 3.75, 0.25 },
{ 3.73, 0.2 },
{ 3.71, 0.15 },
{ 3.69, 0.1 },
{ 3.61, 0.05 },
{ 3.27, 0.0 },
[[maybe_unused]] inline static const auto LiPO_1S_42 = frozen::make_map<float, float>({
{ 4.2, 1.0 }, { 4.15, 0.95 }, { 4.11, 0.9 }, { 4.08, 0.85 }, { 4.02, 0.8 }, { 3.98, 0.75 },
{ 3.95, 0.7 }, { 3.91, 0.65 }, { 3.87, 0.6 }, { 3.85, 0.55 }, { 3.84, 0.5 }, { 3.82, 0.45 },
{ 3.8, 0.4 }, { 3.79, 0.35 }, { 3.77, 0.3 }, { 3.75, 0.25 }, { 3.73, 0.2 }, { 3.71, 0.15 },
{ 3.69, 0.1 }, { 3.61, 0.05 }, { 3.27, 0.0 },
});
} // namespace VoltageMap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

#include <cstdint>

#include "senseshift/battery.hpp"
#include "senseshift/battery/battery.hpp"

#include <senseshift/freertos/task.hpp>
#include <senseshift/input/sensor.hpp>
#include <senseshift/utility.hpp>
#include "senseshift/core/helpers.hpp"
#include "senseshift/input/sensor.hpp"

#ifndef SENSESHIFT_BATTERY_TASK_PRIORITY
#define SENSESHIFT_BATTERY_TASK_PRIORITY 1
#endif

namespace SenseShift::Battery {
namespace SenseShift::Battery::Input {
/// Abstract battery sensor
using IBatterySensor = ::SenseShift::Input::Sensor<BatteryState>;

Expand Down Expand Up @@ -58,4 +53,4 @@ namespace SenseShift::Battery {
VoltageSource* voltage_source_;
Container* lookup_table_;
};
} // namespace SenseShift::Battery
} // namespace SenseShift::Battery::Input
4 changes: 2 additions & 2 deletions lib/bhaptics_ble/senseshift/bh/ble/connection.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <senseshift/battery.hpp>
#include "senseshift/battery/battery.hpp"
#include <senseshift/bh/ble/constants.hpp>
#include <senseshift/bh/constants.hpp>
#include <senseshift/events.hpp>
Expand Down Expand Up @@ -44,7 +44,7 @@ namespace SenseShift::BH::BLE {
void handleEvent(const IEvent* event) const override
{
if (event->eventName == OH_EVENT_BATTERY_LEVEL) {
uint16_t level = simpleMap<uint8_t>(
uint16_t level = remap_simple<std::uint8_t>(
static_cast<const ::SenseShift::Battery::BatteryLevelEvent*>(event)->state.level,
255,
100
Expand Down
3 changes: 1 addition & 2 deletions lib/io/senseshift/input/calibration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ namespace SenseShift::Input::Calibration {
Tp center = (this->range_min_ + this->range_max_) / 2.0F;

// Map the input to the sensor range of motion.
int output =
::SenseShift::accurateMap<Tp>(input, this->output_min_, this->output_max_, 0, this->sensor_max_);
int output = ::SenseShift::remap<Tp>(input, this->output_min_, this->output_max_, 0, this->sensor_max_);

// Find the deviation from the center and clamp it to the maximum that the driver supports.
output = std::clamp<int>(output - center, -(this->driver_max_deviation_), this->driver_max_deviation_);
Expand Down
16 changes: 10 additions & 6 deletions lib/opengloves/opengloves/opengloves.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ namespace og {
template<typename Tf = float>
union InputFingerCurl {
std::array<Tf, 4> curl; // NOLINT(*-magic-numbers): I'm sure our finger aren't changing anytime soon
struct {
union {
Tf curl_total;
Tf curl_joint1;
Tf curl_joint2;
Tf curl_joint3;

struct {
Tf curl_joint0;
Tf curl_joint1;
Tf curl_joint2;
Tf curl_joint3;
};
};
};
using InputFingerCurlData = InputFingerCurl<float>;
Expand Down Expand Up @@ -77,7 +81,7 @@ namespace og {
using InputButtonData = InputButton<bool>;

template<typename Tf = float, typename Tb = bool>
struct InputAnalogButton : InputButton<Tb> {
struct InputAnalogButton : public InputButton<Tb> {
Tf value;
};
using InputAnalogButtonData = InputAnalogButton<float, bool>;
Expand All @@ -93,7 +97,7 @@ namespace og {
InputJoystick<Tf, Tb> joystick;

union {
std::array<InputButton<Tb>, 4> buttons;
std::array<InputButton<Tb>, 5> buttons;
struct {
InputButton<Tb> button_a;
InputButton<Tb> button_b;
Expand Down
21 changes: 0 additions & 21 deletions lib/util/senseshift/range.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion lib/util/senseshift/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#include <type_traits>

#include "senseshift/container.hpp"
#include "senseshift/range.hpp"
#include "senseshift/core/helpers.hpp"
53 changes: 53 additions & 0 deletions test/test_battery/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <senseshift/battery/input/battery_sensor.hpp>
#include <senseshift/input/sensor.hpp>
#include <unity.h>

using namespace SenseShift::Input;
using namespace SenseShift::Battery;
using namespace SenseShift::Battery::Input;

void test_battery_sensor(void)
{
auto* source = new FloatSensor();

auto* battery = new LookupTableInterpolateBatterySensor(source, &VoltageMap::LiPO_1S_42);
battery->init();

source->publishState(0.f);
TEST_ASSERT_EQUAL_INT(0, battery->getValue().level);

source->publishState(4.2f);
TEST_ASSERT_EQUAL_INT(255, battery->getValue().level);

source->publishState(3.7f);
TEST_ASSERT_EQUAL_INT(31, battery->getValue().level);
}

int process(void)
{
UNITY_BEGIN();

RUN_TEST(test_battery_sensor);

return UNITY_END();
}

#ifdef ARDUINO

#include <Arduino.h>

void setup(void)
{
process();
}

void loop(void) {}

#else

int main(int argc, char** argv)
{
return process();
}

#endif
Loading

0 comments on commit 0fc131a

Please sign in to comment.