2014-04-13 22:10:01 -07:00
|
|
|
/*
|
2019-02-18 10:30:19 -08:00
|
|
|
Copyright 2016 - 2019 Benjamin Vedder benjamin@vedder.se
|
2014-04-13 22:10:01 -07:00
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
This file is part of the VESC firmware.
|
|
|
|
|
|
|
|
The VESC firmware is free software: you can redistribute it and/or modify
|
2014-04-13 22:10:01 -07:00
|
|
|
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.
|
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
The VESC firmware is distributed in the hope that it will be useful,
|
2014-04-13 22:10:01 -07:00
|
|
|
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/>.
|
2016-11-04 07:18:34 -07:00
|
|
|
*/
|
2014-04-13 22:10:01 -07:00
|
|
|
|
2014-04-18 15:09:46 -07:00
|
|
|
#include "app.h"
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
2014-10-18 16:00:53 -07:00
|
|
|
#include "hw.h"
|
2015-04-11 01:07:36 -07:00
|
|
|
#include "nrf_driver.h"
|
2016-02-09 03:23:58 -08:00
|
|
|
#include "rfhelp.h"
|
2017-11-08 05:07:35 -08:00
|
|
|
#include "comm_can.h"
|
2019-09-03 11:39:05 -07:00
|
|
|
#include "imu.h"
|
2020-09-13 22:12:11 -07:00
|
|
|
#include "crc.h"
|
2021-03-31 07:28:46 -07:00
|
|
|
#include "servo_simple.h"
|
2014-04-13 22:10:01 -07:00
|
|
|
|
2014-09-17 12:05:57 -07:00
|
|
|
// Private variables
|
2014-09-19 18:22:38 -07:00
|
|
|
static app_configuration appconf;
|
2019-02-18 10:30:19 -08:00
|
|
|
static virtual_timer_t output_vt;
|
|
|
|
static bool output_vt_init_done = false;
|
|
|
|
static volatile bool output_disabled_now = false;
|
|
|
|
|
|
|
|
// Private functions
|
|
|
|
static void output_vt_cb(void *arg);
|
2014-09-17 12:05:57 -07:00
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
const app_configuration* app_get_configuration(void) {
|
|
|
|
return &appconf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reconfigure and restart all apps. Some apps don't have any configuration options.
|
|
|
|
*
|
|
|
|
* @param conf
|
|
|
|
* The new configuration to use.
|
|
|
|
*/
|
|
|
|
void app_set_configuration(app_configuration *conf) {
|
|
|
|
appconf = *conf;
|
2014-09-17 12:05:57 -07:00
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
app_ppm_stop();
|
|
|
|
app_adc_stop();
|
2021-01-13 12:51:55 -08:00
|
|
|
app_uartcomm_stop(UART_PORT_COMM_HEADER);
|
2016-11-04 07:18:34 -07:00
|
|
|
app_nunchuk_stop();
|
2019-07-27 21:25:56 -07:00
|
|
|
app_balance_stop();
|
2020-12-07 11:00:25 -08:00
|
|
|
app_pas_stop();
|
2017-09-04 12:12:43 -07:00
|
|
|
|
|
|
|
if (!conf_general_permanent_nrf_found) {
|
|
|
|
nrf_driver_stop();
|
|
|
|
}
|
|
|
|
|
2017-11-08 05:07:35 -08:00
|
|
|
#if CAN_ENABLE
|
|
|
|
comm_can_set_baud(conf->can_baud_rate);
|
|
|
|
#endif
|
|
|
|
|
2017-09-04 12:12:43 -07:00
|
|
|
#ifdef APP_CUSTOM_TO_USE
|
|
|
|
app_custom_stop();
|
2016-06-27 08:29:09 -07:00
|
|
|
#endif
|
|
|
|
|
2019-09-03 11:39:05 -07:00
|
|
|
imu_init(&conf->imu_conf);
|
2019-08-25 23:49:33 -07:00
|
|
|
|
2021-03-31 07:28:46 -07:00
|
|
|
if (appconf.app_to_use != APP_PPM &&
|
|
|
|
appconf.app_to_use != APP_PPM_UART &&
|
|
|
|
appconf.servo_out_enable) {
|
|
|
|
servo_simple_init();
|
|
|
|
} else {
|
|
|
|
servo_simple_stop();
|
|
|
|
}
|
|
|
|
|
2019-08-10 21:52:21 -07:00
|
|
|
// Configure balance app before starting it.
|
2019-09-08 22:38:09 -07:00
|
|
|
app_balance_configure(&appconf.app_balance_conf, &appconf.imu_conf);
|
2019-08-10 21:52:21 -07:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
switch (appconf.app_to_use) {
|
2014-09-17 12:05:57 -07:00
|
|
|
case APP_PPM:
|
|
|
|
app_ppm_start();
|
|
|
|
break;
|
|
|
|
|
2015-05-04 14:25:43 -07:00
|
|
|
case APP_ADC:
|
2015-08-23 09:26:05 -07:00
|
|
|
app_adc_start(true);
|
2015-05-04 14:25:43 -07:00
|
|
|
break;
|
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
case APP_UART:
|
2014-10-19 10:31:06 -07:00
|
|
|
hw_stop_i2c();
|
2021-01-13 12:51:55 -08:00
|
|
|
app_uartcomm_start(UART_PORT_COMM_HEADER);
|
2014-09-19 18:22:38 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case APP_PPM_UART:
|
2021-04-12 00:48:29 -07:00
|
|
|
hw_stop_i2c();
|
|
|
|
app_ppm_start();
|
|
|
|
app_uartcomm_start(UART_PORT_COMM_HEADER);
|
2014-09-17 12:05:57 -07:00
|
|
|
break;
|
|
|
|
|
2015-05-04 14:25:43 -07:00
|
|
|
case APP_ADC_UART:
|
2021-04-12 00:48:29 -07:00
|
|
|
hw_stop_i2c();
|
2015-08-23 09:26:05 -07:00
|
|
|
app_adc_start(false);
|
2021-01-13 12:51:55 -08:00
|
|
|
app_uartcomm_start(UART_PORT_COMM_HEADER);
|
2015-05-04 14:25:43 -07:00
|
|
|
break;
|
|
|
|
|
2014-10-18 16:00:53 -07:00
|
|
|
case APP_NUNCHUK:
|
|
|
|
app_nunchuk_start();
|
|
|
|
break;
|
|
|
|
|
2019-07-27 21:25:56 -07:00
|
|
|
case APP_BALANCE:
|
|
|
|
app_balance_start();
|
2019-09-21 00:07:20 -07:00
|
|
|
if(appconf.imu_conf.type == IMU_TYPE_INTERNAL){
|
|
|
|
hw_stop_i2c();
|
2021-01-13 12:51:55 -08:00
|
|
|
app_uartcomm_start(UART_PORT_COMM_HEADER);
|
2019-09-21 00:07:20 -07:00
|
|
|
}
|
2019-07-27 21:25:56 -07:00
|
|
|
break;
|
|
|
|
|
2020-12-07 11:00:25 -08:00
|
|
|
case APP_PAS:
|
|
|
|
app_pas_start(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case APP_ADC_PAS:
|
2021-11-19 11:21:39 -08:00
|
|
|
app_adc_start(false);
|
2020-12-07 11:00:25 -08:00
|
|
|
app_pas_start(false);
|
|
|
|
break;
|
|
|
|
|
2015-04-11 01:07:36 -07:00
|
|
|
case APP_NRF:
|
2017-09-04 12:12:43 -07:00
|
|
|
if (!conf_general_permanent_nrf_found) {
|
|
|
|
nrf_driver_init();
|
|
|
|
rfhelp_restart();
|
|
|
|
}
|
2015-04-11 01:07:36 -07:00
|
|
|
break;
|
|
|
|
|
2014-09-17 12:05:57 -07:00
|
|
|
case APP_CUSTOM:
|
2017-09-04 12:12:43 -07:00
|
|
|
#ifdef APP_CUSTOM_TO_USE
|
2021-04-12 00:48:29 -07:00
|
|
|
hw_stop_i2c();
|
|
|
|
app_custom_start();
|
2014-07-19 02:41:16 -07:00
|
|
|
#endif
|
2014-09-17 12:05:57 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
app_ppm_configure(&appconf.app_ppm_conf);
|
2015-05-04 14:25:43 -07:00
|
|
|
app_adc_configure(&appconf.app_adc_conf);
|
2020-12-07 11:00:25 -08:00
|
|
|
app_pas_configure(&appconf.app_pas_conf);
|
2021-01-13 12:51:55 -08:00
|
|
|
app_uartcomm_configure(appconf.app_uart_baudrate, true, UART_PORT_COMM_HEADER);
|
|
|
|
app_uartcomm_configure(0, appconf.permanent_uart_enabled, UART_PORT_BUILTIN);
|
2015-02-19 12:20:07 -08:00
|
|
|
app_nunchuk_configure(&appconf.app_chuk_conf);
|
2017-09-04 12:12:43 -07:00
|
|
|
|
|
|
|
#ifdef APP_CUSTOM_TO_USE
|
|
|
|
app_custom_configure(&appconf);
|
|
|
|
#endif
|
|
|
|
|
2016-02-09 03:23:58 -08:00
|
|
|
rfhelp_update_conf(&appconf.app_nrf_conf);
|
2014-04-18 15:09:46 -07:00
|
|
|
}
|
2019-02-18 10:30:19 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable output on apps
|
|
|
|
*
|
|
|
|
* @param time_ms
|
|
|
|
* The amount of time to disable output in ms
|
|
|
|
* 0: Enable output now
|
|
|
|
* -1: Disable forever
|
|
|
|
* >0: Amount of milliseconds to disable output
|
|
|
|
*/
|
|
|
|
void app_disable_output(int time_ms) {
|
|
|
|
if (!output_vt_init_done) {
|
|
|
|
chVTObjectInit(&output_vt);
|
|
|
|
output_vt_init_done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_ms == 0) {
|
|
|
|
output_disabled_now = false;
|
|
|
|
} else if (time_ms == -1) {
|
|
|
|
output_disabled_now = true;
|
|
|
|
chVTReset(&output_vt);
|
|
|
|
} else {
|
|
|
|
output_disabled_now = true;
|
|
|
|
chVTSet(&output_vt, MS2ST(time_ms), output_vt_cb, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool app_is_output_disabled(void) {
|
|
|
|
return output_disabled_now;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void output_vt_cb(void *arg) {
|
|
|
|
(void)arg;
|
|
|
|
output_disabled_now = false;
|
|
|
|
}
|
2020-09-13 22:12:11 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get app_configuration CRC
|
|
|
|
*
|
|
|
|
* @param conf
|
|
|
|
* Pointer to app_configuration or NULL for current appconf
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* CRC16 (with crc field in struct temporarily set to zero).
|
|
|
|
*/
|
|
|
|
unsigned app_calc_crc(app_configuration* conf) {
|
|
|
|
if(NULL == conf)
|
|
|
|
conf = &appconf;
|
|
|
|
|
|
|
|
unsigned crc_old = conf->crc;
|
|
|
|
conf->crc = 0;
|
|
|
|
unsigned crc_new = crc16((uint8_t*)conf, sizeof(app_configuration));
|
|
|
|
conf->crc = crc_old;
|
|
|
|
return crc_new;
|
|
|
|
}
|