Ejemplo n.º 1
0
Archivo: comm.c Proyecto: rewolff/bldc
static void handle_res_packet(unsigned char *data, unsigned char len) {
	COMM_RES_PACKET_ID car_res_packet;
	uint8_t buffer2[256];
	int32_t index;

	(void)len;

	car_res_packet = data[0];
	data++;
	len--;

	switch (car_res_packet) {
	case COMM_READ_VALUES:
		index = 0;
		buffer2[index++] = COMM_READ_VALUES;
		buffer_append_int16(buffer2, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS1) * 10.0), &index);
		buffer_append_int16(buffer2, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS2) * 10.0), &index);
		buffer_append_int16(buffer2, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS3) * 10.0), &index);
		buffer_append_int16(buffer2, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS4) * 10.0), &index);
		buffer_append_int16(buffer2, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS5) * 10.0), &index);
		buffer_append_int16(buffer2, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS6) * 10.0), &index);
		buffer_append_int16(buffer2, (int16_t)(NTC_TEMP(ADC_IND_TEMP_PCB) * 10.0), &index);
		buffer_append_int32(buffer2, (int32_t)(mcpwm_read_reset_avg_motor_current() * 100.0), &index);
		buffer_append_int32(buffer2, (int32_t)(mcpwm_read_reset_avg_input_current() * 100.0), &index);
		buffer_append_int16(buffer2, (int16_t)(mcpwm_get_duty_cycle_now() * 1000.0), &index);
		buffer_append_int32(buffer2, (int32_t)mcpwm_get_rpm(), &index);
		buffer_append_int16(buffer2, (int16_t)(GET_INPUT_VOLTAGE() * 10.0), &index);
		packet_send_packet(buffer2, index);
		break;

	default:
		break;
	}
}
Ejemplo n.º 2
0
float mc_interface_get_duty_cycle_now(void) {
	float ret = 0.0;

	switch (m_conf.motor_type) {
	case MOTOR_TYPE_BLDC:
	case MOTOR_TYPE_DC:
		ret = mcpwm_get_duty_cycle_now();
		break;

	case MOTOR_TYPE_FOC:
		ret = mcpwm_foc_get_duty_cycle_now();
		break;

	default:
		break;
	}

	return ret;
}
Ejemplo n.º 3
0
/**
 * Process a received buffer with commands and data.
 *
 * @param data
 * The buffer to process.
 *
 * @param len
 * The length of the buffer.
 */
