Could be working version

This commit is contained in:
2024-04-12 15:18:31 +03:00
parent 5bb3ebe1bf
commit f8b62d4b00
19 changed files with 632 additions and 53 deletions

View File

@@ -1,66 +1,116 @@
/**** Includes ****/
#include "utils/utils.h"
#include "utils/interpolate.h"
#include "bsp/mcu/mcu_hal.h"
#include "bsp/ain.h"
#include "bsp/din.h"
#include "bsp/dout.h"
#include "bsp/dio.h"
#include "bsp/halfbridge.h"
#include "bsp/pwm.h"
#include "hw/devices.h"
#include "hw/button.h"
#include "hw/potentiometer.h"
#include "hw/display_led.h"
#include "hw/cv_output.h"
#include "logic/button_force.h"
#include "logic/dccd_force.h"
/**** Private definitions ****/
/**** Private constants ****/
/**** Private variables ****/
static bsp::AnalogIn dccd_i(mcu::ADC0);
static bsp::AnalogIn dccd_u(mcu::ADC1);
static bsp::AnalogIn bat_u(mcu::ADC2);
static bsp::AnalogIn bat_i(mcu::ADC3);
static bsp::Hafbridge hbridge(mcu::PWM0, mcu::GPIO15, 95);
static bsp::AnalogIn ain1(mcu::ADC5); // mode
static bsp::AnalogIn ain2(mcu::ADC4); // pot
static bsp::DigitalIn din1(mcu::GPIO0, 0, bsp::DIN_HIGH); //mode
static bsp::DigitalIn din2(mcu::GPIO1, 0, bsp::DIN_HIGH); //pot
static bsp::DigitalIn din3(mcu::GPIO2, 0, bsp::DIN_HIGH); //down
static bsp::DigitalIn din4(mcu::GPIO3, 0, bsp::DIN_HIGH); //up
static bsp::DigitalIn hvdin1(mcu::GPIO4, 1, bsp::DIN_LOW); //dimm
static bsp::DigitalIn hvdin2(mcu::GPIO5, 1, bsp::DIN_LOW); //brakes
static bsp::DigitalIn hvdin3(mcu::GPIO6, 1, bsp::DIN_LOW); //hbrake
static bsp::DigitalIO hvdin3_pull(mcu::GPIO7, bsp::DIN_HIGH); //hbrake pull
static bsp::DigitalOut odout1(mcu::GPIO9, 1);
static bsp::DigitalOut odout2(mcu::GPIO10, 1);
static bsp::DigitalOut odout3(mcu::GPIO11, 1);
static bsp::DigitalOut odout4(mcu::GPIO12, 1);
static bsp::DigitalOut odout5(mcu::GPIO13, 1);
static bsp::DigitalOut odout6(mcu::GPIO14, 1);
static bsp::PWMout od_pwm(mcu::PWM1);
static hw::Button btn_mode(&din1, bsp::DIN_LOW, 10, hw::BUTTON_OFF);
static hw::Button btn_up(&din4, bsp::DIN_LOW, 10, hw::BUTTON_OFF);
static hw::Button btn_down(&din3, bsp::DIN_LOW, 10, hw::BUTTON_OFF);
static hw::Button sw_dimm(&hvdin1, bsp::DIN_HIGH, 10, hw::BUTTON_OFF);
static hw::Button sw_brakes(&hvdin2, bsp::DIN_HIGH, 10, hw::BUTTON_OFF);
static hw::Button sw_hbrake(&hvdin3, bsp::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, &bat_u);
static logic::ButtonForce button_force = logic::ButtonForce(&btn_up, &btn_down);
static logic::DccdForce dccd_force = logic::DccdForce(&btn_mode, &sw_hbrake, &sw_brakes);
/**** Private function declarations ****/
/**** Public function definitions ****/
int main(void)
{
// HW setup
devices_init();
uint8_t user_force = 0;
button_force.force = mcu::eeprom_read8b(0x0000);
dccd_force.brake_mode = mcu::eeprom_read8b(0x0001);
uint8_t pot_mode = mcu::eeprom_read8b(0x0002);
uint8_t dsp_brigth = mcu::eeprom_read8b(0x0003);
uint8_t dsp_dimm = mcu::eeprom_read8b(0x0004);
uint16_t lock_current = mcu::eeprom_read16b(0x0005);
// Safeguard eeprom values
if(button_force.force > 100)
{
button_force.force = 0;
mcu::eeprom_write8b(0x0000, button_force.force);
};
if(dccd_force.brake_mode > 2)
{
button_force.force = 0;
mcu::eeprom_write8b(0x0001, dccd_force.brake_mode);
};
if(pot_mode > 1)
{
pot_mode = 0;
mcu::eeprom_write8b(0x0002, pot_mode);
};
if(dsp_brigth > 100)
{
dsp_brigth = 100;
mcu::eeprom_write8b(0x0003, dsp_brigth);
};
if(dsp_dimm > 100)
{
dsp_dimm = 50;
mcu::eeprom_write8b(0x0004, dsp_dimm);
};
if(lock_current > 6000)
{
lock_current = 4500;
mcu::eeprom_write16b(0x0005, lock_current);
};
// Super loop
while(1)
{
// Update inputs
devices_update_inputs();
// Update user setting
button_force.update();
// Select user force input
if(pot_mode) user_force = pot.percent;
else user_force = button_force.force;
// Calculate next target force
dccd_force.update(user_force);
// Override force in case of fault
if((sup_fuse.fault)||(out_fuse.fault)) dccd_force.force = 0;
// Convert force to current
ccout.target = util::percent_of(dccd_force.force, lock_current);
// Execute outputs
ccout.update();
// Set display
display.show_percent(dccd_force.force, hw::DisplayLed::LED_DSP_DOT10);
// Process dimm
if(sw_dimm.state == hw::BUTTON_ON) display.set_brigthness(dsp_dimm);
else display.set_brigthness(dsp_brigth);
// Save user setting changes
if(button_force.is_new)
{
mcu::eeprom_write8b(0x0000, button_force.force);
button_force.is_new = 0;
};
if(dccd_force.is_new_bmode)
{
mcu::eeprom_write8b(0x0001, dccd_force.brake_mode);
dccd_force.is_new_bmode = 0;
};
continue; // End of super loop
}
@@ -68,4 +118,4 @@ int main(void)
return 0;
}
/**** Private function definitions ***/
/**** Private function definitions ***/