rusEFI
The most advanced open source ECU
Data Structures | Typedefs | Functions
sensor.h File Reference

Detailed Description

Base class for sensors. Inherit this class to implement a new type of sensor.

This file defines the basis for all sensor inputs to the ECU, and provides a registry so that consumers may be agnostic to how each sensor may work.

HOW TO ADD A NEW SENSOR TYPE:

  1. Add an entry to the enum in sensor_type.h. Be sure to add it ABOVE the placeholder at the end of the list.
  2. In the init/sensor folder, create/modify logic to create an instance of the new sensor, configure it if necessary, and call its Register() function if it should be enabled. See init_oil_pressure.cpp for a minimal example.
  3. Consume the new sensor with instance(s) of SensorConsumer<SensorType::MyNewSensor>

Consumers:

tl;dr: Use a SensorConsumer. See sensor_consumer.h

All a consumer does is look up whether a particular sensor is present in the table, and if so, asks it for the current reading. This could synchronously perform sensor acquisition and conversion (not recommended), or use a previously stored value (recommended!). This functionality is implemented in sensor_consumer.h, and sensor.cpp.

Providers: Instantiate a subclass of Sensor, and implement the Get() function. Call Register() to install the new sensor in the registry, preparing it for use.

Mocking: The sensor table supports mocking each sensors value. Call Sensor::SetMockValue to set a mock value for a particular sensor, and Sensor::ResetMockValue or Sensor::ResetAllMocks to reset one or all stored mock values.

Composite Sensors: Some sensors may be implemented as composite sensors, ie sensors that depend on other sensors to determine their reading. For example, the throttle pedal may have a pair of potentiometers that provide redundancy for the pedal's position. Each one may be its own sensor, then with one "master" sensors that combines the results of the others, and provides validation of whether the readings agree.

Date
September 12, 2019
Author
Matthew Kennedy, (c) 2019

Definition in file sensor.h.

Data Structures

class  Sensor
 

Typedefs

using SensorResult = expected< float >
 

Functions

SensorType findSensorTypeByName (const char *name)
 

Typedef Documentation

◆ SensorResult

using SensorResult = expected<float>

Definition at line 55 of file sensor.h.

Function Documentation

◆ findSensorTypeByName()

SensorType findSensorTypeByName ( const char *  name)

this is definitely not the fastest implementation possible but good enough for now? todo: some sort of hashmap in the future?

Definition at line 268 of file sensor.cpp.

268  {
269  for (int i = 0;i<(int)SensorType::PlaceholderLast;i++) {
270  SensorType type = (SensorType)i;
271  const char *sensorName = getSensorType(type);
272  if (strEqualCaseInsensitive(sensorName, name)) {
273  return type;
274  }
275  }
276 
277  return SensorType::Invalid;
278 }
const char * getSensorType(SensorType value)
SensorType
Definition: sensor_type.h:18

Referenced by findSensorByName().

Here is the call graph for this function:
Here is the caller graph for this function:

Go to the source code of this file.