2 Commits

Author SHA1 Message Date
5bb3ebe1bf Created current fuse logic 2024-04-12 11:51:21 +03:00
22ac8240a2 Added absolute subtract 2024-04-12 11:51:10 +03:00
5 changed files with 136 additions and 1 deletions

67
firmware/src/hw/fuse.cpp Normal file
View File

@@ -0,0 +1,67 @@
/**** Includes ****/
#include "../utils/utils.h"
#include "../utils/interpolate.h"
#include "fuse.h"
using namespace hw;
/**** Private definitions ****/
/**** Private constants ****/
/**** Private variables ****/
/**** Private function declarations ****/
/**** Public function definitions ****/
hw::Fuse::Fuse(bsp::AnalogIn* ain_ch)
{
this->hold_current = 0;
this->trip_cycles = 0;
this->warning = 0;
this->fault = 0;
this->cooldown_counter = 0;
this->cooldown_cycles = 0;
this->retry_cnt = 0;
}
hw::Fuse::~Fuse(void)
{
return;
}
void hw::Fuse::update(void)
{
// Under threshold
if(this->ain_ch->last_read <= this->hold_current)
{
// Clear warning flag
this->warning = 0;
// OC energy counter
if(this->oc_counter > 0) this->oc_counter--;
// Cool down fuse
if(this->cooldown_counter > 0) this->cooldown_counter--;
// Auto reset logic
if((this->fault)&&(this->cooldown_counter==0))
{
this->fault = 0;
this->retry_cnt = util::sat_add(this->retry_cnt, 1);
};
return;
};
// Over current condition
this->warning = 1;
// PC energy counter
this->oc_counter = util::sat_add(this->oc_counter, 1);
// Check for trip threshold
if(this->oc_counter < this->trip_cycles) return;
// Trip fuse
this->fault = 1;
this->cooldown_counter = this->cooldown_cycles;
}
/**** Private function definitions ****/

40
firmware/src/hw/fuse.h Normal file
View File

@@ -0,0 +1,40 @@
#ifndef FUSE_H_
#define FUSE_H_
/**** Includes ****/
#include <stdint.h>
#include "../bsp/ain.h"
namespace hw {
/**** Public definitions ****/
class Fuse
{
protected:
bsp::AnalogIn* ain_ch;
uint16_t oc_counter;
uint16_t cooldown_counter;
public:
Fuse(bsp::AnalogIn* ain_ch);
~Fuse(void);
uint16_t hold_current;
uint16_t trip_cycles;
uint8_t warning;
uint8_t fault;
uint16_t cooldown_cycles;
uint8_t retry_cnt;
void update(void);
};
/**** Public function declarations ****/
#ifdef TESTING
#endif
} //namespace
#endif /* POTENTIOMETER_H_ */

View File

@@ -213,6 +213,12 @@
<Compile Include="hw\display_led.h"> <Compile Include="hw\display_led.h">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="hw\fuse.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\fuse.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="hw\potentiometer.cpp"> <Compile Include="hw\potentiometer.cpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>

View File

@@ -57,7 +57,6 @@ uint16_t util::sat_subtract(uint16_t x, uint16_t y)
if(z > x) return 0; if(z > x) return 0;
else return z; else return z;
} }
uint32_t util::sat_subtract(uint32_t x, uint32_t y) uint32_t util::sat_subtract(uint32_t x, uint32_t y)
{ {
uint32_t z = x - y; uint32_t z = x - y;
@@ -66,6 +65,25 @@ uint32_t util::sat_subtract(uint32_t x, uint32_t y)
else return z; else return z;
} }
uint8_t util::abs_subtract(uint8_t x, uint8_t y)
{
if(x > y) return x - y;
else return y-x;
}
uint16_t util::abs_subtract(uint16_t x, uint16_t y)
{
if(x > y) return x - y;
else return y-x;
}
uint32_t util::abs_subtract(uint32_t x, uint32_t y)
{
if(x > y) return x - y;
else return y-x;
}
uint16_t util::sat_cast(uint32_t x) uint16_t util::sat_cast(uint32_t x)
{ {
if(x > 0x0000FFFF) return 0xFFFF; if(x > 0x0000FFFF) return 0xFFFF;

View File

@@ -27,6 +27,10 @@ uint8_t sat_subtract(uint8_t x, uint8_t y);
uint16_t sat_subtract(uint16_t x, uint16_t y); uint16_t sat_subtract(uint16_t x, uint16_t y);
uint32_t sat_subtract(uint32_t x, uint32_t y); uint32_t sat_subtract(uint32_t x, uint32_t y);
uint8_t abs_subtract(uint8_t x, uint8_t y);
uint16_t abs_subtract(uint16_t x, uint16_t y);
uint32_t abs_subtract(uint32_t x, uint32_t y);
uint16_t interpolate_1d(uint16_t x, uint16_t* x_axis, uint16_t* y_values, uint8_t len_axis); uint16_t interpolate_1d(uint16_t x, uint16_t* x_axis, uint16_t* y_values, uint8_t len_axis);
uint16_t interpolate_2d(uint16_t x, uint16_t y, uint16_t* x_axis, uint8_t len_x_axis, uint16_t* y_axis, uint8_t len_y_axis, uint16_t* z_values); uint16_t interpolate_2d(uint16_t x, uint16_t y, uint16_t* x_axis, uint8_t len_x_axis, uint16_t* y_axis, uint8_t len_y_axis, uint16_t* z_values);