Merge pull request #303 from smaresca/smaresca/integrate-SimpleISA

Add SimpleISA library for use in measuring current/voltage
This commit is contained in:
Daniel Öster 2024-05-16 21:20:31 +03:00 committed by GitHub
commit 44efc43251
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 712 additions and 0 deletions

View file

@ -0,0 +1,4 @@
http://www.digikey.com/short/3c2wwr
This digikey shopping cart contains all the connectors and pins
for the ISA IVT-1K-U3-TOI-CAN2-12 Current Sensor

View file

@ -0,0 +1,9 @@
# SimpleISA
Simple library for IVT shunts.
Based on the EVTV library of 2016, revised for use with CHAdeMO.
Originally intended to integrate with Arduino Due.
Adapted for ESP32 and ESP32-Arduino-CAN for use in the Battery-Emulator project https://github.com/dalathegreat/Battery-Emulator
hosted at https://github.com/smaresca/SimpleISA-ESP32-Arduino-CAN
Derived from https://github.com/isaac96/simpleISA/ and https://github.com/damienmaguire/SimpleISA/

View file

@ -0,0 +1,396 @@
/* 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 "SimpleISA.h"
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; }
ISA::ISA() // Define the constructor.
{
timestamp = millis();
debug=false;
debug2=false;
framecount=0;
firstframe=true;
}
ISA::~ISA() //Define destructor
{
}
void ISA::begin(int Port, int speed)
{
}
void ISA::handleFrame(CAN_frame_t *frame)
//This is our CAN interrupt service routine to catch inbound frames
{
switch (frame->MsgID)
{
case 0x511:
break;
case 0x521:
handle521(frame);
break;
case 0x522:
handle522(frame);
break;
case 0x523:
handle523(frame);
break;
case 0x524:
handle524(frame);
break;
case 0x525:
handle525(frame);
break;
case 0x526:
handle526(frame);
break;
case 0x527:
handle527(frame);
break;
case 0x528:
handle528(frame);
break;
}
if(debug)printCAN(frame);
}
void ISA::handle521(CAN_frame_t *frame) //AMperes
{
framecount++;
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;
if(debug2)Serial<<"Current: "<<Amperes<<" amperes "<<milliamps<<" ma frames:"<<framecount<<"\n";
}
void ISA::handle522(CAN_frame_t *frame) //Voltage
{
framecount++;
long volt=0;
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;
}
if(Voltage<VoltageLO && framecount>150)VoltageLO=Voltage;
if(Voltage>VoltageHI && framecount>150)VoltageHI=Voltage;
if(Voltage1<Voltage1LO && framecount>150)Voltage1LO=Voltage1;
if(Voltage1>Voltage1HI && framecount>150)Voltage1HI=Voltage1;
if(debug2)Serial<<"Voltage: "<<Voltage<<" vdc Voltage 1: "<<Voltage1<<" vdc "<<volt<<" mVdc frames:"<<framecount<<"\n";
}
void ISA::handle523(CAN_frame_t *frame) //Voltage2
{
framecount++;
long volt=0;
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;
if(Voltage2<Voltage2LO && framecount>150)Voltage2LO=Voltage2;
if(Voltage2>Voltage2HI&& framecount>150)Voltage2HI=Voltage2;
if(debug2)Serial<<"Voltage: "<<Voltage<<" vdc Voltage 2: "<<Voltage2<<" vdc "<<volt<<" mVdc frames:"<<framecount<<"\n";
}
void ISA::handle524(CAN_frame_t *frame) //Voltage3
{
framecount++;
long volt=0;
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;
if(Voltage3<Voltage3LO && framecount>150 && Voltage3>10)Voltage3LO=Voltage3;
if(Voltage3>Voltage3HI && framecount>150)Voltage3HI=Voltage3;
if(debug2)Serial<<"Voltage: "<<Voltage<<" vdc Voltage 3: "<<Voltage3<<" vdc "<<volt<<" mVdc frames:"<<framecount<<"\n";
}
void ISA::handle525(CAN_frame_t *frame) //Temperature
{
framecount++;
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;
if(debug2)Serial<<"Temperature: "<<Temperature<<" C frames:"<<framecount<<"\n";
}
void ISA::handle526(CAN_frame_t *frame) //Kilowatts
{
framecount++;
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;
if(debug2)Serial<<"Power: "<<watt<<" Watts "<<KW<<" kW frames:"<<framecount<<"\n";
}
void ISA::handle527(CAN_frame_t *frame) //Ampere-Hours
{
framecount++;
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;
if(debug2)Serial<<"Amphours: "<<AH<<" Ampseconds: "<<As<<" frames:"<<framecount<<"\n";
}
void ISA::handle528(CAN_frame_t *frame) //kiloWatt-hours
{
framecount++;
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;
if(debug2)Serial<<"KiloWattHours: "<<KWH<<" Watt Hours: "<<wh<<" frames:"<<framecount<<"\n";
}
void ISA::printCAN(CAN_frame_t *frame)
{
//This routine simply prints a timestamp and the contents of the
//incoming CAN message
milliseconds = (int) (millis()/1) %1000 ;
seconds = (int) (millis() / 1000) % 60 ;
minutes = (int) ((millis() / (1000*60)) % 60);
hours = (int) ((millis() / (1000*60*60)) % 24);
sprintf(buffer,"%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds);
Serial<<buffer<<" ";
sprintf(bigbuffer,"%02X %02X %02X %02X %02X %02X %02X %02X %02X",
frame->MsgID, frame->data.u8[0],frame->data.u8[1],frame->data.u8[2],
frame->data.u8[3],frame->data.u8[4],frame->data.u8[5],frame->data.u8[6],frame->data.u8[7],0);
Serial<<"Rcvd ISA frame: 0x"<<bigbuffer<<"\n";
}
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);
if(debug)printCAN(&outframe);
delay(500);
sendSTORE();
delay(500);
}
// delay(500);
START();
delay(500);
lastAs=As;
lastWh=wh;
}
void ISA::STOP()
{
//SEND 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);
if(debug) {printCAN(&outframe);} //If the debug variable is set, show our transmitted frame
}
void ISA::sendSTORE()
{
//SEND STORE///////
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);
if(debug)printCAN(&outframe); //If the debug variable is set, show our transmitted frame
}
void ISA::START()
{
//SEND 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);
if(debug)printCAN(&outframe); //If the debug variable is set, show our transmitted frame
}
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);
if(debug)printCAN(&outframe); //If the debug variable is set, show our transmitted frame
}
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);
if(debug)printCAN(&outframe); //If the debug variable is set, show our transmitted frame
}
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);
if(debug)printCAN(&outframe);
delay(500);
sendSTORE();
delay(500);
// delay(500);
START();
delay(500);
lastAs=As;
lastWh=wh;
}

View file

@ -0,0 +1,104 @@
#ifndef SimpleISA_h
#define SimpleISA_h
/* This library supports the 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 2016
You are licensed to use this library for any purpose, commercial or private,
without restriction.
*/
#include <Arduino.h>
#include "../miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
class ISA
{
public:
ISA();
~ISA();
void initialize();
void begin(int Port, int speed);
void initCurrent();
void sendSTORE();
void STOP();
void START();
void RESTART();
void deFAULT();
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 debug;
bool debug2;
bool firstframe;
int framecount;
unsigned long timestamp;
double milliamps;
long watt;
long As;
long lastAs;
long wh;
long lastWh;
void handleFrame(CAN_frame_t *frame); // CAN handler
uint8_t page;
private:
CAN_frame_t frame;
unsigned long elapsedtime;
double ampseconds;
int milliseconds ;
int seconds;
int minutes;
int hours;
char buffer[9];
char bigbuffer[90];
uint32_t inbox;
CAN_frame_t outframe = {.FIR = {.B =
{
.DLC = 8,
.FF = CAN_frame_std,
}},
.MsgID = 0x411,
.data = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
void printCAN(CAN_frame_t *frame);
void handle521(CAN_frame_t *frame);
void handle522(CAN_frame_t *frame);
void handle523(CAN_frame_t *frame);
void handle524(CAN_frame_t *frame);
void handle525(CAN_frame_t *frame);
void handle526(CAN_frame_t *frame);
void handle527(CAN_frame_t *frame);
void handle528(CAN_frame_t *frame);
};
#endif /* SimpleISA_h */

View file

@ -0,0 +1,188 @@
#include <due_can.h>
#include "variant.h"
#include <SimpleISA.h>
#define Serial SerialUSB //Use native port
template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } //Allow streaming
float Version=2.00;
uint16_t loopcount=0;
unsigned long startime=0;
unsigned long elapsedtime=0;
uint port=0;
uint16_t datarate=500;
ISA Sensor; //Instantiate ISA Module Sensor object to measure current and voltage
void setup()
{
Serial.begin(115200);
Sensor.begin(port,datarate); //Start ISA object on CAN 0 at 500 kbps
Serial<<"\nISA Scale Startup Successful \n";
printMenu();
}
void loop()
{
if(loopcount++==40000)
{
printStatus();
loopcount-0;
}
checkforinput(); //Check keyboard for user input
}
void printStatus()
{
char buffer[40];
//printimestamp();
sprintf(buffer,"%4.2f",Sensor.Voltage);
Serial<<"Volt:"<<buffer<<"v ";
sprintf(buffer,"%4.2f",Sensor.Voltage1);
Serial<<"V1:"<<buffer<<"v ";
sprintf(buffer,"%4.2f",Sensor.Voltage2);
Serial<<"V2:"<<buffer<<"v ";
sprintf(buffer,"%4.2f",Sensor.Voltage3);
Serial<<"V3:"<<buffer<<"v ";
sprintf(buffer,"%4.3f",Sensor.Amperes);
Serial<<"Amps:"<<buffer<<"A ";
sprintf(buffer,"%4.3f",Sensor.KW);
Serial<<buffer<<"kW ";
sprintf(buffer,"%4.3f",Sensor.AH);
Serial<<buffer<<"Ah ";
sprintf(buffer,"%4.3f",Sensor.KWH);
Serial<<buffer<<"kWh";
sprintf(buffer,"%4.0f",Sensor.Temperature);
Serial<<buffer<<"C ";
Serial<<"Frame:"<<Sensor.framecount<<" \n";
}
void printimestamp()
{
//Prints a timestamp to the serial port
elapsedtime=millis() - startime;
int milliseconds = (elapsedtime/1) %1000 ;
int seconds = (elapsedtime / 1000) % 60 ;
int minutes = ((elapsedtime / (1000*60)) % 60);
int hours = ((elapsedtime / (1000*60*60)) % 24);
char buffer[19];
sprintf(buffer,"%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds);
Serial<<buffer<<" ";
}
void printMenu()
{
Serial<<"\f\n=========== ISA Scale Sample Program Version "<<Version<<" ==============\n************ List of Available Commands ************\n\n";
Serial<<" ? - Print this menu\n ";
Serial<<" d - toggles Debug off and on to print recieved CAN data traffic\n";
Serial<<" D - toggles Debug2 off and on to print derived values\n";
Serial<<" f - zero frame count\n ";
Serial<<" i - initialize new sensor\n ";
Serial<<" p - Select new CAN port\n ";
Serial<<" r - Set new datarate\n ";
Serial<<" z - zero ampere-hours\n ";
Serial<<"**************************************************************\n==============================================================\n\n";
}
void checkforinput()
{
//Checks for keyboard input from Native port
if (Serial.available())
{
int inByte = Serial.read();
switch (inByte)
{
case 'z': //Zeroes ampere-hours
Sensor.KWH=0;
Sensor.AH=0;
Sensor.RESTART();
break;
case 'p':
getPort();
break;
case 'r':
getRate();
break;
case 'f':
Sensor.framecount=0;
break;
case 'd': //Causes ISA object to print incoming CAN messages for debugging
Sensor.debug=!Sensor.debug;
break;
case 'D': //Causes ISA object to print derived values for debugging
Sensor.debug2=!Sensor.debug2;
break;
case 'i':
Sensor.initialize();
break;
case '?': //Print a menu describing these functions
printMenu();
break;
case '1':
Sensor.STOP();
break;
case '3':
Sensor.START();
break;
}
}
}
void getRate()
{
Serial<<"\n Enter the Data Rate in Kbps you want for CAN : ";
while(Serial.available() == 0){}
float V = Serial.parseFloat();
if(V>0)
{
Serial<<"Datarate:"<<V<<"\n\n";
uint8_t rate=V;
datarate=V*1000;
Sensor.begin(port,datarate);
}
}
void getPort()
{
Serial<<"\n Enter port selection: c0=CAN0 c1=CAN1 ";
while(Serial.available() == 0){}
int P = Serial.parseInt();
if(P>1) Serial<<"Entry out of range, enter 0 or 1 \n";
else
{
port=P;
Sensor.begin(port,datarate);
}
}