CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_EFI/AP_EFI.cpp
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
#include "AP_EFI.h"
17
18
#if HAL_EFI_ENABLED
19
20
#include "AP_EFI_Serial_MS.h"
21
#include "AP_EFI_Serial_Lutan.h"
22
#include "AP_EFI_NWPMU.h"
23
#include "AP_EFI_DroneCAN.h"
24
#include "AP_EFI_Currawong_ECU.h"
25
#include "AP_EFI_Serial_Hirth.h"
26
#include "AP_EFI_Scripting.h"
27
#include "AP_EFI_MAV.h"
28
29
#include <AP_Logger/AP_Logger.h>
30
#include <GCS_MAVLink/GCS.h>
31
#include <AP_Math/AP_Math.h>
32
33
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
34
#include <AP_CANManager/AP_CANManager.h>
35
#endif
36
37
extern const AP_HAL::HAL& hal;
38
39
// table of user settable parameters
40
const AP_Param::GroupInfo AP_EFI::var_info[] = {
41
// @Param: _TYPE
42
// @DisplayName: EFI communication type
43
// @Description: What method of communication is used for EFI #1
44
// @Values: 0:None,1:Serial-MS,2:NWPMU,3:Serial-Lutan,5:DroneCAN,6:Currawong-ECU,7:Scripting,8:Hirth,9:MAVLink
45
// @User: Advanced
46
// @RebootRequired: True
47
AP_GROUPINFO_FLAGS("_TYPE", 1, AP_EFI, type, 0, AP_PARAM_FLAG_ENABLE),
48
49
// @Param: _COEF1
50
// @DisplayName: EFI Calibration Coefficient 1
51
// @Description: Used to calibrate fuel flow for MS protocol (Slope). This should be calculated from a log at constant fuel usage rate. Plot (ECYL[0].InjT*EFI.Rpm)/600.0 to get the duty_cycle. Measure actual fuel usage in cm^3/min, and set EFI_COEF1 = fuel_usage_cm3permin / duty_cycle
52
// @Range: 0 1
53
// @User: Advanced
54
AP_GROUPINFO("_COEF1", 2, AP_EFI, coef1, 0),
55
56
// @Param: _COEF2
57
// @DisplayName: EFI Calibration Coefficient 2
58
// @Description: Used to calibrate fuel flow for MS protocol (Offset). This can be used to correct for a non-zero offset in the fuel consumption calculation of EFI_COEF1
59
// @Range: 0 10
60
// @User: Advanced
61
AP_GROUPINFO("_COEF2", 3, AP_EFI, coef2, 0),
62
63
// @Param: _FUEL_DENS
64
// @DisplayName: ECU Fuel Density
65
// @Description: Used to calculate fuel consumption
66
// @Units: kg/m/m/m
67
// @Range: 0 10000
68
// @User: Advanced
69
AP_GROUPINFO("_FUEL_DENS", 4, AP_EFI, ecu_fuel_density, 0),
70
71
#if AP_EFI_THROTTLE_LINEARISATION_ENABLED
72
// @Group: _THRLIN
73
// @Path: AP_EFI_ThrottleLinearisation.cpp
74
AP_SUBGROUPINFO(throttle_linearisation, "_THRLIN", 5, AP_EFI, AP_EFI_ThrLin),
75
#endif
76
77
AP_GROUPEND
78
};
79
80
AP_EFI *AP_EFI::singleton;
81
82
// Initialize parameters
83
AP_EFI::AP_EFI()
84
{
85
singleton = this;
86
AP_Param::setup_object_defaults(this, var_info);
87
}
88
89
// Initialize backends based on existing params
90
void AP_EFI::init(void)
91
{
92
if (backend != nullptr) {
93
// Init called twice, perhaps
94
return;
95
}
96
switch ((Type)type.get()) {
97
case Type::NONE:
98
break;
99
#if AP_EFI_SERIAL_MS_ENABLED
100
case Type::MegaSquirt:
101
backend = NEW_NOTHROW AP_EFI_Serial_MS(*this);
102
break;
103
#endif
104
#if AP_EFI_SERIAL_LUTAN_ENABLED
105
case Type::Lutan:
106
backend = NEW_NOTHROW AP_EFI_Serial_Lutan(*this);
107
break;
108
#endif
109
#if AP_EFI_NWPWU_ENABLED
110
case Type::NWPMU:
111
backend = NEW_NOTHROW AP_EFI_NWPMU(*this);
112
break;
113
#endif
114
#if AP_EFI_DRONECAN_ENABLED
115
case Type::DroneCAN:
116
backend = NEW_NOTHROW AP_EFI_DroneCAN(*this);
117
break;
118
#endif
119
#if AP_EFI_CURRAWONG_ECU_ENABLED
120
case Type::CurrawongECU:
121
backend = NEW_NOTHROW AP_EFI_Currawong_ECU(*this);
122
break;
123
#endif
124
#if AP_EFI_SCRIPTING_ENABLED
125
case Type::SCRIPTING:
126
backend = NEW_NOTHROW AP_EFI_Scripting(*this);
127
break;
128
#endif
129
#if AP_EFI_SERIAL_HIRTH_ENABLED
130
case Type::Hirth:
131
backend = NEW_NOTHROW AP_EFI_Serial_Hirth(*this);
132
break;
133
#endif
134
#if AP_EFI_MAV_ENABLED
135
case Type::MAV:
136
backend = NEW_NOTHROW AP_EFI_MAV(*this);
137
break;
138
#endif
139
default:
140
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Unknown EFI type");
141
break;
142
}
143
}
144
145
// Ask all backends to update the frontend
146
void AP_EFI::update()
147
{
148
if (backend) {
149
backend->update();
150
#if HAL_LOGGING_ENABLED
151
log_status();
152
#endif
153
}
154
}
155
156
bool AP_EFI::is_healthy(void) const
157
{
158
return (backend && (AP_HAL::millis() - state.last_updated_ms) < HEALTHY_LAST_RECEIVED_MS);
159
}
160
161
#if HAL_LOGGING_ENABLED
162
/*
163
write status to log
164
*/
165
void AP_EFI::log_status(void)
166
{
167
// @LoggerMessage: EFI
168
// @Description: Electronic Fuel Injection system data
169
// @Field: TimeUS: Time since system startup
170
// @Field: LP: Reported engine load
171
// @Field: Rpm: Reported engine RPM
172
// @Field: SDT: Spark Dwell Time
173
// @Field: ATM: Atmospheric pressure
174
// @Field: IMP: Intake manifold pressure
175
// @Field: IMT: Intake manifold temperature
176
// @Field: ECT: Engine Coolant Temperature
177
// @Field: OilP: Oil Pressure
178
// @Field: OilT: Oil temperature
179
// @Field: FP: Fuel Pressure
180
// @Field: FCR: Fuel Consumption Rate
181
// @Field: CFV: Consumed fueld volume
182
// @Field: TPS: Throttle Position
183
// @Field: IDX: Index of the publishing ECU
184
AP::logger().WriteStreaming("EFI",
185
"TimeUS,LP,Rpm,SDT,ATM,IMP,IMT,ECT,OilP,OilT,FP,FCR,CFV,TPS,IDX",
186
"s%qsPPOOPOP--%-",
187
"F00C--00-0-0000",
188
"QBIffffffffffBB",
189
AP_HAL::micros64(),
190
uint8_t(state.engine_load_percent),
191
uint32_t(state.engine_speed_rpm),
192
float(state.spark_dwell_time_ms),
193
float(state.atmospheric_pressure_kpa),
194
float(state.intake_manifold_pressure_kpa),
195
float(state.intake_manifold_temperature),
196
float(state.coolant_temperature),
197
float(state.oil_pressure),
198
float(state.oil_temperature),
199
float(state.fuel_pressure),
200
float(state.fuel_consumption_rate_cm3pm),
201
float(state.estimated_consumed_fuel_volume_cm3),
202
uint8_t(state.throttle_position_percent),
203
uint8_t(state.ecu_index));
204
205
// @LoggerMessage: EFI2
206
// @Description: Electronic Fuel Injection system data - redux
207
// @Field: TimeUS: Time since system startup
208
// @Field: Healthy: True if EFI is healthy
209
// @Field: ES: Engine state
210
// @Field: GE: General error
211
// @Field: CSE: Crankshaft sensor status
212
// @Field: TS: Temperature status
213
// @Field: FPS: Fuel pressure status
214
// @Field: OPS: Oil pressure status
215
// @Field: DS: Detonation status
216
// @Field: MS: Misfire status
217
// @Field: DebS: Debris status
218
// @Field: SPU: Spark plug usage
219
// @Field: IDX: Index of the publishing ECU
220
AP::logger().WriteStreaming("EFI2",
221
"TimeUS,Healthy,ES,GE,CSE,TS,FPS,OPS,DS,MS,DebS,SPU,IDX",
222
"s------------",
223
"F------------",
224
"QBBBBBBBBBBBB",
225
AP_HAL::micros64(),
226
uint8_t(is_healthy()),
227
uint8_t(state.engine_state),
228
uint8_t(state.general_error),
229
uint8_t(state.crankshaft_sensor_status),
230
uint8_t(state.temperature_status),
231
uint8_t(state.fuel_pressure_status),
232
uint8_t(state.oil_pressure_status),
233
uint8_t(state.detonation_status),
234
uint8_t(state.misfire_status),
235
uint8_t(state.debris_status),
236
uint8_t(state.spark_plug_usage),
237
uint8_t(state.ecu_index));
238
239
// @LoggerMessage: ECYL
240
// @Description: EFI per-cylinder information
241
// @Field: TimeUS: Time since system startup
242
// @Field: Inst: Cylinder this data belongs to
243
// @Field: IgnT: Ignition timing
244
// @Field: InjT: Injection time
245
// @Field: CHT: Cylinder head temperature
246
// @Field: EGT: Exhaust gas temperature
247
// @Field: Lambda: Estimated lambda coefficient (dimensionless ratio)
248
// @Field: CHT2: Cylinder2 head temperature
249
// @Field: EGT2: Cylinder2 Exhaust gas temperature
250
// @Field: IDX: Index of the publishing ECU
251
AP::logger().WriteStreaming("ECYL",
252
"TimeUS,Inst,IgnT,InjT,CHT,EGT,Lambda,CHT2,EGT2,IDX",
253
"s#dsOO-OO-",
254
"F-0C000000",
255
"QBffffffff",
256
AP_HAL::micros64(),
257
0,
258
state.cylinder_status.ignition_timing_deg,
259
state.cylinder_status.injection_time_ms,
260
KELVIN_TO_C(state.cylinder_status.cylinder_head_temperature),
261
KELVIN_TO_C(state.cylinder_status.exhaust_gas_temperature),
262
state.cylinder_status.lambda_coefficient,
263
KELVIN_TO_C(state.cylinder_status.cylinder_head_temperature2),
264
KELVIN_TO_C(state.cylinder_status.exhaust_gas_temperature2),
265
state.ecu_index);
266
}
267
#endif // LOGGING_ENABLED
268
269
/*
270
send EFI_STATUS
271
*/
272
void AP_EFI::send_mavlink_status(mavlink_channel_t chan)
273
{
274
if (!backend) {
275
return;
276
}
277
278
float ignition_voltage;
279
if (isnan(state.ignition_voltage) ||
280
is_equal(state.ignition_voltage, -1.0f)) {
281
// zero means "unknown" in mavlink, 0.0001 means 0 volts
282
ignition_voltage = 0;
283
} else if (is_zero(state.ignition_voltage)) {
284
// zero means "unknown" in mavlink, 0.0001 means 0 volts
285
ignition_voltage = 0.0001f;
286
} else {
287
ignition_voltage = state.ignition_voltage;
288
};
289
290
// If fuel pressure is supported, but is exactly zero, shift it to 0.0001
291
// to indicate that it is supported.
292
float fuel_pressure = state.fuel_pressure;
293
if (is_zero(fuel_pressure) && state.fuel_pressure_status != Fuel_Pressure_Status::NOT_SUPPORTED) {
294
fuel_pressure = 0.0001;
295
}
296
297
mavlink_msg_efi_status_send(
298
chan,
299
AP_EFI::is_healthy(),
300
state.ecu_index,
301
state.engine_speed_rpm,
302
state.estimated_consumed_fuel_volume_cm3,
303
state.fuel_consumption_rate_cm3pm,
304
state.engine_load_percent,
305
state.throttle_position_percent,
306
state.spark_dwell_time_ms,
307
state.atmospheric_pressure_kpa,
308
state.intake_manifold_pressure_kpa,
309
KELVIN_TO_C(state.intake_manifold_temperature),
310
KELVIN_TO_C(state.cylinder_status.cylinder_head_temperature),
311
state.cylinder_status.ignition_timing_deg,
312
state.cylinder_status.injection_time_ms,
313
KELVIN_TO_C(state.cylinder_status.exhaust_gas_temperature),
314
state.throttle_out,
315
state.pt_compensation,
316
ignition_voltage,
317
fuel_pressure
318
);
319
}
320
321
// get a copy of state structure
322
void AP_EFI::get_state(EFI_State &_state)
323
{
324
WITH_SEMAPHORE(sem);
325
_state = state;
326
}
327
328
void AP_EFI::handle_EFI_message(const mavlink_message_t &msg) {
329
if (backend != nullptr) {
330
backend->handle_EFI_message(msg);
331
}
332
}
333
334
namespace AP {
335
AP_EFI *EFI()
336
{
337
return AP_EFI::get_singleton();
338
}
339
}
340
341
#endif // HAL_EFI_ENABLED
342
343
344