rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions
obd2.h File Reference

Functions

void obdSendPacket (int mode, int PID, int numBytes, uint32_t iValue, size_t busIndex)
 
void obdWriteSupportedPids (int PID, int bitOffset, const int16_t *supportedPids, size_t busIndex)
 
void obdOnCanPacketRx (const CANRxFrame &rx, size_t busIndex)
 
void handleGetDataRequest (const CANRxFrame &rx, size_t busIndex)
 

Function Documentation

◆ handleGetDataRequest()

void handleGetDataRequest ( const CANRxFrame rx,
size_t  busIndex 
)

Definition at line 106 of file obd2.cpp.

106 {
107 int pid = rx.data8[2];
108 switch (pid) {
109 case PID_SUPPORTED_PIDS_REQUEST_01_20:
110 obdWriteSupportedPids(pid, 1, supportedPids0120, busIndex);
111 break;
112 case PID_SUPPORTED_PIDS_REQUEST_21_40:
113 obdWriteSupportedPids(pid, 0x21, supportedPids2140, busIndex);
114 break;
115 case PID_SUPPORTED_PIDS_REQUEST_41_60:
116 obdWriteSupportedPids(pid, 0x41, supportedPids4160, busIndex);
117 break;
118 case PID_MONITOR_STATUS:
119 obdSendPacket(1, pid, 4, 0, busIndex); // todo: add statuses
120 break;
121 case PID_FUEL_SYSTEM_STATUS:
122 // todo: add statuses
123 obdSendValue(_1_MODE, pid, 2, (2<<8)|(0), busIndex); // 2 = "Closed loop, using oxygen sensor feedback to determine fuel mix"
124 break;
125 case PID_ENGINE_LOAD:
126 obdSendValue(_1_MODE, pid, 1, getFuelingLoad() * ODB_TPS_BYTE_PERCENT, busIndex);
127 break;
128 case PID_COOLANT_TEMP:
129 obdSendValue(_1_MODE, pid, 1, Sensor::getOrZero(SensorType::Clt) + ODB_TEMP_EXTRA, busIndex);
130 break;
131 case PID_STFT_BANK1:
132 obdSendValue(_1_MODE, pid, 1, 128 * engine->engineState.stftCorrection[0], busIndex);
133 break;
134 case PID_STFT_BANK2:
135 obdSendValue(_1_MODE, pid, 1, 128 * engine->engineState.stftCorrection[1], busIndex);
136 break;
137 case PID_INTAKE_MAP:
138 obdSendValue(_1_MODE, pid, 1, Sensor::getOrZero(SensorType::Map), busIndex);
139 break;
140 case PID_RPM:
141 obdSendValue(_1_MODE, pid, 2, Sensor::getOrZero(SensorType::Rpm) * ODB_RPM_MULT, busIndex); // rotation/min. (A*256+B)/4
142 break;
143 case PID_SPEED:
144 obdSendValue(_1_MODE, pid, 1, Sensor::getOrZero(SensorType::VehicleSpeed), busIndex);
145 break;
146 case PID_TIMING_ADVANCE: {
147 float timing = engine->engineState.timingAdvance[0];
148 timing = (timing > 360.0f) ? (timing - 720.0f) : timing;
149 obdSendValue(_1_MODE, pid, 1, (timing + 64.0f) * 2.0f, busIndex); // angle before TDC. (A/2)-64
150 break;
151 }
152 case PID_INTAKE_TEMP:
153 obdSendValue(_1_MODE, pid, 1, Sensor::getOrZero(SensorType::Iat) + ODB_TEMP_EXTRA, busIndex);
154 break;
155 case PID_INTAKE_MAF:
156 obdSendValue(_1_MODE, pid, 2, Sensor::getOrZero(SensorType::Maf) * 100.0f, busIndex); // grams/sec (A*256+B)/100
157 break;
158 case PID_THROTTLE:
159 obdSendValue(_1_MODE, pid, 1, Sensor::getOrZero(SensorType::Tps1) * ODB_TPS_BYTE_PERCENT, busIndex); // (A*100/255)
160 break;
161 case PID_FUEL_AIR_RATIO_1: {
162 float lambda = clampF(0, Sensor::getOrZero(SensorType::Lambda1), 1.99f);
163
164 uint16_t scaled = lambda * 32768;
165
166 obdSendPacket(1, pid, 4, scaled << 16, busIndex);
167 break;
168 } case PID_FUEL_RATE: {
169
170#ifdef MODULE_ODOMETER
171 float gPerSecond = engine->module<TripOdometer>()->getConsumptionGramPerSecond();
172#else
173 float gPerSecond = 0;
174#endif // MODULE_ODOMETER
175
176 float gPerHour = gPerSecond * 3600;
177 float literPerHour = gPerHour * 0.00139f;
178 obdSendValue(_1_MODE, pid, 2, literPerHour * 20.0f, busIndex); // L/h. (A*256+B)/20
179 break;
180 } case PID_CONTROL_UNIT_VOLTAGE: {
181 obdSendValue(_1_MODE, pid, 2, 1000 * Sensor::getOrZero(SensorType::BatteryVoltage), busIndex);
182 break;
183 } case PID_ETHANOL: {
184 obdSendValue(_1_MODE, pid, 1, (255.0f / 100) * Sensor::getOrZero(SensorType::FuelEthanolPercent), busIndex);
185 break;
186 } case PID_OIL_TEMPERATURE: {
187 obdSendValue(_1_MODE, pid, 1, Sensor::getOrZero(SensorType::OilTemperature) + ODB_TEMP_EXTRA, busIndex);
188 break;
189 } default:
190 // ignore unhandled PIDs
191 break;
192 }
193}
EngineState engineState
Definition engine.h:344
constexpr auto & module()
Definition engine.h:200
angle_t timingAdvance[MAX_CYLINDER_COUNT]
static float getOrZero(SensorType type)
Definition sensor.h:83
static EngineAccessor engine
Definition engine.h:413
float getFuelingLoad()
static const int16_t supportedPids4160[]
Definition obd2.cpp:54
void obdSendPacket(int mode, int PID, int numBytes, uint32_t iValue, size_t busIndex)
Definition obd2.cpp:62
static void obdSendValue(int mode, int PID, int numBytes, float value, size_t busIndex)
Definition obd2.cpp:81
static const int16_t supportedPids2140[]
Definition obd2.cpp:49
void obdWriteSupportedPids(int PID, int bitOffset, const int16_t *supportedPids, size_t busIndex)
Definition obd2.cpp:92
static const int16_t supportedPids0120[]
Definition obd2.cpp:33
@ FuelEthanolPercent
uint8_t data8[8]
Frame data.
Definition can_mocks.h:55
float stftCorrection[FT_BANK_COUNT]

