Modify & transmit params via CAN

This commit is contained in:
2023-04-04 22:05:50 +02:00
parent a5f10be4fd
commit 253b10ba15
10 changed files with 199 additions and 24 deletions

View File

@ -5,8 +5,14 @@
extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
#include "util.h"
CountedEnum(ParamType, size_t, PF_BBAL, PF_TC1, PF_TC2, PF_TORQUEMAP, PF_TEST1,
PF_TEST2, PF_TEST3, PF_TEST4);
typedef struct {
float bbal;
unsigned tc1;
@ -17,6 +23,11 @@ typedef struct {
extern Params params;
void params_init();
void params_inc(ParamType param);
void params_dec(ParamType param);
void params_broadcast(ParamType param);
#ifdef __cplusplus
}
#endif

11
Core/Inc/util.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef INC_UTIL_H
#define INC_UTIL_H
// This is wacky preprocessor magic that allows us to count the number of
// members of an enum. Unfortunately, it doesn't work with enum classes, so we
// have to use C-style enums.
#define CountedEnum(NAME, TYPE, ...) \
typedef enum { __VA_ARGS__ } NAME; \
static const size_t NAME##_COUNT = sizeof((int[]){__VA_ARGS__}) / sizeof(int);
#endif // INC_UTIL_H

View File

@ -1,6 +1,7 @@
#ifndef __INC_VEHICLE_H
#define __INC_VEHICLE_H
#include "params.h"
#include "stw_defines.h"
#include "tx_port.h"
@ -106,6 +107,8 @@ void vehicle_thread_entry(ULONG hfdcan_addr);
void vehicle_select_mission(Mission mission);
void vehicle_broadcast_param(ParamType param, int32_t value);
#ifdef __cplusplus
}
#endif

View File

@ -27,6 +27,7 @@
#include "ft_logo_rainbow_rgb565.h"
#include "hx8357d.h"
#include "leds.h"
#include "params.h"
#include "shorttimer.h"
#include <stdint.h>
@ -144,6 +145,7 @@ int main(void) {
if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4) != HAL_OK) {
Error_Handler();
}
params_init();
shorttimer_init(htim_us);
led_init(&hspi3, &htim1);

View File

@ -1,3 +1,118 @@
#include "params.h"
#include "can-halal.h"
#include "vehicle.h"
Params params = {0};
void params_init() { params.bbal = 50; }
void params_inc(ParamType param) {
switch (param) {
case PF_BBAL:
params.bbal += 0.5f;
if (params.bbal > 100.0f) {
params.bbal = 100.0f;
}
break;
case PF_TC1:
params.tc1++;
break;
case PF_TC2:
params.tc2++;
break;
case PF_TORQUEMAP:
params.torque_map++;
break;
case PF_TEST1:
params.test[0]++;
break;
case PF_TEST2:
params.test[1]++;
break;
case PF_TEST3:
params.test[2]++;
break;
case PF_TEST4:
params.test[3]++;
break;
}
}
void params_dec(ParamType param) {
switch (param) {
case PF_BBAL:
params.bbal -= 0.5f;
if (params.bbal < 0.0f) {
params.bbal = 0.0f;
}
break;
case PF_TC1:
if (params.tc1 > 0) {
params.tc1--;
}
break;
case PF_TC2:
if (params.tc2 > 0) {
params.tc2--;
}
break;
case PF_TORQUEMAP:
if (params.torque_map > 0) {
params.torque_map--;
}
break;
case PF_TEST1:
if (params.test[0] > 0) {
params.test[0]--;
}
break;
case PF_TEST2:
if (params.test[1] > 0) {
params.test[1]--;
}
break;
case PF_TEST3:
if (params.test[2] > 0) {
params.test[2]--;
}
break;
case PF_TEST4:
if (params.test[3] > 0) {
params.test[3]--;
}
break;
}
}
void params_broadcast(ParamType param) {
int32_t value;
switch (param) {
case PF_BBAL:
value = params.bbal * 10;
break;
case PF_TC1:
value = params.tc1;
break;
case PF_TC2:
value = params.tc2;
break;
case PF_TORQUEMAP:
value = params.torque_map;
break;
case PF_TEST1:
value = params.test[0];
break;
case PF_TEST2:
value = params.test[1];
break;
case PF_TEST3:
value = params.test[2];
break;
case PF_TEST4:
value = params.test[3];
break;
default:
return;
}
vehicle_broadcast_param(param, value);
}

View File

@ -13,6 +13,7 @@
#define CAN_ID_AMS_STATUS 0xA
#define CAN_ID_MISSION_SELECTED 0x400
#define CAN_ID_STW_PARAM_SET 0x402
#define CAN_ID_AS_MISSION_FB 0x410
#define CAN_ID_STW_STATUS 0x412
#define CAN_ID_SHUNT_CURRENT 0x521
@ -33,8 +34,6 @@ void vehicle_thread_entry(ULONG hfdcan_addr) {
ftcan_add_filter(CAN_ID_SHUNT_VOLTAGE3, 0x7FF);
while (1) {
uint8_t data[] = {0xFF, 0xEE};
ftcan_transmit(0x456, data, 2);
tx_thread_sleep(10);
}
}
@ -44,6 +43,14 @@ void vehicle_select_mission(Mission mission) {
ftcan_transmit(CAN_ID_MISSION_SELECTED, &mission_int, 1);
}
void vehicle_broadcast_param(ParamType param, int32_t value) {
uint8_t data[5];
uint8_t *ptr = data;
ptr = ftcan_marshal_unsigned(ptr, param, 1);
ptr = ftcan_marshal_signed(ptr, value, 4);
ftcan_transmit(CAN_ID_STW_PARAM_SET, data, 5);
}
void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data) {
switch (id) {
case CAN_ID_AMS_STATUS: