8#include <proto/main.pb.h>
13#include <block/mblock.h>
14#include <utils/logging.h>
16#include <carrier/carrier.h>
17#include <carrier/cluster.h>
18#include <entity/visitor.h>
22extern int abs_clamp(
float in,
int min,
int max);
24metadata::eui_t blocks::MIntBlock::get_entity_eui()
const {
26 return hardware->get_entity_eui();
30blocks::MIntBlock::MIntBlock(SLOT slot, MIntBlockHAL *hardware)
31 :
blocks::MBlock{slot, hardware}, hardware(hardware), ic_values{}, time_factors{} {
36bool blocks::MIntBlock::init() {
37 return FunctionBlock::init() && hardware->init();
40const std::array<float, 8> &blocks::MIntBlock::get_ic_values()
const {
return ic_values; }
42float blocks::MIntBlock::get_ic_value(uint8_t idx)
const {
43 if (idx >= ic_values.size())
45 return ic_values[idx];
48bool blocks::MIntBlock::set_ic_values(
const std::array<float, 8> &ic_values_) {
49 for (
auto idx = 0u; idx < ic_values_.size(); idx++)
50 if (!set_ic_value(idx, ic_values_[idx]))
55bool blocks::MIntBlock::set_ic_values(
float value) {
56 for (
auto idx = 0u; idx < ic_values.size(); idx++)
57 if (!set_ic_value(idx, value))
62bool blocks::MIntBlock::set_ic_value(uint8_t idx,
float value) {
63 if (idx >= ic_values.size())
65 if (value > 1.0f or value < -1.0f)
67 ic_values[idx] = value;
71void blocks::MIntBlock::reset_ic_values() { std::fill(ic_values.begin(), ic_values.end(), 0.0f); }
73const std::array<unsigned int, 8> &blocks::MIntBlock::get_time_factors()
const {
77unsigned int blocks::MIntBlock::get_time_factor(uint8_t idx)
const {
78 if (idx >= time_factors.size())
80 return time_factors[idx];
83bool blocks::MIntBlock::set_time_factors(
const std::array<unsigned int, 8> &time_factors_) {
84 for (
auto idx = 0u; idx < time_factors_.size(); idx++)
85 if (!set_time_factor(idx, time_factors_[idx]))
90bool blocks::MIntBlock::set_time_factors(
unsigned int k) {
91 for (
auto idx = 0u; idx < time_factors.size(); idx++)
92 if (!set_time_factor(idx, k))
97bool blocks::MIntBlock::set_time_factor(uint8_t int_idx,
unsigned int k) {
98 if (!(k == 100 or k == 10000))
100 if (int_idx >= NUM_INTEGRATORS)
102 time_factors[int_idx] = k;
106void blocks::MIntBlock::reset_time_factors() {
108 auto default_ = DEFAULT_TIME_FACTOR;
109 std::fill(std::begin(time_factors), std::end(time_factors), default_);
112bool blocks::MIntBlock::write_to_hardware() {
114 for (
decltype(ic_values.size()) i = 0; i < ic_values.size(); i++) {
115 if (!hardware->write_ic(i, ic_values[i])) {
116 LOG(ANABRID_PEDANTIC, __PRETTY_FUNCTION__);
117 return UnitResult::err(
"Error writing IC values to hardware.");
121 std::bitset<NUM_INTEGRATORS> time_factor_switches{};
122 for (
auto idx = 0u; idx < time_factors.size(); idx++)
123 if (time_factors[idx] != DEFAULT_TIME_FACTOR)
124 time_factor_switches.set(idx);
125 if (!hardware->write_time_factor_switches(time_factor_switches))
126 return UnitResult::err(
"Error writing time factor switches.");
128 return UnitResult::ok();
131void blocks::MIntBlock::reset(entities::ResetAction action) {
132 FunctionBlock::reset(action);
134 if (action.has(entities::ResetAction::CIRCUIT_RESET)) {
136 reset_time_factors();
139 if (action.has(entities::ResetAction::OVERLOAD_RESET)) {
142 mode::RealManualControl::enable();
143 mode::RealManualControl::to_ic();
147ConfigResult blocks::MIntBlock::config(
const pb_Item &item) {
148#ifdef ANABRID_DEBUG_ENTITY_CONFIG
149 Serial.println(__PRETTY_FUNCTION__);
152 if (item.which_kind != pb_Item_itor_config_tag)
153 return ConfigResult::ok(
false);
155 auto& itor_config = item.kind.itor_config;
156 for (
size_t idx = 0; idx < itor_config.elements_count; idx++) {
157 auto& element = itor_config.elements[idx];
158 if (!set_ic_value(element.idx, element.ic)) {
159 return ConfigResult::err_fmt(
"MIntBlock: Integrator %d IC is illegal: %f", element.idx, element.ic);
161 if (!set_time_factor(element.idx, element.k)) {
162 return ConfigResult::err_fmt(
"MIntBlock: Integrator %d time factor k illegal: %d", element.idx, element.k);
166 return ConfigResult::ok(
true);
169void blocks::MIntBlock::extract(entities::ExtractVisitor &collector) {
170 Entity::extract(collector);
172 if (!collector.include_configuration())
return;
173 auto& item = collector.create(pb_Item_itor_config_tag);
174 auto& itor_config = item.kind.itor_config;
176 itor_config.elements_count = NUM_INTEGRATORS;
177 for (
auto idx = 0; idx < NUM_INTEGRATORS; ++idx) {
178 auto& element = itor_config.elements[idx];
180 element.ic = ic_values[idx];
181 element.k = time_factors[idx];
int abs_clamp(float in, int min, int max)