From bcf17020ba7644f14c78cea934b24db5293b31a6 Mon Sep 17 00:00:00 2001 From: Steven Maresca Date: Sun, 23 Jun 2024 14:55:08 -0400 Subject: [PATCH] Precommit formatting tweaks --- Software/src/battery/CHADEMO-BATTERY.cpp | 4 +- Software/src/battery/CHADEMO-SHUNTS.cpp | 347 +++++++++++++++++++++++ Software/src/battery/CHADEMO-SHUNTS.h | 16 ++ 3 files changed, 365 insertions(+), 2 deletions(-) create mode 100644 Software/src/battery/CHADEMO-SHUNTS.cpp create mode 100644 Software/src/battery/CHADEMO-SHUNTS.h diff --git a/Software/src/battery/CHADEMO-BATTERY.cpp b/Software/src/battery/CHADEMO-BATTERY.cpp index 04dbc790..e473a7c8 100644 --- a/Software/src/battery/CHADEMO-BATTERY.cpp +++ b/Software/src/battery/CHADEMO-BATTERY.cpp @@ -5,8 +5,8 @@ #include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h" #include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" #include "CHADEMO-BATTERY-INTERNAL.h" -#include "CHADEMO-SHUNTS.h" #include "CHADEMO-BATTERY.h" +#include "CHADEMO-SHUNTS.h" /* CHADEMO handling runs at 6.25 times the rate of most other code, so, rather than the * default value of 12 (for 12 iterations of the 5s value update loop) * 5 for a 60s timeout, @@ -23,7 +23,7 @@ static unsigned long handlerAfterMillis = 0; /* Do not change code below unless you are sure what you are doing */ static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send static unsigned long previousMillis5000 = - 0; // will store last time a 5s threshold was reached for display during debug + 0; // will store last time a 5s threshold was reached for display during debug bool plug_inserted = false; bool vehicle_can_initialized = false; diff --git a/Software/src/battery/CHADEMO-SHUNTS.cpp b/Software/src/battery/CHADEMO-SHUNTS.cpp new file mode 100644 index 00000000..d857e6f4 --- /dev/null +++ b/Software/src/battery/CHADEMO-SHUNTS.cpp @@ -0,0 +1,347 @@ +/* This library supports ISA Scale IVT Modular current/voltage sensor device. These devices measure current, up to three voltages, and provide temperature compensation. + + + + This library was written by Jack Rickard of EVtv - http://www.evtv.me + copyright 2014 + You are licensed to use this library for any purpose, commercial or private, + without restriction. + + 2024 - Modified to make use of ESP32-Arduino-CAN by miwagner + +*/ +#include "../include.h" +#ifdef CHADEMO_BATTERY +#include "../datalayer/datalayer.h" +#include "../devboard/utils/events.h" +#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h" +#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" +#include "CHADEMO-BATTERY-INTERNAL.h" +#include "CHADEMO-BATTERY.h" +#include "CHADEMO-SHUNTS.h" + +static int framecount = 0; + +float Amperes; // Floating point with current in Amperes +double AH; //Floating point with accumulated ampere-hours +double KW; +double KWH; + +double Voltage; +double Voltage1; +double Voltage2; +double Voltage3; +double VoltageHI; +double Voltage1HI; +double Voltage2HI; +double Voltage3HI; +double VoltageLO; +double Voltage1LO; +double Voltage2LO; +double Voltage3LO; + +double Temperature; + +bool firstframe; +unsigned long timestamp; +double milliamps; +long watt; +long As; +long lastAs; +long wh; +long lastWh; +uint8_t page; + +CAN_frame_t outframe = {.FIR = {.B = + { + .DLC = 8, + .unknown_2 = 0, + .RTR = CAN_no_RTR, + .FF = CAN_frame_std, + }}, + + .MsgID = 0x411, + .data = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; + +uint16_t get_measured_voltage() { + return (uint16_t)Voltage; +} + +uint16_t get_measured_current() { + return (uint16_t)Amperes; +} + +//This is our CAN interrupt service routine to catch inbound frames +inline void ISA_handleFrame(CAN_frame_t* frame) { + + if (frame->MsgID < 0x521 || frame->MsgID > 0x528) { + return; + } + + framecount++; + + switch (frame->MsgID) { + case 0x511: + break; + + case 0x521: + ISA_handle521(frame); + break; + + case 0x522: + ISA_handle522(frame); + break; + + case 0x523: + ISA_handle523(frame); + break; + + case 0x524: + ISA_handle524(frame); + break; + + case 0x525: + ISA_handle525(frame); + break; + + case 0x526: + ISA_handle526(frame); + break; + + case 0x527: + ISA_handle527(frame); + break; + + case 0x528: + ISA_handle528(frame); + break; + } + + return; +} + +//handle frame for Amperes +inline void ISA_handle521(CAN_frame_t* frame) { + long current = 0; + current = + (long)((frame->data.u8[5] << 24) | (frame->data.u8[4] << 16) | (frame->data.u8[3] << 8) | (frame->data.u8[2])); + + milliamps = current; + Amperes = current / 1000.0f; +} + +//handle frame for Voltage +inline void ISA_handle522(CAN_frame_t* frame) { + long volt = + (long)((frame->data.u8[5] << 24) | (frame->data.u8[4] << 16) | (frame->data.u8[3] << 8) | (frame->data.u8[2])); + + Voltage = volt / 1000.0f; + Voltage1 = Voltage - (Voltage2 + Voltage3); + + if (framecount < 150) { + VoltageLO = Voltage; + Voltage1LO = Voltage1; + } else { + if (Voltage < VoltageLO) + VoltageLO = Voltage; + if (Voltage > VoltageHI) + VoltageHI = Voltage; + if (Voltage1 < Voltage1LO) + Voltage1LO = Voltage1; + if (Voltage1 > Voltage1HI) + Voltage1HI = Voltage1; + } +} + +//handle frame for Voltage 2 +inline void ISA_handle523(CAN_frame_t* frame) { + long volt = + (long)((frame->data.u8[5] << 24) | (frame->data.u8[4] << 16) | (frame->data.u8[3] << 8) | (frame->data.u8[2])); + + Voltage2 = volt / 1000.0f; + if (Voltage2 > 3) + Voltage2 -= Voltage3; + + if (framecount < 150) { + Voltage2LO = Voltage2; + } else { + if (Voltage2 < Voltage2LO) + Voltage2LO = Voltage2; + if (Voltage2 > Voltage2HI) + Voltage2HI = Voltage2; + } +} + +//handle frame for Voltage3 +inline void ISA_handle524(CAN_frame_t* frame) { + long volt = + (long)((frame->data.u8[5] << 24) | (frame->data.u8[4] << 16) | (frame->data.u8[3] << 8) | (frame->data.u8[2])); + + Voltage3 = volt / 1000.0f; + + if (framecount < 150) { + Voltage3LO = Voltage3; + } else { + if (Voltage3 < Voltage3LO && Voltage3 > 10) + Voltage3LO = Voltage3; + if (Voltage3 > Voltage3HI) + Voltage3HI = Voltage3; + } +} + +//handle frame for Temperature +inline void ISA_handle525(CAN_frame_t* frame) { + long temp = 0; + temp = (long)((frame->data.u8[5] << 24) | (frame->data.u8[4] << 16) | (frame->data.u8[3] << 8) | (frame->data.u8[2])); + + Temperature = temp / 10; +} + +//handle frame for Kilowatts +inline void ISA_handle526(CAN_frame_t* frame) { + watt = 0; + watt = (long)((frame->data.u8[5] << 24) | (frame->data.u8[4] << 16) | (frame->data.u8[3] << 8) | (frame->data.u8[2])); + KW = watt / 1000.0f; +} + +//handle frame for Ampere-Hours +inline void ISA_handle527(CAN_frame_t* frame) { + As = 0; + As = (frame->data.u8[5] << 24) | (frame->data.u8[4] << 16) | (frame->data.u8[3] << 8) | (frame->data.u8[2]); + + AH += (As - lastAs) / 3600.0f; + lastAs = As; +} + +//handle frame for kiloWatt-hours +inline void ISA_handle528(CAN_frame_t* frame) { + wh = (long)((frame->data.u8[5] << 24) | (frame->data.u8[4] << 16) | (frame->data.u8[3] << 8) | (frame->data.u8[2])); + KWH += (wh - lastWh) / 1000.0f; + lastWh = wh; +} + +/* +void ISA_initialize() { + firstframe=false; + STOP(); + delay(700); + for(int i=0;i<9;i++) { + Serial.println("initialization \n"); + + outframe.data.u8[0]=(0x20+i); + outframe.data.u8[1]=0x42; + outframe.data.u8[2]=0x02; + outframe.data.u8[3]=(0x60+(i*18)); + outframe.data.u8[4]=0x00; + outframe.data.u8[5]=0x00; + outframe.data.u8[6]=0x00; + outframe.data.u8[7]=0x00; + + ESP32Can.CANWriteFrame(&outframe); + + delay(500); + + sendSTORE(); + delay(500); + } + + START(); + delay(500); + lastAs=As; + lastWh=wh; + +} + +void ISA_STOP() { + outframe.data.u8[0]=0x34; + outframe.data.u8[1]=0x00; + outframe.data.u8[2]=0x01; + outframe.data.u8[3]=0x00; + outframe.data.u8[4]=0x00; + outframe.data.u8[5]=0x00; + outframe.data.u8[6]=0x00; + outframe.data.u8[7]=0x00; + ESP32Can.CANWriteFrame(&outframe); + +} + +void ISA_sendSTORE() { + outframe.data.u8[0]=0x32; + outframe.data.u8[1]=0x00; + outframe.data.u8[2]=0x00; + outframe.data.u8[3]=0x00; + outframe.data.u8[4]=0x00; + outframe.data.u8[5]=0x00; + outframe.data.u8[6]=0x00; + outframe.data.u8[7]=0x00; + ESP32Can.CANWriteFrame(&outframe); +} + +void ISA_START() { + outframe.data.u8[0]=0x34; + outframe.data.u8[1]=0x01; + outframe.data.u8[2]=0x01; + outframe.data.u8[3]=0x00; + outframe.data.u8[4]=0x00; + outframe.data.u8[5]=0x00; + outframe.data.u8[6]=0x00; + outframe.data.u8[7]=0x00; + ESP32Can.CANWriteFrame(&outframe); +} + +void ISA_RESTART() { + //Has the effect of zeroing AH and KWH + outframe.data.u8[0]=0x3F; + outframe.data.u8[1]=0x00; + outframe.data.u8[2]=0x00; + outframe.data.u8[3]=0x00; + outframe.data.u8[4]=0x00; + outframe.data.u8[5]=0x00; + outframe.data.u8[6]=0x00; + outframe.data.u8[7]=0x00; + ESP32Can.CANWriteFrame(&outframe); +} + +void ISA_deFAULT() { + //Returns module to original defaults + outframe.data.u8[0]=0x3D; + outframe.data.u8[1]=0x00; + outframe.data.u8[2]=0x00; + outframe.data.u8[3]=0x00; + outframe.data.u8[4]=0x00; + outframe.data.u8[5]=0x00; + outframe.data.u8[6]=0x00; + outframe.data.u8[7]=0x00; + ESP32Can.CANWriteFrame(&outframe); +} + +void ISA_initCurrent() { + STOP(); + delay(500); + + Serial.println("initialization \n"); + + outframe.data.u8[0]=0x21; + outframe.data.u8[1]=0x42; + outframe.data.u8[2]=0x01; + outframe.data.u8[3]=0x61; + outframe.data.u8[4]=0x00; + outframe.data.u8[5]=0x00; + outframe.data.u8[6]=0x00; + outframe.data.u8[7]=0x00; + + ESP32Can.CANWriteFrame(&outframe); + + delay(500); + + sendSTORE(); + delay(500); + + START(); + delay(500); + lastAs=As; + lastWh=wh; +} +*/ + +#endif diff --git a/Software/src/battery/CHADEMO-SHUNTS.h b/Software/src/battery/CHADEMO-SHUNTS.h new file mode 100644 index 00000000..c620aca0 --- /dev/null +++ b/Software/src/battery/CHADEMO-SHUNTS.h @@ -0,0 +1,16 @@ +#ifndef CHADEMO_SHUNTS_H +#define CHADEMO_SHUNTS_H + +uint16_t get_measured_voltage(); +uint16_t get_measured_current(); +inline void ISA_handler(CAN_frame_t* frame); +inline void ISA_handle521(CAN_frame_t* frame); +inline void ISA_handle522(CAN_frame_t* frame); +inline void ISA_handle523(CAN_frame_t* frame); +inline void ISA_handle524(CAN_frame_t* frame); +inline void ISA_handle525(CAN_frame_t* frame); +inline void ISA_handle526(CAN_frame_t* frame); +inline void ISA_handle527(CAN_frame_t* frame); +inline void ISA_handle528(CAN_frame_t* frame); + +#endif