Legacy branch migration
This commit is contained in:
127
firmware/logic/coil.c
Normal file
127
firmware/logic/coil.c
Normal file
@@ -0,0 +1,127 @@
|
||||
/**** Includes ****/
|
||||
#include "coil.h"
|
||||
|
||||
/**** Private definitions ****/
|
||||
|
||||
/**** Private constants ****/
|
||||
|
||||
/**** Private variables ****/
|
||||
static uint16_t lock_current = 4500; //mA
|
||||
static uint16_t min_current = 100; //mA
|
||||
static uint16_t target_current = 0; //mA
|
||||
static uint8_t new_target = 1;
|
||||
|
||||
static uint16_t adj_target_current = 0; //mA
|
||||
static uint16_t target_voltage = 0; //mV
|
||||
|
||||
static uint8_t act_force = 0;
|
||||
|
||||
static uint16_t nominal_resitance = 1500; //mR
|
||||
|
||||
static int32_t sum_current = 0;
|
||||
static uint8_t sum_cnt = 0;
|
||||
|
||||
|
||||
/**** Private function declarations ****/
|
||||
static uint16_t UpdateVoltage(uint16_t cur, uint16_t res);
|
||||
|
||||
/**** Public function definitions ****/
|
||||
void Coil_SetLockCurrent(uint16_t lock_i)
|
||||
{
|
||||
lock_current = lock_i;
|
||||
}
|
||||
|
||||
void Coil_SetTarget_Force(uint8_t force)
|
||||
{
|
||||
// Check if worth doing
|
||||
if(force == act_force) return;
|
||||
|
||||
// Calculate new target current
|
||||
act_force = force;
|
||||
|
||||
// Check simple answer
|
||||
uint16_t new_current = 0;
|
||||
if(force==0) new_current = 0;
|
||||
else if(force >= 100) new_current = lock_current;
|
||||
else
|
||||
{
|
||||
uint32_t temp = (uint32_t)force * lock_current;
|
||||
temp /= 100;
|
||||
|
||||
if(temp > 0x0000FFFF) new_current = lock_current;
|
||||
else new_current = (uint16_t) temp;
|
||||
}
|
||||
|
||||
// Update new target
|
||||
Coil_SetTarget_Current(new_current);
|
||||
}
|
||||
|
||||
void Coil_SetTarget_Current(uint16_t current)
|
||||
{
|
||||
if(current >= lock_current) current = lock_current;
|
||||
else if((current > 0)&&(current <= min_current)) current = min_current;
|
||||
|
||||
if(current != target_current) new_target = 1;
|
||||
target_current = current;
|
||||
}
|
||||
|
||||
uint16_t Coil_GetTargetVolatge(void)
|
||||
{
|
||||
return target_voltage;
|
||||
}
|
||||
|
||||
uint16_t Coil_Update(analog_t* meas)
|
||||
{
|
||||
// Collect average current
|
||||
sum_current += meas->hb_currnet;
|
||||
sum_cnt++;
|
||||
|
||||
// Update measurement
|
||||
if(sum_cnt >= 10)
|
||||
{
|
||||
// Calculate average
|
||||
sum_current /= sum_cnt;
|
||||
|
||||
// Calculate error
|
||||
int32_t error = sum_current - (int32_t)target_current;
|
||||
int32_t temp = (int32_t)adj_target_current - error;
|
||||
|
||||
// Limit to 16bits
|
||||
if(temp<0) adj_target_current = 0;
|
||||
else if(temp > 0x0000FFFF) adj_target_current = 0xFFFF;
|
||||
else adj_target_current = (uint16_t)temp;
|
||||
sum_cnt = 0;
|
||||
sum_current = 0;
|
||||
};
|
||||
|
||||
// Closed loop or open loop target set
|
||||
if(new_target)
|
||||
{
|
||||
// Set open-loop HB output
|
||||
target_voltage = UpdateVoltage(target_current, nominal_resitance);
|
||||
adj_target_current = target_current;
|
||||
new_target = 0;
|
||||
sum_cnt = 0;
|
||||
sum_current = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
target_voltage = UpdateVoltage(adj_target_current, nominal_resitance);
|
||||
}
|
||||
|
||||
return target_voltage;
|
||||
}
|
||||
|
||||
/**** Private function definitions ****/
|
||||
static uint16_t UpdateVoltage(uint16_t cur, uint16_t res)
|
||||
{
|
||||
// Update settable voltage
|
||||
if(cur==0) return 0;
|
||||
else
|
||||
{
|
||||
uint32_t volt = (uint32_t)cur * res;
|
||||
volt /= 1000;
|
||||
if(volt > 0x0000FFFF) return 0xFFFF;
|
||||
else return (uint16_t)volt;
|
||||
}
|
||||
}
|
||||
19
firmware/logic/coil.h
Normal file
19
firmware/logic/coil.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef COIL_LOGIC_H_
|
||||
#define COIL_LOGIC_H_
|
||||
|
||||
/**** Includes ****/
|
||||
#include <stdint.h>
|
||||
#include "../devices/analog.h"
|
||||
|
||||
/**** Public definitions ****/
|
||||
|
||||
/**** Public function declarations ****/
|
||||
void Coil_SetTarget_Force(uint8_t force);
|
||||
void Coil_SetTarget_Current(uint16_t current);
|
||||
uint16_t Coil_GetTargetVolatge(void);
|
||||
|
||||
uint16_t Coil_Update(analog_t* meas);
|
||||
|
||||
void Coil_SetLockCurrent(uint16_t lock_i);
|
||||
|
||||
#endif /* COIL_LOGIC_H_ */
|
||||
186
firmware/logic/force.c
Normal file
186
firmware/logic/force.c
Normal file
@@ -0,0 +1,186 @@
|
||||
/**** Includes ****/
|
||||
#include "force.h"
|
||||
|
||||
/**** Private definitions ****/
|
||||
|
||||
/**** Private constants ****/
|
||||
static const int8_t BUTTONS_STEP = 10;
|
||||
static const uint16_t BUTTON_HOLD_TIME = 250;
|
||||
static const uint16_t MODE_HOLD_TIME = 500;
|
||||
|
||||
/**** Private variables ****/
|
||||
static inputMode_t input_mode = IM_BUTTONS;
|
||||
|
||||
static uint8_t user_force = 0;
|
||||
static uint8_t new_user_force = 0;
|
||||
|
||||
static brakeMode_t brake_mode = BM_OPEN;
|
||||
static uint8_t new_brake_mode = 1;
|
||||
|
||||
/**** Private function declarations ****/
|
||||
static uint8_t SaturatedAdd(uint8_t base, int8_t delta);
|
||||
|
||||
/**** Public function definitions ****/
|
||||
inputMode_t Force_GetInputMode(void)
|
||||
{
|
||||
return input_mode;
|
||||
}
|
||||
|
||||
void Force_CfgInputMode(inputMode_t in_mode)
|
||||
{
|
||||
input_mode = in_mode;
|
||||
}
|
||||
|
||||
void Force_SetUserForce(uint8_t force)
|
||||
{
|
||||
if(force > 100) force = 100;
|
||||
user_force = force;
|
||||
}
|
||||
|
||||
void Force_SetBrakeMode(uint8_t bmode)
|
||||
{
|
||||
brake_mode = bmode;
|
||||
}
|
||||
|
||||
uint8_t Force_Update(inputs_t* inputs, analog_t* meas)
|
||||
{
|
||||
// Process user inputs
|
||||
if(input_mode==IM_POT)
|
||||
{
|
||||
// Process potentiometer
|
||||
if(meas->pot_voltage <= 500) user_force = 0;
|
||||
else if(meas->pot_voltage >= 4500 ) user_force = 100;
|
||||
else
|
||||
{
|
||||
uint16_t pot_u = meas->pot_voltage;
|
||||
pot_u /= 50;
|
||||
|
||||
//Limit to 100
|
||||
if(pot_u > 100) user_force = 100;
|
||||
else if(pot_u < 10) user_force = 10;
|
||||
else user_force = (uint8_t)pot_u;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Process +/- timer
|
||||
if((inputs->down.is_active)&&(inputs->down.state_timer > BUTTON_HOLD_TIME))
|
||||
{
|
||||
inputs->down.is_new = 1;
|
||||
inputs->down.state_timer = 0;
|
||||
};
|
||||
|
||||
if((inputs->up.is_active)&&(inputs->up.state_timer > BUTTON_HOLD_TIME))
|
||||
{
|
||||
inputs->up.is_new = 1;
|
||||
inputs->up.state_timer = 0;
|
||||
};
|
||||
|
||||
// Process +/- logic
|
||||
if((inputs->down.is_new)&&(inputs->down.is_active))
|
||||
{
|
||||
user_force = SaturatedAdd(user_force, -1 * BUTTONS_STEP);
|
||||
new_user_force = 1;
|
||||
}
|
||||
else if((inputs->up.is_new)&&(inputs->up.is_active))
|
||||
{
|
||||
user_force = SaturatedAdd(user_force, BUTTONS_STEP);
|
||||
new_user_force = 1;
|
||||
};
|
||||
|
||||
inputs->down.is_new = 0;
|
||||
inputs->up.is_new = 0;
|
||||
}
|
||||
|
||||
// Process mode timer
|
||||
if((inputs->mode.is_active)&&(inputs->mode.state_timer > MODE_HOLD_TIME))
|
||||
{
|
||||
inputs->mode.is_new = 1;
|
||||
inputs->mode.state_timer = 0;
|
||||
};
|
||||
|
||||
// Process mode logic
|
||||
if((inputs->mode.is_new)&&(inputs->mode.is_active))
|
||||
{
|
||||
//Cycle mode
|
||||
switch(brake_mode)
|
||||
{
|
||||
case BM_OPEN:
|
||||
brake_mode = BM_KEEP;
|
||||
break;
|
||||
|
||||
case BM_KEEP:
|
||||
brake_mode = BM_LOCK;
|
||||
break;
|
||||
|
||||
case BM_LOCK:
|
||||
brake_mode = BM_OPEN;
|
||||
break;
|
||||
|
||||
default:
|
||||
brake_mode = BM_OPEN;
|
||||
break;
|
||||
}
|
||||
new_brake_mode = 1;
|
||||
};
|
||||
inputs->mode.is_new = 0;
|
||||
|
||||
|
||||
// Determine next target force from inputs
|
||||
if(inputs->handbrake.is_active)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if(inputs->brakes.is_active)
|
||||
{
|
||||
switch(brake_mode)
|
||||
{
|
||||
case BM_LOCK:
|
||||
return 100;
|
||||
|
||||
case BM_KEEP:
|
||||
return user_force;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return user_force;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t Force_IsNewUserForce(void)
|
||||
{
|
||||
return new_user_force;
|
||||
}
|
||||
|
||||
void Force_ResetNewUserForce(void)
|
||||
{
|
||||
new_user_force = 0;
|
||||
}
|
||||
|
||||
brakeMode_t Force_GetBrakeMode(void)
|
||||
{
|
||||
return brake_mode;
|
||||
}
|
||||
|
||||
uint8_t Force_IsNewBrakeMode(void)
|
||||
{
|
||||
return new_brake_mode;
|
||||
}
|
||||
|
||||
void Force_ResetNewBrakeMode(void)
|
||||
{
|
||||
new_brake_mode = 0;
|
||||
}
|
||||
|
||||
/**** Private function definitions ****/
|
||||
static uint8_t SaturatedAdd(uint8_t base, int8_t delta)
|
||||
{
|
||||
int16_t temp = (int16_t)base + delta;
|
||||
if(temp < 0) return 0;
|
||||
else if(temp >= 100) return 100;
|
||||
else return (uint8_t)temp;
|
||||
}
|
||||
36
firmware/logic/force.h
Normal file
36
firmware/logic/force.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef FORCE_LOGIC_H_
|
||||
#define FORCE_LOGIC_H_
|
||||
|
||||
/**** Includes ****/
|
||||
#include <stdint.h>
|
||||
#include "../devices/analog.h"
|
||||
#include "../devices/inputs.h"
|
||||
|
||||
/**** Public definitions ****/
|
||||
typedef enum {
|
||||
BM_OPEN,
|
||||
BM_KEEP,
|
||||
BM_LOCK
|
||||
} brakeMode_t;
|
||||
|
||||
typedef enum {
|
||||
IM_BUTTONS,
|
||||
IM_POT
|
||||
} inputMode_t;
|
||||
|
||||
/**** Public function declarations ****/
|
||||
void Force_CfgInputMode(inputMode_t in_mode);
|
||||
inputMode_t Force_GetInputMode(void);
|
||||
|
||||
uint8_t Force_Update(inputs_t* inputs, analog_t* meas);
|
||||
|
||||
void Force_SetBrakeMode(uint8_t bmode);
|
||||
brakeMode_t Force_GetBrakeMode(void);
|
||||
uint8_t Force_IsNewBrakeMode(void);
|
||||
void Force_ResetNewBrakeMode(void);
|
||||
|
||||
void Force_SetUserForce(uint8_t force);
|
||||
uint8_t Force_IsNewUserForce(void);
|
||||
void Force_ResetNewUserForce(void);
|
||||
|
||||
#endif /* FORCE_LOGIC_H_ */
|
||||
184
firmware/logic/settings.c
Normal file
184
firmware/logic/settings.c
Normal file
@@ -0,0 +1,184 @@
|
||||
/**** Includes ****/
|
||||
#include "settings.h"
|
||||
|
||||
/**** Private definitions ****/
|
||||
|
||||
/**** Private constants ****/
|
||||
static const uint8_t addr_force = 0x01;
|
||||
static const uint8_t addr_bmode = 0x02;
|
||||
static const uint8_t addr_inmode = 0x03;
|
||||
|
||||
static const uint8_t addr_dsp_bright = 0x04;
|
||||
static const uint8_t addr_dsp_dimm = 0x05;
|
||||
|
||||
static const uint8_t addr_lock_amps = 0x06;
|
||||
|
||||
/**** Private variables ****/
|
||||
static uint16_t save_force_timer = 0;
|
||||
static uint16_t save_bmode_timer = 0;
|
||||
|
||||
/**** Private function declarations ****/
|
||||
|
||||
/**** Public function definitions ****/
|
||||
void Setings_Update(uint8_t is_new_force, uint8_t force, uint8_t is_new_bmode, brakeMode_t bmode)
|
||||
{
|
||||
if(is_new_force) save_force_timer = 5000;
|
||||
|
||||
if(is_new_bmode) save_bmode_timer = 5000;
|
||||
|
||||
if(save_force_timer)
|
||||
{
|
||||
save_force_timer--;
|
||||
if(!save_force_timer)
|
||||
{
|
||||
// Save force setting
|
||||
Setings_SaveForce(force);
|
||||
};
|
||||
};
|
||||
|
||||
if(save_bmode_timer)
|
||||
{
|
||||
save_bmode_timer--;
|
||||
if(!save_bmode_timer)
|
||||
{
|
||||
// Save mode setting
|
||||
Setings_SaveBrakeMode(bmode);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
void Setings_SaveForce(uint8_t value)
|
||||
{
|
||||
MEM_Write8b(addr_force, value);
|
||||
}
|
||||
|
||||
uint8_t Setings_GetForce(void)
|
||||
{
|
||||
uint8_t val = MEM_Read8b(addr_force);
|
||||
if(val > 100) return 0;
|
||||
else return val;
|
||||
}
|
||||
|
||||
|
||||
void Setings_SaveBrakeMode(brakeMode_t bmode)
|
||||
{
|
||||
// Convert and save input mode setting
|
||||
// Convert and save mode setting
|
||||
uint8_t val = 0x00;
|
||||
switch(bmode)
|
||||
{
|
||||
case BM_LOCK:
|
||||
val = 'L';
|
||||
break;
|
||||
|
||||
case BM_KEEP:
|
||||
val = 'K';
|
||||
break;
|
||||
|
||||
default:
|
||||
val = 'O';
|
||||
break;
|
||||
}
|
||||
MEM_Write8b(addr_bmode, val);
|
||||
}
|
||||
|
||||
brakeMode_t Setings_GetBrakeMode(void)
|
||||
{
|
||||
// Convert and return mode setting
|
||||
uint8_t val = MEM_Read8b(addr_bmode);
|
||||
switch(val)
|
||||
{
|
||||
case 'L':
|
||||
return BM_LOCK;
|
||||
|
||||
case 'K':
|
||||
return BM_KEEP;
|
||||
|
||||
default:
|
||||
return BM_OPEN;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Setings_SaveInputMode(inputMode_t inmode)
|
||||
{
|
||||
// Convert and save input mode setting
|
||||
uint8_t val = 0x00;
|
||||
switch(inmode)
|
||||
{
|
||||
case IM_POT:
|
||||
val = 'P';
|
||||
break;
|
||||
|
||||
default:
|
||||
val = 'B';
|
||||
break;
|
||||
}
|
||||
MEM_Write8b(addr_inmode, val);
|
||||
}
|
||||
|
||||
inputMode_t Setings_GetInputMode(void)
|
||||
{
|
||||
// Convert and return input mode setting
|
||||
uint8_t val = MEM_Read8b(addr_inmode);
|
||||
switch(val)
|
||||
{
|
||||
case 'P':
|
||||
return IM_POT;
|
||||
|
||||
default:
|
||||
return IM_BUTTONS;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Setings_SaveDisplayBrigthLvl(uint8_t value)
|
||||
{
|
||||
MEM_Write8b(addr_dsp_bright, value);
|
||||
}
|
||||
|
||||
uint8_t Setings_GetDisplayBrigthLvl(void)
|
||||
{
|
||||
uint8_t val = MEM_Read8b(addr_dsp_bright);
|
||||
if(val > 100) return 100;
|
||||
else return val;
|
||||
}
|
||||
|
||||
|
||||
void Setings_SaveDisplayDimmLvl(uint8_t value)
|
||||
{
|
||||
MEM_Write8b(addr_dsp_dimm, value);
|
||||
}
|
||||
|
||||
uint8_t Setings_GetDisplayDimmLvl(void)
|
||||
{
|
||||
uint8_t val = MEM_Read8b(addr_dsp_dimm);
|
||||
if(val > 100) return 50;
|
||||
else return val;
|
||||
}
|
||||
|
||||
|
||||
void Setings_SaveLockCurrent(uint16_t value)
|
||||
{
|
||||
MEM_Write16b(addr_lock_amps, value);
|
||||
}
|
||||
|
||||
uint16_t Setings_GetLockCurrent(void)
|
||||
{
|
||||
uint16_t val = MEM_Read16b(addr_lock_amps);
|
||||
if(val > 6000) return 4500;
|
||||
else return val;
|
||||
}
|
||||
|
||||
void Setings_SaveDefault(void)
|
||||
{
|
||||
Setings_SaveForce(0);
|
||||
Setings_SaveBrakeMode(BM_OPEN);
|
||||
Setings_SaveInputMode(IM_BUTTONS);
|
||||
Setings_SaveDisplayBrigthLvl(100);
|
||||
Setings_SaveDisplayDimmLvl(50);
|
||||
Setings_SaveLockCurrent(4500);
|
||||
}
|
||||
|
||||
/**** Private function definitions ****/
|
||||
34
firmware/logic/settings.h
Normal file
34
firmware/logic/settings.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef SETTINGS_LOGIC_H_
|
||||
#define SETTINGS_LOGIC_H_
|
||||
|
||||
/**** Includes ****/
|
||||
#include <stdint.h>
|
||||
#include "../devices/memory.h"
|
||||
#include "force.h"
|
||||
|
||||
/**** Public definitions ****/
|
||||
|
||||
/**** Public function declarations ****/
|
||||
void Setings_Update(uint8_t is_new_force, uint8_t force, uint8_t is_new_bmode, brakeMode_t bmode);
|
||||
|
||||
void Setings_SaveForce(uint8_t value);
|
||||
uint8_t Setings_GetForce(void);
|
||||
|
||||
void Setings_SaveBrakeMode(brakeMode_t bmode);
|
||||
brakeMode_t Setings_GetBrakeMode(void);
|
||||
|
||||
void Setings_SaveInputMode(inputMode_t inmode);
|
||||
inputMode_t Setings_GetInputMode(void);
|
||||
|
||||
void Setings_SaveDisplayBrigthLvl(uint8_t value);
|
||||
uint8_t Setings_GetDisplayBrigthLvl(void);
|
||||
|
||||
void Setings_SaveDisplayDimmLvl(uint8_t value);
|
||||
uint8_t Setings_GetDisplayDimmLvl(void);
|
||||
|
||||
void Setings_SaveLockCurrent(uint16_t value);
|
||||
uint16_t Setings_GetLockCurrent(void);
|
||||
|
||||
void Setings_SaveDefault(void);
|
||||
|
||||
#endif /* SETTINGS_LOGIC_H_ */
|
||||
Reference in New Issue
Block a user