Skip to content

Commit 7d88898

Browse files
dr0psJürgen Ebert
authored andcommitted
Added uart functions
1 parent 4e0288d commit 7d88898

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

comm/comm_can.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void comm_can_set_current_off_delay(uint8_t controller_id, float current, float
4242
void comm_can_set_current_brake(uint8_t controller_id, float current);
4343
void comm_can_set_rpm(uint8_t controller_id, float rpm);
4444
void comm_can_set_pos(uint8_t controller_id, float pos);
45+
void comm_can_set_handbrake(uint8_t controller_id, float current);
4546
void comm_can_set_current_rel(uint8_t controller_id, float current_rel);
4647
void comm_can_set_current_rel_off_delay(uint8_t controller_id, float current_rel, float off_delay);
4748
void comm_can_set_current_brake_rel(uint8_t controller_id, float current_rel);

comm/commands.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,141 @@ void commands_process_packet(unsigned char *data, unsigned int len,
12341234
}
12351235
} break;
12361236

1237+
case COMM_CAN_SET_DUTY: {
1238+
int32_t ind = 0;
1239+
float duty = (float)buffer_get_int32(data, &ind);
1240+
1241+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1242+
can_status_msg *msg = comm_can_get_status_msg_index(i);
1243+
1244+
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
1245+
comm_can_set_duty(msg->id, duty);
1246+
}
1247+
}
1248+
timeout_reset();
1249+
} break;
1250+
1251+
case COMM_CAN_SET_CURRENT: {
1252+
int32_t ind = 0;
1253+
float current = (float)buffer_get_int32(data, &ind);
1254+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1255+
can_status_msg *msg = comm_can_get_status_msg_index(i);
1256+
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
1257+
comm_can_set_current(msg->id, current / 1000.0);
1258+
}
1259+
}
1260+
timeout_reset();
1261+
} break;
1262+
1263+
case COMM_TC_SET_CURRENT: {
1264+
int32_t ind = 0;
1265+
float current = (float)buffer_get_int32(data, &ind);
1266+
float current_out = current;
1267+
1268+
1269+
float rpm_local = fabsf(mc_interface_get_rpm());
1270+
float rpm_lowest = rpm_local;
1271+
1272+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1273+
can_status_msg *msg = comm_can_get_status_msg_index(i);
1274+
1275+
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
1276+
float rpm_tmp = fabsf(msg->rpm);
1277+
1278+
if (rpm_tmp < rpm_lowest) {
1279+
rpm_lowest = rpm_tmp;
1280+
}
1281+
}
1282+
}
1283+
1284+
mc_configuration *mcconf = mempools_alloc_mcconf();
1285+
*mcconf = *mc_interface_get_configuration();
1286+
1287+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1288+
can_status_msg *msg = comm_can_get_status_msg_index(i);
1289+
1290+
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
1291+
bool is_braking = (current > 0.0 && msg->duty < 0.0) || (current < 0.0 && msg->duty > 0.0);
1292+
1293+
if (!is_braking) {
1294+
float rpm_tmp = fabsf(msg->rpm);
1295+
1296+
float diff = rpm_tmp - rpm_lowest;
1297+
current_out = utils_map(diff, 0.0, 3000.0, current, 0.0);
1298+
if (fabsf(current_out) < mcconf->cc_min_current) {
1299+
current_out = 0.0;
1300+
}
1301+
}
1302+
1303+
comm_can_set_current(msg->id, current_out / 1000.0);
1304+
}
1305+
}
1306+
1307+
const float duty_now = mc_interface_get_duty_cycle_now();
1308+
1309+
bool is_braking = (current > 0.0 && duty_now < 0.0) || (current < 0.0 && duty_now > 0.0);
1310+
1311+
if (!is_braking) {
1312+
float diff = rpm_local - rpm_lowest;
1313+
current_out = utils_map(diff, 0.0, 3000.0, current, 0.0);
1314+
if (fabsf(current_out) < mcconf->cc_min_current) {
1315+
current_out = 0.0;
1316+
}
1317+
}
1318+
1319+
mempools_free_mcconf(mcconf);
1320+
mc_interface_set_current(current_out / 1000.0);
1321+
timeout_reset();
1322+
} break;
1323+
1324+
case COMM_CAN_SET_CURRENT_BRAKE: {
1325+
int32_t ind = 0;
1326+
float current = (float)buffer_get_int32(data, &ind);
1327+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1328+
can_status_msg *msg = comm_can_get_status_msg_index(i);
1329+
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
1330+
comm_can_set_current_brake(msg->id, current / 1000.0);
1331+
}
1332+
}
1333+
timeout_reset();
1334+
} break;
1335+
1336+
case COMM_CAN_SET_RPM: {
1337+
int32_t ind = 0;
1338+
float rpm = (float)buffer_get_int32(data, &ind);
1339+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1340+
can_status_msg *msg = comm_can_get_status_msg_index(i);
1341+
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
1342+
comm_can_set_rpm(msg->id, rpm);
1343+
}
1344+
}
1345+
timeout_reset();
1346+
} break;
1347+
1348+
case COMM_CAN_SET_POS: {
1349+
int32_t ind = 0;
1350+
float pos = (float)buffer_get_int32(data, &ind);
1351+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1352+
can_status_msg *msg = comm_can_get_status_msg_index(i);
1353+
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
1354+
comm_can_set_pos(msg->id, pos);
1355+
}
1356+
}
1357+
timeout_reset();
1358+
} break;
1359+
1360+
case COMM_CAN_SET_HANDBRAKE: {
1361+
int32_t ind = 0;
1362+
float handbrake = buffer_get_float32(data, 1e3, &ind);
1363+
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
1364+
can_status_msg *msg = comm_can_get_status_msg_index(i);
1365+
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
1366+
comm_can_set_handbrake(msg->id, handbrake);
1367+
}
1368+
}
1369+
timeout_reset();
1370+
} break;
1371+
12371372
case COMM_SET_BATTERY_CUT: {
12381373
int32_t ind = 0;
12391374
float start = buffer_get_float32(data, 1e3, &ind);

comm/commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define COMMANDS_H_
2222

2323
#include "datatypes.h"
24+
#define MAX_CAN_AGE 0.1
2425

2526
// Functions
2627
void commands_init(void);

datatypes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,13 @@ typedef enum {
993993
COMM_BM_WRITE_FLASH_LZO = 83,
994994
COMM_SET_CURRENT_REL = 84,
995995
COMM_CAN_FWD_FRAME = 85,
996+
COMM_CAN_SET_DUTY = 153,
997+
COMM_CAN_SET_CURRENT = 154,
998+
COMM_CAN_SET_CURRENT_BRAKE = 155,
999+
COMM_CAN_SET_RPM = 156,
1000+
COMM_CAN_SET_POS = 157,
1001+
COMM_CAN_SET_HANDBRAKE = 158,
1002+
COMM_TC_SET_CURRENT = 159,
9961003
COMM_SET_BATTERY_CUT = 86,
9971004
COMM_SET_BLE_NAME = 87,
9981005
COMM_SET_BLE_PIN = 88,

0 commit comments

Comments
 (0)