Skip to content

Commit

Permalink
FW 1.13: Hw 4.8 support, new CC implementation for nunchuk, refactori…
Browse files Browse the repository at this point in the history
…ng, custom app and hw config support
  • Loading branch information
vedderb committed Sep 17, 2015
1 parent c3e82f7 commit 06129bb
Show file tree
Hide file tree
Showing 19 changed files with 1,063 additions and 313 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ INCDIR = $(PORTINC) $(KERNINC) \
$(HALINC) $(PLATFORMINC) $(BOARDINC) \
$(CHIBIOS)/os/various \
mcconf \
appconf \
$(HWINC) \
$(APPINC) \
$(NRFINC)
Expand Down
166 changes: 166 additions & 0 deletions appconf/appconf_default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
Copyright 2015 Benjamin Vedder [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef APPCONF_DEFAULT_H_
#define APPCONF_DEFAULT_H_

// Default app configuration
#ifndef APPCONF_CONTROLLER_ID
#define APPCONF_CONTROLLER_ID 0
#endif
#ifndef APPCONF_TIMEOUT_MSEC
#define APPCONF_TIMEOUT_MSEC 1000
#endif
#ifndef APPCONF_TIMEOUT_BRAKE_CURRENT
#define APPCONF_TIMEOUT_BRAKE_CURRENT 0.0
#endif
#ifndef APPCONF_SEND_CAN_STATUS
#define APPCONF_SEND_CAN_STATUS false
#endif
#ifndef APPCONF_SEND_CAN_STATUS_RATE_HZ
#define APPCONF_SEND_CAN_STATUS_RATE_HZ 500
#endif

// The default app is UART in case the UART port is used for
// firmware updates.
#ifndef APPCONF_APP_TO_USE
#define APPCONF_APP_TO_USE APP_UART
#endif

// PPM app
#ifndef APPCONF_PPM_CTRL_TYPE
#define APPCONF_PPM_CTRL_TYPE PPM_CTRL_TYPE_NONE
#endif
#ifndef APPCONF_PPM_PID_MAX_ERPM
#define APPCONF_PPM_PID_MAX_ERPM 15000
#endif
#ifndef APPCONF_PPM_HYST
#define APPCONF_PPM_HYST 0.15
#endif
#ifndef APPCONF_PPM_PULSE_START
#define APPCONF_PPM_PULSE_START 1.0
#endif
#ifndef APPCONF_PPM_PULSE_END
#define APPCONF_PPM_PULSE_END 2.0
#endif
#ifndef APPCONF_PPM_MEDIAN_FILTER
#define APPCONF_PPM_MEDIAN_FILTER true
#endif
#ifndef APPCONF_PPM_SAFE_START
#define APPCONF_PPM_SAFE_START true
#endif
#ifndef APPCONF_PPM_RPM_LIM_START
#define APPCONF_PPM_RPM_LIM_START 150000
#endif
#ifndef APPCONF_PPM_RPM_LIM_END
#define APPCONF_PPM_RPM_LIM_END 200000
#endif
#ifndef APPCONF_PPM_MULTI_ESC
#define APPCONF_PPM_MULTI_ESC false
#endif
#ifndef APPCONF_PPM_TC
#define APPCONF_PPM_TC false
#endif
#ifndef APPCONF_PPM_TC_MAX_DIFF
#define APPCONF_PPM_TC_MAX_DIFF 3000.0
#endif

// ADC app configureation
#ifndef APPCONF_ADC_CTRL_TYPE
#define APPCONF_ADC_CTRL_TYPE ADC_CTRL_TYPE_NONE
#endif
#ifndef APPCONF_ADC_HYST
#define APPCONF_ADC_HYST 0.15
#endif
#ifndef APPCONF_ADC_VOLTAGE_START
#define APPCONF_ADC_VOLTAGE_START 0.9
#endif
#ifndef APPCONF_ADC_VOLTAGE_END
#define APPCONF_ADC_VOLTAGE_END 3.0
#endif
#ifndef APPCONF_ADC_USE_FILTER
#define APPCONF_ADC_USE_FILTER true
#endif
#ifndef APPCONF_ADC_SAFE_START
#define APPCONF_ADC_SAFE_START true
#endif
#ifndef APPCONF_ADC_CC_BUTTON_INVERTED
#define APPCONF_ADC_CC_BUTTON_INVERTED false
#endif
#ifndef APPCONF_ADC_REV_BUTTON_INVERTED
#define APPCONF_ADC_REV_BUTTON_INVERTED false
#endif
#ifndef APPCONF_ADC_VOLTAGE_INVERTED
#define APPCONF_ADC_VOLTAGE_INVERTED false
#endif
#ifndef APPCONF_ADC_RPM_LIM_START
#define APPCONF_ADC_RPM_LIM_START 150000.0
#endif
#ifndef APPCONF_ADC_RPM_LIM_END
#define APPCONF_ADC_RPM_LIM_END 200000.0
#endif
#ifndef APPCONF_ADC_MULTI_ESC
#define APPCONF_ADC_MULTI_ESC false
#endif
#ifndef APPCONF_ADC_TC
#define APPCONF_ADC_TC false
#endif
#ifndef APPCONF_ADC_TC_MAX_DIFF
#define APPCONF_ADC_TC_MAX_DIFF 3000.0
#endif
#ifndef APPCONF_ADC_UPDATE_RATE_HZ
#define APPCONF_ADC_UPDATE_RATE_HZ 500
#endif

// UART app
#ifndef APPCONF_UART_BAUDRATE
#define APPCONF_UART_BAUDRATE 115200
#endif

// Nunchuk app
#ifndef APPCONF_CHUK_CTRL_TYPE
#define APPCONF_CHUK_CTRL_TYPE CHUK_CTRL_TYPE_CURRENT
#endif
#ifndef APPCONF_CHUK_HYST
#define APPCONF_CHUK_HYST 0.15
#endif
#ifndef APPCONF_CHUK_RPM_LIM_START
#define APPCONF_CHUK_RPM_LIM_START 150000.0
#endif
#ifndef APPCONF_CHUK_RPM_LIM_END
#define APPCONF_CHUK_RPM_LIM_END 200000.0
#endif
#ifndef APPCONF_CHUK_RAMP_TIME_POS
#define APPCONF_CHUK_RAMP_TIME_POS 0.9
#endif
#ifndef APPCONF_CHUK_RAMP_TIME_NEG
#define APPCONF_CHUK_RAMP_TIME_NEG 0.3
#endif
#ifndef APPCONF_STICK_ERPM_PER_S_IN_CC
#define APPCONF_STICK_ERPM_PER_S_IN_CC 3000.0
#endif
#ifndef APPCONF_CHUK_MULTI_ESC
#define APPCONF_CHUK_MULTI_ESC false
#endif
#ifndef APPCONF_CHUK_TC
#define APPCONF_CHUK_TC false
#endif
#ifndef APPCONF_CHUK_TC_MAX_DIFF
#define APPCONF_CHUK_TC_MAX_DIFF 3000.0
#endif

#endif /* APPCONF_DEFAULT_H_ */
32 changes: 32 additions & 0 deletions appconf/appconf_example_ppm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2015 Benjamin Vedder [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* appconf_example_ppm.h
*
* Created on: 9 sep 2015
* Author: benjamin
*/

#ifndef APPCONF_APPCONF_EXAMPLE_PPM_H_
#define APPCONF_APPCONF_EXAMPLE_PPM_H_

// Override app configuration parameters such that the PPM app becomes default
#define APPCONF_APP_TO_USE APP_PPM
#define APPCONF_PPM_CTRL_TYPE PPM_CTRL_TYPE_CURRENT_NOREV_BRAKE

#endif /* APPCONF_APPCONF_EXAMPLE_PPM_H_ */
34 changes: 28 additions & 6 deletions applications/app_nunchuk.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ static msg_t output_thread(void *arg) {

// If c is pressed and no throttle is used, maintain the current speed with PID control
static bool was_pid = false;
const volatile mc_configuration *mcconf = mcpwm_get_configuration();

// Filter RPM to avoid glitches
static float filter_buffer[RPM_FILTER_SAMPLES];
Expand All @@ -246,18 +247,40 @@ static msg_t output_thread(void *arg) {
}
rpm_filtered /= RPM_FILTER_SAMPLES;

if (chuck_d.bt_c && out_val == 0.0) {
if (chuck_d.bt_c) {
static float pid_rpm = 0.0;

if (!was_pid) {
was_pid = true;
pid_rpm = rpm_filtered;
}

if ((is_reverse && pid_rpm < 0.0) || (!is_reverse && pid_rpm > 0.0)) {
mcpwm_set_pid_speed(pid_rpm);
if ((is_reverse && pid_rpm > 0.0) || (!is_reverse && pid_rpm < 0.0)) {
if (fabsf(pid_rpm) > mcconf->s_pid_min_erpm) {
// Abort if the speed is too high in the opposite direction
continue;
} else {
pid_rpm = 0.0;
}
}

was_pid = true;
} else {
if (is_reverse) {
if (pid_rpm > 0.0) {
pid_rpm = 0.0;
}

pid_rpm -= (out_val * config.stick_erpm_per_s_in_cc) / ((float)OUTPUT_ITERATION_TIME_MS * 1000.0);
} else {
if (pid_rpm < 0.0) {
pid_rpm = 0.0;
}

pid_rpm += (out_val * config.stick_erpm_per_s_in_cc) / ((float)OUTPUT_ITERATION_TIME_MS * 1000.0);
}
}

mcpwm_set_pid_speed(pid_rpm);

// Send the same duty cycle to the other controllers
if (config.multi_esc) {
float duty = mcpwm_get_duty_cycle_now();
Expand All @@ -281,7 +304,6 @@ static msg_t output_thread(void *arg) {
was_pid = false;

float current = 0;
const volatile mc_configuration *mcconf = mcpwm_get_configuration();

if (out_val >= 0.0) {
current = out_val * mcconf->l_current_max;
Expand Down
28 changes: 15 additions & 13 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void commands_process_packet(unsigned char *data, unsigned int len) {
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.s_pid_min_erpm = (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;
Expand Down Expand Up @@ -348,7 +348,7 @@ void commands_process_packet(unsigned char *data, unsigned int len) {
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.s_pid_min_erpm * 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);
Expand Down Expand Up @@ -411,14 +411,15 @@ void commands_process_packet(unsigned char *data, unsigned int len) {
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.hyst = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.rpm_lim_start = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.rpm_lim_end = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.ramp_time_pos = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.ramp_time_neg = buffer_get_float32(data, 1000.0, &ind);
appconf.app_chuk_conf.stick_erpm_per_s_in_cc = buffer_get_float32(data, 1000.0, &ind);
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;
appconf.app_chuk_conf.tc_max_diff = buffer_get_float32(data, 1000.0, &ind);

conf_general_store_app_configuration(&appconf);
app_set_configuration(&appconf);
Expand Down Expand Up @@ -474,11 +475,12 @@ void commands_process_packet(unsigned char *data, unsigned int len) {
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);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.hyst, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.rpm_lim_start, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.rpm_lim_end, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.ramp_time_pos, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.ramp_time_neg, 1000.0, &ind);
buffer_append_float32(send_buffer, appconf.app_chuk_conf.stick_erpm_per_s_in_cc, 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);
Expand Down
Loading

0 comments on commit 06129bb

Please sign in to comment.