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/can_message.c

381 lines
14 KiB
C
Raw Normal View History

2023-08-25 12:13:51 +00:00
/**
* @author R<EFBFBD>mi Heredero
* @version 1.0.0
* @date August 2023
* @file can_message.c
*/
2023-08-27 15:39:45 +00:00
#include "can_message.h"
2023-08-25 16:23:36 +00:00
#include "car.h"
#include "../app/factory/factory.h"
2023-08-28 10:57:16 +00:00
#include "../middleware/can_interface.h"
2023-08-31 11:42:31 +00:00
#include "kartculator.h"
2023-08-25 12:13:51 +00:00
2023-08-25 16:23:36 +00:00
typedef union {
struct {
uint8_t byte0;
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
} separate;
2023-08-30 13:37:53 +00:00
uint32_t full;
2023-08-25 16:23:36 +00:00
} BYTES_4;
2023-08-31 11:42:31 +00:00
typedef union {
struct {
uint8_t byte0;
uint8_t byte1;
} separate;
uint16_t full;
} BYTES_2;
2023-09-04 06:43:14 +00:00
void CM_processIncome(uint8_t idSender, uint8_t idMsg, bool rtr, uint32_t data){
2023-09-01 08:40:11 +00:00
BYTES_4 incomeData;
incomeData.full = data;
BYTES_4 revertData;
revertData.separate.byte0 = incomeData.separate.byte3;
revertData.separate.byte1 = incomeData.separate.byte2;
revertData.separate.byte2 = incomeData.separate.byte1;
revertData.separate.byte3 = incomeData.separate.byte0;
2023-08-25 16:23:36 +00:00
switch(idSender){
2023-08-28 10:57:16 +00:00
/*********************
* BROADCAST / DEBUG *
*********************/
case 0:
2023-08-28 07:09:54 +00:00
if(idMsg == 0x0) { // CONTROL_SETUP
2023-08-29 14:28:12 +00:00
// steeringMode eraseMemory - controlAliveTime
2023-09-01 08:40:11 +00:00
if (incomeData.separate.byte1) {
2023-08-29 14:28:12 +00:00
MEM_reset();
}
2023-09-01 08:40:11 +00:00
KART_CST.CONTROL_STEERING_MODE = incomeData.separate.byte0;
KART_CST.CONTROL_ALIVE_TIME = incomeData.separate.byte3;
2023-08-30 13:37:53 +00:00
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);
ALIVE_setAliveTime(ALcontroller(), KART_CST.CONTROL_ALIVE_TIME);
2023-09-01 14:38:10 +00:00
ALIVE_emitStart(ALcontroller(), 0, 0);
2023-08-28 07:09:54 +00:00
}
if(idMsg == 0x1) { // CONTROL_SPEED_FACTOR
2023-08-28 10:57:16 +00:00
// valHH valH valL valLL
2023-09-04 06:43:14 +00:00
KART_CST.CONTROL_SPEED_FACTOR = revertData.full;
2023-08-30 13:37:53 +00:00
MEM_write_4_byte(MEMADD_CONTROL_SPEED_FACTOR, KART_CST.CONTROL_SPEED_FACTOR);
2023-08-28 07:09:54 +00:00
}
if(idMsg == 0x2) { // CONTROL_POWER_FACTOR
2023-08-28 10:57:16 +00:00
// valHH valH valL valLL
2023-09-04 06:43:14 +00:00
KART_CST.CONTROL_POWER_FACTOR = revertData.full;
2023-08-30 13:37:53 +00:00
MEM_write_4_byte(MEMADD_CONTROL_POWER_FACTOR, KART_CST.CONTROL_POWER_FACTOR);
2023-09-01 16:41:25 +00:00
CAN_Send(0, 5, KART_CST.CONTROL_POWER_FACTOR);
2023-08-28 07:09:54 +00:00
}
if(idMsg == 0x3) { // CONTROL_STEERING_FACTOR
2023-08-28 10:57:16 +00:00
// valHH valH valL valLL
2023-09-04 06:43:14 +00:00
KART_CST.CONTROL_STEERING_FACTOR = revertData.full;
2023-08-30 13:37:53 +00:00
MEM_write_4_byte(MEMADD_CONTROL_STEERING_FACTOR, KART_CST.CONTROL_STEERING_FACTOR);
2023-08-28 07:09:54 +00:00
}
if(idMsg == 0x4) { // CONTROL_SECURITY_PARAM
2023-08-28 10:57:16 +00:00
// maxSpeedFw maxSpeedBw - -
2023-09-01 08:40:11 +00:00
KART_CST.CONTROL_MAX_SPEED_FW = incomeData.separate.byte0;
KART_CST.CONTROL_MAX_SPEED_BW = incomeData.separate.byte1;
2023-08-30 13:37:53 +00:00
MEM_write_1_byte(MEMADD_CONTROL_MAX_SPEED_FW, KART_CST.CONTROL_MAX_SPEED_FW);
MEM_write_1_byte(MEMADD_CONTROL_MAX_SPEED_BW, KART_CST.CONTROL_MAX_SPEED_BW);
2023-08-28 07:09:54 +00:00
}
if(idMsg == 0x5) { // CONTROL_SETUP_PARAM
2023-08-28 10:57:16 +00:00
// displayAliveTime steeringAliveTime - -
2023-09-01 08:40:11 +00:00
KART_CST.DISPLAY_ALIVE_TIME = incomeData.separate.byte0;
KART_CST.STEERING_ALIVE_TIME = incomeData.separate.byte1;
2023-08-30 13:37:53 +00:00
MEM_write_1_byte(MEMADD_DISPLAY_ALIVE_TIME, KART_CST.DISPLAY_ALIVE_TIME);
MEM_write_1_byte(MEMADD_STEERING_ALIVE_TIME, KART_CST.STEERING_ALIVE_TIME);
2023-09-01 15:24:32 +00:00
2023-08-30 13:37:53 +00:00
// TODO set alive times
2023-09-01 15:24:32 +00:00
// start alives
CM_DISPLAY_SETUP(NULL);
CM_STEERING_SETUP(NULL);
2023-08-28 07:09:54 +00:00
}
if(idMsg == 0x6) { // CONTROL_SETUP_PARAM_JOY
// joystickMode joystickParam1 joystickParam2 joystickAliveTime
2023-09-01 08:40:11 +00:00
KART_CST.JOYSTICK_MODE = incomeData.separate.byte0;
KART_CST.JOYSTICK_PARAM1 = incomeData.separate.byte1;
KART_CST.JOYSTICK_PARAM2 = incomeData.separate.byte2;
KART_CST.JOYSTICK_ALIVE_TIME = incomeData.separate.byte3;
2023-08-30 13:37:53 +00:00
MEM_write_1_byte(MEMADD_JOYSTICK_MODE, KART_CST.JOYSTICK_MODE);
MEM_write_1_byte(MEMADD_JOYSTICK_PARAM1, KART_CST.JOYSTICK_PARAM1);
MEM_write_1_byte(MEMADD_JOYSTICK_PARAM2, KART_CST.JOYSTICK_PARAM2);
MEM_write_1_byte(MEMADD_JOYSTICK_ALIVE_TIME, KART_CST.JOYSTICK_ALIVE_TIME);
ALIVE_setAliveTime(ALjoy(), KART_CST.JOYSTICK_ALIVE_TIME);
2023-09-01 14:38:10 +00:00
ALIVE_emitStart(ALjoy(), 0, 0);
2023-08-28 07:09:54 +00:00
CM_JOY_SETUP(NULL);
}
if(idMsg == 0x7) { // CONTROL_SETUP_PARAM_DRIVE
2023-08-28 10:57:16 +00:00
// driveAliveTime driveSpeedTime driveStopTime -
2023-09-01 08:40:11 +00:00
KART_CST.DRIVE_ALIVE_TIME = incomeData.separate.byte0;
KART_CST.DRIVE_SPEED_TIME = incomeData.separate.byte1;
KART_CST.DRIVE_STOP_TIME = incomeData.separate.byte2;
2023-08-30 13:37:53 +00:00
MEM_write_1_byte(MEMADD_DRIVE_ALIVE_TIME, KART_CST.DRIVE_ALIVE_TIME);
MEM_write_1_byte(MEMADD_DRIVE_SPEED_TIME, KART_CST.DRIVE_SPEED_TIME);
MEM_write_1_byte(MEMADD_DRIVE_STOP_TIME, KART_CST.DRIVE_STOP_TIME);
2023-09-01 15:24:32 +00:00
ALIVE_setAliveTime(&drive()->myChecker, KART_CST.DRIVE_ALIVE_TIME);
ALIVE_emitStart(&drive()->myChecker, 0, 0);
2023-09-01 14:38:10 +00:00
CM_DRIVE_SETUP(NULL);
2023-08-28 07:09:54 +00:00
}
if(idMsg == 0x8) { // CONTROL_SETUP_PARAM_BATTERY
2023-08-28 10:57:16 +00:00
// batteryVoltTime batteryCurrentTime batteryEnergyTime batteryAliveTime
2023-09-01 08:40:11 +00:00
KART_CST.BATTERY_VOLT_TIME = incomeData.separate.byte0;
KART_CST.BATTERY_CURRENT_TIME = incomeData.separate.byte1;
KART_CST.BATTERY_ENERGY_TIME = incomeData.separate.byte2;
KART_CST.BATTERY_ALIVE_TIME = incomeData.separate.byte3;
2023-08-30 13:37:53 +00:00
MEM_write_1_byte(MEMADD_BATTERY_VOLT_TIME, KART_CST.BATTERY_VOLT_TIME);
MEM_write_1_byte(MEMADD_BATTERY_CURRENT_TIME, KART_CST.BATTERY_CURRENT_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);
2023-09-01 15:24:32 +00:00
2023-08-30 13:37:53 +00:00
// TODO set alive time
2023-09-01 15:24:32 +00:00
// TODO start alive
CM_SUPPLY_SETUP(NULL);
2023-08-28 07:09:54 +00:00
}
2023-08-25 16:23:36 +00:00
break;
2023-08-28 10:57:16 +00:00
/************
* JOYSTICK *
************/
case 2:
2023-08-25 16:23:36 +00:00
if(idMsg == 0x1) { // JOY_MESURE
2023-08-28 10:57:16 +00:00
// posX posY button -
2023-09-01 08:40:11 +00:00
calcTorque(incomeData.separate.byte1);
calcPosition(incomeData.separate.byte0);
2023-08-25 16:23:36 +00:00
}
if(idMsg == 0xF) { // JOY_ALIVE
2023-08-28 10:57:16 +00:00
// - - - -
ALIVE_ISALIVE(ALjoy());
if(ALjoy()->state == STAL_DEAD){
2023-08-30 13:37:53 +00:00
KART_CST.JOYSTICK_MODE = MEM_read_1_byte(MEMADD_JOYSTICK_MODE);
KART_CST.JOYSTICK_PARAM1 = MEM_read_1_byte(MEMADD_JOYSTICK_PARAM1);
KART_CST.JOYSTICK_PARAM2 = MEM_read_1_byte(MEMADD_JOYSTICK_PARAM2);
KART_CST.JOYSTICK_ALIVE_TIME = MEM_read_1_byte(MEMADD_JOYSTICK_ALIVE_TIME);
ALIVE_setAliveTime(ALjoy(), KART_CST.JOYSTICK_ALIVE_TIME);
2023-08-31 18:02:41 +00:00
ALIVE_emitResurrect(ALjoy(), 0, 0);
2023-09-01 11:22:35 +00:00
ALIVE_emitBorn(ALjoy(), 0, 0);
ALIVE_emitReady(ALjoy(), 0, 0);
2023-08-25 20:53:04 +00:00
}
2023-08-25 16:23:36 +00:00
}
break;
2023-08-28 10:57:16 +00:00
/***********
* DISPLAY *
***********/
case 3:
if(idMsg == 0xF) { // DISPLAY_ALIVE
// powerMode - - -
2023-08-30 13:37:53 +00:00
// TODO display say ALIVE
2023-08-28 10:57:16 +00:00
}
2023-08-25 16:23:36 +00:00
break;
2023-08-28 10:57:16 +00:00
/*********
* DRIVE *
*********/
case 4:
if(idMsg == 0x0) { // DRIVE_SPEED
// speedHH speedH speedL speedLL
2023-09-01 08:40:11 +00:00
calcSpeed(revertData.full);
2023-08-31 18:02:41 +00:00
DRIVE_emitPollSpeed(drive());
2023-08-28 10:57:16 +00:00
}
if(idMsg == 0xF) { // DRIVE_ALIVE
// statusH statusL - -
2023-08-31 18:02:41 +00:00
ALIVE_ISALIVE(&drive()->myChecker);
if(drive()->myChecker.state == STAL_DEAD){
2023-08-31 15:02:23 +00:00
KART_CST.DRIVE_SPEED_TIME = MEM_read_1_byte(MEMADD_DRIVE_SPEED_TIME);
KART_CST.DRIVE_STOP_TIME = MEM_read_1_byte(MEMADD_DRIVE_STOP_TIME);
KART_CST.DRIVE_ALIVE_TIME = MEM_read_1_byte(MEMADD_DRIVE_ALIVE_TIME);
2023-08-31 18:02:41 +00:00
ALIVE_emitResurrect(&drive()->myChecker, 0, 0);
ALIVE_emitBorn(&drive()->myChecker, 0, 0);
2023-08-31 15:02:23 +00:00
}
2023-08-28 10:57:16 +00:00
}
2023-08-25 16:23:36 +00:00
break;
2023-08-28 10:57:16 +00:00
/************
* STEERING *
************/
case 5:
if(idMsg == 0x1) { // STEERING_GET_CENTER
// valHH valH valL valLL
2023-09-01 08:40:11 +00:00
eKart.center = revertData.full;
2023-08-28 10:57:16 +00:00
}
if(idMsg == 0x2) { // STEERING_GET_POSITION
// valHH valH valL valLL
}
if(idMsg == 0xF) { // STEERING_ALIVE
// statusH statusL - -
2023-08-30 13:37:53 +00:00
// TODO steering say ALIVE with his status
2023-08-28 10:57:16 +00:00
}
2023-08-25 16:23:36 +00:00
break;
2023-08-28 10:57:16 +00:00
/**********
* SUPPLY *
**********/
case 6:
if(idMsg == 0xF) { // BATTERY_ALIVE
// - - - -
2023-08-30 13:37:53 +00:00
// TODO battery say ALIVE
2023-08-28 10:57:16 +00:00
}
2023-08-25 16:23:36 +00:00
break;
2023-08-28 10:57:16 +00:00
/*************
* UNDEFINED *
*************/
case 7:
2023-08-25 16:23:36 +00:00
break;
}
}
void CM_CONTROLLER_ALIVE(void* p) {
2023-08-30 13:37:53 +00:00
// - - - -
2023-08-25 12:13:51 +00:00
CAN_Send(0x0, 0xF, 0);
2023-08-25 16:23:36 +00:00
}
void CM_JOY_SETUP(void* p) {
2023-08-30 13:37:53 +00:00
// mode parm1 param2 aliveTime
2023-08-25 16:23:36 +00:00
BYTES_4 joy;
2023-08-30 13:37:53 +00:00
joy.separate.byte0 = KART_CST.JOYSTICK_MODE;
joy.separate.byte1 = KART_CST.JOYSTICK_PARAM1;
joy.separate.byte2 = KART_CST.JOYSTICK_PARAM2;
joy.separate.byte3 = KART_CST.JOYSTICK_ALIVE_TIME;
CAN_Send(2, 0, joy.full);
2023-08-25 16:23:36 +00:00
}
void CM_DISPLAY_SETUP(void* p) {
2023-08-30 13:37:53 +00:00
// reset - - aliveTime
BYTES_4 display;
display.separate.byte0 = 0; // reset
display.separate.byte3 = KART_CST.DISPLAY_ALIVE_TIME;
CAN_Send(3, 0, display.full);
2023-08-25 16:23:36 +00:00
}
void CM_DISPLAY_SPEED(void* p) {
2023-08-30 13:37:53 +00:00
// valH valL - -
BYTES_4 tmpData;
// byte0 should be valH but isn't possible to go enough fast for use it
tmpData.separate.byte0 = 0;
tmpData.separate.byte1 = *((uint8_t*) p);
tmpData.separate.byte2 = 0;
tmpData.separate.byte3 = 0;
CAN_Send(3, 2, tmpData.full);
2023-08-25 16:23:36 +00:00
}
void CM_DISPLAY_DIRECTION(void* p) {
2023-08-30 13:37:53 +00:00
// direction - - -
BYTES_4 tmpData;
tmpData.separate.byte0 = (uint8_t) p;
tmpData.separate.byte1 = 0;
tmpData.separate.byte2 = 0;
tmpData.separate.byte3 = 0;
CAN_Send(3, 3, tmpData.full);
2023-08-25 16:23:36 +00:00
}
void CM_DRIVE_SETUP(void* p) {
2023-08-30 13:37:53 +00:00
// reset/init speedTime stopTime aliveTime
BYTES_4 tmpData;
tmpData.separate.byte0 = (uint8_t) p;
tmpData.separate.byte1 = KART_CST.DRIVE_SPEED_TIME;
tmpData.separate.byte2 = KART_CST.DRIVE_STOP_TIME;
tmpData.separate.byte3 = KART_CST.DRIVE_ALIVE_TIME;
CAN_Send(4, 0, tmpData.full);
2023-08-25 16:23:36 +00:00
}
void CM_DRIVE_POWER(void* p) {
2023-08-30 13:37:53 +00:00
// valH valL - -
2023-08-31 11:42:31 +00:00
BYTES_2 torque;
BYTES_4 tmpData;
torque.full = *((int16_t*) p);
tmpData.separate.byte0 = torque.separate.byte1;
tmpData.separate.byte1 = torque.separate.byte0;
2023-08-31 15:02:23 +00:00
tmpData.separate.byte2 = 0;
tmpData.separate.byte3 = 0;
2023-08-31 11:42:31 +00:00
CAN_Send(4, 1, tmpData.full);
2023-08-25 16:23:36 +00:00
}
void CM_STEERING_SETUP(void* p) {
2023-08-30 13:37:53 +00:00
// reset/init homing setCenter aliveTime
// TODO not working have to fix it
BYTES_4 tmpData;
uint8_t choice = (uint8_t) p;
switch (choice) {
case 1:
tmpData.separate.byte0 = 1;
tmpData.separate.byte1 = 0;
tmpData.separate.byte2 = 0;
break;
case 2:
tmpData.separate.byte0 = 0;
tmpData.separate.byte1 = 1;
tmpData.separate.byte2 = 0;
break;
case 3:
tmpData.separate.byte0 = 0;
tmpData.separate.byte1 = 0;
tmpData.separate.byte2 = 1;
break;
default:
tmpData.separate.byte0 = 0;
tmpData.separate.byte1 = 0;
tmpData.separate.byte2 = 0;
break;
}
tmpData.separate.byte3 = KART_CST.STEERING_ALIVE_TIME; // 0 -> no alive
CAN_Send(5, 0, tmpData.full);
2023-08-25 16:23:36 +00:00
}
void CM_STEERING_SET(void* p) {
2023-08-30 13:37:53 +00:00
// valHH valH valL valLL
BYTES_4 pPosition;
pPosition.full = *((uint32_t*) p);
BYTES_4 sPosition;
sPosition.separate.byte0 = pPosition.separate.byte3;
sPosition.separate.byte1 = pPosition.separate.byte2;
sPosition.separate.byte2 = pPosition.separate.byte1;
sPosition.separate.byte3 = pPosition.separate.byte0;
CAN_Send(5, 1, sPosition.full);
2023-08-25 16:23:36 +00:00
}
2023-08-30 13:37:53 +00:00
void CM_SUPPLY_SETUP(void* p) {
// batteryVoltTime batteryCurrentTime batteryEnergyTime aliveTime
BYTES_4 supply;
supply.separate.byte0 = KART_CST.BATTERY_VOLT_TIME;
supply.separate.byte1 = KART_CST.BATTERY_CURRENT_TIME;
supply.separate.byte2 = KART_CST.BATTERY_ENERGY_TIME;
supply.separate.byte3 = KART_CST.BATTERY_ALIVE_TIME;
CAN_Send(6, 0, supply.full);
2023-08-25 16:23:36 +00:00
}