Skip to content

Commit

Permalink
Feature: First version of GridProfile Parser which shows all values c…
Browse files Browse the repository at this point in the history
…ontained in the profile.
  • Loading branch information
tbnobody committed Dec 11, 2023
1 parent f851aca commit 06651f3
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 18 deletions.
1 change: 1 addition & 0 deletions include/WebApi_gridprofile.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class WebApiGridProfileClass {

private:
void onGridProfileStatus(AsyncWebServerRequest* request);
void onGridProfileRawdata(AsyncWebServerRequest* request);

AsyncWebServer* _server;
};
328 changes: 328 additions & 0 deletions lib/Hoymiles/src/parser/GridProfileParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "GridProfileParser.h"
#include "../Hoymiles.h"
#include <cstring>
#include <frozen/map.h>
#include <frozen/string.h>

const std::array<const ProfileType_t, PROFILE_TYPE_COUNT> GridProfileParser::_profileTypes = { {
{ 0x02, 0x00, "no data (yet)" },
Expand All @@ -16,6 +18,263 @@ const std::array<const ProfileType_t, PROFILE_TYPE_COUNT> GridProfileParser::_pr
{ 0x37, 0x00, "Swiss - CH_NA EEA-NE7-CH2020" },
} };

constexpr frozen::map<uint8_t, frozen::string, 12> profile_section = {
{ 0x00, "Voltage (H/LVRT)" },
{ 0x10, "Frequency (H/LFRT)" },
{ 0x20, "Island Detection (ID)" },
{ 0x30, "Reconnection (RT)" },
{ 0x40, "Ramp Rates (RR)" },
{ 0x50, "Frequency Watt (FW)" },
{ 0x60, "Volt Watt (VW)" },
{ 0x70, "Active Power Control (APC)" },
{ 0x80, "Volt Var (VV)" },
{ 0x90, "Specified Power Factor (SPF)" },
{ 0xA0, "Reactive Power Control (RPC)" },
{ 0xB0, "Watt Power Factor (WPF)" },
};

struct GridProfilePartialValue_t {
frozen::string Name;
frozen::string Unit;
uint8_t Dividor;
};

constexpr GridProfilePartialValue_t make_value(frozen::string Name, frozen::string Unit, uint8_t divisor)
{
GridProfilePartialValue_t v = { Name, Unit, divisor };
return v;
}

constexpr frozen::map<uint8_t, GridProfilePartialValue_t, 0x38> value_names = {
{ 0x01, make_value("Nominale Voltage (NV)", "V", 10) },
{ 0x02, make_value("Low Voltage 1 (LV1)", "V", 10) },
{ 0x03, make_value("LV1 Maximum Trip Time (MTT)", "s", 10) },
{ 0x04, make_value("High Voltage 1 (HV1)", "V", 10) },
{ 0x05, make_value("HV1 Maximum Trip Time (MTT)", "s", 10) },
{ 0x06, make_value("Low Voltage 2 (LV2)", "V", 10) },
{ 0x07, make_value("LV2 Maximum Trip Time (MTT)", "s", 10) },
{ 0x08, make_value("High Voltage 2 (HV2)", "V", 10) },
{ 0x09, make_value("HV2 Maximum Trip Time (MTT)", "s", 10) },
{ 0x0a, make_value("10mins Average High Voltage (AHV)", "V", 10) },
{ 0x0b, make_value("High Voltage 3 (HV3)", "V", 10) },
{ 0x0c, make_value("HV3 Maximum Trip Time (MTT)", "s", 10) },
{ 0x0d, make_value("Nominal Frequency", "Hz", 100) },
{ 0x0e, make_value("Low Frequency 1 (LF1)", "Hz", 100) },
{ 0x0f, make_value("LF1 Maximum Trip Time (MTT)", "s", 10) },
{ 0x10, make_value("High Frequency 1 (HF1)", "Hz", 100) },
{ 0x11, make_value("HF1 Maximum Trip time (MTT)", "s", 10) },
{ 0x12, make_value("Low Frequency 2 (LF2)", "Hz", 100) },
{ 0x13, make_value("LF2 Maximum Trip Time (MTT)", "s", 10) },
{ 0x14, make_value("High Frequency 2 (HF2)", "Hz", 100) },
{ 0x15, make_value("HF2 Maximum Trip time (MTT)", "s", 10) },
{ 0x16, make_value("ID Function Activated", "bool", 1) },
{ 0x17, make_value("Reconnect Time (RT)", "s", 10) },
{ 0x18, make_value("Reconnect High Voltage (RHV)", "V", 10) },
{ 0x19, make_value("Reconnect Low Voltage (RLV)", "V", 10) },
{ 0x1a, make_value("Reconnect High Frequency (RHF)", "Hz", 100) },
{ 0x1b, make_value("Reconnect Low Frequency (RLF)", "Hz", 100) },
{ 0x1c, make_value("Normal Ramp up Rate(RUR_NM)", "Rated%/s", 100) },
{ 0x1d, make_value("Soft Start Ramp up Rate (RUR_SS)", "Rated%/s", 100) },
{ 0x1e, make_value("FW Function Activated", "bool", 1) },
{ 0x1f, make_value("Start of Frequency Watt Droop (Fstart)", "Hz", 100) },
{ 0x20, make_value("FW Droop Slope (Kpower_Freq)", "Pn%/Hz", 10) },
{ 0x21, make_value("Recovery Ramp Rate (RRR)", "Pn%/s", 100) },
{ 0x22, make_value("Recovery High Frequency (RVHF)", "Hz", 100) },
{ 0x23, make_value("Recovery Low Frequency (RVLF)", "Hz", 100) },
{ 0x24, make_value("VW Function Activated", "bool", 1) },
{ 0x25, make_value("Start of Voltage Watt Droop (Vstart)", "V", 10) },
{ 0x26, make_value("End of Voltage Watt Droop (Vend)", "V", 10) },
{ 0x27, make_value("Droop Slope (Kpower_Volt)", "Pn%/V", 100) },
{ 0x28, make_value("APC Function Activated", "bool", 1) },
{ 0x29, make_value("Power Ramp Rate (PRR)", "Pn%/s", 100) },
{ 0x2a, make_value("VV Function Activated", "bool", 1) },
{ 0x2b, make_value("Voltage Set Point V1", "V", 10) },
{ 0x2c, make_value("Reactive Set Point Q1", "%Pn", 10) },
{ 0x2d, make_value("Voltage Set Point V2", "V", 10) },
{ 0x2e, make_value("Voltage Set Point V3", "V", 10) },
{ 0x2f, make_value("Voltage Set Point V4", "V", 10) },
{ 0x30, make_value("Reactive Set Point Q4", "%Pn", 10) },
{ 0x31, make_value("Setting Time (Tr)", "s", 10) },
{ 0x32, make_value("SPF Function Activated", "bool", 1) },
{ 0x33, make_value("Power Factor (PF)", "", 100) },
{ 0x34, make_value("RPC Function Activated", "bool", 1) },
{ 0x35, make_value("Reactive Power (VAR)", "%Sn", 1) },
{ 0x36, make_value("WPF Function Activated", "bool", 1) },
{ 0x37, make_value("Start of Power of WPF (Pstart)", "%Pn", 10) },
{ 0x38, make_value("Power Factor ar Rated Power (PFRP)", "", 100) },
};

