Skip to content

Commit

Permalink
update throttles in sync with ADC conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
mck1117 committed Aug 7, 2024
1 parent 47d0ce1 commit 7b4fc13
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 29 deletions.
27 changes: 1 addition & 26 deletions firmware/controllers/actuators/electronic_throttle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

static pedal2tps_t pedal2tpsMap;

constexpr float etbPeriodSeconds = 1.0f / ETB_LOOP_FREQUENCY;
static constexpr float etbPeriodSeconds = 1.0f / SLOW_ADC_RATE;

static bool startupPositionError = false;

Expand Down Expand Up @@ -754,23 +754,6 @@ static EtbImpl<EtbController2> etb2;
static_assert(ETB_COUNT == 2);
static EtbController* etbControllers[] = { &etb1, &etb2 };

#if !EFI_UNIT_TEST

struct DcThread final : public PeriodicController<512> {
DcThread() : PeriodicController("DC", PRIO_ETB, ETB_LOOP_FREQUENCY) {}

void PeriodicTask(efitick_t) override {
// Simply update all controllers
for (int i = 0 ; i < ETB_COUNT; i++) {
etbControllers[i]->update();
}
}
};

static DcThread dcThread CCM_OPTIONAL;

#endif // EFI_UNIT_TEST

void etbPidReset() {
for (int i = 0 ; i < ETB_COUNT; i++) {
if (auto controller = engine->etbControllers[i]) {
Expand Down Expand Up @@ -957,14 +940,6 @@ void doInitElectronicThrottle() {
startupPositionError = true;
}
#endif /* EFI_UNIT_TEST */

#if !EFI_UNIT_TEST
static bool started = false;
if (started == false) {
dcThread.start();
started = true;
}
#endif
}

void initElectronicThrottle() {
Expand Down
1 change: 0 additions & 1 deletion firmware/controllers/actuators/electronic_throttle_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* it's locked to 500hz, along with the ADC.
* https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem
*/
#define ETB_LOOP_FREQUENCY 500
#define DEFAULT_ETB_PWM_FREQUENCY 800

class EtbController : public IEtbController, public electronic_throttle_s {
Expand Down
1 change: 0 additions & 1 deletion firmware/controllers/thread_priority.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

// ADC and ETB get highest priority - not much else actually runs the engine
#define PRIO_ADC (NORMALPRIO + 10)
#define PRIO_ETB (NORMALPRIO + 9)

// GPIO chips should be fast and go right back to sleep, plus can be timing sensitive
#define PRIO_GPIOCHIP (NORMALPRIO + 8)
Expand Down
10 changes: 9 additions & 1 deletion firmware/hw_layer/adc/adc_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ float __attribute__((weak)) getAnalogInputDividerCoefficient(adc_channel_e) {
#include "mpu_util.h"
#include "periodic_thread_controller.h"
#include "protected_gpio.h"
#include "electronic_throttle.h"

/* Depth of the conversion buffer, channels are sampled X times each.*/
#ifndef ADC_BUF_DEPTH_FAST
Expand Down Expand Up @@ -277,7 +278,7 @@ int getSlowAdcCounter() {
}


class SlowAdcController : public PeriodicController<256> {
class SlowAdcController : public PeriodicController<512> {
public:
SlowAdcController()
: PeriodicController("ADC", PRIO_ADC, SLOW_ADC_RATE)
Expand Down Expand Up @@ -305,6 +306,13 @@ class SlowAdcController : public PeriodicController<256> {

protectedGpio_check(nowNt);
}

{
// Update throttle control in sync with ADC
for (int i = 0; i < ETB_COUNT; i++) {
engine->etbControllers[i]->update();
}
}
}
};

Expand Down

0 comments on commit 7b4fc13

Please sign in to comment.