rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
vr_pwm.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#include "vr_pwm.h"
4
5static OutputPin pins[VR_THRESHOLD_COUNT];
6static SimplePwm pwms[VR_THRESHOLD_COUNT];
7
8// Default to 3.3v if not defined, most boards wire the VR threshold input directly to an MCU pin.
9#ifndef VR_SUPPLY_VOLTAGE
10#define VR_SUPPLY_VOLTAGE 3.3f
11#endif
12
13static void updateVrThresholdPwm(float rpm, size_t index) {
14 auto& cfg = engineConfiguration->vrThreshold[index];
15
16 if (!isBrainPinValid(cfg.pin)) {
17 return;
18 }
19
20 float thresholdVoltage = interpolate2d(rpm, cfg.rpmBins, cfg.values);
21
22 // 0v threshold voltage = 3.3v output from mcu = 100% duty
23 // 2.5v threshold voltage = 0v output from mcu = 0% duty
24 float thresholdInputVoltage = interpolateClamped(0, 3.3f, 2.5f, 0, thresholdVoltage);
25
26 float duty = thresholdInputVoltage / VR_SUPPLY_VOLTAGE;
27
29}
30
33
34 for (size_t i = 0; i < efi::size(engineConfiguration->vrThreshold); i++) {
36 }
37}
38
40 for (size_t i = 0; i < efi::size(engineConfiguration->vrThreshold); i++) {
41 auto& cfg = engineConfiguration->vrThreshold[i];
42
43 if (!isBrainPinValid(cfg.pin)) {
44 continue;
45 }
46
47 startSimplePwmHard(&pwms[i], "VR Threshold",
49 cfg.pin,
50 &pins[i],
51 10000, // it's guaranteed to be hardware PWM, the faster the PWM, the less noise makes it through
52 0
53 );
54 }
55}
56
58 for (int i = 0;i<VR_THRESHOLD_COUNT;i++) {
61 }
62}
SingleTimerExecutor scheduler
Definition engine.h:271
Single output pin reference and state.
Definition efi_output.h:49
static float getOrZero(SensorType type)
Definition sensor.h:83
void setSimplePwmDutyCycle(float dutyCycle) override
float interpolateClamped(float x1, float y1, float x2, float y2, float x)
static EngineAccessor engine
Definition engine.h:413
static constexpr engine_configuration_s * engineConfiguration
bool isBrainPinValid(brain_pin_e brainPin)
void startSimplePwmHard(SimplePwm *state, const char *msg, Scheduler *executor, brain_pin_e brainPin, OutputPin *output, float frequency, float dutyCycle)
void setRpmTableBin(TValue(&array)[TSize])
void setLinearCurve(TValue(&array)[TSize], float from, float to, float precision=0.01f)
static float duty
void setDefaultVrThresholds()
Definition vr_pwm.cpp:57
static OutputPin pins[VR_THRESHOLD_COUNT]
Definition vr_pwm.cpp:5
void initVrThresholdPwm()
Definition vr_pwm.cpp:39
static SimplePwm pwms[VR_THRESHOLD_COUNT]
Definition vr_pwm.cpp:6
void updateVrThresholdPwm()
Definition vr_pwm.cpp:31