This commit is contained in:
Rémi Heredero 2023-09-05 20:02:33 +02:00
parent 0329c762a0
commit 44d4d1210c
3 changed files with 39 additions and 3 deletions

View File

@ -43,6 +43,8 @@
#define MEMADD_BATTERY_CURRENT_TIME 0x1B #define MEMADD_BATTERY_CURRENT_TIME 0x1B
#define MEMADD_BATTERY_ENERGY_TIME 0x1C #define MEMADD_BATTERY_ENERGY_TIME 0x1C
#define MEMADD_BATTERY_ALIVE_TIME 0x1D #define MEMADD_BATTERY_ALIVE_TIME 0x1D
#define MEMADD_CONTROL_PARAM_MAX_CHANGE_STEERING 0x1E
#define MEMADD_CONTROL_PARAM_MAX_CHANGE_DRIVE 0x1F
const bool ALWAYSTRUE = true; const bool ALWAYSTRUE = true;
const bool ALWAYSFALSE = false; const bool ALWAYSFALSE = false;
@ -72,6 +74,9 @@ typedef struct {
uint8_t BATTERY_CURRENT_TIME; uint8_t BATTERY_CURRENT_TIME;
uint8_t BATTERY_ENERGY_TIME; uint8_t BATTERY_ENERGY_TIME;
uint8_t BATTERY_ALIVE_TIME; uint8_t BATTERY_ALIVE_TIME;
uint8_t CONTROL_PARAM_MAX_CHANGE_STEERING;
uint8_t CONTROL_PARAM_MAX_CHANGE_DRIVE;
} KART_CST_TYPE; } KART_CST_TYPE;
KART_CST_TYPE KART_CST; KART_CST_TYPE KART_CST;

View File

