From 91a7247bee4bf4b0b2ac4f26e59bf71b4d3b7866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andis=20Z=C4=ABle?= Date: Sat, 13 Apr 2024 14:20:45 +0300 Subject: [PATCH] Created regulated output --- firmware/src/hw/devices.cpp | 8 ++-- firmware/src/hw/devices.h | 5 +-- firmware/src/hw/reg_out.cpp | 71 ++++++++++++++++++++++++++++++++++ firmware/src/hw/reg_out.h | 45 +++++++++++++++++++++ firmware/src/logic/cfg_mem.cpp | 40 ++++++++++++++++++- firmware/src/logic/cfg_mem.h | 4 ++ firmware/src/main.cpp | 19 ++++++++- firmware/src/uDCCD.cppproj | 18 +++------ 8 files changed, 189 insertions(+), 21 deletions(-) create mode 100644 firmware/src/hw/reg_out.cpp create mode 100644 firmware/src/hw/reg_out.h diff --git a/firmware/src/hw/devices.cpp b/firmware/src/hw/devices.cpp index 6fd6933..a1a105b 100644 --- a/firmware/src/hw/devices.cpp +++ b/firmware/src/hw/devices.cpp @@ -5,7 +5,8 @@ /**** Private definitions ****/ /**** Private constants ****/ static const uint16_t def_button_hold_time = 1000; -static const uint16_t def_min_current = 100; +static const uint16_t def_max_voltage = 7000; +static const uint16_t def_min_voltage = 100; static const uint16_t def_fuse_treshold = 6000; static const uint16_t def_fuse_hold_cycles = 50; static const uint16_t def_fuse_cooldown_cycles = 1000; @@ -20,8 +21,9 @@ void devices_init(void) btn_up.hold_time = def_button_hold_time; btn_down.hold_time = def_button_hold_time; - ccout.target = 0; - ccout.min_out = def_min_current; + ccout.max_voltage = def_max_voltage; + ccout.max_current = 0; + ccout.min_voltage = def_min_voltage; sup_fuse.hold_current = def_fuse_treshold; sup_fuse.trip_cycles = def_fuse_hold_cycles; diff --git a/firmware/src/hw/devices.h b/firmware/src/hw/devices.h index 76d480a..01395f3 100644 --- a/firmware/src/hw/devices.h +++ b/firmware/src/hw/devices.h @@ -9,8 +9,7 @@ #include "button.h" #include "potentiometer.h" #include "display_led.h" -#include "cv_output.h" -#include "cc_output.h" +#include "reg_out.h" #include "fuse.h" static hw::Button btn_mode = hw::Button(&din1, bsp::DIN_LOW, 10, hw::BUTTON_OFF); @@ -25,7 +24,7 @@ static hw::Potentiometer pot = hw::Potentiometer(&ain2, 500, 4500); static hw::DisplayLed display = hw::DisplayLed(&odout1, &odout2, &odout3, &odout4, &odout5, &odout6, &od_pwm); -static hw::CCoutput ccout = hw::CCoutput(&hbridge, &bat_u, &dccd_u, &dccd_i); +static hw::RegOut ccout = hw::RegOut(&hbridge, &bat_u, &dccd_u, &dccd_i); static hw::Fuse sup_fuse = hw::Fuse(&bat_i); static hw::Fuse out_fuse = hw::Fuse(&dccd_i); diff --git a/firmware/src/hw/reg_out.cpp b/firmware/src/hw/reg_out.cpp new file mode 100644 index 0000000..9edc744 --- /dev/null +++ b/firmware/src/hw/reg_out.cpp @@ -0,0 +1,71 @@ +/**** Includes ****/ +#include "../utils/utils.h" +#include "reg_out.h" + +using namespace hw; + +/**** Private definitions ****/ +/**** Private constants ****/ +/**** Private variables ****/ +/**** Private function declarations ****/ + +/**** Public function definitions ****/ +hw::RegOut::RegOut(bsp::Hafbridge* hbridge, bsp::AnalogIn* sup_u, bsp::AnalogIn* out_u, bsp::AnalogIn* out_i) +{ + this->hbridge = hbridge; + this->supply_voltage = sup_u; + this->out_voltage = out_u; + this->out_currnet = out_i; + + this->max_voltage = 6750; + this->max_current = 4500; + this->min_voltage = 200; + + this->set_voltage = 0; + this->out_impedance = 0xFFFF; + + this->hbridge->disable(); +} + +hw::RegOut::~RegOut(void) +{ + this->hbridge->write((uint16_t)0x0000); + this->hbridge->disable(); + return; +} + +void hw::RegOut::update(void) +{ + // Calculate output impedance + if((this->out_currnet == 0)||(this->out_voltage->last_read == 0)) this->out_impedance = 0xFFFF; + else this->out_impedance = util::sat_div_kilo(this->out_voltage->last_read, this->out_currnet->last_read); + + // Recalculate output set voltage + if((this->max_voltage==0)||(this->max_current==0)) this->set_voltage = 0; + else this->set_voltage = util::sat_mul_kilo(this->max_current, this->out_impedance); + + // Limit set voltage + if(this->set_voltage > this->max_voltage) this->set_voltage = this->max_voltage; + + if((this->set_voltage>0)&&(this->set_voltage < this->min_voltage)) this->set_voltage = this->min_voltage; + + // Set output + this->hbridge->write(util::sat_ratio(this->set_voltage, this->supply_voltage->last_read)); +} + +void hw::RegOut::enable(void) +{ + this->hbridge->enable(); +} + +void hw::RegOut::disable(void) +{ + this->hbridge->disable(); +} + +uint8_t hw::RegOut::is_enabled(void) +{ + return this->hbridge->is_enabled(); +} + +/**** Private function definitions ****/ diff --git a/firmware/src/hw/reg_out.h b/firmware/src/hw/reg_out.h new file mode 100644 index 0000000..f2356c7 --- /dev/null +++ b/firmware/src/hw/reg_out.h @@ -0,0 +1,45 @@ +#ifndef REGULATED_OUTPUT_H_ +#define REGULATED_OUTPUT_H_ + +/**** Includes ****/ +#include +#include "../bsp/ain.h" +#include "../bsp/halfbridge.h" + +namespace hw { + +/**** Public definitions ****/ + +class RegOut +{ + protected: + bsp::Hafbridge* hbridge; + bsp::AnalogIn* supply_voltage; + bsp::AnalogIn* out_voltage; + bsp::AnalogIn* out_currnet; + + public: + RegOut(bsp::Hafbridge* hbridge, bsp::AnalogIn* sup_u, bsp::AnalogIn* out_u, bsp::AnalogIn* out_i); + ~RegOut(void); + + uint16_t max_voltage; + uint16_t max_current; + uint16_t min_voltage; + + uint16_t set_voltage; + uint16_t out_impedance; + + void update(void); + void enable(void); + void disable(void); + uint8_t is_enabled(void); +}; + +/**** Public function declarations ****/ + +#ifdef TESTING +#endif + +} //namespace + +#endif /* REGULATED_OUTPUT_H_ */ \ No newline at end of file diff --git a/firmware/src/logic/cfg_mem.cpp b/firmware/src/logic/cfg_mem.cpp index 553e7de..0ac10e5 100644 --- a/firmware/src/logic/cfg_mem.cpp +++ b/firmware/src/logic/cfg_mem.cpp @@ -14,6 +14,8 @@ static const uint16_t addr_dsp_dimm = 0x0004; static const uint16_t addr_brake_force = 0x0005; static const uint16_t addr_max_hbrake_time = 0x0006; static const uint16_t addr_lock_current = 0x0008; +static const uint16_t addr_max_out_voltage = 0x000A; +static const uint16_t addr_min_out_voltage = 0x000C; static const uint8_t def_btn_force = 0; static const uint8_t def_pot_mode = 0; @@ -23,8 +25,12 @@ static const uint8_t def_brigth = 100; static const uint8_t def_brake_force = 100; static const uint16_t def_max_hbrake_time = 1000; static const uint16_t def_lock_current = 4500; +static const uint16_t def_max_out_voltage = 7000; +static const uint16_t def_min_out_voltage = 200; -static const uint16_t max_lock_current = 6000; +static const uint16_t lim_saved_lock_current = 6000; +static const uint16_t lim_saved_max_voltage = 8000; +static const uint16_t lim_saved_min_voltage = 1000; /**** Private variables ****/ /**** Private function declarations ****/ @@ -39,6 +45,8 @@ logic::CfgMemory::CfgMemory(void) this->mem_brake_force = 0; this->mem_max_hbrake_time = 0; this->mem_lock_current = 0; + this->mem_max_out_voltage = 0; + this->mem_min_out_voltage = 0; this->restore(); } @@ -58,6 +66,8 @@ void logic::CfgMemory::init(void) this->mem_brake_force = mcu::eeprom_read8b(addr_brake_force); this->mem_max_hbrake_time = mcu::eeprom_read16b(addr_max_hbrake_time); this->mem_lock_current = mcu::eeprom_read16b(addr_lock_current); + this->mem_max_out_voltage = mcu::eeprom_read16b(addr_max_out_voltage); + this->mem_min_out_voltage = mcu::eeprom_read16b(addr_min_out_voltage); // Validate EEPROM data if(this->mem_btn_force > 100) @@ -105,12 +115,24 @@ void logic::CfgMemory::init(void) }; */ - if(this->mem_lock_current > max_lock_current) + if(this->mem_lock_current > lim_saved_lock_current) { this->mem_lock_current = def_lock_current; mcu::eeprom_write16b(addr_lock_current, this->mem_lock_current); }; + if(this->mem_max_out_voltage > lim_saved_max_voltage) + { + this->mem_max_out_voltage = def_max_out_voltage; + mcu::eeprom_write16b(addr_max_out_voltage, this->mem_max_out_voltage); + }; + + if(this->mem_min_out_voltage > lim_saved_min_voltage) + { + this->mem_min_out_voltage = def_min_out_voltage; + mcu::eeprom_write16b(addr_min_out_voltage, this->mem_min_out_voltage); + }; + this->restore(); } @@ -168,6 +190,18 @@ void logic::CfgMemory::save_all(void) this->mem_lock_current = this->lock_current; mcu::eeprom_write16b(addr_lock_current, this->mem_lock_current); }; + + if(this->max_out_voltage != this->mem_max_out_voltage) + { + this->mem_max_out_voltage = this->max_out_voltage; + mcu::eeprom_write16b(addr_max_out_voltage, this->mem_max_out_voltage); + }; + + if(this->min_out_voltage != this->mem_min_out_voltage) + { + this->mem_min_out_voltage = this->min_out_voltage; + mcu::eeprom_write16b(addr_min_out_voltage, this->mem_min_out_voltage); + }; } void logic::CfgMemory::restore(void) @@ -180,6 +214,8 @@ void logic::CfgMemory::restore(void) this->brake_force = this->mem_brake_force; this->max_hbrake_time = this->mem_max_hbrake_time; this->lock_current = this->mem_lock_current; + this->max_out_voltage = this->mem_max_out_voltage; + this->min_out_voltage = this->mem_min_out_voltage; } /**** Private function definitions ****/ diff --git a/firmware/src/logic/cfg_mem.h b/firmware/src/logic/cfg_mem.h index a7d2f1d..522f757 100644 --- a/firmware/src/logic/cfg_mem.h +++ b/firmware/src/logic/cfg_mem.h @@ -20,6 +20,8 @@ class CfgMemory uint8_t mem_brake_force; uint16_t mem_max_hbrake_time; uint16_t mem_lock_current; + uint16_t mem_max_out_voltage; + uint16_t mem_min_out_voltage; public: CfgMemory(void); @@ -33,6 +35,8 @@ class CfgMemory uint8_t brake_force; uint16_t max_hbrake_time; uint16_t lock_current; + uint16_t max_out_voltage; + uint16_t min_out_voltage; void init(void); void save(void); diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index bf0ffa7..a5f1edf 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -29,8 +29,25 @@ int main(void) // Read saved config cfg_mem.init(); + #ifdef OVERRIDE_CONFIG + cfg_mem.btn_force = 0; + cfg_mem.bmode = 0; + cfg_mem.pot_mode = 0; + cfg_mem.dsp_brigth = 100; + cfg_mem.dsp_dimm = 50; + cfg_mem.brake_force = 100; + cfg_mem.max_hbrake_time = 1000; + cfg_mem.lock_current = 4200; + cfg_mem.max_out_voltage = 6500; + cfg_mem.min_out_voltage = 500; + cfg_mem.save_all(); + #endif + uint8_t user_force = 0; + ccout.max_voltage = cfg_mem.max_out_voltage; + ccout.min_voltage = cfg_mem.min_out_voltage; + button_force.force = cfg_mem.btn_force; dccd_force.brake_mode = cfg_mem.bmode; dccd_force.max_hbrake_time = cfg_mem.max_hbrake_time; @@ -56,7 +73,7 @@ int main(void) if((sup_fuse.fault)||(out_fuse.fault)) dccd_force.force = 0; // Convert force to current - ccout.target = util::percent_of(dccd_force.force, cfg_mem.lock_current); + ccout.max_current = util::percent_of(dccd_force.force, cfg_mem.lock_current); // Execute outputs ccout.update(); diff --git a/firmware/src/uDCCD.cppproj b/firmware/src/uDCCD.cppproj index a4b7be9..04782f9 100644 --- a/firmware/src/uDCCD.cppproj +++ b/firmware/src/uDCCD.cppproj @@ -207,18 +207,6 @@ compile - - compile - - - compile - - - compile - - - compile - compile @@ -243,6 +231,12 @@ compile + + compile + + + compile + compile