Referenced by obdOnCanPacketRx().

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

◆ obdOnCanPacketRx()

void obdOnCanPacketRx ( const CANRxFrame rx,
size_t  busIndex 
)

Definition at line 210 of file obd2.cpp.

210 {
211 if (CAN_SID(rx) != OBD_TEST_REQUEST) {
212 return;
213 }
214
215 if (rx.data8[0] == _OBD_2 && rx.data8[1] == OBD_CURRENT_DATA) {
216 handleGetDataRequest(rx, busIndex);
217 } else if (rx.data8[0] == 1 && rx.data8[1] == OBD_STORED_DIAGNOSTIC_TROUBLE_CODES) {
218 // todo: implement stored/pending difference?
220 } else if (rx.data8[0] == 1 && rx.data8[1] == OBD_PENDING_DIAGNOSTIC_TROUBLE_CODES) {
221 // todo: implement stored/pending difference?
223 }
224}
WarningCodeState warnings
ObdCode lastErrorCode
static void handleDtcRequest(int numCodes, ObdCode *dtcCode)
Definition obd2.cpp:195
void handleGetDataRequest(const CANRxFrame &rx, size_t busIndex)
Definition obd2.cpp:106

Referenced by processCanRxMessage().

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

◆ obdSendPacket()

void obdSendPacket ( int  mode,
int  PID,
int  numBytes,
uint32_t  iValue,
size_t  busIndex 
)

Definition at line 62 of file obd2.cpp.

62 {
63 CanTxMessage resp(CanCategory::OBD, OBD_TEST_RESPONSE);
64
65 // Respond on the same bus we got the request from
66 resp.busIndex = busIndex;
67
68 // write number of bytes
69 resp[0] = (uint8_t)(2 + numBytes);
70 // write 2 bytes of header
71 resp[1] = (uint8_t)(0x40 + mode);
72 resp[2] = (uint8_t)PID;
73 // write N data bytes
74 for (int i = 8 * (numBytes - 1), j = 3; i >= 0; i -= 8, j++) {
75 resp[j] = (uint8_t)((iValue >> i) & 0xff);
76 }
77}

Referenced by handleGetDataRequest(), obdSendValue(), and obdWriteSupportedPids().

Here is the caller graph for this function:

◆ obdWriteSupportedPids()

void obdWriteSupportedPids ( int  PID,
int  bitOffset,
const int16_t *  supportedPids,
size_t  busIndex 
)

Definition at line 92 of file obd2.cpp.

92 {
93 uint32_t value = 0;
94 // gather all 32 bit fields
95 for (int i = 0; i < 32 && supportedPids[i] > 0; i++)
96 value |= 1 << (31 + bitOffset - supportedPids[i]);
97
98#ifdef MOCK_SUPPORTED_PIDS
99 // for OBD debug
100 value = MOCK_SUPPORTED_PIDS;
101#endif
102
103 obdSendPacket(1, PID, 4, value, busIndex);
104}

Referenced by handleGetDataRequest().

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

Go to the source code of this file.