Compare commits
2 Commits
4adcb7eba9
...
a05c53401f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a05c53401f | ||
|
|
8bac1f4787 |
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -8,33 +8,6 @@ namespace mcu {
|
||||
|
||||
/**** Public definitions ****/
|
||||
/*
|
||||
GPIO0 Down
|
||||
GPIO1 Up
|
||||
GPIO2 Mode
|
||||
GPIO3 Handbrake
|
||||
GPIO4 Brakes
|
||||
GPIO5 Dimm
|
||||
GPIO6 LED0
|
||||
GPIO7 LED1
|
||||
GPIO8 LED2
|
||||
GPIO9 LED3
|
||||
GPIO10 LED4
|
||||
GPIO11 LED5
|
||||
GPIO12 DCCD Enable
|
||||
GPIO13 Handbrake pull
|
||||
GPIO14 Speed pull
|
||||
GPIO15 DCCD PWM
|
||||
GPIO16 LED PWM
|
||||
|
||||
ADC0 Output current
|
||||
ADC1 Output voltage
|
||||
ADC2 Battery current
|
||||
ADC3 Battery voltage
|
||||
ADC4 Potentiometer
|
||||
ADC5 Mode
|
||||
ADC8 MCU temperature
|
||||
ADC14 MCU internal reference
|
||||
ADC15 MCU ground
|
||||
*/
|
||||
|
||||
const uint8_t GPIO0 = 0; //PC5 Mode
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
#ifndef UDCCD_BOARD_H_
|
||||
#define UDCCD_BOARD_H_
|
||||
|
||||
/**** Includes ****/
|
||||
#include <stdint.h>
|
||||
|
||||
using namespace board;
|
||||
|
||||
/**** Public definitions ****/
|
||||
static AnalogIn dccd_i(mcu::ADC0);
|
||||
static AnalogIn dccd_u(mcu::ADC1);
|
||||
static AnalogIn bat_u(mcu::ADC2);
|
||||
static AnalogIn bat_i(mcu::ADC3);
|
||||
|
||||
static Hafbridge hbridge(mcu::PWM0, mcu::GPIO15, 95);
|
||||
|
||||
static AnalogIn ain1(mcu::ADC5);
|
||||
static AnalogIn ain2(mcu::ADC4);
|
||||
|
||||
static DigitalIn din1(mcu::GPIO0, 0, board::DIN_HIGH);
|
||||
static DigitalIn din2(mcu::GPIO1, 0, board::DIN_HIGH);
|
||||
static DigitalIn din3(mcu::GPIO2, 0, board::DIN_HIGH);
|
||||
static DigitalIn din4(mcu::GPIO3, 0, board::DIN_HIGH);
|
||||
|
||||
static DigitalIn hvdin1(mcu::GPIO4, 1, board::DIN_LOW);
|
||||
static DigitalIn hvdin2(mcu::GPIO5, 1, board::DIN_LOW);
|
||||
static DigitalIn hvdin3(mcu::GPIO6, 1, board::DIN_LOW);
|
||||
static DigitalIO hvdin3_pull(mcu::GPIO3, board::DIN_HIGH);
|
||||
|
||||
static DigitalOut odout1(mcu::GPIO9, 1);
|
||||
static DigitalOut odout2(mcu::GPIO10, 1);
|
||||
static DigitalOut odout3(mcu::GPIO11, 1);
|
||||
static DigitalOut odout4(mcu::GPIO12, 1);
|
||||
static DigitalOut odout5(mcu::GPIO13, 1);
|
||||
static DigitalOut odout6(mcu::GPIO14, 1);
|
||||
|
||||
static PWMout od_pwm(mcu::PWM1);
|
||||
|
||||
/**** Public function declarations ****/
|
||||
|
||||
#ifdef TESTING
|
||||
#endif
|
||||
|
||||
#endif /* UDCCD_BOARD_H_ */
|
||||
@@ -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 ****/
|
||||
@@ -1,37 +0,0 @@
|
||||
#ifndef CONST_VOLTAGE_DRIVER_H_
|
||||
#define CONST_VOLTAGE_DRIVER_H_
|
||||
|
||||
/**** Includes ****/
|
||||
#include <stdint.h>
|
||||
#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_ */
|
||||
52
firmware/src/hw/cv_otput.cpp
Normal file
52
firmware/src/hw/cv_otput.cpp
Normal file
@@ -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 ****/
|
||||
37
firmware/src/hw/cv_output.h
Normal file
37
firmware/src/hw/cv_output.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef CONST_VOLTAGE_OUTPUT_H_
|
||||
#define CONST_VOLTAGE_OUTPUT_H_
|
||||
|
||||
/**** Includes ****/
|
||||
#include <stdint.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_ */
|
||||
@@ -12,23 +12,65 @@
|
||||
#include "hw/button.h"
|
||||
#include "hw/potentiometer.h"
|
||||
#include "hw/display_led.h"
|
||||
|
||||
#include "board/udccd_board.h"
|
||||
#include "hw/cv_output.h"
|
||||
|
||||
/**** Private definitions ****/
|
||||
static board::AnalogIn dccd_i(mcu::ADC0);
|
||||
static board::AnalogIn dccd_u(mcu::ADC1);
|
||||
static board::AnalogIn bat_u(mcu::ADC2);
|
||||
static board::AnalogIn bat_i(mcu::ADC3);
|
||||
|
||||
static board::Hafbridge hbridge(mcu::PWM0, mcu::GPIO15, 95);
|
||||
|
||||
static board::AnalogIn ain1(mcu::ADC5); // mode
|
||||
static board::AnalogIn ain2(mcu::ADC4); // pot
|
||||
|
||||
static board::DigitalIn din1(mcu::GPIO0, 0, board::DIN_HIGH); //mode
|
||||
static board::DigitalIn din2(mcu::GPIO1, 0, board::DIN_HIGH); //pot
|
||||
static board::DigitalIn din3(mcu::GPIO2, 0, board::DIN_HIGH); //down
|
||||
static board::DigitalIn din4(mcu::GPIO3, 0, board::DIN_HIGH); //up
|
||||
|
||||
static board::DigitalIn hvdin1(mcu::GPIO4, 1, board::DIN_LOW); //dimm
|
||||
static board::DigitalIn hvdin2(mcu::GPIO5, 1, board::DIN_LOW); //brakes
|
||||
static board::DigitalIn hvdin3(mcu::GPIO6, 1, board::DIN_LOW); //hbrake
|
||||
static board::DigitalIO hvdin3_pull(mcu::GPIO7, board::DIN_HIGH); //hbrake pull
|
||||
|
||||
static board::DigitalOut odout1(mcu::GPIO9, 1);
|
||||
static board::DigitalOut odout2(mcu::GPIO10, 1);
|
||||
static board::DigitalOut odout3(mcu::GPIO11, 1);
|
||||
static board::DigitalOut odout4(mcu::GPIO12, 1);
|
||||
static board::DigitalOut odout5(mcu::GPIO13, 1);
|
||||
static board::DigitalOut odout6(mcu::GPIO14, 1);
|
||||
|
||||
static board::PWMout od_pwm(mcu::PWM1);
|
||||
|
||||
static hw::Button btn_mode(&din1, board::DIN_LOW, 10, hw::BUTTON_OFF);
|
||||
static hw::Button btn_up(&din4, board::DIN_LOW, 10, hw::BUTTON_OFF);
|
||||
static hw::Button btn_down(&din3, board::DIN_LOW, 10, hw::BUTTON_OFF);
|
||||
|
||||
static hw::Button sw_dimm(&hvdin1, board::DIN_HIGH, 10, hw::BUTTON_OFF);
|
||||
static hw::Button sw_brakes(&hvdin2, board::DIN_HIGH, 10, hw::BUTTON_OFF);
|
||||
static hw::Button sw_hbrake(&hvdin3, board::DIN_LOW, 10, hw::BUTTON_OFF);
|
||||
|
||||
static hw::Potentiometer pot(&ain2, 500, 4500);
|
||||
|
||||
static hw::DisplayLed display(&odout1, &odout2, &odout3, &odout4, &odout5, &odout6, &od_pwm);
|
||||
|
||||
static hw::CVoutput cvout(&hbridge);
|
||||
|
||||
/**** Private constants ****/
|
||||
/**** Private variables ****/
|
||||
/**** Private function declarations ****/
|
||||
static void board_setup(void);
|
||||
|
||||
/**** Public function definitions ****/
|
||||
int main(void)
|
||||
{
|
||||
mcu::startupCfg_t mcu_cfg;
|
||||
mcu_cfg.adc_clk = mcu::ADC_DIV2;
|
||||
mcu_cfg.pwm_clk = mcu::TIM_DIV1;
|
||||
mcu_cfg.pwm_top = 200;
|
||||
mcu_cfg.pwm_ch1_en = 1;
|
||||
board_setup();
|
||||
|
||||
mcu::startup(&mcu_cfg);
|
||||
cvout.target = 0;
|
||||
cvout.min_out = 500;
|
||||
cvout.enable();
|
||||
|
||||
// Super loop
|
||||
while(1)
|
||||
@@ -41,3 +83,35 @@ int main(void)
|
||||
}
|
||||
|
||||
/**** Private function definitions ***/
|
||||
static void board_setup(void)
|
||||
{
|
||||
mcu::startupCfg_t mcu_cfg;
|
||||
mcu_cfg.adc_clk = mcu::ADC_DIV2;
|
||||
mcu_cfg.pwm_clk = mcu::TIM_DIV1;
|
||||
mcu_cfg.pwm_top = 200;
|
||||
mcu_cfg.pwm_ch1_en = 1;
|
||||
|
||||
mcu::startup(&mcu_cfg);
|
||||
|
||||
dccd_i.mul = 215;
|
||||
dccd_i.div = 22;
|
||||
dccd_i.offset = 0;
|
||||
dccd_i.last_read = 0;
|
||||
|
||||
dccd_u.mul = 20;
|
||||
dccd_u.div = 1;
|
||||
dccd_u.offset = 0;
|
||||
dccd_u.last_read = 0;
|
||||
|
||||
bat_u.mul = 20;
|
||||
bat_u.div = 1;
|
||||
bat_u.offset = 0;
|
||||
bat_u.last_read = 12000;
|
||||
|
||||
bat_i.mul = 235;
|
||||
bat_i.div = 6;
|
||||
bat_i.offset = 0;
|
||||
bat_i.last_read = 0;
|
||||
|
||||
od_pwm.write(100);
|
||||
}
|
||||
@@ -195,19 +195,16 @@
|
||||
<Compile Include="board\pwm.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="board\udccd_board.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="hw\button.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="hw\button.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="hw\cv_driver.cpp">
|
||||
<Compile Include="hw\cv_otput.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="hw\cv_driver.h">
|
||||
<Compile Include="hw\cv_output.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="hw\display_led.cpp">
|
||||
|
||||
Reference in New Issue
Block a user