Handle can message cyclicity (#2573)

* Handle can message cyclicity

update of canDash to support different cyclicity of messages

* review updates
This commit is contained in:
shadowm60 2021-04-24 03:16:27 +03:00 committed by GitHub
parent 466e0da406
commit 61446a6170
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 376 additions and 247 deletions

View File

@ -16,6 +16,25 @@
#define CAN_TIMEOUT MS2NT(100)
//can tx periodic task cycle time in frequency, 200hz -> 5ms period
#define CAN_CYCLE_FREQ (200.0f)
//can tx periodic task cycle time in ms
#define CAN_CYCLE_PERIOD (CH_CFG_ST_FREQUENCY / CAN_CYCLE_FREQ)
enum class CanInterval : uint16_t {
None = 0,
_5ms = 1 << 0,
_10ms = 1 << 1,
_20ms = 1 << 2,
_50ms = 1 << 3,
_100ms = 1 << 4,
_200ms = 1 << 5,
_250ms = 1 << 6,
_500ms = 1 << 7,
_1000ms = 1 << 8,
_MAX_Cycle = _1000ms,
};
class CanListener;
class CanSensorBase;
@ -39,6 +58,42 @@ public:
void PeriodicTask(efitime_t nowNt) override;
};
// allow using shorthand CI
using CI = CanInterval;
// logical and/or operators so we can use our enum like an int
constexpr CI operator |(CI lhs, CI rhs) {
using T = std::underlying_type_t<CI>;
return static_cast<CI>(static_cast<T>(lhs) | static_cast<T>(rhs));
}
constexpr CI operator &(CI lhs, CI rhs) {
using T = std::underlying_type_t<CI>;
return static_cast<CI>(static_cast<T>(lhs) & static_cast<T>(rhs));
}
constexpr CI& operator |=(CI& lhs, CI rhs) {
lhs = lhs | rhs;
return lhs;
}
class CanCycle {
public:
explicit CanCycle(uint32_t cycleCounter200hz)
: m_cycleFlags(computeFlags(cycleCounter200hz))
{
}
bool isInterval(CanInterval interval) {
return CanInterval::None != (m_cycleFlags & interval);
}
private:
static CanInterval computeFlags(uint32_t cycleCount);
const CanInterval m_cycleFlags;
};
// We need these helpers because the frame layout is different on STM32H7
#ifdef STM32H7XX
#define CAN_SID(f) ((f).std.SID)

View File