void commands_process_packet(unsigned char *data, unsigned int len) {
	if (!len) {
		return;
	}

	COMM_PACKET_ID packet_id;
	int32_t ind = 0;
	uint16_t sample_len;
	uint8_t decimation;
	bool at_start;
	mc_configuration mcconf;
	app_configuration appconf;
	uint16_t flash_res;
	uint32_t new_app_offset;

	(void)len;

	packet_id = data[0];
	data++;
	len--;

	switch (packet_id) {
	case COMM_FW_VERSION:
		ind = 0;
		send_buffer[ind++] = COMM_FW_VERSION;
		send_buffer[ind++] = FW_VERSION_MAJOR;
		send_buffer[ind++] = FW_VERSION_MINOR;
		commands_send_packet(send_buffer, ind);
		break;

	case COMM_JUMP_TO_BOOTLOADER:
		flash_helper_jump_to_bootloader();
		break;

	case COMM_ERASE_NEW_APP:
		ind = 0;
		flash_res = flash_helper_erase_new_app(buffer_get_uint32(data, &ind));

		ind = 0;
		send_buffer[ind++] = COMM_ERASE_NEW_APP;
		send_buffer[ind++] = flash_res == FLASH_COMPLETE ? 1 : 0;
		commands_send_packet(send_buffer, ind);
		break;

	case COMM_WRITE_NEW_APP_DATA:
		ind = 0;
		new_app_offset = buffer_get_uint32(data, &ind);
		flash_res = flash_helper_write_new_app_data(new_app_offset, data + ind, len - ind);

		ind = 0;
		send_buffer[ind++] = COMM_WRITE_NEW_APP_DATA;
		send_buffer[ind++] = flash_res == FLASH_COMPLETE ? 1 : 0;
		commands_send_packet(send_buffer, ind);
		break;

	case COMM_GET_VALUES:
		ind = 0;
		send_buffer[ind++] = COMM_GET_VALUES;
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS1) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS2) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS3) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS4) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS5) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS6) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_PCB) * 10.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_read_reset_avg_motor_current() * 100.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_read_reset_avg_input_current() * 100.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(mcpwm_get_duty_cycle_now() * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)mcpwm_get_rpm(), &ind);
		buffer_append_int16(send_buffer, (int16_t)(GET_INPUT_VOLTAGE() * 10.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_get_amp_hours(false) * 10000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_get_amp_hours_charged(false) * 10000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_get_watt_hours(false) * 10000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_get_watt_hours_charged(false) * 10000.0), &ind);
		buffer_append_int32(send_buffer, mcpwm_get_tachometer_value(false), &ind);
		buffer_append_int32(send_buffer, mcpwm_get_tachometer_abs_value(false), &ind);
		send_buffer[ind++] = mcpwm_get_fault();
		commands_send_packet(send_buffer, ind);
		break;

	case COMM_SET_DUTY:
		ind = 0;
		mcpwm_set_duty((float)buffer_get_int32(data, &ind) / 100000.0);
		timeout_reset();
		break;

	case COMM_SET_CURRENT:
		ind = 0;
		mcpwm_set_current((float)buffer_get_int32(data, &ind) / 1000.0);
		timeout_reset();
		break;

	case COMM_SET_CURRENT_BRAKE:
		ind = 0;
		mcpwm_set_brake_current((float)buffer_get_int32(data, &ind) / 1000.0);
		timeout_reset();
		break;

	case COMM_SET_RPM:
		ind = 0;
		mcpwm_set_pid_speed((float)buffer_get_int32(data, &ind));
		timeout_reset();
		break;

	case COMM_SET_POS:
		ind = 0;
		mcpwm_set_pid_pos((float)buffer_get_int32(data, &ind) / 1000000.0);
		timeout_reset();
		break;

	case COMM_SET_DETECT:
		mcpwm_set_detect();
		timeout_reset();
		break;

	case COMM_SET_SERVO_OFFSET:
		servos[0].offset = data[0];
		break;

	case COMM_SET_MCCONF:
		mcconf = *mcpwm_get_configuration();

		ind = 0;
		mcconf.pwm_mode = data[ind++];
		mcconf.comm_mode = data[ind++];
		mcconf.motor_type = data[ind++];
		mcconf.sensor_mode = data[ind++];

		mcconf.l_current_max = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_current_min = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_in_current_max = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_in_current_min = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_abs_current_max = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_min_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_max_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_max_erpm_fbrake = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_max_erpm_fbrake_cc = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_min_vin = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_max_vin = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_slow_abs_current = data[ind++];
		mcconf.l_rpm_lim_neg_torque = data[ind++];
		mcconf.l_temp_fet_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_temp_fet_end = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_temp_motor_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_temp_motor_end = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_min_duty = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.l_max_duty = (float)buffer_get_int32(data, &ind) / 1000000.0;

		mcconf.lo_current_max = mcconf.l_current_max;
		mcconf.lo_current_min = mcconf.l_current_min;
		mcconf.lo_in_current_max = mcconf.l_in_current_max;
		mcconf.lo_in_current_min = mcconf.l_in_current_min;

		mcconf.sl_min_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_min_erpm_cycle_int_limit = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_max_fullbreak_current_dir_change = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_cycle_int_limit = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_phase_advance_at_br = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_cycle_int_rpm_br = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_bemf_coupling_k = (float)buffer_get_int32(data, &ind) / 1000.0;

		memcpy(mcconf.hall_table, data + ind, 8);
		ind += 8;
		mcconf.hall_sl_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;

		mcconf.s_pid_kp = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.s_pid_ki = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.s_pid_kd = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.s_pid_min_rpm = (float)buffer_get_int32(data, &ind) / 1000.0;

		mcconf.p_pid_kp = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.p_pid_ki = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.p_pid_kd = (float)buffer_get_int32(data, &ind) / 1000000.0;

		mcconf.cc_startup_boost_duty = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.cc_min_current = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.cc_gain = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.cc_ramp_step_max = (float)buffer_get_int32(data, &ind) / 1000000.0;

		mcconf.m_fault_stop_time_ms = buffer_get_int32(data, &ind);

		conf_general_store_mc_configuration(&mcconf);
		mcpwm_set_configuration(&mcconf);

		ind = 0;
		send_buffer[ind++] = COMM_SET_MCCONF;
		commands_send_packet(send_buffer, ind);
		break;

	case COMM_GET_MCCONF:
		mcconf = *mcpwm_get_configuration();

		ind = 0;
		send_buffer[ind++] = COMM_GET_MCCONF;

		send_buffer[ind++] = mcconf.pwm_mode;
		send_buffer[ind++] = mcconf.comm_mode;
		send_buffer[ind++] = mcconf.motor_type;
		send_buffer[ind++] = mcconf.sensor_mode;

		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_current_max * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_current_min * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_in_current_max * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_in_current_min * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_abs_current_max * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_min_erpm * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_erpm * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_erpm_fbrake * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_erpm_fbrake_cc * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_min_vin * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_vin * 1000.0), &ind);
		send_buffer[ind++] = mcconf.l_slow_abs_current;
		send_buffer[ind++] = mcconf.l_rpm_lim_neg_torque;
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_temp_fet_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_temp_fet_end * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_temp_motor_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_temp_motor_end * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_min_duty * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_duty * 1000000.0), &ind);

		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_min_erpm * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_min_erpm_cycle_int_limit * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_max_fullbreak_current_dir_change * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_cycle_int_limit * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_phase_advance_at_br * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_cycle_int_rpm_br * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_bemf_coupling_k * 1000.0), &ind);

		memcpy(send_buffer + ind, mcconf.hall_table, 8);
		ind += 8;
		buffer_append_int32(send_buffer, (int32_t)(mcconf.hall_sl_erpm * 1000.0), &ind);

		buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_kp * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_ki * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_kd * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_min_rpm * 1000.0), &ind);

		buffer_append_int32(send_buffer, (int32_t)(mcconf.p_pid_kp * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.p_pid_ki * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.p_pid_kd * 1000000.0), &ind);

		buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_startup_boost_duty * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_min_current * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_gain * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_ramp_step_max * 1000000.0), &ind);

		buffer_append_int32(send_buffer, mcconf.m_fault_stop_time_ms, &ind);

		commands_send_packet(send_buffer, ind);
		break;

	case COMM_SET_APPCONF:
		appconf = *app_get_configuration();

		ind = 0;
		appconf.controller_id = data[ind++];
		appconf.timeout_msec = buffer_get_uint32(data, &ind);
		appconf.timeout_brake_current = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.send_can_status = data[ind++];
		appconf.send_can_status_rate_hz = buffer_get_uint16(data, &ind);

		appconf.app_to_use = data[ind++];

		appconf.app_ppm_conf.ctrl_type = data[ind++];
		appconf.app_ppm_conf.pid_max_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_conf.hyst = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_conf.pulse_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_conf.pulse_end = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_conf.median_filter = data[ind++];
		appconf.app_ppm_conf.safe_start = data[ind++];
		appconf.app_ppm_conf.rpm_lim_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_conf.rpm_lim_end = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_conf.multi_esc = data[ind++];
		appconf.app_ppm_conf.tc = data[ind++];
		appconf.app_ppm_conf.tc_max_diff = (float)buffer_get_int32(data, &ind) / 1000.0;

		appconf.app_adc_conf.ctrl_type = data[ind++];
		appconf.app_adc_conf.hyst = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_adc_conf.voltage_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_adc_conf.voltage_end = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_adc_conf.use_filter = data[ind++];
		appconf.app_adc_conf.safe_start = data[ind++];
		appconf.app_adc_conf.button_inverted = data[ind++];
		appconf.app_adc_conf.voltage_inverted = data[ind++];
		appconf.app_adc_conf.rpm_lim_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_adc_conf.rpm_lim_end = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_adc_conf.multi_esc = data[ind++];
		appconf.app_adc_conf.tc = data[ind++];
		appconf.app_adc_conf.tc_max_diff = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_adc_conf.update_rate_hz = buffer_get_uint16(data, &ind);

		appconf.app_uart_baudrate = buffer_get_uint32(data, &ind);

		appconf.app_chuk_conf.ctrl_type = data[ind++];
		appconf.app_chuk_conf.hyst = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_chuk_conf.rpm_lim_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_chuk_conf.rpm_lim_end = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_chuk_conf.ramp_time_pos = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_chuk_conf.ramp_time_neg = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_chuk_conf.multi_esc = data[ind++];
		appconf.app_chuk_conf.tc = data[ind++];
		appconf.app_chuk_conf.tc_max_diff = (float)buffer_get_int32(data, &ind) / 1000.0;

		conf_general_store_app_configuration(&appconf);
		app_set_configuration(&appconf);
		timeout_configure(appconf.timeout_msec, appconf.timeout_brake_current);

		ind = 0;
		send_buffer[ind++] = COMM_SET_APPCONF;
		commands_send_packet(send_buffer, ind);
		break;

	case COMM_GET_APPCONF:
		appconf = *app_get_configuration();

		ind = 0;
		send_buffer[ind++] = COMM_GET_APPCONF;
		send_buffer[ind++] = appconf.controller_id;
		buffer_append_uint32(send_buffer, appconf.timeout_msec, &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.timeout_brake_current * 1000.0), &ind);
		send_buffer[ind++] = appconf.send_can_status;
		buffer_append_uint16(send_buffer, appconf.send_can_status_rate_hz, &ind);

		send_buffer[ind++] = appconf.app_to_use;

		send_buffer[ind++] = appconf.app_ppm_conf.ctrl_type;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.pid_max_erpm * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.hyst * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.pulse_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.pulse_end * 1000.0), &ind);
		send_buffer[ind++] = appconf.app_ppm_conf.median_filter;
		send_buffer[ind++] = appconf.app_ppm_conf.safe_start;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.rpm_lim_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.rpm_lim_end * 1000.0), &ind);
		send_buffer[ind++] = appconf.app_ppm_conf.multi_esc;
		send_buffer[ind++] = appconf.app_ppm_conf.tc;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_conf.tc_max_diff * 1000.0), &ind);

		send_buffer[ind++] = appconf.app_adc_conf.ctrl_type;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.hyst * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.voltage_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.voltage_end * 1000.0), &ind);
		send_buffer[ind++] = appconf.app_adc_conf.use_filter;
		send_buffer[ind++] = appconf.app_adc_conf.safe_start;
		send_buffer[ind++] = appconf.app_adc_conf.button_inverted;
		send_buffer[ind++] = appconf.app_adc_conf.voltage_inverted;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.rpm_lim_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.rpm_lim_end * 1000.0), &ind);
		send_buffer[ind++] = appconf.app_adc_conf.multi_esc;
		send_buffer[ind++] = appconf.app_adc_conf.tc;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_adc_conf.tc_max_diff * 1000.0), &ind);
		buffer_append_uint16(send_buffer, appconf.app_adc_conf.update_rate_hz, &ind);

		buffer_append_uint32(send_buffer, appconf.app_uart_baudrate, &ind);

		send_buffer[ind++] = appconf.app_chuk_conf.ctrl_type;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_conf.hyst * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_conf.rpm_lim_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_conf.rpm_lim_end * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_conf.ramp_time_pos * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_conf.ramp_time_neg * 1000.0), &ind);
		send_buffer[ind++] = appconf.app_chuk_conf.multi_esc;
		send_buffer[ind++] = appconf.app_chuk_conf.tc;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_conf.tc_max_diff * 1000.0), &ind);

		commands_send_packet(send_buffer, ind);
		break;

	case COMM_SAMPLE_PRINT:
		ind = 0;
		at_start = data[ind++];
		sample_len = buffer_get_uint16(data, &ind);
		decimation = data[ind++];
		main_sample_print_data(at_start, sample_len, decimation);
		break;

	case COMM_TERMINAL_CMD:
		data[len] = '\0';
		terminal_process_string((char*)data);
		break;

	case COMM_DETECT_MOTOR_PARAM:
		ind = 0;
		detect_current = (float)buffer_get_int32(data, &ind) / 1000.0;
		detect_min_rpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		detect_low_duty = (float)buffer_get_int32(data, &ind) / 1000.0;

		chEvtSignal(detect_tp, (eventmask_t) 1);
		break;

	case COMM_REBOOT:
		// Lock the system and enter an infinite loop. The watchdog will reboot.
		__disable_irq();
		for(;;){};
		break;

	case COMM_ALIVE:
		timeout_reset();
		break;

	case COMM_GET_DECODED_PPM:
		ind = 0;
		send_buffer[ind++] = COMM_GET_DECODED_PPM;
		buffer_append_int32(send_buffer, (int32_t)(servodec_get_servo(0) * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(servodec_get_last_pulse_len(0) * 1000000.0), &ind);
		commands_send_packet(send_buffer, ind);
		break;

	case COMM_GET_DECODED_ADC:
		ind = 0;
		send_buffer[ind++] = COMM_GET_DECODED_ADC;
		buffer_append_int32(send_buffer, (int32_t)(app_adc_get_decoded_level() * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(app_adc_get_voltage() * 1000000.0), &ind);
		commands_send_packet(send_buffer, ind);
		break;

	case COMM_GET_DECODED_CHUK:
		ind = 0;
		send_buffer[ind++] = COMM_GET_DECODED_CHUK;
		buffer_append_int32(send_buffer, (int32_t)(app_nunchuk_get_decoded_chuk() * 1000000.0), &ind);
		commands_send_packet(send_buffer, ind);
		break;

	case COMM_FORWARD_CAN:
		comm_can_send_buffer(data[0], data + 1, len - 1, false);
		break;

	default:
		break;
	}
}
Ejemplo n.º 4
0
static msg_t adc_thread(void *arg) {
    (void)arg;

    chRegSetThreadName("APP_ADC");

    // Set servo pin as an input with pullup
    palSetPadMode(HW_ICU_GPIO, HW_ICU_PIN, PAL_MODE_INPUT_PULLUP);

    for(;;) {
        // Sleep for a time according to the specified rate
        systime_t sleep_time = CH_FREQUENCY / config.update_rate_hz;

        // At least one tick should be slept to not block the other threads
        if (sleep_time == 0) {
            sleep_time = 1;
        }
        chThdSleep(sleep_time);

        // Read the external ADC pin and convert the value to a voltage.
        float pwr = (float)ADC_Value[ADC_IND_EXT];
        pwr /= 4095;
        pwr *= V_REG;

        read_voltage = pwr;

        // Optionally apply a mean value filter
        if (config.use_filter) {
            static float filter_buffer[FILTER_SAMPLES];
            static int filter_ptr = 0;

            filter_buffer[filter_ptr++] = pwr;
            if (filter_ptr >= FILTER_SAMPLES) {
                filter_ptr = 0;
            }

            pwr = 0.0;
            for (int i = 0; i < FILTER_SAMPLES; i++) {
                pwr += filter_buffer[i];
            }
            pwr /= FILTER_SAMPLES;
        }

        // Map and truncate the read voltage
        pwr = utils_map(pwr, config.voltage_start, config.voltage_end, 0.0, 1.0);
        utils_truncate_number(&pwr, 0.0, 1.0);

        // Optionally invert the read voltage
        if (config.voltage_inverted) {
            pwr = 1.0 - pwr;
        }

        decoded_level = pwr;

        // Read the servo pin and optionally invert it.
        bool button_val = !palReadPad(HW_ICU_GPIO, HW_ICU_PIN);
        if (config.button_inverted) {
            button_val = !button_val;
        }

        switch (config.ctrl_type) {
        case ADC_CTRL_TYPE_CURRENT_REV_CENTER:
        case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_CENTER:
        case ADC_CTRL_TYPE_DUTY_REV_CENTER:
            // Scale the voltage and set 0 at the center
            pwr *= 2.0;
            pwr -= 1.0;
            break;

        case ADC_CTRL_TYPE_CURRENT_REV_BUTTON:
        case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_BUTTON:
        case ADC_CTRL_TYPE_DUTY_REV_BUTTON:
            // Invert the voltage if the button is pressed
            if (button_val) {
                pwr = -pwr;
            }
            break;

        default:
            break;
        }

        // Apply a deadband
        utils_deadband(&pwr, config.hyst, 1.0);

        float current = 0;
        bool current_mode = false;
        bool current_mode_brake = false;
        const volatile mc_configuration *mcconf = mcpwm_get_configuration();
        bool send_duty = false;

        // Use the filtered and mapped voltage for control according to the configuration.
        switch (config.ctrl_type) {
        case ADC_CTRL_TYPE_CURRENT:
        case ADC_CTRL_TYPE_CURRENT_REV_CENTER:
        case ADC_CTRL_TYPE_CURRENT_REV_BUTTON:
            current_mode = true;
            if (pwr >= 0.0) {
                current = pwr * mcconf->l_current_max;
            } else {
                current = pwr * fabsf(mcconf->l_current_min);
            }

            if (fabsf(pwr) < 0.001) {
                ms_without_power += (1000.0 * (float)sleep_time) / (float)CH_FREQUENCY;
            }
            break;

        case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_CENTER:
        case ADC_CTRL_TYPE_CURRENT_NOREV_BRAKE_BUTTON:
            current_mode = true;
            if (pwr >= 0.0) {
                current = pwr * mcconf->l_current_max;
            } else {
                current = fabsf(pwr * mcconf->l_current_min);
                current_mode_brake = true;
            }

            if (pwr < 0.001) {
                ms_without_power += (1000.0 * (float)sleep_time) / (float)CH_FREQUENCY;
            }
            break;

        case ADC_CTRL_TYPE_DUTY:
        case ADC_CTRL_TYPE_DUTY_REV_CENTER:
        case ADC_CTRL_TYPE_DUTY_REV_BUTTON:
            if (fabsf(pwr) < 0.001) {
                ms_without_power += (1000.0 * (float)sleep_time) / (float)CH_FREQUENCY;
            }

            if (!(ms_without_power < MIN_MS_WITHOUT_POWER && config.safe_start)) {
                mcpwm_set_duty(pwr);
                send_duty = true;
            }
            break;

        default:
            continue;
        }

        // If safe start is enabled and the output has not been zero for long enough
        if (ms_without_power < MIN_MS_WITHOUT_POWER && config.safe_start) {
            static int pulses_without_power_before = 0;
            if (ms_without_power == pulses_without_power_before) {
                ms_without_power = 0;
            }
            pulses_without_power_before = ms_without_power;
            mcpwm_set_brake_current(timeout_get_brake_current());
            continue;
        }

        // Reset timeout
        timeout_reset();

        // Find lowest RPM (for traction control)
        float rpm_local = mcpwm_get_rpm();
        float rpm_lowest = rpm_local;
        if (config.multi_esc) {
            for (int i = 0; i < CAN_STATUS_MSGS_TO_STORE; i++) {
                can_status_msg *msg = comm_can_get_status_msg_index(i);

                if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
                    float rpm_tmp = msg->rpm;

                    if (fabsf(rpm_tmp) < fabsf(rpm_lowest)) {
                        rpm_lowest = rpm_tmp;
                    }
                }
            }
        }

        // Optionally send the duty cycles to the other ESCs seen on the CAN-bus
        if (send_duty && config.multi_esc) {
            float duty = mcpwm_get_duty_cycle_now();

            for (int i = 0; i < CAN_STATUS_MSGS_TO_STORE; i++) {
                can_status_msg *msg = comm_can_get_status_msg_index(i);

                if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
                    comm_can_set_duty(msg->id, duty);
                }
            }
        }

        if (current_mode) {
            if (current_mode_brake) {
                mcpwm_set_brake_current(current);

                // Send brake command to all ESCs seen recently on the CAN bus
                for (int i = 0; i < CAN_STATUS_MSGS_TO_STORE; i++) {
                    can_status_msg *msg = comm_can_get_status_msg_index(i);

                    if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
                        comm_can_set_current_brake(msg->id, current);
                    }
                }
            } else {
                // Apply soft RPM limit
                if (rpm_lowest > config.rpm_lim_end && current > 0.0) {
                    current = mcconf->cc_min_current;
                } else if (rpm_lowest > config.rpm_lim_start && current > 0.0) {
                    current = utils_map(rpm_lowest, config.rpm_lim_start, config.rpm_lim_end, current, mcconf->cc_min_current);
                } else if (rpm_lowest < -config.rpm_lim_end && current < 0.0) {
                    current = mcconf->cc_min_current;
                } else if (rpm_lowest < -config.rpm_lim_start && current < 0.0) {
                    rpm_lowest = -rpm_lowest;
                    current = -current;
                    current = utils_map(rpm_lowest, config.rpm_lim_start, config.rpm_lim_end, current, mcconf->cc_min_current);
                    current = -current;
                    rpm_lowest = -rpm_lowest;
                }

                float current_out = current;
                bool is_reverse = false;
                if (current_out < 0.0) {
                    is_reverse = true;
                    current_out = -current_out;
                    current = -current;
                    rpm_local = -rpm_local;
                    rpm_lowest = -rpm_lowest;
                }

                // Traction control
                if (config.multi_esc) {
                    for (int i = 0; i < CAN_STATUS_MSGS_TO_STORE; i++) {
                        can_status_msg *msg = comm_can_get_status_msg_index(i);

                        if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < MAX_CAN_AGE) {
                            if (config.tc) {
                                float rpm_tmp = msg->rpm;
                                if (is_reverse) {
                                    rpm_tmp = -rpm_tmp;
                                }

                                float diff = rpm_tmp - rpm_lowest;
                                current_out = utils_map(diff, 0.0, config.tc_max_diff, current, 0.0);
                                if (current_out < mcconf->cc_min_current) {
                                    current_out = 0.0;
                                }
                            }

                            if (is_reverse) {
                                comm_can_set_current(msg->id, -current_out);
                            } else {
                                comm_can_set_current(msg->id, current_out);
                            }
                        }
                    }

                    if (config.tc) {
                        float diff = rpm_local - rpm_lowest;
                        current_out = utils_map(diff, 0.0, config.tc_max_diff, current, 0.0);
                        if (current_out < mcconf->cc_min_current) {
                            current_out = 0.0;
                        }
                    }
                }

                if (is_reverse) {
                    mcpwm_set_current(-current_out);
                } else {
                    mcpwm_set_current(current_out);
                }
            }
        }
    }

    return 0;
}
Ejemplo n.º 5
0
/**
 * Process a received buffer with commands and data.
 *
 * @param data
 * The buffer to process.
 *
 * @param len
 * The length of the buffer.
 */
