rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
custom_engine.cpp
Go to the documentation of this file.
1/**
2 * @file custom_engine.cpp
3 *
4 *
5 * set engine_type 49
6 * FRANKENSO_QA_ENGINE
7 * See also DEFAULT_ENGINE_TYPE
8 * Frankenso QA 12 cylinder engine
9 *
10 * @date Jan 18, 2015
11 * @author Andrey Belomutskiy, (c) 2012-2020
12 */
13
14#include "pch.h"
15
16#include "custom_engine.h"
17#include "mre_meta.h"
18#include "proteus_meta.h"
19#include "hellen_meta.h"
20#include "odometer.h"
21#include "defaults.h"
22#if EFI_PROD_CODE
24#endif /* EFI_PROD_CODE */
25
26#if EFI_ELECTRONIC_THROTTLE_BODY
27#include "electronic_throttle.h"
28#endif
29
30#if EFI_PROD_CODE
31#include "can_hw.h"
32#include "scheduler.h"
33#endif /* EFI_PROD_CODE */
34
36}
37
38#if defined(HW_NUCLEO_F767) || defined(HW_NUCLEO_H743) || defined(HW_FRANKENSO)
39
40/**
41 * set engine_type 59
42 */
70#endif // HW_FRANKENSO
71
72// todo: should this be part of more default configurations?
74#ifdef HW_FRANKENSO
76
78
79 /**
80 * Frankenso analog #1 PC2 ADC12 CLT
81 * Frankenso analog #2 PC1 ADC11 IAT
82 * Frankenso analog #3 PA0 ADC0 MAP
83 * Frankenso analog #4 PC3 ADC13 WBO / O2
84 * Frankenso analog #5 PA2 ADC2 TPS
85 * Frankenso analog #6 PA1 ADC1
86 * Frankenso analog #7 PA4 ADC4
87 * Frankenso analog #8 PA3 ADC3
88 * Frankenso analog #9 PA7 ADC7
89 * Frankenso analog #10 PA6 ADC6
90 * Frankenso analog #11 PC5 ADC15
91 * Frankenso analog #12 PC4 ADC14 VBatt
92 */
93 engineConfiguration->tps1_1AdcChannel = EFI_ADC_2; // PA2
94
96
99 engineConfiguration->afr.hwChannel = EFI_ADC_13;
100
101 // Frankenso hardware
104
105 /**
106 * http://rusefi.com/wiki/index.php?title=Manual:Hardware_Frankenso_board
107 */
108 // Frankenso low out #1: PE6
109 // Frankenso low out #2: PE5
110 // Frankenso low out #3: PD7 Main Relay
111 // Frankenso low out #4: PC13 Idle valve solenoid
112 // Frankenso low out #5: PE3
113 // Frankenso low out #6: PE4 fuel pump relay
114 // Frankenso low out #7: PE1 (do not use with discovery!)
115 // Frankenso low out #8: PE2 injector #2
116 // Frankenso low out #9: PB9 injector #1
117 // Frankenso low out #10: PE0 (do not use with discovery!)
118 // Frankenso low out #11: PB8 injector #3
119 // Frankenso low out #12: PB7 injector #4
120
124
126
130#ifndef EFI_INJECTOR_PIN3
132#else /* EFI_INJECTOR_PIN3 */
133 engineConfiguration->injectionPins[3] = EFI_INJECTOR_PIN3; // #4
134#endif /* EFI_INJECTOR_PIN3 */
135
136 setAlgorithm(engine_load_mode_e::LM_SPEED_DENSITY);
137
146
150 // set_ignition_pin 4 PE10
152
153 // todo: 8.2 or 10k?
154 engineConfiguration->vbattDividerCoeff = ((float) (10 + 33)) / 10 * 2;
155#endif // HW_FRANKENSO
156}
157
158// ETB_BENCH_ENGINE
159// set engine_type 58
161 // VAG test ETB
163 // by the way this ETB has default position of ADC=74 which is about 4%
165
166 // yes, 30K - that's a test configuration
168
171
172
177 /**
178 * remember that some H-bridges require 5v control lines, not just 3v logic outputs we have on stm32
179 */
180 engineConfiguration->etbIo[0].directionPin1 = Gpio::C7; // Frankenso high-side in order to get 5v control
183
184#if EFI_ELECTRONIC_THROTTLE_BODY
186#endif /* EFI_ELECTRONIC_THROTTLE_BODY */
187
188 engineConfiguration->tps1_1AdcChannel = EFI_ADC_2; // PA2
190
191 // turning off other PWMs to simplify debugging
197
198 // no analog dividers - all sensors with 3v supply, naked discovery bench setup
200
201 // see also setDefaultEtbBiasCurve
202}
203
204#if defined(HW_FRANKENSO) && EFI_PROD_CODE && HAL_USE_EEPROM
205
206// todo: page_size + 2
207// todo: CC_SECTION(".nocache")
208static uint8_t write_buf[EE_PAGE_SIZE + 10];
209
210
211#define EEPROM_WRITE_TIME_MS 10 /* time to write one page in ms. Consult datasheet! */
212
213/**
214 * https://www.onsemi.com/pdf/datasheet/cat24c32-d.pdf
215 * CAT24C32
216 */
217static const I2CEepromFileConfig i2cee = {
218 .barrier_low = 0,
219 .barrier_hi = EE_SIZE - 1,
220 .size = EE_SIZE,
221 .pagesize = EE_PAGE_SIZE,
222 .write_time = TIME_MS2I(EEPROM_WRITE_TIME_MS),
223 .i2cp = &EE_U2CD,
224 .addr = 0x50,
225 .write_buf = write_buf
226};
227
228extern EepromDevice eepdev_24xx;
229static I2CEepromFileStream ifile;
230
231/**
232 * set engine_type 61
233 */
237 // dirty hack
239 efiSetPadMode("I2C", Gpio::A8, PAL_MODE_ALTERNATE(4));
240 efiSetPadMode("I2C", Gpio::C9, PAL_MODE_ALTERNATE(4));
241
242
243 addConsoleActionI("ee_read",
244 [](int value) {
245 if (ifile.vmt != eepdev_24xx.efsvmt) {
246 EepromFileOpen((EepromFileStream *)&ifile, (EepromFileConfig *)&i2cee, &eepdev_24xx);
247 }
248
249 ifile.vmt->setposition(&ifile, 0);
250// chFileStreamSeek(&ifile, 0);
251 int v2;
252 streamRead(&ifile, (uint8_t *)&v2, 4);
253 efiPrintf("EE has %d", v2);
254
255 v2 += 3;
256 ifile.vmt->setposition(&ifile, 0);
257 streamWrite(&ifile, (uint8_t *)&v2, 4);
258
259
260 });
261}
262#endif //HW_FRANKENSO
263
264// F407 discovery
266 // enable_spi 3
268 // Wire up spi3
269 // green
271 // blue
273 // white
275
276 engineConfiguration->l9779spiDevice = SPI_DEVICE_3;
277 // orange
279}
280
281#if HW_PROTEUS
282/*
283 * set engine_type 96
284 */
285
288 engineConfiguration->etbFunctions[0] = DC_Wastegate;
289 engineConfiguration->etbFunctions[1] = DC_None;
290 engineConfiguration->map.sensor.hwChannel = EFI_ADC_NONE;
291
293 setTPS1Calibration(98, 926, 891, 69);
294
298
299 strncpy(config->luaScript, R"(
300
301mapSensor = Sensor.new("map")
302mapSensor : setTimeout(3000)
303
304function onTick()
305 local tps = getSensor("TPS1")
306 tps = (tps == nil and 0 or tps)
307 mapSensor : set(tps)
308end
309
310 )", efi::size(config->luaScript));
311}
312
313#endif // HW_PROTEUS
314
335
337 for (int i = 0; i < MAX_CYLINDER_COUNT;i++) {
340 }
342}
343
346
347 engineConfiguration->auxAnalogInputs[0] = MRE_IN_TPS;
348 engineConfiguration->auxAnalogInputs[1] = MRE_IN_MAP;
349 engineConfiguration->auxAnalogInputs[2] = MRE_IN_CLT;
350 engineConfiguration->auxAnalogInputs[3] = MRE_IN_IAT;
351 // engineConfiguration->auxAnalogInputs[0] =
352
353
354 // EFI_ADC_14: "32 - AN volt 6"
355// engineConfiguration->afr.hwChannel = EFI_ADC_14;
356
357
358 strncpy(config->luaScript, R"(
359txPayload = {}
360function onTick()
361 auxV = getAuxAnalog(0)
362 print('Hello analog ' .. auxV )
363 -- first byte: integer part, would be autoboxed to int
364 txPayload[1] = auxV
365 -- second byte: fractional part, would be autoboxed to int, overflow would be ignored
366 txPayload[2] = auxV * 256;
367 auxV = getAuxAnalog(1)
368 print('Hello analog ' .. auxV )
369 txPayload[3] = auxV
370 txPayload[4] = auxV * 256;
371 auxV = getAuxAnalog(2)
372 print('Hello analog ' .. auxV )
373 txPayload[5] = auxV
374 txPayload[6] = auxV * 256;
375 txCan(1, 0x600, 1, txPayload)
376end
377)", efi::size(config->luaScript));
378
379}
380
381void mreBCM() {
383 // maybe time to kill this feature is pretty soon?
385}
386
388#if HPFP_LOBE_PROFILE_SIZE == 16
389static const float hardCodedHpfpLobeProfileQuantityBins[16] = {0.0, 1.0, 4.5, 9.5,
39016.5, 25.0, 34.5, 45.0 ,
39155.0, 65.5, 75.0, 83.5,
39290.5, 95.5, 99.0, 100.0};
393 copyArray(config->hpfpLobeProfileQuantityBins, hardCodedHpfpLobeProfileQuantityBins);
394#endif // HPFP_LOBE_PROFILE_SIZE
397
399 setLinearCurve(config->hpfpCompensationLoadBins, 0.005, 0.120, 0.001);
400
401 // This is the configuration for bosch HDEV 5 injectors
402 // all times in microseconds/us
414
415 engineConfiguration->mc33_hpfp_i_peak = 5; // A not mA like above
418 engineConfiguration->mc33_hpfp_max_hold = 10; // this value in ms not us
419
420}
421
422/**
423 * set engine_type 107
424 */
444
445/**
446 * set engine_type 103
447 */
449
450 // grey
451 // default spi3mosiPin PB5
452 // white
453 // default spi3misoPin PB4
454 // violet
455 // default spi3sckPin PB3
456
457
460
465
466
467 // blue
469 // green
472 // yellow
474
476
477 // enable_spi 3
479 // Wire up spi3
483
485
487}
488
490#if HW_PROTEUS
493
496
497 strcpy(engineConfiguration->scriptCurveName[2 - 1], "rateofchange");
498
499 strcpy(engineConfiguration->scriptCurveName[3 - 1], "bias");
500
501 /**
502 * for this demo I use ETB just a sample object to control with PID. No reasonable person should consider actually using
503 * Lua for actual intake ETB control while driving around the racing track - hard-coded ETB control is way smarter!
504 */
505 static const float defaultBiasBins[] = {
506 0, 1, 2, 4, 7, 98, 99, 100
507 };
508 static const float defaultBiasValues[] = {
509 -20, -18, -17, 0, 20, 21, 22, 25
510 };
511
515
518
521
522 engineConfiguration->auxAnalogInputs[0] = PROTEUS_IN_ANALOG_VOLT_10;
523 engineConfiguration->afr.hwChannel = EFI_ADC_NONE;
524
525
526 // ETB direction #1 PD10
528 // ETB control PD12
530 // ETB disable PD11
532
533/**
534controlIndex = 0
535directionIndex = 1
536
537 print('pid output ' .. output)
538 print('')
539
540
541
542 local duty = (bias + output) / 100
543
544-- isPositive = duty > 0;
545-- pwmValue = isPositive and duty or -duty
546-- setPwmDuty(controlIndex, pwmValue)
547
548-- dirValue = isPositive and 1 or 0;
549-- setPwmDuty(directionIndex, dirValue)
550
551-- print('pwm ' .. pwmValue .. ' dir ' .. dirValue)
552
553 *
554 */
555
556 auto script = R"(
557
558startPwm(0, 800, 0.1)
559-- direction
560startPwm(1, 80, 1.0)
561-- disable
562startPwm(2, 80, 0.0)
563
564pid = Pid.new(2, 0, 0, -100, 100)
565
566biasCurveIndex = findCurveIndex("bias")
567
568voltageFromCan = nil
569canRxAdd(0x600)
570
571function onCanRx(bus, id, dlc, data)
572 print('got CAN id=' .. id .. ' dlc=' .. dlc)
573 voltageFromCan = data[2] / 256.0 + data[1]
574end
575
576function onTick()
577 local targetVoltage = getAuxAnalog(0)
578
579-- local target = interpolate(1, 0, 3.5, 100, targetVoltage)
580 local target = interpolate(1, 0, 3.5, 100, voltageFromCan)
581-- clamp 0 to 100
582 target = math.max(0, target)
583 target = math.min(100, target)
584
585 print('Decoded target: ' .. target)
586
587 local tps = getSensor("TPS1")
588 tps = (tps == nil and 'invalid TPS' or tps)
589 print('Tps ' .. tps)
590
591 local output = pid:get(target, tps)
592
593 local bias = curve(biasCurveIndex, target)
594 print('bias ' .. bias)
595
596 local duty = (bias + output) / 100
597 isPositive = duty > 0;
598 pwmValue = isPositive and duty or -duty
599 setPwmDuty(0, pwmValue)
600
601 dirValue = isPositive and 1 or 0;
602 setPwmDuty(1, dirValue)
603
604 print('pwm ' .. pwmValue .. ' dir ' .. dirValue)
605 print('')
606end
607 )";
608 strncpy(config->luaScript, script, efi::size(config->luaScript));
609#endif
610}
611
613#if HW_HELLEN && EFI_PROD_CODE
615#endif //HW_HELLEN EFI_PROD_CODE
616 // todo: add board ID detection?
617 // see hellen128 which has/had alternative i2c board id?
618}
619
620// set engine_type 15
623#if EFI_ENGINE_CONTROL
625#endif // EFI_ENGINE_CONTROL
630}
631
632#if HW_PROTEUS
633// PROTEUS_STIM_QC
634// set engine_type 73
637 engineConfiguration->vvtMode[0] = VVT_SINGLE_TOOTH;
638 engineConfiguration->vvtMode[1] = VVT_SINGLE_TOOTH;
639
640 engineConfiguration->triggerInputPins[0] = PROTEUS_DIGITAL_1;
641 engineConfiguration->triggerInputPins[1] = PROTEUS_DIGITAL_2;
642 engineConfiguration->camInputs[0] = PROTEUS_DIGITAL_3;
643 engineConfiguration->camInputs[1] = PROTEUS_DIGITAL_4;
645 engineConfiguration->brakePedalPin = PROTEUS_DIGITAL_6;
646
648 // EFI_ADC_13: "Analog Volt 4"
649 engineConfiguration->tps2_1AdcChannel = PROTEUS_IN_TPS2_1;
650 // EFI_ADC_0: "Analog Volt 5"
651 engineConfiguration->tps2_2AdcChannel = PROTEUS_IN_ANALOG_VOLT_5;
652 engineConfiguration->oilPressure.hwChannel = PROTEUS_IN_ANALOG_VOLT_6;
653 // pps2 volt 7
654
655 // pps1 volt 9
656 // afr volt 10
657 engineConfiguration->oilTempSensor.adcChannel = PROTEUS_IN_ANALOG_VOLT_11;
659
660
661 engineConfiguration->auxLinear1.hwChannel = PROTEUS_IN_ANALOG_TEMP_1;
662 engineConfiguration->auxLinear2.hwChannel = PROTEUS_IN_ANALOG_TEMP_4;
663
664// engineConfiguration->fan2Pin = Gpio::PROTEUS_LS_9;
665// engineConfiguration->malfunctionIndicatorPin = Gpio::PROTEUS_LS_13;
666// engineConfiguration->tachOutputPin = Gpio::PROTEUS_LS_14;
667//
668// engineConfiguration->vvtPins[0] = Gpio::PROTEUS_LS_15;
669// engineConfiguration->vvtPins[1] = Gpio::PROTEUS_LS_16;
670}
671#endif // HW_PROTEUS
672
673// set engine_type 93
687
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
void addConsoleActionI(const char *token, VoidInt callback)
Register a console command with one Integer parameter.
@ MC33810_0_GD_3
@ Unassigned
@ MC33810_0_OUT_0
@ MC33810_0_GD_2
@ MC33810_0_OUT_1
@ MC33810_0_GD_1
@ MC33810_0_GD_0
void setDiscoveryPdm()
void setEtbTestConfiguration()
void mreBCM()
void detectBoardType()
EepromDevice eepdev_24xx
void mreSecondaryCan()
void fuelBenchMode()
void setFrankensoConfiguration()
void setL9779TestConfiguration()
static uint8_t write_buf[EE_PAGE_SIZE+10]
void setRotary()
void setBodyControlUnit()
void testEngine6451()
void setTest33816EngineConfiguration()
void setEepromTestConfiguration()
void setDiscovery33810Test()
static void setBasicNotECUmode()
void proteusStimQc()
void proteusLuaDemo()
void proteusDcWastegateTest()
void setBoschHDEV_5_injectors()
static const I2CEepromFileConfig i2cee
static I2CEepromFileStream ifile
void setHpfpLobeProfileAngle(int lobes)
void setTPS1Calibration(uint16_t tpsMin, uint16_t tpsMax)
static const float defaultBiasValues[]
static const float defaultBiasBins[]
void setBoschVNH2SP30Curve()
void commonFrankensoAnalogInputs()
void setCrankOperationMode()
void setWholeTimingTable(angle_t value)
static constexpr persistent_config_s * config
static constexpr engine_configuration_s * engineConfiguration
void setAlgorithm(engine_load_mode_e algo)
void setFlatInjectorLag(float value)
void detectHellenBoardType()
void brain_pin_markUnused(brain_pin_e brainPin)
void setProteusEtbIO()
brain_input_pin_e triggerInputPins[TRIGGER_INPUT_PIN_COUNT]
scaled_channel< uint8_t, 2, 1 > hpfpLobeProfileQuantityBins[HPFP_LOBE_PROFILE_SIZE]
float crankingCycleBaseFuel[CRANKING_CYCLE_CLT_SIZE][CRANKING_CURVE_SIZE]
scaled_channel< uint16_t, 1000, 1 > hpfpCompensationLoadBins[HPFP_COMPENSATION_SIZE]
float postCrankingFactor[CRANKING_ENRICH_CLT_COUNT][CRANKING_ENRICH_COUNT]
scaled_channel< uint8_t, 1, 50 > hpfpCompensationRpmBins[HPFP_COMPENSATION_SIZE]
scaled_channel< int16_t, 10, 1 > ignitionIatCorrTable[IAT_IGN_CORR_LOAD_COUNT][IAT_IGN_CORR_COUNT]
constexpr void setTable(TElement(&dest)[N][M], const VElement value)
void setRpmTableBin(TValue(&array)[TSize])
void setLinearCurve(TValue(&array)[TSize], float from, float to, float precision=0.01f)
void setArrayValues(TValue(&array)[TSize], float value)
void setCommonNTCSensor(ThermistorConf *thermistorConf, float pullup)