diff --git a/firmware/src/board/halfbridge.cpp b/firmware/src/board/halfbridge.cpp index 6ede670..7047e32 100644 --- a/firmware/src/board/halfbridge.cpp +++ b/firmware/src/board/halfbridge.cpp @@ -16,8 +16,10 @@ board::Hafbridge::Hafbridge(uint8_t hs_pwm_ch, uint8_t ls_gpio_ch, uint8_t max_d this->pwm_ch = hs_pwm_ch; this->gpio_ch = ls_gpio_ch; - if(max_dc>100) this->max_dc = 100; - else this->max_dc = max_dc; + if(max_dc>100) max_dc = 100; + + this->max_dc = util::percent_to_16b(max_dc); + this->disable(); } @@ -27,18 +29,22 @@ board::Hafbridge::~Hafbridge(void) this->disable(); } -void board::Hafbridge::write(uint8_t duty) +void board::Hafbridge::write(uint16_t dividend) { // Limit duty - if(duty > this->max_dc) duty = this->max_dc; - this->last_duty = duty; + if(dividend > this->max_dc) dividend = this->max_dc; + this->last_duty = dividend; if(this->enabled == 0) return; - // Convert percent to 16b duty cycle - uint16_t dc = util::percent_to_16b(duty); // Set PWM - mcu::pwm_write(this->pwm_ch, dc); + mcu::pwm_write(this->pwm_ch, dividend); +} + +void board::Hafbridge::write(uint8_t percent) +{ + // Convert to dividend/0xFFFF + this->write(util::percent_to_16b(percent)); } void board::Hafbridge::enable(void) diff --git a/firmware/src/board/halfbridge.h b/firmware/src/board/halfbridge.h index ef6414f..6ea7a63 100644 --- a/firmware/src/board/halfbridge.h +++ b/firmware/src/board/halfbridge.h @@ -12,15 +12,16 @@ class Hafbridge protected: uint8_t pwm_ch; uint8_t gpio_ch; - uint8_t last_duty; + uint16_t last_duty; uint8_t enabled; - uint8_t max_dc; + uint16_t max_dc; public: Hafbridge(uint8_t hs_pwm_ch, uint8_t ls_gpio_ch, uint8_t max_dc); ~Hafbridge(void); - void write(uint8_t duty); + void write(uint16_t dividend); + void write(uint8_t percent); void enable(void); void disable(void); uint8_t get_set_duty(void); diff --git a/firmware/src/hw/cv_driver.cpp b/firmware/src/hw/cv_driver.cpp deleted file mode 100644 index 734d8eb..0000000 --- a/firmware/src/hw/cv_driver.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/**** Includes ****/ -#include "../utils/utils.h" -#include "cv_driver.h" - -using namespace hw; - -/**** Private definitions ****/ -/**** Private constants ****/ -/**** Private variables ****/ -/**** Private function declarations ****/ - -/**** Public function definitions ****/ -hw::CVdriver::CVdriver(board::AnalogIn* sup_voltage, board::AnalogIn* out_voltage, board::Hafbridge* hbridge) -{ - this->sup_voltage = sup_voltage; - this->hbridge = hbridge; - this->target = 0; - this->off = 1; -} - -hw::CVdriver::~CVdriver(void) -{ - return; -} - -void hw::CVdriver::update(void) -{ - // Update all inputs - this->sup_voltage->read(); - - // Calculate ratio - uint16_t ratio = util::sat_ratio(this->target, this->sup_voltage->last_read); - - // Set output - this->hbridge->write(ratio); -} - - - -/**** Private function definitions ****/ diff --git a/firmware/src/hw/cv_driver.h b/firmware/src/hw/cv_driver.h deleted file mode 100644 index a39e357..0000000 --- a/firmware/src/hw/cv_driver.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef CONST_VOLTAGE_DRIVER_H_ -#define CONST_VOLTAGE_DRIVER_H_ - -/**** Includes ****/ -#include -#include "../board/ain.h" -#include "../board/halfbridge.h" - -namespace hw { - -/**** Public definitions ****/ - -class CVdriver -{ - protected: - board::AnalogIn* sup_voltage; - - board::Hafbridge* hbridge; - - public: - CVdriver(board::AnalogIn* sup_voltage, board::Hafbridge* hbridge); - ~CVdriver(void); - - uint16_t target = 0; - uint8_t off = 0; - - void process(void); -}; - -/**** Public function declarations ****/ - -#ifdef TESTING -#endif - -} //namespace - -#endif /* CONST_VOLTAGE_DRIVER_H_ */ \ No newline at end of file diff --git a/firmware/src/hw/cv_otput.cpp b/firmware/src/hw/cv_otput.cpp new file mode 100644 index 0000000..f2c25f8 --- /dev/null +++ b/firmware/src/hw/cv_otput.cpp @@ -0,0 +1,52 @@ +/**** Includes ****/ +#include "../utils/utils.h" +#include "cv_output.h" + +using namespace hw; + +/**** Private definitions ****/ +/**** Private constants ****/ +/**** Private variables ****/ +/**** Private function declarations ****/ + +/**** Public function definitions ****/ +hw::CVoutput::CVoutput(board::Hafbridge* hbridge) +{ + this->hbridge = hbridge; + this->target = 0; + this->min_out = 0; + this->hbridge->disable(); +} + +hw::CVoutput::~CVoutput(void) +{ + this->hbridge->write((uint16_t)0); + this->hbridge->disable(); + return; +} + +void hw::CVoutput::update(uint16_t supply_mv) +{ + // Check target + if((this->target < this->min_out)&&(this->target > 0)) this->target = this->min_out; + + // Set output + this->hbridge->write(util::sat_ratio(this->target, supply_mv)); +} + +void hw::CVoutput::enable(void) +{ + this->hbridge->enable(); +} + +void hw::CVoutput::disable(void) +{ + this->hbridge->disable(); +} + +uint8_t hw::CVoutput::is_enabled(void) +{ + return this->hbridge->is_enabled(); +} + +/**** Private function definitions ****/ diff --git a/firmware/src/hw/cv_output.h b/firmware/src/hw/cv_output.h new file mode 100644 index 0000000..75a0a27 --- /dev/null +++ b/firmware/src/hw/cv_output.h @@ -0,0 +1,38 @@ +#ifndef CONST_VOLTAGE_OUTPUT_H_ +#define CONST_VOLTAGE_OUTPUT_H_ + +/**** Includes ****/ +#include +#include "../board/ain.h" +#include "../board/halfbridge.h" + +namespace hw { + +/**** Public definitions ****/ + +class CVoutput +{ + protected: + board::Hafbridge* hbridge; + + public: + CVoutput(board::Hafbridge* hbridge); + ~CVoutput(void); + + uint16_t target; + uint16_t min_out; + + void update(uint16_t supply_mv); + void enable(void); + void disable(void); + uint8_t is_enabled(void); +}; + +/**** Public function declarations ****/ + +#ifdef TESTING +#endif + +} //namespace + +#endif /* CONST_VOLTAGE_OUTPUT_H_ */ \ No newline at end of file diff --git a/firmware/src/uDCCD.cppproj b/firmware/src/uDCCD.cppproj index 7601bb2..5fc501d 100644 --- a/firmware/src/uDCCD.cppproj +++ b/firmware/src/uDCCD.cppproj @@ -204,10 +204,10 @@ compile - + compile - + compile