@ -40,16 +40,18 @@ EXTERN_ENGINE;
#define CAN_MAZDA_RX_STEERING_WARNING 0x300
#define CAN_MAZDA_RX_STATUS_1 0x212
#define CAN_MAZDA_RX_STATUS_2 0x420
// https://wiki.openstreetmap.org/wiki/VW-CAN
#define CAN_VAG_RPM 0x280
#define CAN_VAG_CLT 0x288
#define CAN_VAG_CLT_V2 0x420
#define CAN_VAG_IMMO 0x3D0
#define CAN_VAG_RPM 0x280 /* _10ms cycle */
#define CAN_VAG_CLT 0x288 /* _10ms cycle */
#define CAN_VAG_CLT_V2 0x420 /* _10ms cycle */
#define CAN_VAG_IMMO 0x3D0 /* _10ms cycle */
//w202 DASH
#define W202_STAT_1 0x308
#define W202_STAT_2 0x608
#define W202_ALIVE 0x210
#define W202_STAT_3 0x310
#define W202_STAT_1 0x308 /* _20ms cycle */
#define W202_STAT_2 0x608 /* _100ms cycle */
#define W202_ALIVE 0x210 /* _200ms cycle */
#define W202_STAT_3 0x310 /* _200ms cycle */
//BMW E90 DASH
#define E90_ABS_COUNTER 0x0C0
@ -66,10 +68,9 @@ EXTERN_ENGINE;
// Nissan z33 350Z and else
// 0x23d = 573
#define NISSAN_RPM_CLT 0x23d
#define NISSAN_RPM_CLT 0x23D
static uint8_t rpmcounter;
static uint16_t e90msgcounter;
static uint8_t seatbeltcnt;
static uint8_t abscounter = 0xF0;
static uint8_t brakecnt_1 = 0xF0, brakecnt_2 = 0xF0;
@ -81,8 +82,49 @@ static bool cluster_time_set;
constexpr uint8_t e90_temp_offset = 49;
void canDashboardBMW(void) {
//BMW Dashboard
void canDashboardBMW(CanCycle cycle);
void canDashboardFiat(CanCycle cycle);
void canDashboardVAG(CanCycle cycle);
void canMazdaRX8(CanCycle cycle);
void canDashboardW202(CanCycle cycle);
void canDashboardBMWE90(CanCycle cycle);
void canDashboardVagMqb(CanCycle cycle);
void updateDash(CanCycle cycle) {
// Transmit dash data, if enabled
switch (CONFIG(canNbcType)) {
case CAN_BUS_NBC_BMW:
canDashboardBMW(cycle);
break;
case CAN_BUS_NBC_FIAT:
canDashboardFiat(cycle);
break;
case CAN_BUS_NBC_VAG:
canDashboardVAG(cycle);
break;
case CAN_BUS_MAZDA_RX8:
canMazdaRX8(cycle);
break;
case CAN_BUS_W202_C180:
canDashboardW202(cycle);
break;
case CAN_BUS_BMW_E90:
canDashboardBMWE90(cycle);
break;
case CAN_BUS_MQB:
canDashboardVagMqb(cycle);
break;
default:
break;
}
}
//BMW Dashboard
//todo: we use 50ms fixed cycle, trace is needed to check for correct period
void canDashboardBMW(CanCycle cycle) {
if (cycle.isInterval(CI::_50ms)) {
{
CanTxMessage msg(CAN_BMW_E46_SPEED);
msg.setShortValue(10 * 8, 1);
@ -97,9 +139,12 @@ void canDashboardBMW(void) {
CanTxMessage msg(CAN_BMW_E46_DME2);
msg.setShortValue((int) ((Sensor::get(SensorType::Clt).value_or(0) + 48.373) / 0.75), 1);
}
}
}
void canMazdaRX8(void) {
//todo: we use 50ms fixed cycle, trace is needed to check for correct period
void canMazdaRX8(CanCycle cycle) {
if (cycle.isInterval(CI::_50ms)) {
{
CanTxMessage msg(CAN_MAZDA_RX_STEERING_WARNING);
// todo: something needs to be set here? see http://rusefi.com/wiki/index.php?title=Vehicle:Mazda_Rx8_2004
@ -149,18 +194,23 @@ void canMazdaRX8(void) {
//oil pressure warning lamp bit is 7
msg[7] = 0x00; //unused
}
}
}
void canDashboardFiat(void) {
void canDashboardFiat(CanCycle cycle) {
if (cycle.isInterval(CI::_50ms)) {
{
//Fiat Dashboard
CanTxMessage msg(CAN_FIAT_MOTOR_INFO);
msg.setShortValue((int) (Sensor::get(SensorType::Clt).value_or(0) - 40), 3); //Coolant Temp
msg.setShortValue(GET_RPM() / 32, 6); //RPM
}
}
}
void canDashboardVAG(void) {
void canDashboardVAG(CanCycle cycle) {
if (cycle.isInterval(CI::_10ms)) {
{
//VAG Dashboard
CanTxMessage msg(CAN_VAG_RPM);
@ -183,9 +233,11 @@ void canDashboardVAG(void) {
CanTxMessage msg(CAN_VAG_IMMO);
msg.setShortValue(0x80, 1);
}
}
}
void canDashboardW202(void) {
void canDashboardW202(CanCycle cycle) {
if (cycle.isInterval(CI::_20ms)) {
{
CanTxMessage msg(W202_STAT_1);
uint16_t tmp = GET_RPM();
@ -198,7 +250,9 @@ void canDashboardW202(void) {
msg[6] = 0x00; // Unknown - oil info
msg[7] = 0x00; // Unknown - oil info
}
}
if (cycle.isInterval(CI::_100ms)) {
{
CanTxMessage msg(W202_STAT_2); //dlc 7
msg[0] = (int)(Sensor::get(SensorType::Clt).value_or(0) + 40); // CLT -40 offset
@ -210,7 +264,9 @@ void canDashboardW202(void) {
msg[6] = 0x50; // TBD
msg[7] = 0x00; // Unknown
}
}
if (cycle.isInterval(CI::_200ms)) {
{
CanTxMessage msg(W202_ALIVE);
msg[0] = 0x0A; // Const
@ -234,30 +290,33 @@ void canDashboardW202(void) {
msg[6] = 0x33; // Const
msg[7] = 0x05; // Const
}
}
}
/**
* https://docs.google.com/spreadsheets/d/1XMfeGlhgl0lBL54lNtPdmmFd8gLr2T_YTriokb30kJg
*/
void canDashboardVagMqb() {
void canDashboardVagMqb(CanCycle cycle) {
if (cycle.isInterval(CI::_50ms)) {
{ // 'turn-on'
CanTxMessage msg(0x3C0, 4);
// ignition ON
msg[2] = 3;
}
{ //RPM
CanTxMessage msg(0x107, 8);
msg[3] = ((int)(GET_RPM() / 3.5)) & 0xFF;
msg[4] = ((int)(GET_RPM() / 3.5)) >> 8;
}
}
}
void canDashboardBMWE90()
void canDashboardBMWE90(CanCycle cycle)
{
if (e90msgcounter == UINT16_MAX)
e90msgcounter = 0;
e90msgcounter++;
if (cycle.isInterval(CI::_50ms)) {
{ //T15 'turn-on'
CanTxMessage msg(E90_T15, 5);
@ -298,9 +357,11 @@ void canDashboardBMWE90()
msg[6] = 0x0D;
msg[7] = 0xA8;
}
}
{ //Seatbelt counter
if (e90msgcounter % 2) {
if (cycle.isInterval(CI::_100ms)) {
{
//Seatbelt counter
seatbeltcnt++;
if (seatbeltcnt > 0xFE)
seatbeltcnt = 0x00;
@ -308,10 +369,9 @@ void canDashboardBMWE90()
msg[0] = seatbeltcnt;
msg[1] = 0xFF;
}
}
{ //Brake counter
if (e90msgcounter % 2) {
{
//Brake counter 100ms
brakecnt_1 += 16;
brakecnt_2 += 16;
if (brakecnt_1 > 0xEF)
@ -328,10 +388,8 @@ void canDashboardBMWE90()
msg[6] = 0x00;
msg[7] = brakecnt_2;
}
}
{ //ABS counter
if (e90msgcounter % 2) {
abscounter++;
if (abscounter > 0xFE)
abscounter = 0xF0;
@ -339,10 +397,8 @@ void canDashboardBMWE90()
msg[0] = abscounter;
msg[1] = 0xFF;
}
}
{ //Fuel gauge
if (e90msgcounter % 2) {
CanTxMessage msg(E90_FUEL, 5); //fuel gauge
msg[0] = 0x76;
msg[1] = 0x0F;
@ -350,10 +406,8 @@ void canDashboardBMWE90()
msg[3] = 0x1A;
msg[4] = 0x00;
}
}
{ //Gear indicator/counter
if (e90msgcounter % 2) {
gear_cnt++;
if (gear_cnt >= 0x0F)
gear_cnt = 0x00;
@ -365,10 +419,8 @@ void canDashboardBMWE90()
msg[4] = 0xF1;
msg[5] = 0xFF;
}
}
{ //E90_SPEED
if (e90msgcounter % 2) {
float mph = getVehicleSpeed() * 0.6213712;
mph_ctr = ((TIME_I2MS(chVTGetSystemTime()) - mph_timer) / 50);
mph_a = (mph_ctr * mph / 2);

View File

@ -6,11 +6,6 @@
*/
#pragma once
#include "can.h"
void canDashboardBMW();
void canDashboardFiat();
void canDashboardVAG();
void canMazdaRX8();
void canDashboardW202();
void canDashboardBMWE90();
void canDashboardVagMqb();
void updateDash(CanCycle cycle);

View File

@ -23,17 +23,25 @@ EXTERN_ENGINE;
extern CanListener* canListeners_head;
CanWrite::CanWrite()
: PeriodicController("CAN TX", PRIO_CAN_TX, 50)
: PeriodicController("CAN TX", PRIO_CAN_TX, CAN_CYCLE_FREQ)
{
}
void CanWrite::PeriodicTask(efitime_t nowNt) {
UNUSED(nowNt);
static uint16_t cycleCount = 0;
CanCycle cycle(cycleCount);
//in case we have Verbose Can enabled, we should keep user configured period
if (CONFIG(enableVerboseCanTx)) {
uint16_t cycleCountsPeriodMs = cycleCount * CAN_CYCLE_PERIOD;
if (0 != CONFIG(canSleepPeriodMs)) {
if (cycleCountsPeriodMs % CONFIG(canSleepPeriodMs)) {
void sendCanVerbose();
sendCanVerbose();
}
}
}
CanListener* current = canListeners_head;
@ -41,32 +49,52 @@ void CanWrite::PeriodicTask(efitime_t nowNt) {
current = current->request();
}
// Transmit dash data, if enabled
switch (CONFIG(canNbcType)) {
case CAN_BUS_NBC_BMW:
canDashboardBMW();
break;
case CAN_BUS_NBC_FIAT:
canDashboardFiat();
break;
case CAN_BUS_NBC_VAG:
canDashboardVAG();
break;
case CAN_BUS_MAZDA_RX8:
canMazdaRX8();
break;
case CAN_BUS_W202_C180:
canDashboardW202();
break;
case CAN_BUS_BMW_E90:
canDashboardBMWE90();
break;
case CAN_BUS_MQB:
canDashboardVagMqb();
break;
default:
break;
if (cycle.isInterval(CI::_MAX_Cycle)) {
//we now reset cycleCount since we reached max cycle count
cycleCount = 0;
}
updateDash(cycle);
cycleCount++;
}
CanInterval CanCycle::computeFlags(uint32_t cycleCount) {
CanInterval cycleMask = CanInterval::_5ms;
if ((cycleCount % 2) == 0) {
cycleMask |= CI::_10ms;
}
if ((cycleCount % 4) == 0) {
cycleMask |= CI::_20ms;
}
if ((cycleCount % 10) == 0) {
cycleMask |= CI::_50ms;
}
if ((cycleCount % 20) == 0) {
cycleMask |= CI::_100ms;
}
if ((cycleCount % 40) == 0) {
cycleMask |= CI::_200ms;
}
if ((cycleCount % 50) == 0) {
cycleMask |= CI::_250ms;
}
if ((cycleCount % 100) == 0) {
cycleMask |= CI::_500ms;
}
if ((cycleCount % 200) == 0) {
cycleMask |= CI::_1000ms;
}
return cycleMask;
}
#endif // EFI_CAN_SUPPORT

View File

@ -278,7 +278,6 @@ void initCan(void) {
// fire up threads, as necessary
if (CONFIG(canWriteEnabled)) {
canWrite.setPeriod(CONFIG(canSleepPeriodMs));
canWrite.Start();
}