This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
SummerSchool2-Controller/306-controller_interface.X/app/kartculator.c

80 lines
2.0 KiB
C
Raw Normal View History

2023-08-31 11:42:31 +00:00
/**
* @author R<EFBFBD>mi Heredero
* @version. 0.0.0
* @date August 2023
* @file kartculator.c
*/
#include "kartculator.h"
2023-09-05 18:02:25 +00:00
void deadJoystick(void* p){
eKart.torque = 0;
eKart.position = eKart.center;
}
2023-09-05 18:02:33 +00:00
2023-08-31 11:42:31 +00:00
void calcTorque(uint8_t joy_pos) {
int32_t calcTorque;
2023-09-05 18:02:33 +00:00
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
}
lastPos = calcTorque;
2023-08-31 11:42:31 +00:00
calcTorque *= KART_CST.CONTROL_POWER_FACTOR; // convert by power factor
calcTorque /= 1000; // torque define by joystick
eKart.torque = (int16_t) calcTorque;
}
void calcPosition(uint8_t joy_pos){
int32_t calcPosition;
2023-09-05 18:02:33 +00:00
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;
}
lastPos = calcPosition;
2023-09-06 19:03:23 +00:00
calcPosition *= -1; // change left and right
2023-09-05 15:43:12 +00:00
calcPosition *= (int32_t) KART_CST.CONTROL_STEERING_FACTOR;
calcPosition /= 1000;
calcPosition += eKart.center;
eKart.position = (uint32_t) calcPosition;
2023-08-31 11:42:31 +00:00
}
void calcSpeed(int32_t rpm) {
2023-09-01 14:37:46 +00:00
int32_t calcSpeed;
if(rpm>=0){
calcSpeed = rpm;
} else {
calcSpeed = -rpm;
}
calcSpeed *= 1000;
calcSpeed /= KART_CST.CONTROL_SPEED_FACTOR;
eKart.speed = (uint8_t) calcSpeed;
2023-08-31 11:42:31 +00:00
}
int16_t getTorque() {
}
uint32_t getPosition() {
}
uint8_t getSpeed() {
}