2020-03-31 20:21:05 -07:00
|
|
|
/**
|
|
|
|
* @file can_sensor.h
|
|
|
|
*
|
|
|
|
* @date March 31, 2020
|
|
|
|
* @author Matthew Kennedy, (c) 2020
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "stored_value_sensor.h"
|
2022-09-04 20:11:19 -07:00
|
|
|
#include "efi_scaled_channel.h"
|
2020-08-31 19:47:33 -07:00
|
|
|
#include "can_msg_tx.h"
|
|
|
|
#include "obd2.h"
|
2020-09-06 20:39:25 -07:00
|
|
|
#include "can.h"
|
2021-03-31 19:51:49 -07:00
|
|
|
#include "can_listener.h"
|
2020-03-31 20:21:05 -07:00
|
|
|
|
2020-08-29 11:36:23 -07:00
|
|
|
/**
|
|
|
|
* Sensor which reads it's value from CAN
|
|
|
|
*/
|
2021-03-31 19:51:49 -07:00
|
|
|
class CanSensorBase : public StoredValueSensor, public CanListener {
|
2020-03-31 20:21:05 -07:00
|
|
|
public:
|
|
|
|
CanSensorBase(uint32_t eid, SensorType type, efitick_t timeout)
|
|
|
|
: StoredValueSensor(type, timeout)
|
2021-03-31 19:51:49 -07:00
|
|
|
, CanListener(eid)
|
2020-03-31 20:21:05 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:02:32 -07:00
|
|
|
void showInfo(const char* sensorName) const override;
|
2020-03-31 20:21:05 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TStorage, int TScale>
|
|
|
|
class CanSensor : public CanSensorBase {
|
|
|
|
public:
|
|
|
|
CanSensor(uint32_t eid, uint8_t offset, SensorType type, efitick_t timeout)
|
|
|
|
: CanSensorBase(eid, type, timeout)
|
|
|
|
, m_offset(offset)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void decodeFrame(const CANRxFrame& frame, efitick_t nowNt) override {
|
|
|
|
// Compute the location of our data within the frame
|
|
|
|
const uint8_t* dataLocation = &frame.data8[m_offset];
|
|
|
|
|
|
|
|
// Reinterpret as a scaled_channel - it already has the logic for decoding a scaled integer to a float
|
|
|
|
const auto scaler = reinterpret_cast<const scaled_channel<TStorage, TScale>*>(dataLocation);
|
|
|
|
|
|
|
|
// Actually do the conversion
|
|
|
|
float value = *scaler;
|
|
|
|
setValidValue(value, nowNt);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const uint8_t m_offset;
|
|
|
|
};
|
2020-08-31 01:39:27 -07:00
|
|
|
|
2022-03-15 12:52:50 -07:00
|
|
|
#if EFI_PROD_CODE
|
|
|
|
|
2020-09-06 20:39:25 -07:00
|
|
|
template <int Size, int Offset>
|
2020-08-31 19:47:33 -07:00
|
|
|
class ObdCanSensor: public CanSensorBase {
|
|
|
|
public:
|
2020-09-06 20:39:25 -07:00
|
|
|
ObdCanSensor(int PID, float Scale, SensorType type) :
|
2020-09-07 17:06:13 -07:00
|
|
|
CanSensorBase(OBD_TEST_RESPONSE, type, /* timeout, never expire */ 0) {
|
2020-09-06 19:36:31 -07:00
|
|
|
this->PID = PID;
|
2020-09-06 20:39:25 -07:00
|
|
|
this->Scale = Scale;
|
2020-08-31 19:47:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void decodeFrame(const CANRxFrame& frame, efitick_t nowNt) override {
|
2020-09-06 19:36:31 -07:00
|
|
|
if (frame.data8[2] != PID) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-31 19:47:33 -07:00
|
|
|
|
2020-09-06 20:19:32 -07:00
|
|
|
int iValue;
|
|
|
|
if (Size == 2) {
|
|
|
|
iValue = frame.data8[3] * 256 + frame.data8[4];
|
|
|
|
} else {
|
|
|
|
iValue = frame.data8[3];
|
|
|
|
}
|
2020-08-31 19:47:33 -07:00
|
|
|
|
2020-09-06 20:39:25 -07:00
|
|
|
float fValue = (1.0 * iValue / Scale) - Offset;
|
2020-09-06 20:19:32 -07:00
|
|
|
setValidValue(fValue, nowNt);
|
2020-08-31 19:47:33 -07:00
|
|
|
}
|
|
|
|
|
2021-03-31 19:51:49 -07:00
|
|
|
CanListener* request() override {
|
2020-08-31 19:47:33 -07:00
|
|
|
{
|
2022-05-09 13:49:23 -07:00
|
|
|
CanTxMessage msg(CanCategory::OBD, OBD_TEST_REQUEST);
|
2020-08-31 19:47:33 -07:00
|
|
|
msg[0] = _OBD_2;
|
|
|
|
msg[1] = OBD_CURRENT_DATA;
|
2020-09-06 20:19:32 -07:00
|
|
|
msg[2] = PID;
|
2020-08-31 19:47:33 -07:00
|
|
|
}
|
2020-09-07 13:25:00 -07:00
|
|
|
// let's sleep on write update after each OBD request, this would give read thread a chance to read response
|
|
|
|
// todo: smarter logic of all this with with semaphore not just sleep
|
|
|
|
chThdSleepMilliseconds(300);
|
2021-03-31 19:51:49 -07:00
|
|
|
return CanListener::request();
|
2020-08-31 19:47:33 -07:00
|
|
|
}
|
2020-09-06 19:36:31 -07:00
|
|
|
|
|
|
|
int PID;
|
2020-09-06 20:39:25 -07:00
|
|
|
float Scale;
|
2020-08-31 01:39:27 -07:00
|
|
|
};
|
2022-03-15 12:52:50 -07:00
|
|
|
|
|
|
|
#endif // EFI_PROD_CODE
|