From d330f2949830d0d502722beb134150f7f0a3a836 Mon Sep 17 00:00:00 2001 From: HaydenHour <154443692+HaydenHour@users.noreply.github.com> Date: Tue, 4 Mar 2025 01:48:12 -0500 Subject: [PATCH 1/3] Support Pedals as Built-in Devices (#480) * Added paddle support. * Initial Pedal Code (Need to adust numbers) * Adjusted values * #206 fixed variables to more percise values --- arduino/leads_vec_power/Pedal.h | 22 +++++++++++++++++++++ arduino/leads_vec_power/leads_vec_power.ino | 1 + 2 files changed, 23 insertions(+) create mode 100644 arduino/leads_vec_power/Pedal.h diff --git a/arduino/leads_vec_power/Pedal.h b/arduino/leads_vec_power/Pedal.h new file mode 100644 index 00000000..aefb3885 --- /dev/null +++ b/arduino/leads_vec_power/Pedal.h @@ -0,0 +1,22 @@ +const int hallSensorPin = A0; // Analog pin connected to Hall sensor +float restValue = 0.157380254154448; // Variable for rest value +float maxValue = 0.697947214076246; // Variable for max value + +// Function to map ADC value to a range between 0 and 1 +float mapToRange(int adcValue, int adcMaxValue) { + return (float)adcValue / adcMaxValue; +} +void setup() { + Serial.begin(9600); // Initialize serial communication at 9600 bps +} +void loop() { + // Read the ADC value from the Hall sensor + int adcValue = analogRead(hallSensorPin); + // Map the ADC value to a range between 0 and 1 + float pedalValue = (mapToRange(adcValue, 1023) - restValue) / maxValue; + // Print the pedal value to the Serial Monitor + Serial.print("Pedal Value: "); + Serial.println(pedalValue, 2); // Print with 2 decimal places + + delay(500); // Delay for half a second +} \ No newline at end of file diff --git a/arduino/leads_vec_power/leads_vec_power.ino b/arduino/leads_vec_power/leads_vec_power.ino index abb6447d..4b2dc0eb 100644 --- a/arduino/leads_vec_power/leads_vec_power.ino +++ b/arduino/leads_vec_power/leads_vec_power.ino @@ -1,4 +1,5 @@ #include "LEADS.h" +#include "Pedal.h" const int PIN_VOT[] = {A0}; From f76ec08528a1936cf458e4596cdded45c701236a Mon Sep 17 00:00:00 2001 From: ATATC Date: Tue, 4 Mar 2025 01:49:29 -0500 Subject: [PATCH 2/3] Wrapped as a LEADS device. (#206) --- arduino/leads_vec_power/Pedal.cpp | 10 ++++++++ arduino/leads_vec_power/Pedal.h | 38 ++++++++++++++----------------- 2 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 arduino/leads_vec_power/Pedal.cpp diff --git a/arduino/leads_vec_power/Pedal.cpp b/arduino/leads_vec_power/Pedal.cpp new file mode 100644 index 00000000..e444cbeb --- /dev/null +++ b/arduino/leads_vec_power/Pedal.cpp @@ -0,0 +1,10 @@ +#include "Pedal.h" + +Pedal::Pedal(const ArrayList &pins, float restValue, float maxValue) : Device(pins), _restValue(restValue), _maxValue(maxValue) {} + +Pedal::initialize(const ArrayList &parentTags) { + Device::initialize(parentTags); + pinMode(_pins[0], INPUT); +} + +float Pedal::read() { return ((float) analogRead(_pins[0]) / 1023 - _restValue) / _maxValue; } diff --git a/arduino/leads_vec_power/Pedal.h b/arduino/leads_vec_power/Pedal.h index aefb3885..e9456605 100644 --- a/arduino/leads_vec_power/Pedal.h +++ b/arduino/leads_vec_power/Pedal.h @@ -1,22 +1,18 @@ -const int hallSensorPin = A0; // Analog pin connected to Hall sensor -float restValue = 0.157380254154448; // Variable for rest value -float maxValue = 0.697947214076246; // Variable for max value +#ifndef PEDAL_H +#define PEDAL_H -// Function to map ADC value to a range between 0 and 1 -float mapToRange(int adcValue, int adcMaxValue) { - return (float)adcValue / adcMaxValue; -} -void setup() { - Serial.begin(9600); // Initialize serial communication at 9600 bps -} -void loop() { - // Read the ADC value from the Hall sensor - int adcValue = analogRead(hallSensorPin); - // Map the ADC value to a range between 0 and 1 - float pedalValue = (mapToRange(adcValue, 1023) - restValue) / maxValue; - // Print the pedal value to the Serial Monitor - Serial.print("Pedal Value: "); - Serial.println(pedalValue, 2); // Print with 2 decimal places - - delay(500); // Delay for half a second -} \ No newline at end of file + +#include "Device.h" + +class Pedal : public Device { +protected: + float _restValue, _maxValue; + +public: + VoltageSensor(const ArrayList &pins, float restValue, float maxValue); + void initialize(const ArrayList &parentTags) override; + float read() override; +}; + + +#endif // PEDAL_H \ No newline at end of file From 2228446eb078c2f99af76dee3d480b736a96eede Mon Sep 17 00:00:00 2001 From: ATATC Date: Tue, 4 Mar 2025 02:02:11 -0500 Subject: [PATCH 3/3] Added the corresponding Python binding. (#206) --- leads_arduino/__init__.py | 1 + leads_arduino/pedal.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 leads_arduino/pedal.py diff --git a/leads_arduino/__init__.py b/leads_arduino/__init__.py index fedf6cbe..c26b661f 100644 --- a/leads_arduino/__init__.py +++ b/leads_arduino/__init__.py @@ -7,5 +7,6 @@ from leads_arduino.arduino_proto import * from leads_arduino.arduino_nano import * from leads_arduino.arduino_micro import * +from leads_arduino.pedal import * from leads_arduino.voltage_sensor import * from leads_arduino.wheel_speed_sensor import * diff --git a/leads_arduino/pedal.py b/leads_arduino/pedal.py new file mode 100644 index 00000000..dc1186ea --- /dev/null +++ b/leads_arduino/pedal.py @@ -0,0 +1,27 @@ +from typing import override as _override + +from leads import Device as _Device + + +class Pedal(_Device): + """ + See LEADS-Arduino. + + Supports: + - Any analog pedal + """ + + def __init__(self) -> None: + super().__init__() + self._position: float = 0 + + @_override + def update(self, data: str) -> None: + self._position = float(data) + + @_override + def read(self) -> float: + """ + :return: pedal position that ranges from 0 to 1 + """ + return self._position