const std::array<const GridProfileValue_t, SECTION_VALUE_COUNT> GridProfileParser::_profile_values = { {
// Voltage (H/LVRT)
// Version 0x00
{ 0x00, 0x00, 0x01 },
{ 0x00, 0x00, 0x02 },
{ 0x00, 0x00, 0x03 },
{ 0x00, 0x00, 0x04 },
{ 0x00, 0x00, 0x05 },

// Version 0x03
{ 0x00, 0x03, 0x01 },
{ 0x00, 0x03, 0x02 },
{ 0x00, 0x03, 0x03 },
{ 0x00, 0x03, 0x05 },
{ 0x00, 0x03, 0x06 },
{ 0x00, 0x03, 0x07 },
{ 0x00, 0x03, 0x08 },
{ 0x00, 0x03, 0x09 },

// Version 0x0a
{ 0x00, 0x0a, 0x01 },
{ 0x00, 0x0a, 0x02 },
{ 0x00, 0x0a, 0x03 },
{ 0x00, 0x0a, 0x04 },
{ 0x00, 0x0a, 0x05 },
{ 0x00, 0x0a, 0x06 },
{ 0x00, 0x0a, 0x07 },
{ 0x00, 0x0a, 0x0a },

// Version 0x0b
{ 0x00, 0x0b, 0x01 },
{ 0x00, 0x0b, 0x02 },
{ 0x00, 0x0b, 0x03 },
{ 0x00, 0x0b, 0x04 },
{ 0x00, 0x0b, 0x05 },
{ 0x00, 0x0b, 0x06 },
{ 0x00, 0x0b, 0x07 },
{ 0x00, 0x0b, 0x08 },
{ 0x00, 0x0b, 0x09 },
{ 0x00, 0x0b, 0x0a },

// Version 0x0c
{ 0x00, 0x0c, 0x01 },
{ 0x00, 0x0c, 0x02 },
{ 0x00, 0x0c, 0x03 },
{ 0x00, 0x0c, 0x04 },
{ 0x00, 0x0c, 0x05 },
{ 0x00, 0x0c, 0x06 },
{ 0x00, 0x0c, 0x07 },
{ 0x00, 0x0c, 0x08 },
{ 0x00, 0x0c, 0x09 },
{ 0x00, 0x0c, 0x0b },
{ 0x00, 0x0c, 0x0c },
{ 0x00, 0x0c, 0x0a },

// Frequency (H/LFRT)
// Version 0x00
{ 0x10, 0x00, 0x0d },
{ 0x10, 0x00, 0x0e },
{ 0x10, 0x00, 0x0f },
{ 0x10, 0x00, 0x10 },
{ 0x10, 0x00, 0x11 },

// Version 0x03
{ 0x10, 0x03, 0x0d },
{ 0x10, 0x03, 0x0e },
{ 0x10, 0x03, 0x0f },
{ 0x10, 0x03, 0x10 },
{ 0x10, 0x03, 0x11 },
{ 0x10, 0x03, 0x12 },
{ 0x10, 0x03, 0x13 },
{ 0x10, 0x03, 0x14 },
{ 0x10, 0x03, 0x15 },

// Island Detection (ID)
// Version 0x00
{ 0x20, 0x00, 0x16 },

// Reconnection (RT)
// Version 0x03
{ 0x30, 0x03, 0x17 },
{ 0x30, 0x03, 0x18 },
{ 0x30, 0x03, 0x19 },
{ 0x30, 0x03, 0x1a },
{ 0x30, 0x03, 0x1b },

// Ramp Rates (RR)
// Version 0x00
{ 0x40, 0x00, 0x1c },
{ 0x40, 0x00, 0x1d },

// Frequency Watt (FW)
// Version 0x00
{ 0x50, 0x00, 0x1e },
{ 0x50, 0x00, 0x1f },
{ 0x50, 0x00, 0x20 },
{ 0x50, 0x00, 0x21 },

// Version 0x01
{ 0x50, 0x01, 0x1e },
{ 0x50, 0x01, 0x1f },
{ 0x50, 0x01, 0x20 },
{ 0x50, 0x01, 0x21 },
{ 0x50, 0x01, 0x22 },

// Version 0x08
{ 0x50, 0x08, 0x1e },
{ 0x50, 0x08, 0x1f },
{ 0x50, 0x08, 0x20 },
{ 0x50, 0x08, 0x21 },
{ 0x50, 0x08, 0x22 },
{ 0x50, 0x08, 0x23 },

// Volt Watt (VW)
// Version 0x00
{ 0x60, 0x00, 0x24 },
{ 0x60, 0x00, 0x25 },
{ 0x60, 0x00, 0x26 },
{ 0x60, 0x00, 0x27 },

// Version 0x04
{ 0x60, 0x04, 0x24 },
{ 0x60, 0x04, 0x25 },
{ 0x60, 0x04, 0x26 },
{ 0x60, 0x04, 0x27 },

// Active Power Control (APC)
// Version 0x00
{ 0x70, 0x00, 0x28 },

// Version 0x02
{ 0x70, 0x02, 0x28 },
{ 0x70, 0x02, 0x29 },

// Volt Var (VV)
// Version 0x00
{ 0x80, 0x00, 0x2a },
{ 0x80, 0x00, 0x2b },
{ 0x80, 0x00, 0x2c },
{ 0x80, 0x00, 0x2d },
{ 0x80, 0x00, 0x2e },
{ 0x80, 0x00, 0x2f },
{ 0x80, 0x00, 0x30 },

// Version 0x01
{ 0x80, 0x01, 0x2a },
{ 0x80, 0x01, 0x2b },
{ 0x80, 0x01, 0x2c },
{ 0x80, 0x01, 0x2d },
{ 0x80, 0x01, 0x2e },
{ 0x80, 0x01, 0x2f },
{ 0x80, 0x01, 0x30 },
{ 0x80, 0x01, 0x31 },

// Specified Power Factor (SPF)
// Version 0x00
{ 0x90, 0x00, 0x32 },
{ 0x90, 0x00, 0x33 },

// Reactive Power Control (RPC)
// Version 0x02
{ 0xa0, 0x02, 0x34 },
{ 0xa0, 0x02, 0x35 },

// Watt Power Factor (WPF)
// Version 0x00
{ 0xb0, 0x00, 0x36 },
{ 0xb0, 0x00, 0x37 },
{ 0xb0, 0x00, 0x38 },
} };

