Skip to content

Commit

Permalink
Introduce meter.cpp and meter.h
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo35 committed Aug 1, 2024
1 parent 78b734b commit 33d3292
Show file tree
Hide file tree
Showing 7 changed files with 636 additions and 624 deletions.
15 changes: 0 additions & 15 deletions SmartEVSE-3/include/evse.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,6 @@ extern RemoteDebug Debug;
#define EM_UNUSED_SLOT4 16
#define EM_CUSTOM 17

#define ENDIANESS_LBF_LWF 0
#define ENDIANESS_LBF_HWF 1
#define ENDIANESS_HBF_LWF 2
#define ENDIANESS_HBF_HWF 3

#define OWNER_FACT "SmartEVSE"
#define REPO_FACT "SmartEVSE-3"
#define OWNER_COMM "dingo35"
Expand All @@ -467,16 +462,10 @@ extern struct tm timeinfo;
extern uint8_t Mode; // EVSE mode
extern uint8_t LoadBl; // Load Balance Setting (Disable, Master or Node)
extern uint8_t Grid;
extern uint8_t MainsMeterAddress;
extern uint8_t EVMeter; // Type of EV electric meter (0: Disabled / Constants EM_*)
extern uint8_t EVMeterAddress;
#if FAKE_RFID
extern uint8_t Show_RFID;
#endif

extern int16_t Irms[3]; // Momentary current per Phase (Amps *10) (23 = 2.3A)
extern int16_t Irms_EV[3]; // Momentary current per Phase (Amps *10) (23 = 2.3A)

extern uint8_t State;
extern uint8_t ErrorFlags;
extern uint8_t NextState;
Expand All @@ -499,9 +488,7 @@ extern uint16_t CardOffset;

extern uint8_t GridActive; // When the CT's are used on Sensorbox2, it enables the GRID menu option.
extern uint16_t SolarStopTimer;
extern int32_t EnergyCharged;
extern int32_t EnergyCapacity;
extern int16_t PowerMeasured;
extern uint8_t RFIDstatus;
extern uint8_t OcppMode;
extern bool LocalTimeSet;
Expand Down Expand Up @@ -592,8 +579,6 @@ struct EMstruct {
int8_t EDivisor_Exp; // 10^x
};

extern struct EMstruct EMConfig[EM_CUSTOM + 1];

struct DelayedTimeStruct {
uint32_t epoch2; // in case of Delayed Charging the StartTime in epoch2; if zero we are NOT Delayed Charging
// epoch2 is the number of seconds since 1/1/2023 00:00 UTC, which equals epoch 1672531200
Expand Down
68 changes: 68 additions & 0 deletions SmartEVSE-3/include/meter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

/*
; Project: Smart EVSE v3
;
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
*/

#ifndef __EVSE_METER

#define __EVSE_METER

#include "evse.h"


extern struct EMstruct EMConfig[EM_CUSTOM + 1];
extern struct ModBus MB;

class Meter {
public:
uint8_t Type; // previously: MainsMeter; Type of Mains electric meter (0: Disabled / Constants EM_*)
uint8_t Address;
int16_t Irms[3]; // Momentary current per Phase (23 = 2.3A) (resolution 100mA)
int16_t Imeasured; // Max of all Phases (Amps *10) of mains power
int16_t Power[3];
int16_t PowerMeasured; // Measured Charge power in Watt by kWh meter (sum of all phases)
uint8_t Timeout;
int32_t Import_active_energy; // Imported active energy
int32_t Export_active_energy; // Exported active energy
int32_t Energy; // Wh -> Import_active_energy - Export_active_energy
int32_t EnergyCharged; // kWh meter value energy charged. (Wh) (will reset if state changes from A->B)
int32_t EnergyMeterStart; // kWh meter value is stored once EV is connected to EVSE (Wh)
uint8_t ResetKwh; // if set, reset kwh meter at state transition B->C
// cleared when charging, reset to 1 when disconnected (state A)
// constructor
Meter(uint8_t type, uint8_t address, uint8_t timeout);
void UpdateEnergies();
void ResponseToMeasurement();
void CalcImeasured(void);
private:
uint8_t receiveCurrentMeasurement(uint8_t *buf);
signed int receivePowerMeasurement(uint8_t *buf);
signed int receiveEnergyMeasurement(uint8_t *buf);
void combineBytes(void *var, uint8_t *buf, uint8_t pos, uint8_t endianness, MBDataType dataType);
signed int decodeMeasurement(uint8_t *buf, uint8_t Count, signed char Divisor);
signed int decodeMeasurement(uint8_t *buf, uint8_t Count, uint8_t Endianness, MBDataType dataType, signed char Divisor);
};

extern Meter MainsMeter;
extern Meter EVMeter; // Type of EV electric meter (0: Disabled / Constants EM_*)

#endif
8 changes: 6 additions & 2 deletions SmartEVSE-3/include/modbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#ifndef __EVSE_MODBUS
#define __EVSE_MODBUS

#include "meter.h"
#include "ModbusServerRTU.h"
#include "ModbusClientRTU.h"

struct ModBus {
uint8_t Address;
uint8_t Function;
Expand All @@ -40,6 +44,8 @@ struct ModBus {
uint8_t Exception;
};

extern struct ModBus MB;

// definition of MBserver / MBclient class is done in evse.cpp
extern ModbusServerRTU MBserver;
extern ModbusClientRTU MBclient;
Expand All @@ -58,10 +64,8 @@ void ModbusDecode(uint8_t *buf, uint8_t len);

// ########################### EVSE modbus functions ###########################

signed int receiveMeasurement(uint8_t *buf, uint8_t pos, uint8_t Endianness, MBDataType dataType, signed char Divisor);
void requestMeasurement(uint8_t Meter, uint8_t Address, uint16_t Register, uint8_t Count);
void requestCurrentMeasurement(uint8_t Meter, uint8_t Address);
uint8_t receiveCurrentMeasurement(uint8_t *buf, uint8_t Meter, signed int *var);

//void ReadItemValueResponse(void);
//void WriteItemValueResponse(void);
Expand Down
Loading

0 comments on commit 33d3292

Please sign in to comment.