mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 02:39:57 +02:00
Precommit formatting tweaks
This commit is contained in:
parent
95ef27ab29
commit
bcf17020ba
3 changed files with 365 additions and 2 deletions
|
@ -5,8 +5,8 @@
|
||||||
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
|
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
|
||||||
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
|
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
|
||||||
#include "CHADEMO-BATTERY-INTERNAL.h"
|
#include "CHADEMO-BATTERY-INTERNAL.h"
|
||||||
#include "CHADEMO-SHUNTS.h"
|
|
||||||
#include "CHADEMO-BATTERY.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
|
/* 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,
|
* default value of 12 (for 12 iterations of the 5s value update loop) * 5 for a 60s timeout,
|
||||||
|
|
347
Software/src/battery/CHADEMO-SHUNTS.cpp
Normal file
347
Software/src/battery/CHADEMO-SHUNTS.cpp
Normal file
|
@ -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
|
16
Software/src/battery/CHADEMO-SHUNTS.h
Normal file
16
Software/src/battery/CHADEMO-SHUNTS.h
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue