2021-07-25 22:05:17 -07:00
# include "pch.h"
2020-02-03 22:54:05 -08:00
# include "adc_subscription.h"
# include "functional_sensor.h"
2020-05-18 11:32:00 -07:00
# include "redundant_sensor.h"
2021-04-05 12:57:03 -07:00
# include "redundant_ford_tps.h"
2020-03-22 14:29:01 -07:00
# include "proxy_sensor.h"
2020-02-03 22:54:05 -08:00
# include "linear_func.h"
# include "tps.h"
2020-03-22 14:09:46 -07:00
LinearFunc tpsFunc1p ( TPS_TS_CONVERSION ) ;
2020-05-18 11:32:00 -07:00
LinearFunc tpsFunc1s ( TPS_TS_CONVERSION ) ;
2020-03-22 14:09:46 -07:00
LinearFunc tpsFunc2p ( TPS_TS_CONVERSION ) ;
2020-05-18 11:32:00 -07:00
LinearFunc tpsFunc2s ( TPS_TS_CONVERSION ) ;
2020-02-03 22:54:05 -08:00
2020-05-18 11:32:00 -07:00
FunctionalSensor tpsSens1p ( SensorType : : Tps1Primary , MS2NT ( 10 ) ) ;
FunctionalSensor tpsSens1s ( SensorType : : Tps1Secondary , MS2NT ( 10 ) ) ;
FunctionalSensor tpsSens2p ( SensorType : : Tps2Primary , MS2NT ( 10 ) ) ;
FunctionalSensor tpsSens2s ( SensorType : : Tps2Secondary , MS2NT ( 10 ) ) ;
2021-04-05 12:57:03 -07:00
// Used in case of "normal", non-Ford ETB TPS
2020-05-18 11:32:00 -07:00
RedundantSensor tps1 ( SensorType : : Tps1 , SensorType : : Tps1Primary , SensorType : : Tps1Secondary ) ;
RedundantSensor tps2 ( SensorType : : Tps2 , SensorType : : Tps2Primary , SensorType : : Tps2Secondary ) ;
2020-02-03 22:54:05 -08:00
2021-04-05 12:57:03 -07:00
// Used only in case of weird Ford-style ETB TPS
RedundantFordTps fordTps1 ( SensorType : : Tps1 , SensorType : : Tps1Primary , SensorType : : Tps1Secondary ) ;
RedundantFordTps fordTps2 ( SensorType : : Tps2 , SensorType : : Tps2Primary , SensorType : : Tps2Secondary ) ;
2020-05-23 16:48:32 -07:00
LinearFunc pedalFuncPrimary ;
LinearFunc pedalFuncSecondary ;
FunctionalSensor pedalSensorPrimary ( SensorType : : AcceleratorPedalPrimary , MS2NT ( 10 ) ) ;
FunctionalSensor pedalSensorSecondary ( SensorType : : AcceleratorPedalSecondary , MS2NT ( 10 ) ) ;
RedundantSensor pedal ( SensorType : : AcceleratorPedal , SensorType : : AcceleratorPedalPrimary , SensorType : : AcceleratorPedalSecondary ) ;
2020-03-22 14:09:46 -07:00
2020-03-22 14:29:01 -07:00
// This sensor indicates the driver's throttle intent - Pedal if we have one, TPS if not.
ProxySensor driverIntent ( SensorType : : DriverThrottleIntent ) ;
2020-09-18 00:04:07 -07:00
// These sensors are TPS-like, so handle them in here too
LinearFunc wastegateFunc ( PACK_MULT_VOLTAGE ) ;
LinearFunc idlePosFunc ( PACK_MULT_VOLTAGE ) ;
FunctionalSensor wastegateSens ( SensorType : : WastegatePosition , MS2NT ( 10 ) ) ;
FunctionalSensor idlePosSens ( SensorType : : IdlePosition , MS2NT ( 10 ) ) ;
2020-12-06 20:19:57 -08:00
static bool configureTps ( LinearFunc & func , adc_channel_e channel , float closed , float open , float min , float max , const char * msg ) {
// Only configure if we have a channel
2021-01-05 13:02:20 -08:00
if ( ! isAdcChannelValid ( channel ) ) {
2020-12-06 20:19:57 -08:00
return false ;
}
2020-12-05 21:41:49 -08:00
float scaledClosed = closed / func . getDivideInput ( ) ;
float scaledOpen = open / func . getDivideInput ( ) ;
float split = absF ( scaledOpen - scaledClosed ) ;
// If the voltage for closed vs. open is very near, something is wrong with your calibration
if ( split < 0.5f ) {
2021-08-19 12:51:33 -07:00
firmwareError ( OBD_Throttle_Position_Sensor_Circuit_Malfunction , " \" %s \" problem: open %.2f/closed %.2f cal values are too close together. Check your calibration and wiring! " , msg ,
2020-12-06 05:30:31 -08:00
open ,
closed ) ;
2020-12-05 21:41:49 -08:00
return false ;
}
2020-02-03 22:54:05 -08:00
func . configure (
2020-03-22 14:09:46 -07:00
closed , 0 ,
open , 100 ,
2020-04-05 06:10:08 -07:00
min , max
2020-02-03 22:54:05 -08:00
) ;
2020-12-05 21:41:49 -08:00
return true ;
2020-02-08 01:22:23 -08:00
}
2020-05-18 11:32:00 -07:00
static bool initTpsFunc ( LinearFunc & func , FunctionalSensor & sensor , adc_channel_e channel , float closed , float open , float min , float max ) {
2020-12-06 20:19:57 -08:00
// If the configuration was invalid, don't continue to configure the sensor
if ( ! configureTps ( func , channel , closed , open , min , max , sensor . getSensorName ( ) ) ) {
2020-12-05 21:41:49 -08:00
return false ;
}
2020-02-03 22:54:05 -08:00
sensor . setFunction ( func ) ;
2020-08-21 16:47:12 -07:00
AdcSubscription : : SubscribeSensor ( sensor , channel , 200 ) ;
2020-02-03 22:54:05 -08:00
2020-12-06 12:00:30 -08:00
return sensor . Register ( ) ;
2020-05-18 11:32:00 -07:00
}
2021-04-05 12:57:03 -07:00
static void initTpsFuncAndRedund ( RedundantSensor & redund , RedundantFordTps * fordTps , bool isFordTps , LinearFunc & func , FunctionalSensor & sensor , adc_channel_e channel , float closed , float open , float min , float max ) {
2020-05-18 11:32:00 -07:00
bool hasSecond = initTpsFunc ( func , sensor , channel , closed , open , min , max ) ;
2021-04-05 12:57:03 -07:00
if ( isFordTps & & fordTps ) {
fordTps - > configure ( 5.0f , 52.6f ) ;
fordTps - > Register ( ) ;
} else {
redund . configure ( 5.0f , ! hasSecond ) ;
redund . Register ( ) ;
}
2020-02-03 22:54:05 -08:00
}
2021-11-16 01:15:29 -08:00
void initTps ( ) {
2021-11-17 00:54:21 -08:00
percent_t min = engineConfiguration - > tpsErrorDetectionTooLow ;
percent_t max = engineConfiguration - > tpsErrorDetectionTooHigh ;
2020-04-05 06:10:08 -07:00
2021-11-17 00:54:21 -08:00
if ( ! engineConfiguration - > consumeObdSensors ) {
2021-04-05 12:57:03 -07:00
// primary TPS sensors
2021-11-17 00:54:21 -08:00
initTpsFunc ( tpsFunc1p , tpsSens1p , engineConfiguration - > tps1_1AdcChannel , engineConfiguration - > tpsMin , engineConfiguration - > tpsMax , min , max ) ;
initTpsFunc ( tpsFunc2p , tpsSens2p , engineConfiguration - > tps2_1AdcChannel , engineConfiguration - > tps2Min , engineConfiguration - > tps2Max , min , max ) ;
2021-04-05 12:57:03 -07:00
// Secondary TPS sensors (and redundant combining)
2021-11-17 00:54:21 -08:00
bool isFordTps = engineConfiguration - > useFordRedundantTps ;
initTpsFuncAndRedund ( tps1 , & fordTps1 , isFordTps , tpsFunc1s , tpsSens1s , engineConfiguration - > tps1_2AdcChannel , engineConfiguration - > tps1SecondaryMin , engineConfiguration - > tps1SecondaryMax , min , max ) ;
initTpsFuncAndRedund ( tps2 , & fordTps2 , isFordTps , tpsFunc2s , tpsSens2s , engineConfiguration - > tps2_2AdcChannel , engineConfiguration - > tps2SecondaryMin , engineConfiguration - > tps2SecondaryMax , min , max ) ;
2021-04-05 12:57:03 -07:00
// Pedal sensors
2021-11-17 00:54:21 -08:00
initTpsFunc ( pedalFuncPrimary , pedalSensorPrimary , engineConfiguration - > throttlePedalPositionAdcChannel , engineConfiguration - > throttlePedalUpVoltage , engineConfiguration - > throttlePedalWOTVoltage , min , max ) ;
initTpsFuncAndRedund ( pedal , nullptr , false , pedalFuncSecondary , pedalSensorSecondary , engineConfiguration - > throttlePedalPositionSecondAdcChannel , engineConfiguration - > throttlePedalSecondaryUpVoltage , engineConfiguration - > throttlePedalSecondaryWOTVoltage , min , max ) ;
2020-09-18 00:04:07 -07:00
// TPS-like stuff that isn't actually a TPS
2021-11-17 00:54:21 -08:00
initTpsFunc ( wastegateFunc , wastegateSens , engineConfiguration - > wastegatePositionSensor , engineConfiguration - > wastegatePositionMin , engineConfiguration - > wastegatePositionMax , min , max ) ;
initTpsFunc ( idlePosFunc , idlePosSens , engineConfiguration - > idlePositionSensor , engineConfiguration - > idlePositionMin , engineConfiguration - > idlePositionMax , min , max ) ;
2020-09-06 17:21:01 -07:00
}
2020-03-22 14:29:01 -07:00
// Route the pedal or TPS to driverIntent as appropriate
2021-11-17 00:54:21 -08:00
if ( isAdcChannelValid ( engineConfiguration - > throttlePedalPositionAdcChannel ) ) {
2020-03-22 14:29:01 -07:00
driverIntent . setProxiedSensor ( SensorType : : AcceleratorPedal ) ;
} else {
driverIntent . setProxiedSensor ( SensorType : : Tps1 ) ;
}
2020-04-01 17:21:03 -07:00
2020-12-06 12:00:30 -08:00
driverIntent . Register ( ) ;
2020-02-03 22:54:05 -08:00
}
2020-02-08 01:22:23 -08:00
2021-08-24 13:41:16 -07:00
void deinitTps ( ) {
AdcSubscription : : UnsubscribeSensor ( tpsSens1p ) ;
AdcSubscription : : UnsubscribeSensor ( tpsSens1s ) ;
2020-04-05 06:10:08 -07:00
2021-08-24 13:41:16 -07:00
AdcSubscription : : UnsubscribeSensor ( tpsSens2p ) ;
AdcSubscription : : UnsubscribeSensor ( tpsSens2s ) ;
2020-05-18 11:32:00 -07:00
2021-08-24 13:41:16 -07:00
AdcSubscription : : UnsubscribeSensor ( pedalSensorPrimary ) ;
AdcSubscription : : UnsubscribeSensor ( pedalSensorSecondary ) ;
2020-09-18 00:04:07 -07:00
2021-08-24 13:41:16 -07:00
AdcSubscription : : UnsubscribeSensor ( wastegateSens ) ;
AdcSubscription : : UnsubscribeSensor ( idlePosSens ) ;
2020-02-08 01:22:23 -08:00
}