void commands_process_packet(unsigned char *data, unsigned char len) {
	if (!len) {
		return;
	}

	COMM_PACKET_ID packet_id;
	int32_t ind = 0;
	uint16_t sample_len;
	uint8_t decimation;
	bool at_start;
	mc_configuration mcconf;
	app_configuration appconf;

	(void)len;

	packet_id = data[0];
	data++;
	len--;

	switch (packet_id) {
	case COMM_GET_VALUES:
		ind = 0;
		send_buffer[ind++] = COMM_GET_VALUES;
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS1) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS2) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS3) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS4) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS5) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_MOS6) * 10.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(NTC_TEMP(ADC_IND_TEMP_PCB) * 10.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_read_reset_avg_motor_current() * 100.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_read_reset_avg_input_current() * 100.0), &ind);
		buffer_append_int16(send_buffer, (int16_t)(mcpwm_get_duty_cycle_now() * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)mcpwm_get_rpm(), &ind);
		buffer_append_int16(send_buffer, (int16_t)(GET_INPUT_VOLTAGE() * 10.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_get_amp_hours(false) * 10000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_get_amp_hours_charged(false) * 10000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_get_watt_hours(false) * 10000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcpwm_get_watt_hours_charged(false) * 10000.0), &ind);
		buffer_append_int32(send_buffer, mcpwm_get_tachometer_value(false), &ind);
		buffer_append_int32(send_buffer, mcpwm_get_tachometer_abs_value(false), &ind);
		send_buffer[ind++] = mcpwm_get_fault();
		send_packet(send_buffer, ind);
		break;

	case COMM_SET_DUTY:
		ind = 0;
		mcpwm_set_duty((float)buffer_get_int32(data, &ind) / 100000.0);
		timeout_reset();
		break;

	case COMM_SET_CURRENT:
		ind = 0;
		mcpwm_set_current((float)buffer_get_int32(data, &ind) / 1000.0);
		timeout_reset();
		break;

	case COMM_SET_CURRENT_BRAKE:
		ind = 0;
		mcpwm_set_brake_current((float)buffer_get_int32(data, &ind) / 1000.0);
		timeout_reset();
		break;

	case COMM_SET_RPM:
		ind = 0;
		mcpwm_set_pid_speed((float)buffer_get_int32(data, &ind));
		timeout_reset();
		break;

	case COMM_SET_DETECT:
		mcpwm_set_detect();
		timeout_reset();
		break;

	case COMM_SET_SERVO_OFFSET:
		servos[0].offset = data[0];
		break;

	case COMM_SET_MCCONF:
		mcconf = *mcpwm_get_configuration();

		ind = 0;
		mcconf.pwm_mode = data[ind++];
		mcconf.comm_mode = data[ind++];

		mcconf.l_current_max = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_current_min = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_in_current_max = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_in_current_min = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_abs_current_max = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_min_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_max_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_max_erpm_fbrake = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_min_vin = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_max_vin = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.l_slow_abs_current = data[ind++];
		mcconf.l_rpm_lim_neg_torque = data[ind++];

		mcconf.lo_current_max = mcconf.l_current_max;
		mcconf.lo_current_min = mcconf.l_current_min;
		mcconf.lo_in_current_max = mcconf.l_in_current_max;
		mcconf.lo_in_current_min = mcconf.l_in_current_min;

		mcconf.sl_is_sensorless = data[ind++];
		mcconf.sl_min_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_min_erpm_cycle_int_limit = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_cycle_int_limit = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_cycle_int_limit_high_fac = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_cycle_int_rpm_br = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.sl_bemf_coupling_k = (float)buffer_get_int32(data, &ind) / 1000.0;

		mcconf.hall_dir = data[ind++];
		mcconf.hall_fwd_add = data[ind++];
		mcconf.hall_rev_add = data[ind++];

		mcconf.s_pid_kp = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.s_pid_ki = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.s_pid_kd = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.s_pid_min_rpm = (float)buffer_get_int32(data, &ind) / 1000.0;

		mcconf.cc_startup_boost_duty = (float)buffer_get_int32(data, &ind) / 1000000.0;
		mcconf.cc_min_current = (float)buffer_get_int32(data, &ind) / 1000.0;
		mcconf.cc_gain = (float)buffer_get_int32(data, &ind) / 1000000.0;

		mcconf.m_fault_stop_time_ms = buffer_get_int32(data, &ind);

		conf_general_store_mc_configuration(&mcconf);
		mcpwm_set_configuration(&mcconf);
		break;

	case COMM_GET_MCCONF:
		mcconf = *mcpwm_get_configuration();

		ind = 0;
		send_buffer[ind++] = COMM_GET_MCCONF;

		send_buffer[ind++] = mcconf.pwm_mode;
		send_buffer[ind++] = mcconf.comm_mode;

		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_current_max * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_current_min * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_in_current_max * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_in_current_min * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_abs_current_max * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_min_erpm * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_erpm * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_erpm_fbrake * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_min_vin * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.l_max_vin * 1000.0), &ind);
		send_buffer[ind++] = mcconf.l_slow_abs_current;
		send_buffer[ind++] = mcconf.l_rpm_lim_neg_torque;

		send_buffer[ind++] = mcconf.sl_is_sensorless;
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_min_erpm * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_min_erpm_cycle_int_limit * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_cycle_int_limit * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_cycle_int_limit_high_fac * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_cycle_int_rpm_br * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.sl_bemf_coupling_k * 1000.0), &ind);

		send_buffer[ind++] = mcconf.hall_dir;
		send_buffer[ind++] = mcconf.hall_fwd_add;
		send_buffer[ind++] = mcconf.hall_rev_add;

		buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_kp * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_ki * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_kd * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.s_pid_min_rpm * 1000.0), &ind);

		buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_startup_boost_duty * 1000000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_min_current * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(mcconf.cc_gain * 1000000.0), &ind);

		buffer_append_int32(send_buffer, mcconf.m_fault_stop_time_ms, &ind);

		send_packet(send_buffer, ind);
		break;

	case COMM_SET_APPCONF:
		appconf = *app_get_configuration();

		ind = 0;
		appconf.timeout_msec = buffer_get_uint32(data, &ind);
		appconf.timeout_brake_current = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_to_use = data[ind++];

		appconf.app_ppm_ctrl_type = data[ind++];
		appconf.app_ppm_pid_max_erpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_hyst = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_pulse_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_pulse_width = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_rpm_lim_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_ppm_rpm_lim_end = (float)buffer_get_int32(data, &ind) / 1000.0;

		appconf.app_uart_baudrate = buffer_get_uint32(data, &ind);

		appconf.app_chuk_ctrl_type = data[ind++];
		appconf.app_chuk_hyst = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_chuk_rpm_lim_start = (float)buffer_get_int32(data, &ind) / 1000.0;
		appconf.app_chuk_rpm_lim_end = (float)buffer_get_int32(data, &ind) / 1000.0;

		conf_general_store_app_configuration(&appconf);
		app_set_configuration(&appconf);
		timeout_configure(appconf.timeout_msec, appconf.timeout_brake_current);
		break;

	case COMM_GET_APPCONF:
		appconf = *app_get_configuration();

		ind = 0;
		send_buffer[ind++] = COMM_GET_APPCONF;
		buffer_append_uint32(send_buffer, appconf.timeout_msec, &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.timeout_brake_current * 1000.0), &ind);
		send_buffer[ind++] = appconf.app_to_use;

		send_buffer[ind++] = appconf.app_ppm_ctrl_type;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_pid_max_erpm * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_hyst * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_pulse_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_pulse_width * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_rpm_lim_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_ppm_rpm_lim_end * 1000.0), &ind);

		buffer_append_uint32(send_buffer, appconf.app_uart_baudrate, &ind);

		send_buffer[ind++] = appconf.app_chuk_ctrl_type;
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_hyst * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_rpm_lim_start * 1000.0), &ind);
		buffer_append_int32(send_buffer, (int32_t)(appconf.app_chuk_rpm_lim_end * 1000.0), &ind);

		send_packet(send_buffer, ind);
		break;

	case COMM_SAMPLE_PRINT:
		ind = 0;
		at_start = data[ind++];
		sample_len = buffer_get_uint16(data, &ind);
		decimation = data[ind++];
		main_sample_print_data(at_start, sample_len, decimation);
		break;

	case COMM_TERMINAL_CMD:
		data[len] = '\0';
		terminal_process_string((char*)data);
		break;

	case COMM_DETECT_MOTOR_PARAM:
		ind = 0;
		detect_current = (float)buffer_get_int32(data, &ind) / 1000.0;
		detect_min_rpm = (float)buffer_get_int32(data, &ind) / 1000.0;
		detect_low_duty = (float)buffer_get_int32(data, &ind) / 1000.0;

		chEvtSignal(detect_tp, (eventmask_t) 1);
		break;

	case COMM_REBOOT:
		// Lock the system and enter an infinite loop. The watchdog will reboot.
		__disable_irq();
		for(;;){};
		break;

	case COMM_ALIVE:
		timeout_reset();
		break;

	case COMM_GET_DECODED_PPM:
		ind = 0;
		send_buffer[ind++] = COMM_GET_DECODED_PPM;
		buffer_append_int32(send_buffer, (int32_t)(servodec_get_servo(0) * 1000000.0), &ind);
		send_packet(send_buffer, ind);
		break;

	case COMM_GET_DECODED_CHUK:
		ind = 0;
		send_buffer[ind++] = COMM_GET_DECODED_CHUK;
		buffer_append_int32(send_buffer, (int32_t)(app_nunchuk_get_decoded_chuk() * 1000000.0), &ind);
		send_packet(send_buffer, ind);
		break;

	default:
		break;
	}
}