@@ -99,11 +99,6 @@ static void set_timing(int brp, int ts1, int ts2);
99
99
#if CAN_ENABLE
100
100
static void send_packet_wrapper (unsigned char * data , unsigned int len );
101
101
static void decode_msg (uint32_t eid , uint8_t * data8 , int len , bool is_replaced );
102
- static void send_status1 (uint8_t id , bool replace );
103
- static void send_status2 (uint8_t id , bool replace );
104
- static void send_status3 (uint8_t id , bool replace );
105
- static void send_status4 (uint8_t id , bool replace );
106
- static void send_status5 (uint8_t id , bool replace );
107
102
#endif
108
103
109
104
// Function pointers
@@ -977,6 +972,17 @@ void comm_can_psw_switch(int id, bool is_on, bool plot) {
977
972
buffer , send_index , true);
978
973
}
979
974
975
+ void comm_can_update_pid_pos_offset (int id , float angle_now , bool store ) {
976
+ int32_t send_index = 0 ;
977
+ uint8_t buffer [8 ];
978
+
979
+ buffer_append_float32 (buffer , angle_now , 1e4 , & send_index );
980
+ buffer [send_index ++ ] = store ;
981
+
982
+ comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_UPDATE_PID_POS_OFFSET << 8 ),
983
+ buffer , send_index , true);
984
+ }
985
+
980
986
CANRxFrame * comm_can_get_rx_frame (void ) {
981
987
#if CAN_ENABLE
982
988
chMtxLock (& can_rx_mtx );
@@ -998,6 +1004,55 @@ CANRxFrame *comm_can_get_rx_frame(void) {
998
1004
#endif
999
1005
}
1000
1006
1007
+ void comm_can_send_status1 (uint8_t id , bool replace ) {
1008
+ int32_t send_index = 0 ;
1009
+ uint8_t buffer [8 ];
1010
+ buffer_append_int32 (buffer , (int32_t )mc_interface_get_rpm (), & send_index );
1011
+ buffer_append_int16 (buffer , (int16_t )(mc_interface_get_tot_current_filtered () * 1e1 ), & send_index );
1012
+ buffer_append_int16 (buffer , (int16_t )(mc_interface_get_duty_cycle_now () * 1e3 ), & send_index );
1013
+ comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS << 8 ),
1014
+ buffer , send_index , replace );
1015
+ }
1016
+
1017
+ void comm_can_send_status2 (uint8_t id , bool replace ) {
1018
+ int32_t send_index = 0 ;
1019
+ uint8_t buffer [8 ];
1020
+ buffer_append_int32 (buffer , (int32_t )(mc_interface_get_amp_hours (false) * 1e4 ), & send_index );
1021
+ buffer_append_int32 (buffer , (int32_t )(mc_interface_get_amp_hours_charged (false) * 1e4 ), & send_index );
1022
+ comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS_2 << 8 ),
1023
+ buffer , send_index , replace );
1024
+ }
1025
+
1026
+ void comm_can_send_status3 (uint8_t id , bool replace ) {
1027
+ int32_t send_index = 0 ;
1028
+ uint8_t buffer [8 ];
1029
+ buffer_append_int32 (buffer , (int32_t )(mc_interface_get_watt_hours (false) * 1e4 ), & send_index );
1030
+ buffer_append_int32 (buffer , (int32_t )(mc_interface_get_watt_hours_charged (false) * 1e4 ), & send_index );
1031
+ comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS_3 << 8 ),
1032
+ buffer , send_index , replace );
1033
+ }
1034
+
1035
+ void comm_can_send_status4 (uint8_t id , bool replace ) {
1036
+ int32_t send_index = 0 ;
1037
+ uint8_t buffer [8 ];
1038
+ buffer_append_int16 (buffer , (int16_t )(mc_interface_temp_fet_filtered () * 1e1 ), & send_index );
1039
+ buffer_append_int16 (buffer , (int16_t )(mc_interface_temp_motor_filtered () * 1e1 ), & send_index );
1040
+ buffer_append_int16 (buffer , (int16_t )(mc_interface_get_tot_current_in_filtered () * 1e1 ), & send_index );
1041
+ buffer_append_int16 (buffer , (int16_t )(mc_interface_get_pid_pos_now () * 50.0 ), & send_index );
1042
+ comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS_4 << 8 ),
1043
+ buffer , send_index , replace );
1044
+ }
1045
+
1046
+ void comm_can_send_status5 (uint8_t id , bool replace ) {
1047
+ int32_t send_index = 0 ;
1048
+ uint8_t buffer [8 ];
1049
+ buffer_append_int32 (buffer , mc_interface_get_tachometer_value (false), & send_index );
1050
+ buffer_append_int16 (buffer , (int16_t )(mc_interface_get_input_voltage_filtered () * 1e1 ), & send_index );
1051
+ buffer_append_int16 (buffer , 0 , & send_index ); // Reserved for now
1052
+ comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS_5 << 8 ),
1053
+ buffer , send_index , replace );
1054
+ }
1055
+
1001
1056
#if CAN_ENABLE
1002
1057
static THD_FUNCTION (cancom_read_thread , arg ) {
1003
1058
(void )arg ;
@@ -1108,11 +1163,11 @@ static THD_FUNCTION(cancom_status_internal_thread, arg) {
1108
1163
mc_interface_select_motor_thread (2 );
1109
1164
1110
1165
for (;;) {
1111
- send_status1 (utils_second_motor_id (), true);
1112
- send_status2 (utils_second_motor_id (), true);
1113
- send_status3 (utils_second_motor_id (), true);
1114
- send_status4 (utils_second_motor_id (), true);
1115
- send_status5 (utils_second_motor_id (), true);
1166
+ comm_can_send_status1 (utils_second_motor_id (), true);
1167
+ comm_can_send_status2 (utils_second_motor_id (), true);
1168
+ comm_can_send_status3 (utils_second_motor_id (), true);
1169
+ comm_can_send_status4 (utils_second_motor_id (), true);
1170
+ comm_can_send_status5 (utils_second_motor_id (), true);
1116
1171
chThdSleepMilliseconds (2 );
1117
1172
}
1118
1173
}
@@ -1132,10 +1187,10 @@ static THD_FUNCTION(cancom_status_thread, arg) {
1132
1187
conf -> send_can_status == CAN_STATUS_1_2_3_4 ||
1133
1188
conf -> send_can_status == CAN_STATUS_1_2_3_4_5 ) {
1134
1189
mc_interface_select_motor_thread (1 );
1135
- send_status1 (conf -> controller_id , false);
1190
+ comm_can_send_status1 (conf -> controller_id , false);
1136
1191
#ifdef HW_HAS_DUAL_MOTORS
1137
1192
mc_interface_select_motor_thread (2 );
1138
- send_status1 (utils_second_motor_id (), false);
1193
+ comm_can_send_status1 (utils_second_motor_id (), false);
1139
1194
#endif
1140
1195
}
1141
1196
@@ -1144,40 +1199,40 @@ static THD_FUNCTION(cancom_status_thread, arg) {
1144
1199
conf -> send_can_status == CAN_STATUS_1_2_3_4 ||
1145
1200
conf -> send_can_status == CAN_STATUS_1_2_3_4_5 ) {
1146
1201
mc_interface_select_motor_thread (1 );
1147
- send_status2 (conf -> controller_id , false);
1202
+ comm_can_send_status2 (conf -> controller_id , false);
1148
1203
#ifdef HW_HAS_DUAL_MOTORS
1149
1204
mc_interface_select_motor_thread (2 );
1150
- send_status2 (utils_second_motor_id (), false);
1205
+ comm_can_send_status2 (utils_second_motor_id (), false);
1151
1206
#endif
1152
1207
}
1153
1208
1154
1209
if (conf -> send_can_status == CAN_STATUS_1_2_3 ||
1155
1210
conf -> send_can_status == CAN_STATUS_1_2_3_4 ||
1156
1211
conf -> send_can_status == CAN_STATUS_1_2_3_4_5 ) {
1157
1212
mc_interface_select_motor_thread (1 );
1158
- send_status3 (conf -> controller_id , false);
1213
+ comm_can_send_status3 (conf -> controller_id , false);
1159
1214
#ifdef HW_HAS_DUAL_MOTORS
1160
1215
mc_interface_select_motor_thread (2 );
1161
- send_status3 (utils_second_motor_id (), false);
1216
+ comm_can_send_status3 (utils_second_motor_id (), false);
1162
1217
#endif
1163
1218
}
1164
1219
1165
1220
if (conf -> send_can_status == CAN_STATUS_1_2_3_4 ||
1166
1221
conf -> send_can_status == CAN_STATUS_1_2_3_4_5 ) {
1167
1222
mc_interface_select_motor_thread (1 );
1168
- send_status4 (conf -> controller_id , false);
1223
+ comm_can_send_status4 (conf -> controller_id , false);
1169
1224
#ifdef HW_HAS_DUAL_MOTORS
1170
1225
mc_interface_select_motor_thread (2 );
1171
- send_status4 (utils_second_motor_id (), false);
1226
+ comm_can_send_status4 (utils_second_motor_id (), false);
1172
1227
#endif
1173
1228
}
1174
1229
1175
1230
if (conf -> send_can_status == CAN_STATUS_1_2_3_4_5 ) {
1176
1231
mc_interface_select_motor_thread (1 );
1177
- send_status5 (conf -> controller_id , false);
1232
+ comm_can_send_status5 (conf -> controller_id , false);
1178
1233
#ifdef HW_HAS_DUAL_MOTORS
1179
1234
mc_interface_select_motor_thread (2 );
1180
- send_status5 (utils_second_motor_id (), false);
1235
+ comm_can_send_status5 (utils_second_motor_id (), false);
1181
1236
#endif
1182
1237
}
1183
1238
}
@@ -1550,6 +1605,13 @@ static void decode_msg(uint32_t eid, uint8_t *data8, int len, bool is_replaced)
1550
1605
#endif
1551
1606
} break ;
1552
1607
1608
+ case CAN_PACKET_UPDATE_PID_POS_OFFSET : {
1609
+ ind = 0 ;
1610
+ float angle_now = buffer_get_float32 (data8 , 1e4 , & ind );
1611
+ bool store = data8 [ind ++ ];
1612
+ mc_interface_update_pid_pos_offset (angle_now , store );
1613
+ } break ;
1614
+
1553
1615
default :
1554
1616
break ;
1555
1617
}
@@ -1710,55 +1772,6 @@ static void decode_msg(uint32_t eid, uint8_t *data8, int len, bool is_replaced)
1710
1772
#endif
1711
1773
}
1712
1774
1713
- static void send_status1 (uint8_t id , bool replace ) {
1714
- int32_t send_index = 0 ;
1715
- uint8_t buffer [8 ];
1716
- buffer_append_int32 (buffer , (int32_t )mc_interface_get_rpm (), & send_index );
1717
- buffer_append_int16 (buffer , (int16_t )(mc_interface_get_tot_current_filtered () * 1e1 ), & send_index );
1718
- buffer_append_int16 (buffer , (int16_t )(mc_interface_get_duty_cycle_now () * 1e3 ), & send_index );
1719
- comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS << 8 ),
1720
- buffer , send_index , replace );
1721
- }
1722
-
1723
- static void send_status2 (uint8_t id , bool replace ) {
1724
- int32_t send_index = 0 ;
1725
- uint8_t buffer [8 ];
1726
- buffer_append_int32 (buffer , (int32_t )(mc_interface_get_amp_hours (false) * 1e4 ), & send_index );
1727
- buffer_append_int32 (buffer , (int32_t )(mc_interface_get_amp_hours_charged (false) * 1e4 ), & send_index );
1728
- comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS_2 << 8 ),
1729
- buffer , send_index , replace );
1730
- }
1731
-
1732
- static void send_status3 (uint8_t id , bool replace ) {
1733
- int32_t send_index = 0 ;
1734
- uint8_t buffer [8 ];
1735
- buffer_append_int32 (buffer , (int32_t )(mc_interface_get_watt_hours (false) * 1e4 ), & send_index );
1736
- buffer_append_int32 (buffer , (int32_t )(mc_interface_get_watt_hours_charged (false) * 1e4 ), & send_index );
1737
- comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS_3 << 8 ),
1738
- buffer , send_index , replace );
1739
- }
1740
-
1741
- static void send_status4 (uint8_t id , bool replace ) {
1742
- int32_t send_index = 0 ;
1743
- uint8_t buffer [8 ];
1744
- buffer_append_int16 (buffer , (int16_t )(mc_interface_temp_fet_filtered () * 1e1 ), & send_index );
1745
- buffer_append_int16 (buffer , (int16_t )(mc_interface_temp_motor_filtered () * 1e1 ), & send_index );
1746
- buffer_append_int16 (buffer , (int16_t )(mc_interface_get_tot_current_in_filtered () * 1e1 ), & send_index );
1747
- buffer_append_int16 (buffer , (int16_t )(mc_interface_get_pid_pos_now () * 50.0 ), & send_index );
1748
- comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS_4 << 8 ),
1749
- buffer , send_index , replace );
1750
- }
1751
-
1752
- static void send_status5 (uint8_t id , bool replace ) {
1753
- int32_t send_index = 0 ;
1754
- uint8_t buffer [8 ];
1755
- buffer_append_int32 (buffer , mc_interface_get_tachometer_value (false), & send_index );
1756
- buffer_append_int16 (buffer , (int16_t )(mc_interface_get_input_voltage_filtered () * 1e1 ), & send_index );
1757
- buffer_append_int16 (buffer , 0 , & send_index ); // Reserved for now
1758
- comm_can_transmit_eid_replace (id | ((uint32_t )CAN_PACKET_STATUS_5 << 8 ),
1759
- buffer , send_index , replace );
1760
- }
1761
-
1762
1775
#endif
1763
1776
1764
1777
/**
0 commit comments