Created regulated output

This commit is contained in:
2024-04-13 14:20:45 +03:00
parent e4bd6d2e04
commit 91a7247bee
8 changed files with 189 additions and 21 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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 ****/

45
firmware/src/hw/reg_out.h Normal file
View File

@@ -0,0 +1,45 @@
#ifndef REGULATED_OUTPUT_H_
#define REGULATED_OUTPUT_H_
/**** Includes ****/
#include <stdint.h>
#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_ */

View File

@@ -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 ****/

View File

@@ -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);

View File

@@ -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();

View File

@@ -207,18 +207,6 @@
<Compile Include="hw\button.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\cc_output.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\cc_output.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\cv_output.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\cv_output.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\display_led.cpp">
<SubType>compile</SubType>
</Compile>
@@ -243,6 +231,12 @@
<Compile Include="hw\potentiometer.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\reg_out.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\reg_out.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="logic\button_force.cpp">
<SubType>compile</SubType>
</Compile>