fome-fw/firmware/controllers/can/can_vss.cpp

112 lines
2.7 KiB
C++
Raw Normal View History

/**
* @file can_vss.cpp
*
2021-07-30 12:49:08 -07:00
* This file handles incoming vss values from can.
*
* @date Apr 19, 2020
* @author Alex Miculescu, (c) 2020
*/
#include "pch.h"
#if EFI_CAN_SUPPORT
#include "can.h"
#include "dynoview.h"
2021-10-24 16:04:47 -07:00
#include "stored_value_sensor.h"
static bool isInit = false;
static uint16_t filterCanID = 0;
2023-04-12 00:57:09 -07:00
static expected<uint16_t> look_up_can_id(can_vss_nbc_e type) {
2021-10-24 16:04:47 -07:00
switch (type) {
case BMW_e46:
return 0x01F0; /* BMW e46 ABS Message */
2023-11-26 16:51:29 -08:00
case BMW_e90:
return 0x10A; // BMW E90 ABS speed frame (not wheel speeds, vehicle speed)
2021-10-24 16:04:47 -07:00
case W202:
return 0x0200; /* W202 C180 ABS signal */
default:
firmwareError(ObdCode::OBD_Vehicle_Speed_SensorB, "Wrong Can DBC selected: %d", type);
2021-10-24 16:04:47 -07:00
return unexpected;
}
}
/* Module specitifc processing functions */
/* source: http://z4evconversion.blogspot.com/2016/07/completely-forgot-but-it-does-live-on.html */
2021-10-24 16:04:47 -07:00
float processBMW_e46(const CANRxFrame& frame) {
2023-04-12 00:57:09 -07:00
// average the rear wheels since those are the driven ones (more accurate gear detection!)
2023-04-12 23:51:26 -07:00
uint16_t left = (((frame.data8[5] & 0x0f) << 8) | frame.data8[4]);
uint16_t right = (((frame.data8[7] & 0x0f) << 8) | frame.data8[6]);
2023-04-12 00:57:09 -07:00
return (left + right) / (16 * 2);
}
2023-11-26 16:51:29 -08:00
float processBMW_e90(const CANRxFrame& frame) {
uint8_t low = frame.data8[0];
uint8_t high = frame.data8[1] & 0x0F;
return (low | high << 8);
}
2021-10-24 16:04:47 -07:00
float processW202(const CANRxFrame& frame) {
uint16_t tmp = (frame.data8[2] << 8);
tmp |= frame.data8[3];
return tmp * 0.0625;
}
/* End of specific processing functions */
2021-10-24 16:04:47 -07:00
expected<float> processCanRxVssImpl(const CANRxFrame& frame) {
switch (engineConfiguration->canVssNbcType){
2021-10-24 16:04:47 -07:00
case BMW_e46:
return processBMW_e46(frame);
2023-11-26 16:51:29 -08:00
case BMW_e90:
return processBMW_e90(frame);
2021-10-24 16:04:47 -07:00
case W202:
return processW202(frame);
default:
efiPrintf("vss unsupported can option selected %x", engineConfiguration->canVssNbcType );
2021-10-24 16:04:47 -07:00
}
return unexpected;
}
2021-10-24 16:04:47 -07:00
static StoredValueSensor canSpeed(SensorType::VehicleSpeed, MS2NT(500));
2021-10-24 16:04:47 -07:00
void processCanRxVss(const CANRxFrame& frame, efitick_t nowNt) {
if ((!engineConfiguration->enableCanVss) || (!isInit)) {
2021-10-24 16:04:47 -07:00
return;
}
2021-10-24 16:04:47 -07:00
//filter it we need to process the can message or not
if (CAN_SID(frame) != filterCanID ) {
return;
}
2021-10-24 16:04:47 -07:00
if (auto speed = processCanRxVssImpl(frame)) {
2023-09-18 00:50:35 -07:00
canSpeed.setValidValue(speed.Value * engineConfiguration->canVssScaling, nowNt);
2021-10-24 16:04:47 -07:00
#if EFI_DYNO_VIEW
updateDynoViewCan();
2021-10-24 16:04:47 -07:00
#endif
}
}
2021-10-24 16:04:47 -07:00
void initCanVssSupport() {
if (engineConfiguration->enableCanVss) {
if (auto canId = look_up_can_id(engineConfiguration->canVssNbcType)) {
2021-10-24 16:04:47 -07:00
filterCanID = canId.Value;
canSpeed.Register();
isInit = true;
} else {
isInit = false;
}
}
}
void setCanVss(int type) {
engineConfiguration->canVssNbcType = (can_vss_nbc_e)type;
}
2023-04-12 23:51:26 -07:00
#endif // EFI_CAN_SUPPORT