Skip to content

Commit

Permalink
Explicit types, avoid float division
Browse files Browse the repository at this point in the history
This shaves off a good amount of CPU cycles here and there.
Since the ESP32 FPU doesn't have division, this is done in software.

GCC doesn't simplify division by a constant into multiplication
without using flags, so multiplication is done explicitly in code
where possible.

Also specifying a floating point value implicitly makes it double
precision, which in many cases caused a sign extension, only for
it to be turned back into single precision before being returned.
  • Loading branch information
GAsplund committed Oct 27, 2023
1 parent d97baa6 commit 2415f77
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/fuel_economy_meter/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// The maximum allowed time between opening and closing the injector
#define INJ_DELTA_MAX 500000
// Amount of time for an injector to have injected 1 liter of fuel
#define INJ_USEC_LITER 53454766.0
#define INJ_USEC_LITER 53454766.0f

// The maximum allowed time between VSS pulses to calculate velocity
#define VSS_DELTA_MAX 5000000

// Amount of VSS pulses to have travelled 1 Km
#define VSS_PULSE_KM 6840.0
#define VSS_PULSE_KM 6840.0f

/*
* Bluetooth parameters
Expand Down
24 changes: 12 additions & 12 deletions src/fuel_economy_meter/trip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ void Trip::begin(void)
*/
void Trip::injChange()
{
uint64_t changeTimestamp = esp_timer_get_time();
if (gpio_get_level(INJ_GPIO) == 0)
{
uint64_t openTimestamp = esp_timer_get_time();
if (this->injOpenTimestamp > 0)
this->latestInjectionPeriod = openTimestamp - this->injOpenTimestamp;
this->injOpenTimestamp = openTimestamp;
this->latestInjectionPeriod = changeTimestamp - this->injOpenTimestamp;
this->injOpenTimestamp = changeTimestamp;
this->totalInjectionPulses += 1;
}
else
{
uint64_t delta = esp_timer_get_time() - this->injOpenTimestamp;
uint32_t delta = changeTimestamp - this->injOpenTimestamp;

if (delta < INJ_DELTA_MAX)
{
Expand Down Expand Up @@ -83,11 +83,11 @@ void IRAM_ATTR Trip::updateTripInjISR(void*)
void Trip::vssPulse()
{
uint64_t pulseTimestamp = esp_timer_get_time();
uint64_t delta = pulseTimestamp - this->latestVssTimestamp;
uint32_t delta = pulseTimestamp - this->latestVssTimestamp;
if (delta < VSS_DELTA_MAX)
this->latestVssPeriod = delta;
this->totalVssPulses += 1;
this->latestVssTimestamp = esp_timer_get_time();
this->latestVssTimestamp = pulseTimestamp;
}

void IRAM_ATTR Trip::updateTripVssISR(void*)
Expand All @@ -110,12 +110,12 @@ void IRAM_ATTR Trip::updateTripVssISR(void*)
*
* @return the current engine RPM
*/
uint16_t Trip::getRpm(void) { return (this->latestInjectionPeriod > 0) ? 60000000 / this->latestInjectionPeriod : 0; }
uint_fast16_t Trip::getRpm(void) { return (this->latestInjectionPeriod > 0) ? 60000000 / this->latestInjectionPeriod : 0; }

/**
* Calculates the total fuel spent
*/
float Trip::getLiters(void) { return this->totalInjectionTime / INJ_USEC_LITER; }
float Trip::getLiters(void) { return this->totalInjectionTime * (1 / INJ_USEC_LITER); }

/**
* @brief Calculates the duty cycle on the injector
Expand All @@ -125,17 +125,17 @@ float Trip::getDuty(void) { return (this->latestInjectionPeriod > 0) ? (float) t
/**
* @brief Calculates momentary fuel consumption
*/
float Trip::getLph(void) { return (3600000000.0 * getDuty()) / INJ_USEC_LITER; }
float Trip::getLph(void) { return (3600000000.0f / INJ_USEC_LITER) * getDuty(); }

/**
* Calculates the total distance traveled in km
*/
float Trip::getKm(void) { return this->totalVssPulses / VSS_PULSE_KM; }
float Trip::getKm(void) { return this->totalVssPulses * (1 / VSS_PULSE_KM); }

/**
* Calculates the velocity in km/h
*/
float Trip::getKmh(void) { return (esp_timer_get_time() - this->latestVssTimestamp > VSS_DELTA_MAX) ? 0 : (this->getVel() / VSS_PULSE_KM) * 3600; }
float Trip::getKmh(void) { return (esp_timer_get_time() - this->latestVssTimestamp > VSS_DELTA_MAX) ? 0 : ( 3600/VSS_PULSE_KM ) * this->getVel(); }

/**
* @brief Calculates the momentary fuel efficiency
Expand All @@ -158,4 +158,4 @@ float Trip::getEfficiency() {
*
* @return the amount of VSS pulses per second based on the latest period
*/
float Trip::getVel() { return (this->latestVssPeriod > 0) ? 1000000.0 / this->latestVssPeriod : 0; }
float Trip::getVel() { return (this->latestVssPeriod > 0) ? 1000000.0f / this->latestVssPeriod : 0; }
4 changes: 2 additions & 2 deletions src/fuel_economy_meter/trip.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class Trip
public:
void begin(void);

uint16_t getRpm(void);
uint_fast16_t getRpm(void);
float getLiters(void);
float getKm(void);
float getKmh(void);
float getEfficiency();
float getLph(void);

volatile uint64_t latestInjectionTime = 0; /// The duty time of the last injection pulse
volatile uint32_t latestInjectionTime = 0; /// The duty time of the last injection pulse

private:
static Trip *sTrip;
Expand Down

0 comments on commit 2415f77

Please sign in to comment.