rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
sensor.h
Go to the documentation of this file.
1/**
2 * @file sensor.h
3 * @brief Base class for sensors. Inherit this class to implement a new type of sensor.
4 *
5 * This file defines the basis for all sensor inputs to the ECU, and provides a registry
6 * so that consumers may be agnostic to how each sensor may work.
7 *
8 * HOW TO ADD A NEW SENSOR TYPE:
9 *
10 * 1. Add an entry to the enum in sensor_type.h. Be sure to add it ABOVE the placeholder
11 * at the end of the list.
12 *
13 * 2. In the init/sensor folder, create/modify logic to create an instance of the new sensor,
14 * configure it if necessary, and call its Register() function if it should be enabled.
15 * See init_fluid_pressure.cpp for a minimal example.
16 *
17 * 3. Consume the new sensor with Sensor::get(SensorType::MyNewSensor)
18 *
19 * Providers:
20 * Instantiate a subclass of Sensor, and implement the Get() function.
21 * Call Register() to install the new sensor in the registry, preparing it for use.
22 *
23 * Mocking:
24 * The sensor table supports mocking each sensors value. Call Sensor::SetMockValue to
25 * set a mock value for a particular sensor, and Sensor::ResetMockValue or
26 * Sensor::ResetAllMocks to reset one or all stored mock values.
27 *
28 * Composite Sensors:
29 * Some sensors may be implemented as composite sensors, ie sensors that depend on other
30 * sensors to determine their reading. For example, the throttle pedal may have a pair of
31 * potentiometers that provide redundancy for the pedal's position. Each one may be its
32 * own sensor, then with one "master" sensors that combines the results of the others, and
33 * provides validation of whether the readings agree.
34 *
35 * @date September 12, 2019
36 * @author Matthew Kennedy, (c) 2019
37 */
38
39#pragma once
40
41#include "sensor_type.h"
42#include <rusefi/expected.h>
43
44#include <cstddef>
45
46#ifndef VERBOSE_SENSOR_DEBUG
47#define VERBOSE_SENSOR_DEBUG false
48#endif
49
50using SensorResult = expected<float>;
51
52// Fwd declare - nobody outside of Sensor.cpp needs to see inside this type
53class SensorRegistryEntry;
54
55class Sensor {
56public:
57 // Register this sensor in the sensor registry.
58 // Returns true if registration succeeded, or false if
59 // another sensor of the same type is already registered.
60 bool Register();
61
62 // Print information about this sensor
63 virtual void showInfo(const char* sensorName) const = 0;
64
65 // Print information about all sensors
66 static void showAllSensorInfo();
67
68 // Print information about a particular sensor
69 static void showInfo(SensorType type);
70
71 // Remove all sensors from the sensor registry - tread carefully if you use this outside of a unit test
72 static void resetRegistry();
73
74 /*
75 * Static helper for sensor lookup
76 */
78
79 /*
80 * Get a reading from the specified sensor.
81 */
83
84 /*
85 * Get a reading from the specified sensor, or zero if unavailable.
86 */
87 static float getOrZero(SensorType type) {
88 return Sensor::get(type).value_or(0);
89 }
90
91 /*
92 * Get a raw (unconverted) value from the sensor, if available.
93 */
94 static float getRaw(SensorType type);
95
96 /*
97 * Get whether a sensor is redundant (a composite of multiple other sensors that can check consistency between them)
98 */
99 static bool isRedundant(SensorType type);
100
101 /*
102 * Query whether there is a sensor of a particular type currently registered.
103 */
104 static bool hasSensor(SensorType type);
105
106 /*
107 * Mock a value for a particular sensor.
108 */
109 static void setMockValue(SensorType type, float value, bool mockRedundant = false);
110
111
113
114 /*
115 * Reset mock for a particular sensor.
116 */
117 static void resetMockValue(SensorType type);
118
119 /*
120 * Reset mocking for all sensors.
121 */
122 static void resetAllMocks();
123
124 /*
125 * Inhibit sensor timeouts. Used if you're doing something that will block sensor updates, such as
126 * erasing flash memory (which stalls the CPU on some MCUs)
127 */
128 static void inhibitTimeouts(bool inhibit);
129
130 /*
131 * Get a friendly name for the sensor.
132 * For example, CLT, IAT, Throttle Position 2, etc.
133 */
134 const char* getSensorName() const { return getSensorName(m_type); }
135 static const char* getSensorName(SensorType type);
136
137 // Retrieve the current reading from the sensor.
138 //
139 // Override this in a particular sensor's implementation. As reading sensors is in many hot paths,
140 // it is unwise to synchronously read the sensor or do anything otherwise costly here. At the most,
141 // this should be field lookup and simple math.
142 virtual SensorResult get() const = 0;
143
144 // Retrieve whether the sensor is present. Some sensors may be registered but not present, i.e. if initialization failed.
145 virtual bool hasSensor() const {
146 return true;
147 }
148
149 /*
150 * Get an unconverted value from the sensor, if available.
151 */
152 virtual float getRaw() const {
153 return 0;
154 }
155
156 /*
157 * Get whether this sensor is redundant (backed by multiple other sensors)
158 */
159 virtual bool isRedundant() const {
160 // By default sensors are not redundant
161 return false;
162 }
163
164 void unregister();
165
166 SensorType type() const {
167 return m_type;
168 }
169
170protected:
171 // Protected constructor - only subclasses call this
173 : m_type(type) {}
174
176
177private:
179
180 // Get this sensor's index in the list
181 constexpr size_t getIndex() {
182 return getIndex(m_type);
183 }
184
185 // Get the index in the list for a sensor of particular type
186 static constexpr size_t getIndex(SensorType type) {
187 return static_cast<size_t>(type);
188 }
189
190 /*
191 * Static helper for sensor lookup
192 */
193 static SensorRegistryEntry *getEntryForType(SensorType type);
194};
195
196SensorType findSensorTypeByName(const char *name);
const SensorType m_type
Definition sensor.h:178
static void setMockValue(SensorType type, float value, bool mockRedundant=false)
Definition sensor.cpp:215
static void resetMockValue(SensorType type)
Definition sensor.cpp:223
Sensor(SensorType type)
Definition sensor.h:172
static SensorRegistryEntry * getEntryForType(SensorType type)
Definition sensor.cpp:160
bool Register()
Definition sensor.cpp:143
static void setInvalidMockValue(SensorType type)
Definition sensor.cpp:207
static constexpr size_t getIndex(SensorType type)
Definition sensor.h:186
virtual bool hasSensor() const
Definition sensor.h:145
static const Sensor * getSensorOfType(SensorType type)
Definition sensor.cpp:170
virtual SensorResult get() const =0
virtual bool isRedundant() const
Definition sensor.h:159
virtual float getRaw() const
Definition sensor.h:152
static void showAllSensorInfo()
Definition sensor.cpp:249
virtual void showInfo(const char *sensorName) const =0
static void inhibitTimeouts(bool inhibit)
Definition sensor.cpp:244
static float getOrZero(SensorType type)
Definition sensor.h:87
static void resetAllMocks()
Definition sensor.cpp:231
static void resetRegistry()
Definition sensor.cpp:151
static bool s_inhibitSensorTimeouts
Definition sensor.h:175
const char * getSensorName() const
Definition sensor.h:134
SensorType type() const
Definition sensor.h:166
void unregister()
Definition sensor.cpp:147
constexpr size_t getIndex()
Definition sensor.h:181
SensorType findSensorTypeByName(const char *name)
Definition sensor.cpp:271
expected< float > SensorResult
Definition sensor.h:50
Enumeration of sensors supported by the ECU.
SensorType
Definition sensor_type.h:18