@@ -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 );
0 commit comments