GridProfileParser::GridProfileParser()
: Parser()
{
Expand Down Expand Up @@ -51,7 +310,9 @@ String GridProfileParser::getProfileName()
String GridProfileParser::getProfileVersion()
{
char buffer[10];
HOY_SEMAPHORE_TAKE();
snprintf(buffer, sizeof(buffer), "%d.%d.%d", (_payloadGridProfile[2] >> 4) & 0x0f, _payloadGridProfile[2] & 0x0f, _payloadGridProfile[3]);
HOY_SEMAPHORE_GIVE();
return buffer;
}

Expand All @@ -65,3 +326,70 @@ std::vector<uint8_t> GridProfileParser::getRawData()
HOY_SEMAPHORE_GIVE();
return ret;
}

std::list<GridProfileSection_t> GridProfileParser::getProfile()
{
std::list<GridProfileSection_t> l;

if (_gridProfileLength > 4) {
uint16_t pos = 4;
do {
uint8_t section_id = _payloadGridProfile[pos];
uint8_t section_version = _payloadGridProfile[pos + 1];
int8_t section_start = getSectionStart(section_id, section_version);
uint8_t section_size = getSectionSize(section_id, section_version);
pos += 2;

GridProfileSection_t section;
try {
section.SectionName = profile_section.at(section_id).data();
} catch (const std::out_of_range&) {
section.SectionName = "Unknown";
break;
}

for (uint8_t val_id = 0; val_id < section_size; val_id++) {
auto value_setting = value_names.at(_profile_values[section_start + val_id].Type);

float value = (_payloadGridProfile[pos] << 8) | _payloadGridProfile[pos + 1];
value /= value_setting.Dividor;

GridProfileItem_t v;
v.Name = value_setting.Name.data();
v.Unit = value_setting.Unit.data();
v.Value = value;
section.items.push_back(v);

pos += 2;
}

l.push_back(section);

} while (pos < _gridProfileLength);
}

return l;
}

uint8_t GridProfileParser::getSectionSize(uint8_t section_id, uint8_t section_version)
{
uint8_t count = 0;
for (auto& values : _profile_values) {
if (values.Section == section_id && values.Version == section_version) {
count++;
}
}
return count;
}

int8_t GridProfileParser::getSectionStart(uint8_t section_id, uint8_t section_version)
{
uint8_t count = -1;
for (auto& values : _profile_values) {
count++;
if (values.Section == section_id && values.Version == section_version) {
break;
}
}
return count;
}
Loading

0 comments on commit 06651f3

Please sign in to comment.