Dual motor can-fix 2, indentation fix

This commit is contained in:
Benjamin Vedder 2022-11-22 14:06:59 +01:00
parent 15d26d95bc
commit 57c675f868
1 changed files with 193 additions and 189 deletions

View File

@ -1571,7 +1571,7 @@ static void decode_msg(uint32_t eid, uint8_t *data8, int len, bool is_replaced)
}
break;
case CAN_PACKET_PROCESS_RX_BUFFER:
case CAN_PACKET_PROCESS_RX_BUFFER: {
ind = 0;
unsigned int last_id = data8[ind++];
commands_send = data8[ind++];
@ -1618,13 +1618,17 @@ static void decode_msg(uint32_t eid, uint8_t *data8, int len, bool is_replaced)
break;
}
}
break;
} break;
case CAN_PACKET_PROCESS_SHORT_BUFFER:
case CAN_PACKET_PROCESS_SHORT_BUFFER: {
ind = 0;
rx_buffer_last_id = data8[ind++];
unsigned int last_id = data8[ind++];
commands_send = data8[ind++];
if (commands_send == 0) {
rx_buffer_last_id = last_id;
}
if (is_replaced) {
if (data8[ind] == COMM_JUMP_TO_BOOTLOADER ||
data8[ind] == COMM_ERASE_NEW_APP ||
@ -1648,227 +1652,227 @@ static void decode_msg(uint32_t eid, uint8_t *data8, int len, bool is_replaced)
default:
break;
}
} break;
case CAN_PACKET_SET_CURRENT_REL:
ind = 0;
mc_interface_set_current_rel(buffer_get_float32(data8, 1e5, &ind));
if (len >= 6) {
mc_interface_set_current_off_delay(buffer_get_float16(data8, 1e3, &ind));
}
timeout_reset();
break;
case CAN_PACKET_SET_CURRENT_REL:
ind = 0;
mc_interface_set_current_rel(buffer_get_float32(data8, 1e5, &ind));
case CAN_PACKET_SET_CURRENT_BRAKE_REL:
ind = 0;
mc_interface_set_brake_current_rel(buffer_get_float32(data8, 1e5, &ind));
timeout_reset();
break;
if (len >= 6) {
mc_interface_set_current_off_delay(buffer_get_float16(data8, 1e3, &ind));
case CAN_PACKET_SET_CURRENT_HANDBRAKE:
ind = 0;
mc_interface_set_handbrake(buffer_get_float32(data8, 1e3, &ind));
timeout_reset();
break;
case CAN_PACKET_SET_CURRENT_HANDBRAKE_REL:
ind = 0;
mc_interface_set_handbrake_rel(buffer_get_float32(data8, 1e5, &ind));
timeout_reset();
break;
case CAN_PACKET_PING: {
uint8_t buffer[2];
buffer[0] = is_replaced ? utils_second_motor_id() : id;
buffer[1] = HW_TYPE_VESC;
comm_can_transmit_eid_replace(data8[0] |
((uint32_t)CAN_PACKET_PONG << 8), buffer, 2, true, 0);
} break;
case CAN_PACKET_PONG:
if (ping_tp && ping_hw_last_id == data8[0]) {
if (len >= 2) {
ping_hw_last = data8[1];
} else {
ping_hw_last = HW_TYPE_VESC;
}
chEvtSignal(ping_tp, 1 << 29);
}
break;
timeout_reset();
case CAN_PACKET_DETECT_APPLY_ALL_FOC: {
if (is_replaced) {
break;
}
case CAN_PACKET_SET_CURRENT_BRAKE_REL:
ind = 0;
mc_interface_set_brake_current_rel(buffer_get_float32(data8, 1e5, &ind));
timeout_reset();
ind = 1;
bool activate_status = data8[ind++];
float max_power_loss = buffer_get_float32(data8, 1e3, &ind);
int res = conf_general_detect_apply_all_foc(max_power_loss, true, false);
if (res >= 0 && activate_status) {
app_configuration *appconf = mempools_alloc_appconf();
*appconf = *app_get_configuration();
if (appconf->can_status_msgs_r1 != 0b00001111) {
appconf->can_status_msgs_r1 = 0b00001111;
conf_general_store_app_configuration(appconf);
app_set_configuration(appconf);
}
mempools_free_appconf(appconf);
}
int8_t buffer[1];
buffer[0] = res;
comm_can_transmit_eid_replace(data8[0] |
((uint32_t)CAN_PACKET_DETECT_APPLY_ALL_FOC_RES << 8), (uint8_t*)buffer, 1, true, 0);
} break;
case CAN_PACKET_DETECT_APPLY_ALL_FOC_RES: {
if (is_replaced) {
break;
}
case CAN_PACKET_SET_CURRENT_HANDBRAKE:
ind = 0;
mc_interface_set_handbrake(buffer_get_float32(data8, 1e3, &ind));
timeout_reset();
break;
detect_all_foc_res[detect_all_foc_res_index++] = (int8_t)data8[0];
detect_all_foc_res_index %= sizeof(detect_all_foc_res);
} break;
case CAN_PACKET_SET_CURRENT_HANDBRAKE_REL:
ind = 0;
mc_interface_set_handbrake_rel(buffer_get_float32(data8, 1e5, &ind));
timeout_reset();
break;
case CAN_PACKET_CONF_CURRENT_LIMITS:
case CAN_PACKET_CONF_STORE_CURRENT_LIMITS: {
ind = 0;
float min = buffer_get_float32(data8, 1e3, &ind);
float max = buffer_get_float32(data8, 1e3, &ind);
case CAN_PACKET_PING: {
uint8_t buffer[2];
buffer[0] = is_replaced ? utils_second_motor_id() : id;
buffer[1] = HW_TYPE_VESC;
comm_can_transmit_eid_replace(data8[0] |
((uint32_t)CAN_PACKET_PONG << 8), buffer, 2, true, 0);
} break;
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
case CAN_PACKET_PONG:
if (ping_tp && ping_hw_last_id == data8[0]) {
if (len >= 2) {
ping_hw_last = data8[1];
} else {
ping_hw_last = HW_TYPE_VESC;
}
chEvtSignal(ping_tp, 1 << 29);
}
break;
if (mcconf->l_current_min != min || mcconf->l_current_max != max) {
mcconf->l_current_min = min;
mcconf->l_current_max = max;
case CAN_PACKET_DETECT_APPLY_ALL_FOC: {
if (is_replaced) {
break;
if (cmd == CAN_PACKET_CONF_STORE_CURRENT_LIMITS) {
conf_general_store_mc_configuration(mcconf,
mc_interface_get_motor_thread() == 2);
}
ind = 1;
bool activate_status = data8[ind++];
float max_power_loss = buffer_get_float32(data8, 1e3, &ind);
int res = conf_general_detect_apply_all_foc(max_power_loss, true, false);
if (res >= 0 && activate_status) {
app_configuration *appconf = mempools_alloc_appconf();
*appconf = *app_get_configuration();
mc_interface_set_configuration(mcconf);
}
if (appconf->can_status_msgs_r1 != 0b00001111) {
appconf->can_status_msgs_r1 = 0b00001111;
conf_general_store_app_configuration(appconf);
app_set_configuration(appconf);
}
mempools_free_mcconf(mcconf);
} break;
mempools_free_appconf(appconf);
case CAN_PACKET_CONF_CURRENT_LIMITS_IN:
case CAN_PACKET_CONF_STORE_CURRENT_LIMITS_IN: {
ind = 0;
float min = buffer_get_float32(data8, 1e3, &ind);
float max = buffer_get_float32(data8, 1e3, &ind);
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
if (mcconf->l_in_current_min != min || mcconf->l_in_current_max != max) {
mcconf->l_in_current_min = min;
mcconf->l_in_current_max = max;
if (cmd == CAN_PACKET_CONF_STORE_CURRENT_LIMITS_IN) {
conf_general_store_mc_configuration(mcconf,
mc_interface_get_motor_thread() == 2);
}
int8_t buffer[1];
buffer[0] = res;
comm_can_transmit_eid_replace(data8[0] |
((uint32_t)CAN_PACKET_DETECT_APPLY_ALL_FOC_RES << 8), (uint8_t*)buffer, 1, true, 0);
} break;
mc_interface_set_configuration(mcconf);
}
case CAN_PACKET_DETECT_APPLY_ALL_FOC_RES: {
if (is_replaced) {
break;
mempools_free_mcconf(mcconf);
} break;
case CAN_PACKET_CONF_FOC_ERPMS:
case CAN_PACKET_CONF_STORE_FOC_ERPMS: {
ind = 0;
float foc_openloop_rpm = buffer_get_float32(data8, 1e3, &ind);
float foc_sl_erpm = buffer_get_float32(data8, 1e3, &ind);
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
if (mcconf->foc_openloop_rpm != foc_openloop_rpm ||
mcconf->foc_sl_erpm != foc_sl_erpm) {
mcconf->foc_openloop_rpm = foc_openloop_rpm;
mcconf->foc_sl_erpm = foc_sl_erpm;
if (cmd == CAN_PACKET_CONF_STORE_FOC_ERPMS) {
conf_general_store_mc_configuration(mcconf,
mc_interface_get_motor_thread() == 2);
}
detect_all_foc_res[detect_all_foc_res_index++] = (int8_t)data8[0];
detect_all_foc_res_index %= sizeof(detect_all_foc_res);
} break;
mc_interface_set_configuration(mcconf);
}
case CAN_PACKET_CONF_CURRENT_LIMITS:
case CAN_PACKET_CONF_STORE_CURRENT_LIMITS: {
ind = 0;
float min = buffer_get_float32(data8, 1e3, &ind);
float max = buffer_get_float32(data8, 1e3, &ind);
mempools_free_mcconf(mcconf);
} break;
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
case CAN_PACKET_POLL_TS5700N8501_STATUS: {
comm_can_transmit_eid_replace(app_get_configuration()->controller_id |
((uint32_t)CAN_PACKET_POLL_TS5700N8501_STATUS << 8),
enc_ts5700n8501_get_raw_status(&encoder_cfg_TS5700N8501), 8, true, 0);
} break;
if (mcconf->l_current_min != min || mcconf->l_current_max != max) {
mcconf->l_current_min = min;
mcconf->l_current_max = max;
case CAN_PACKET_CONF_BATTERY_CUT:
case CAN_PACKET_CONF_STORE_BATTERY_CUT: {
ind = 0;
float start = buffer_get_float32(data8, 1e3, &ind);
float end = buffer_get_float32(data8, 1e3, &ind);
if (cmd == CAN_PACKET_CONF_STORE_CURRENT_LIMITS) {
conf_general_store_mc_configuration(mcconf,
mc_interface_get_motor_thread() == 2);
}
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
mc_interface_set_configuration(mcconf);
if (mcconf->l_battery_cut_start != start || mcconf->l_battery_cut_end != end) {
mcconf->l_battery_cut_start = start;
mcconf->l_battery_cut_end = end;
if (cmd == CAN_PACKET_CONF_STORE_BATTERY_CUT) {
conf_general_store_mc_configuration(mcconf,
mc_interface_get_motor_thread() == 2);
}
mempools_free_mcconf(mcconf);
} break;
mc_interface_set_configuration(mcconf);
}
case CAN_PACKET_CONF_CURRENT_LIMITS_IN:
case CAN_PACKET_CONF_STORE_CURRENT_LIMITS_IN: {
ind = 0;
float min = buffer_get_float32(data8, 1e3, &ind);
float max = buffer_get_float32(data8, 1e3, &ind);
mempools_free_mcconf(mcconf);
} break;
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
if (mcconf->l_in_current_min != min || mcconf->l_in_current_max != max) {
mcconf->l_in_current_min = min;
mcconf->l_in_current_max = max;
if (cmd == CAN_PACKET_CONF_STORE_CURRENT_LIMITS_IN) {
conf_general_store_mc_configuration(mcconf,
mc_interface_get_motor_thread() == 2);
}
mc_interface_set_configuration(mcconf);
}
mempools_free_mcconf(mcconf);
} break;
case CAN_PACKET_CONF_FOC_ERPMS:
case CAN_PACKET_CONF_STORE_FOC_ERPMS: {
ind = 0;
float foc_openloop_rpm = buffer_get_float32(data8, 1e3, &ind);
float foc_sl_erpm = buffer_get_float32(data8, 1e3, &ind);
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
if (mcconf->foc_openloop_rpm != foc_openloop_rpm ||
mcconf->foc_sl_erpm != foc_sl_erpm) {
mcconf->foc_openloop_rpm = foc_openloop_rpm;
mcconf->foc_sl_erpm = foc_sl_erpm;
if (cmd == CAN_PACKET_CONF_STORE_FOC_ERPMS) {
conf_general_store_mc_configuration(mcconf,
mc_interface_get_motor_thread() == 2);
}
mc_interface_set_configuration(mcconf);
}
mempools_free_mcconf(mcconf);
} break;
case CAN_PACKET_POLL_TS5700N8501_STATUS: {
comm_can_transmit_eid_replace(app_get_configuration()->controller_id |
((uint32_t)CAN_PACKET_POLL_TS5700N8501_STATUS << 8),
enc_ts5700n8501_get_raw_status(&encoder_cfg_TS5700N8501), 8, true, 0);
} break;
case CAN_PACKET_CONF_BATTERY_CUT:
case CAN_PACKET_CONF_STORE_BATTERY_CUT: {
ind = 0;
float start = buffer_get_float32(data8, 1e3, &ind);
float end = buffer_get_float32(data8, 1e3, &ind);
mc_configuration *mcconf = mempools_alloc_mcconf();
*mcconf = *mc_interface_get_configuration();
if (mcconf->l_battery_cut_start != start || mcconf->l_battery_cut_end != end) {
mcconf->l_battery_cut_start = start;
mcconf->l_battery_cut_end = end;
if (cmd == CAN_PACKET_CONF_STORE_BATTERY_CUT) {
conf_general_store_mc_configuration(mcconf,
mc_interface_get_motor_thread() == 2);
}
mc_interface_set_configuration(mcconf);
}
mempools_free_mcconf(mcconf);
} break;
case CAN_PACKET_SHUTDOWN: {
case CAN_PACKET_SHUTDOWN: {
#ifdef HW_SHUTDOWN_HOLD_ON
SHUTDOWN_SET_SAMPLING_DISABLED(true);
mc_interface_lock();
DISABLE_GATE();
HW_SHUTDOWN_HOLD_OFF();
chThdSleepMilliseconds(5000);
HW_SHUTDOWN_HOLD_ON();
ENABLE_GATE();
mc_interface_unlock();
SHUTDOWN_SET_SAMPLING_DISABLED(false);
SHUTDOWN_SET_SAMPLING_DISABLED(true);
mc_interface_lock();
DISABLE_GATE();
HW_SHUTDOWN_HOLD_OFF();
chThdSleepMilliseconds(5000);
HW_SHUTDOWN_HOLD_ON();
ENABLE_GATE();
mc_interface_unlock();
SHUTDOWN_SET_SAMPLING_DISABLED(false);
#endif
} break;
} break;
case CAN_PACKET_UPDATE_PID_POS_OFFSET: {
ind = 0;
float angle_now = buffer_get_float32(data8, 1e4, &ind);
bool store = data8[ind++];
mc_interface_update_pid_pos_offset(angle_now, store);
} break;
case CAN_PACKET_UPDATE_PID_POS_OFFSET: {
ind = 0;
float angle_now = buffer_get_float32(data8, 1e4, &ind);
bool store = data8[ind++];
mc_interface_update_pid_pos_offset(angle_now, store);
} break;
case CAN_PACKET_POLL_ROTOR_POS: {
uint8_t buffer[4];
int32_t index = 0;
buffer_append_int32(buffer, (int32_t)(encoder_read_deg() * 100000.0), &index);
comm_can_transmit_eid_replace(app_get_configuration()->controller_id |
((uint32_t)CAN_PACKET_POLL_ROTOR_POS << 8), (uint8_t*)buffer, 4, true, 0);
} break;
case CAN_PACKET_POLL_ROTOR_POS: {
uint8_t buffer[4];
int32_t index = 0;
buffer_append_int32(buffer, (int32_t)(encoder_read_deg() * 100000.0), &index);
comm_can_transmit_eid_replace(app_get_configuration()->controller_id |
((uint32_t)CAN_PACKET_POLL_ROTOR_POS << 8), (uint8_t*)buffer, 4, true, 0);
} break;
default:
break;
default:
break;
}
}