rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
obd2.cpp
Go to the documentation of this file.
1/*
2 * @file obd2.cpp
3 *
4 * ISO 15765-4
5 * http://en.wikipedia.org/wiki/OBD-II_PIDs
6 *
7 * @date Jun 9, 2015
8 * @author Andrey Belomutskiy, (c) 2012-2020
9 *
10 * This file is part of rusEfi - see http://rusefi.com
11 *
12 * rusEfi is free software; you can redistribute it and/or modify it under the terms of
13 * the GNU General Public License as published by the Free Software Foundation; either
14 * version 3 of the License, or (at your option) any later version.
15 *
16 * rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
17 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along with this program.
21 * If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include "pch.h"
25
26#if EFI_CAN_SUPPORT || EFI_UNIT_TEST
27
28#include "obd2.h"
29#include "can.h"
30#include "can_msg_tx.h"
31#include "fuel_math.h"
32
33static const int16_t supportedPids0120[] = {
34 PID_MONITOR_STATUS,
35 PID_FUEL_SYSTEM_STATUS,
36 PID_ENGINE_LOAD,
37 PID_COOLANT_TEMP,
38 PID_STFT_BANK1,
39 PID_STFT_BANK2,
40 PID_INTAKE_MAP,
41 PID_RPM,
42 PID_SPEED,
43 PID_TIMING_ADVANCE,
44 PID_INTAKE_TEMP,
45 PID_THROTTLE,
46 -1
47};
48
49static const int16_t supportedPids2140[] = {
50 PID_FUEL_AIR_RATIO_1,
51 -1
52};
53
54static const int16_t supportedPids4160[] = {
55 PID_CONTROL_UNIT_VOLTAGE,
56 PID_ETHANOL,
57 PID_FUEL_RATE,
58 PID_OIL_TEMPERATURE,
59 -1
60};
61
62void obdSendPacket(int mode, int PID, int numBytes, uint32_t iValue, size_t busIndex) {
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}
78
79#define _1_MODE 1
80
81static void obdSendValue(int mode, int PID, int numBytes, float value, size_t busIndex) {
82 efiAssertVoid(ObdCode::CUSTOM_ERR_6662, numBytes <= 2, "invalid numBytes");
83 int iValue = (int)efiRound(value, 1.0f);
84 // clamp to uint8_t (0..255) or uint16_t (0..65535)
85 iValue = maxI(minI(iValue, (numBytes == 1) ? 255 : 65535), 0);
86 obdSendPacket(mode, PID, numBytes, iValue, busIndex);
87}
88
89
90// #define MOCK_SUPPORTED_PIDS 0xffffffff
91
92void obdWriteSupportedPids(int PID, int bitOffset, const int16_t *supportedPids, size_t busIndex) {
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}
105
106void handleGetDataRequest(const CANRxFrame& rx, size_t busIndex) {
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}
194
195static void handleDtcRequest(int numCodes, ObdCode* dtcCode) {
196 // TODO: this appears to be unfinished?
197 UNUSED(numCodes);
198 UNUSED(dtcCode);
199
200 // int numBytes = numCodes * 2;
201 // // write CAN-TP Single Frame header?
202 // txmsg.data8[0] = (uint8_t)((0 << 4) | numBytes);
203 // for (int i = 0, j = 1; i < numCodes; i++) {
204 // txmsg.data8[j++] = (uint8_t)((dtcCode[i] >> 8) & 0xff);
205 // txmsg.data8[j++] = (uint8_t)(dtcCode[i] & 0xff);
206 // }
207}
208
209#if HAS_CAN_FRAME
210void obdOnCanPacketRx(const CANRxFrame& rx, size_t busIndex) {
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}
225#endif /* HAS_CAN_FRAME */
226
227#endif /* EFI_CAN_SUPPORT */
size_t busIndex
Definition can_msg_tx.h:53
EngineState engineState
Definition engine.h:344
constexpr auto & module()
Definition engine.h:200
WarningCodeState warnings
angle_t timingAdvance[MAX_CYLINDER_COUNT]
static float getOrZero(SensorType type)
Definition sensor.h:83
ObdCode lastErrorCode
float efiRound(float value, float precision)
Definition efilib.cpp:34
static EngineAccessor engine
Definition engine.h:413
float getFuelingLoad()
UNUSED(samplingTimeSeconds)
static void handleDtcRequest(int numCodes, ObdCode *dtcCode)
Definition obd2.cpp:195
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
void handleGetDataRequest(const CANRxFrame &rx, size_t busIndex)
Definition obd2.cpp:106
void obdOnCanPacketRx(const CANRxFrame &rx, size_t busIndex)
Definition obd2.cpp:210
ObdCode
@ CUSTOM_ERR_6662
@ FuelEthanolPercent
uint8_t data8[8]
Frame data.
Definition can_mocks.h:55
float stftCorrection[FT_BANK_COUNT]