@ -49,6 +49,9 @@ void MEM_init(){
KART_CST.BATTERY_ENERGY_TIME = 50; KART_CST.BATTERY_ENERGY_TIME = 50;
KART_CST.BATTERY_ALIVE_TIME = 50; KART_CST.BATTERY_ALIVE_TIME = 50;
KART_CST.CONTROL_PARAM_MAX_CHANGE_STEERING = 10;
KART_CST.CONTROL_PARAM_MAX_CHANGE_DRIVE = 10;
MEM_write_1_byte(MEMADD_CONTROL_STEERING_MODE, KART_CST.CONTROL_STEERING_MODE); MEM_write_1_byte(MEMADD_CONTROL_STEERING_MODE, KART_CST.CONTROL_STEERING_MODE);
MEM_write_1_byte(MEMADD_CONTROL_ALIVE_TIME, KART_CST.CONTROL_ALIVE_TIME); MEM_write_1_byte(MEMADD_CONTROL_ALIVE_TIME, KART_CST.CONTROL_ALIVE_TIME);
MEM_write_4_byte(MEMADD_CONTROL_SPEED_FACTOR, KART_CST.CONTROL_SPEED_FACTOR); MEM_write_4_byte(MEMADD_CONTROL_SPEED_FACTOR, KART_CST.CONTROL_SPEED_FACTOR);
@ -75,6 +78,9 @@ void MEM_init(){
MEM_write_1_byte(MEMADD_BATTERY_ENERGY_TIME, KART_CST.BATTERY_ENERGY_TIME); MEM_write_1_byte(MEMADD_BATTERY_ENERGY_TIME, KART_CST.BATTERY_ENERGY_TIME);
MEM_write_1_byte(MEMADD_BATTERY_ALIVE_TIME, KART_CST.BATTERY_ALIVE_TIME); MEM_write_1_byte(MEMADD_BATTERY_ALIVE_TIME, KART_CST.BATTERY_ALIVE_TIME);
MEM_write_1_byte(MEMADD_CONTROL_PARAM_MAX_CHANGE_DRIVE, KART_CST.CONTROL_PARAM_MAX_CHANGE_DRIVE);
MEM_write_1_byte(MEMADD_CONTROL_PARAM_MAX_CHANGE_STEERING, KART_CST.CONTROL_PARAM_MAX_CHANGE_STEERING);
MEM_write_1_byte(0x0, 0x42); MEM_write_1_byte(0x0, 0x42);
} else { } else {
KART_CST.CONTROL_STEERING_MODE = MEM_read_1_byte(MEMADD_CONTROL_STEERING_MODE); KART_CST.CONTROL_STEERING_MODE = MEM_read_1_byte(MEMADD_CONTROL_STEERING_MODE);
@ -103,8 +109,10 @@ void MEM_init(){
KART_CST.BATTERY_ENERGY_TIME = MEM_read_1_byte(MEMADD_BATTERY_ENERGY_TIME); KART_CST.BATTERY_ENERGY_TIME = MEM_read_1_byte(MEMADD_BATTERY_ENERGY_TIME);
KART_CST.BATTERY_ALIVE_TIME = MEM_read_1_byte(MEMADD_BATTERY_ALIVE_TIME); KART_CST.BATTERY_ALIVE_TIME = MEM_read_1_byte(MEMADD_BATTERY_ALIVE_TIME);
KART_CST.CONTROL_PARAM_MAX_CHANGE_DRIVE = MEM_read_1_byte(MEMADD_CONTROL_PARAM_MAX_CHANGE_DRIVE);
KART_CST.CONTROL_PARAM_MAX_CHANGE_STEERING = MEM_read_1_byte(MEMADD_CONTROL_PARAM_MAX_CHANGE_STEERING);
} }
CAN_Send(0, 5, KART_CST.CONTROL_POWER_FACTOR);
} }
void MEM_reset() { void MEM_reset() {

View File

@ -11,9 +11,21 @@ void deadJoystick(void* p){
eKart.torque = 0; eKart.torque = 0;
eKart.position = eKart.center; eKart.position = eKart.center;
} }
void calcTorque(uint8_t joy_pos) { void calcTorque(uint8_t joy_pos) {
int32_t calcTorque; int32_t calcTorque;
static int8_t lastPos = 0;
int8_t diff = ((int8_t)joy_pos) - lastPos;
if (diff > KART_CST.CONTROL_PARAM_MAX_CHANGE_DRIVE) {
calcTorque = lastPos + KART_CST.CONTROL_PARAM_MAX_CHANGE_DRIVE;
} else if (diff < -KART_CST.CONTROL_PARAM_MAX_CHANGE_DRIVE) {
calcTorque = lastPos - KART_CST.CONTROL_PARAM_MAX_CHANGE_DRIVE;
} else {
calcTorque = (int8_t) joy_pos; // joystick position calcTorque = (int8_t) joy_pos; // joystick position
}
lastPos = calcTorque;
calcTorque *= KART_CST.CONTROL_POWER_FACTOR; // convert by power factor calcTorque *= KART_CST.CONTROL_POWER_FACTOR; // convert by power factor
calcTorque /= 1000; // torque define by joystick calcTorque /= 1000; // torque define by joystick
eKart.torque = (int16_t) calcTorque; eKart.torque = (int16_t) calcTorque;
@ -21,7 +33,18 @@ void calcTorque(uint8_t joy_pos) {
void calcPosition(uint8_t joy_pos){ void calcPosition(uint8_t joy_pos){
int32_t calcPosition; int32_t calcPosition;
static int8_t lastPos = 0;
int8_t diff = ((int8_t)joy_pos) - lastPos;
if (diff > KART_CST.CONTROL_PARAM_MAX_CHANGE_STEERING) {
calcPosition = lastPos + KART_CST.CONTROL_PARAM_MAX_CHANGE_STEERING;
} else if (diff < -KART_CST.CONTROL_PARAM_MAX_CHANGE_STEERING) {
calcPosition = lastPos - KART_CST.CONTROL_PARAM_MAX_CHANGE_STEERING;
} else {
calcPosition = (int8_t) joy_pos; calcPosition = (int8_t) joy_pos;
}
lastPos = calcPosition;
calcPosition *= (int32_t) KART_CST.CONTROL_STEERING_FACTOR; calcPosition *= (int32_t) KART_CST.CONTROL_STEERING_FACTOR;
calcPosition /= 1000; calcPosition /= 1000;
calcPosition += eKart.center; calcPosition += eKart.center;