LEADS icon indicating copy to clipboard operation
LEADS copied to clipboard

Support Pedals as Built-in Devices

Open ATATC opened this issue 1 year ago • 9 comments

As we've discussed, over the summer, @HaydenHour is assigned to add support for pedals.

ATATC avatar Jun 02 '24 17:06 ATATC

@HaydenHour see #207 for test plan details of analog sensors - it should provide a framework for functional testing of your summer task. We can discuss further at our meeting on Monday.

qmascarenhas avatar Jun 04 '24 17:06 qmascarenhas

@HaydenHour Hi Hayden, how it is going?

ATATC avatar Jun 26 '24 04:06 ATATC

@ATATC Hi Terry, its going all right so far. I've decided to do a couple small projects to acclimate myself to using C. In a bit I am planning to do some stuff with the micro Arduino, and eventually beginning to complete the support for pedals as we discussed

HaydenHour avatar Jun 28 '24 18:06 HaydenHour

@ATATC I have mostly completed the initial pedal. The video is blurry, but a value between 0-1 is being outputted based on the throttles position. I just want to ticker a bit with the code to make it a bit better. As expected, there are a couple variables in the code that are dependent on certain calibrations (i.e. the initial non 0 rest value being returned).

https://github.com/user-attachments/assets/0743f43b-9bf5-4157-8721-1311dfbb102a

HaydenHour avatar Jul 26 '24 17:07 HaydenHour

Great work.

Now you may want to wrap the code in LEADS framework as I described in the meeting earlier.

As an example, this is the voltage sensor's code:

#ifndef VOLTAGESENSOR_H
#define VOLTAGESENSOR_H


#include "Device.h"

class VoltageSensor : public Device<float> {
protected:
    float _factor;
public:
    VoltageSensor(float r1, float r2, int *const pins);
    void initialize();
    float read();
};


#endif //VOLTAGESENSOR_H
#include "VoltageSensor.h"

VoltageSensor::VoltageSensor(float r1, float r2, int *const pins) : _factor((r1 + r2) / r2), Device<float>(pins) {}

void VoltageSensor::initialize() { pinMode(_pins[0], INPUT); }

float VoltageSensor::read() { return (float) analogRead(_pins[0]) * _factor * 5 / 1023; }

You can download the library here: https://leads-docs.projectneura.org/en/latest/arduino/index.html.

In this example, the dependent variables are r1 and r2 in the constructor.

Here's a detailed setup for development:

  1. Create a file named "Pedal.h" under the same directory where your .ino file is located
  2. Wrap that file with this:
    #ifndef PEDAL_H
    #define PEDAL_H
    
    
    #include "Device.h"
    
    // your code...
    
    
    #endif //PEDAL_H
    
  3. Create a file named "Pedal.cpp" under the same directory where your .ino file is located and implement the methods declared in the .h file
  4. In your .ino file, import your .h file and instantiate a Pedal object like this:
    #include "LEADS.h"
    #include "Pedal.h"
    
    Pedal TPD(...);
    
    void setup() {
        Serial.begin(9600);
        TPD.initialize();
    }
    
    void loop() {
        returnFloat(THROTTLE_PEDAL, TPD.read());
        delay(100);
    }
    

ATATC avatar Jul 26 '24 17:07 ATATC

Sorry for the confusion, we are recently preparing for a massive launch of breaking changes to our Arduino Library. A corresponding revision will be released then. The newer version shall prevail. You may wait until then.

ATATC avatar Jul 28 '24 11:07 ATATC

@qmascarenhas @HaydenHour

Guide to the Next Step

Please read this page first: https://leads-docs.projectneura.org/en/latest/arduino/sketch.html. Your Pedal.h should look like this:

#ifndef PEDAL_H
#define PEDAL_H


#include "Device.h"

class Pedal : public Device<float> {
protected:
    float _restValue, _maxValue;

public:
    VoltageSensor(const ArrayList<int> &pins, float restValue, float maxValue);
    void initialize(const ArrayList<String> &parentTags) override;
    float read() override;
};


#endif // PEDAL_H

From there, these methods can be implemented in Pedal.cpp. This is a good example: https://github.com/ProjectNeura/LEADS-Arduino/blob/main/src/VoltageSensor.cpp.

ATATC avatar Jul 29 '24 13:07 ATATC

@ATATC Hi Terry, sorry I haven't responded sooner, I have been a bit busy with family. Just to avoid confusion, should I reconfigure my code to resemble what you have shown, or is this something that should be done in a separate file?

HaydenHour avatar Aug 03 '24 23:08 HaydenHour

@HaydenHour Basically, your project structure should be like this:

LEADS
-arduino
--leads_vec_power # open your Arduino IDE here
---leads_vec_power.ino
---Pedal.h
---Pedal.cpp

Your current code belongs to a .ino file because it has setup() and loop(). You will need to move the key lines that read the input to what I mentioned in Guide to the Next Step.

ATATC avatar Aug 04 '24 04:08 ATATC

@qmascarenhas @HaydenHour how are we doing on this?

ATATC avatar Dec 02 '24 23:12 ATATC