From 53009258622fa6d4158ea94a023173c0da19a757 Mon Sep 17 00:00:00 2001 From: casainho Date: Fri, 8 May 2020 16:56:47 +0100 Subject: [PATCH] Added torque sensor filter calibration. SW102: added Bluetooth UART that sends some variables. --- .../SW102/.settings/language.settings.xml | 37 +++++--- firmware/SW102/SW102-Flash_Debug.launch | 2 +- firmware/SW102/SW102-full-Flash_Debug.launch | 35 ++++++++ firmware/SW102/include/ble_services.h | 3 + firmware/SW102/include/sdk_config.h | 6 +- firmware/SW102/src/sw102/ble_services.c | 85 +++++++++++-------- firmware/common/include/configscreen.h | 1 + firmware/common/include/eeprom.h | 58 +++++++------ firmware/common/include/state.h | 2 + firmware/common/include/uart.h | 2 +- firmware/common/src/configscreen.c | 6 +- firmware/common/src/eeprom.c | 5 ++ firmware/common/src/mainscreen.c | 46 +++++++--- firmware/common/src/state.c | 11 ++- 14 files changed, 204 insertions(+), 95 deletions(-) create mode 100644 firmware/SW102/SW102-full-Flash_Debug.launch diff --git a/firmware/SW102/.settings/language.settings.xml b/firmware/SW102/.settings/language.settings.xml index 05cf8d18..5c4ab7c4 100755 --- a/firmware/SW102/.settings/language.settings.xml +++ b/firmware/SW102/.settings/language.settings.xml @@ -1,15 +1,28 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/firmware/SW102/SW102-Flash_Debug.launch b/firmware/SW102/SW102-Flash_Debug.launch index a4124e87..f1a0f4d1 100755 --- a/firmware/SW102/SW102-Flash_Debug.launch +++ b/firmware/SW102/SW102-Flash_Debug.launch @@ -16,7 +16,7 @@ - + diff --git a/firmware/SW102/SW102-full-Flash_Debug.launch b/firmware/SW102/SW102-full-Flash_Debug.launch new file mode 100644 index 00000000..677f1436 --- /dev/null +++ b/firmware/SW102/SW102-full-Flash_Debug.launch @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/firmware/SW102/include/ble_services.h b/firmware/SW102/include/ble_services.h index be46cbf6..68893136 100755 --- a/firmware/SW102/include/ble_services.h +++ b/firmware/SW102/include/ble_services.h @@ -2,6 +2,9 @@ #ifndef INCLUDE_BLE_SERVICES_H_ #define INCLUDE_BLE_SERVICES_H_ +#include "state.h" + void ble_init(void); +void send_bluetooth(rt_vars_t *rt_vars); #endif /* INCLUDE_BLE_SERVICES_H_ */ diff --git a/firmware/SW102/include/sdk_config.h b/firmware/SW102/include/sdk_config.h index d6ce6112..f576e6c6 100755 --- a/firmware/SW102/include/sdk_config.h +++ b/firmware/SW102/include/sdk_config.h @@ -108,14 +108,14 @@ #ifndef BLE_BAS_ENABLED -#define BLE_BAS_ENABLED 1 +#define BLE_BAS_ENABLED 0 #endif // BLE_CSCS_ENABLED - ble_cscs - Cycling Speed and Cadence Service #ifndef BLE_CSCS_ENABLED -#define BLE_CSCS_ENABLED 1 +#define BLE_CSCS_ENABLED 0 #endif // BLE_CTS_C_ENABLED - ble_cts_c - Current Time Service Client @@ -129,7 +129,7 @@ #ifndef BLE_DIS_ENABLED -#define BLE_DIS_ENABLED 1 +#define BLE_DIS_ENABLED 0 #endif // BLE_GLS_ENABLED - ble_gls - Glucose Service diff --git a/firmware/SW102/src/sw102/ble_services.c b/firmware/SW102/src/sw102/ble_services.c index 3307518f..40aa2f3d 100755 --- a/firmware/SW102/src/sw102/ble_services.c +++ b/firmware/SW102/src/sw102/ble_services.c @@ -3,6 +3,7 @@ * * Released under the GPL License, Version 3 */ +#include #include "common.h" #include "fstorage.h" #include "ble_services.h" @@ -19,13 +20,14 @@ #include "ble_dis.h" #include "fds.h" #include "state.h" +#include "ble_conn_state.h" -// define to enable the (not yet used) serial service -// #define BLE_SERIAL +// define to enable the serial service +#define BLE_SERIAL // define to able reporting speed and cadence via bluetooth -#define BLE_CSC +//#define BLE_CSC // define to enable reporting battery SOC via bluetooth -#define BLE_BAS +//#define BLE_BAS #define IS_SRVC_CHANGED_CHARACT_PRESENT 0 /**< Include the service_changed characteristic. If not enabled, the server's database cannot be changed for the lifetime of the device. */ @@ -41,7 +43,7 @@ #define DEVICE_NAME "OS-EBike" /**< Name of device. Will be included in the advertising data. */ #define MANUFACTURER_NAME "https://github.com/OpenSource-EBike-firmware" -#define APP_ADV_INTERVAL 40 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 100 ms). */ +#define APP_ADV_INTERVAL 64 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 100 ms). */ #define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout (in units of seconds). */ #define MIN_CONN_INTERVAL MSEC_TO_UNITS(500, UNIT_1_25_MS) /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */ @@ -73,8 +75,6 @@ static ble_uuid_t m_adv_uuids[] = { #endif }; /**< Universally unique service identifier. */ - - /**@brief Function for the GAP initialization. * * @details This function will set up all the necessary GAP (Generic Access Profile) parameters of @@ -89,7 +89,7 @@ static void gap_params_init(void) APP_ERROR_CHECK(sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) DEVICE_NAME, strlen(DEVICE_NAME))); - APP_ERROR_CHECK(sd_ble_gap_appearance_set(BLE_APPEARANCE_GENERIC_CYCLING)); +// APP_ERROR_CHECK(sd_ble_gap_appearance_set(BLE_APPEARANCE_GENERIC_CYCLING)); memset(&gap_conn_params, 0, sizeof(gap_conn_params)); @@ -393,16 +393,16 @@ static void services_init(void) bas_init(); #endif - // Initialize Device Information Service. - ble_dis_init_t dis_init; - memset(&dis_init, 0, sizeof(dis_init)); - - ble_srv_ascii_to_utf8(&dis_init.manufact_name_str, MANUFACTURER_NAME); - - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&dis_init.dis_attr_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&dis_init.dis_attr_md.write_perm); - - APP_ERROR_CHECK(ble_dis_init(&dis_init)); +// // Initialize Device Information Service. +// ble_dis_init_t dis_init; +// memset(&dis_init, 0, sizeof(dis_init)); +// +// ble_srv_ascii_to_utf8(&dis_init.manufact_name_str, MANUFACTURER_NAME); +// +// BLE_GAP_CONN_SEC_MODE_SET_OPEN(&dis_init.dis_attr_md.read_perm); +// BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&dis_init.dis_attr_md.write_perm); +// +// APP_ERROR_CHECK(ble_dis_init(&dis_init)); } @@ -461,8 +461,6 @@ static void conn_params_init(void) cp_init.error_handler = conn_params_error_handler; APP_ERROR_CHECK(ble_conn_params_init(&cp_init)); - - APP_ERROR_CHECK(ble_advertising_start(BLE_ADV_MODE_FAST)); } @@ -503,8 +501,6 @@ static void on_ble_evt(ble_evt_t * p_ble_evt) break; // BLE_GAP_EVT_DISCONNECTED case BLE_GAP_EVT_SEC_PARAMS_REQUEST: - // Pairing not supported - sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL); break; // BLE_GAP_EVT_SEC_PARAMS_REQUEST case BLE_GATTS_EVT_SYS_ATTR_MISSING: @@ -576,12 +572,14 @@ static void on_ble_evt(ble_evt_t * p_ble_evt) */ static void ble_evt_dispatch(ble_evt_t * p_ble_evt) { - ble_conn_params_on_ble_evt(p_ble_evt); + ble_conn_state_on_ble_evt(p_ble_evt); + pm_on_ble_evt(p_ble_evt); + ble_conn_params_on_ble_evt(p_ble_evt); #ifdef BLE_SERIAL - ble_nus_on_ble_evt(&m_nus, p_ble_evt); + ble_nus_on_ble_evt(&m_nus, p_ble_evt); #endif - on_ble_evt(p_ble_evt); - ble_advertising_on_ble_evt(p_ble_evt); + on_ble_evt(p_ble_evt); + ble_advertising_on_ble_evt(p_ble_evt); } @@ -619,16 +617,18 @@ static void ble_stack_init(void) SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL); ble_enable_params_t ble_enable_params; - softdevice_enable_get_default_config(CENTRAL_LINK_COUNT, PERIPHERAL_LINK_COUNT, &ble_enable_params); + softdevice_enable_get_default_config(CENTRAL_LINK_COUNT, + PERIPHERAL_LINK_COUNT, + &ble_enable_params); //Check the ram settings against the used number of links - CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT); + CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT, PERIPHERAL_LINK_COUNT); // Enable BLE stack. #if (NRF_SD_BLE_API_VERSION == 3) ble_enable_params.gatt_enable_params.att_mtu = NRF_BLE_MAX_MTU_SIZE; #endif - ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT; +// ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT; APP_ERROR_CHECK(softdevice_enable(&ble_enable_params)); // Subscribe for BLE events. @@ -650,7 +650,7 @@ static void advertising_init(void) // Build advertising data struct to pass into @ref ble_advertising_init. memset(&advdata, 0, sizeof(advdata)); advdata.name_type = BLE_ADVDATA_FULL_NAME; - advdata.include_appearance = true; + advdata.include_appearance = false; advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE; memset(&scanrsp, 0, sizeof(scanrsp)); @@ -672,13 +672,13 @@ static void peer_manager_event_handler(pm_evt_t const *p_evt) { case PM_EVT_BONDED_PEER_CONNECTED: // Update the rank of the peer. - err_code = pm_peer_rank_highest(p_evt->peer_id); +// err_code = pm_peer_rank_highest(p_evt->peer_id); break; case PM_EVT_CONN_SEC_START: break; case PM_EVT_CONN_SEC_SUCCEEDED: // Update the rank of the peer. - err_code = pm_peer_rank_highest(p_evt->peer_id); + ble_conn_state_role(p_evt->conn_handle); break; case PM_EVT_CONN_SEC_FAILED: // In some cases, when securing fails, it can be restarted directly. Sometimes it can be @@ -686,6 +686,9 @@ static void peer_manager_event_handler(pm_evt_t const *p_evt) // restarted until the link is disconnected and reconnected. Sometimes it is impossible // to secure the link, or the peer device does not support it. How to handle this error // is highly application-dependent. + m_conn_handle = BLE_CONN_HANDLE_INVALID; + err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); + APP_ERROR_CHECK(err_code); break; case PM_EVT_CONN_SEC_CONFIG_REQ: { @@ -727,6 +730,7 @@ static void peer_manager_event_handler(pm_evt_t const *p_evt) break; case PM_EVT_PEERS_DELETE_SUCCEEDED: // At this point it is safe to start advertising or scanning. + ble_advertising_start(BLE_ADV_MODE_FAST); break; case PM_EVT_PEERS_DELETE_FAILED: // Assert. @@ -753,7 +757,7 @@ static void peer_init() { APP_ERROR_CHECK(err_code); if (erase_bonds) { - pm_peers_delete(); + pm_peers_delete(); } ble_gap_sec_params_t sec_param; @@ -782,9 +786,22 @@ static void peer_init() { void ble_init(void) { ble_stack_init(); + peer_init(); gap_params_init(); services_init(); advertising_init(); conn_params_init(); - peer_init(); + APP_ERROR_CHECK(ble_advertising_start(BLE_ADV_MODE_FAST)); +} + +void send_bluetooth(rt_vars_t *rt_vars) { + static uint8_t data_array[BLE_NUS_MAX_DATA_LEN]; // 19 bytes max + + sprintf(data_array, "%d,%d,%d,%d\n", + rt_vars->ui16_adc_pedal_torque_sensor, // torque sensor RAW + rt_vars->ui8_adc_throttle, // position + rt_vars->ui8_pedal_weight_with_offset, // weight in kgs with offset + rt_vars->ui8_pedal_cadence); + + ble_nus_string_send(&m_nus, data_array, strlen(data_array)); } diff --git a/firmware/common/include/configscreen.h b/firmware/common/include/configscreen.h index 78e51824..b82d9294 100755 --- a/firmware/common/include/configscreen.h +++ b/firmware/common/include/configscreen.h @@ -6,3 +6,4 @@ extern Screen configScreen; extern uint8_t ui8_g_configuration_display_reset_to_defaults; extern uint32_t ui32_g_configuration_wh_100_percent; +extern uint8_t ui8_g_configuration_display_reset_bluetooth_peers; diff --git a/firmware/common/include/eeprom.h b/firmware/common/include/eeprom.h index a91716d9..bdfcfd61 100755 --- a/firmware/common/include/eeprom.h +++ b/firmware/common/include/eeprom.h @@ -15,8 +15,8 @@ // For compatible changes, just add new fields at the end of the table (they will be inited to 0xff for old eeprom images). For incompatible // changes bump up EEPROM_MIN_COMPAT_VERSION and the user's EEPROM settings will be discarded. -#define EEPROM_MIN_COMPAT_VERSION 0x38 -#define EEPROM_VERSION 0x38 +#define EEPROM_MIN_COMPAT_VERSION 0x39 +#define EEPROM_VERSION 0x39 typedef struct { graph_auto_max_min_t auto_max_min; @@ -149,6 +149,7 @@ typedef struct eeprom_data { uint8_t ui8_coast_brake_adc; uint8_t ui8_adc_lights_current_offset; uint8_t ui8_throttle_virtual_step; + uint8_t ui8_torque_sensor_filter; uint8_t ui8_coast_brake_enable; // FIXME align to 32 bit value by end of structure and pack other fields @@ -274,6 +275,7 @@ void eeprom_init_defaults(void); #define DEFAULT_FIELD_WEAKENING 1 // 1 enabled #define DEFAULT_ADC_LIGHTS_CURRENT_OFFSET 1 #define DEFAULT_THROTTLE_VIRTUAL_STEP 5 +#define DEFAULT_TORQUE_SENSOR_FILTER 20 // 20% #define DEFAULT_COAST_BRAKE_ENABLE 0 // disable #define BICYCLE_1 @@ -283,37 +285,37 @@ void eeprom_init_defaults(void); #define DEFAULT_TORQUE_SENSOR_CALIBRATION_FEATURE_ENABLE 0 // disabled #define DEFAULT_TORQUE_SENSOR_CALIBRATION_PEDAL_GROUND 0 // left pedal #define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_1 0 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_1 203 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_2 5 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_2 225 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_3 10 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_3 235 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_4 15 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_4 240 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_5 19 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_5 243 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_1 157 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_2 10 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_2 207 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_3 15 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_3 230 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_4 19 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_4 241 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_5 25 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_5 250 #define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_6 40 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_6 254 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_6 260 #define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_7 54 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_7 259 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_8 100 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_8 273 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_7 266 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_WEIGHT_8 105 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_LEFT_ADC_8 279 #define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_1 0 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_1 209 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_2 5 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_2 236 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_3 10 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_3 247 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_4 15 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_4 254 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_5 19 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_5 258 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_1 161 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_2 15 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_2 233 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_3 19 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_3 245 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_4 25 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_4 258 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_5 30 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_5 266 #define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_6 40 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_6 272 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_6 276 #define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_7 54 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_7 278 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_8 100 -#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_8 288 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_7 284 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_WEIGHT_8 105 +#define DEFAULT_TORQUE_SENSOR_CALIBRATION_RIGHT_ADC_8 300 #endif #ifdef BICYCLE_2 diff --git a/firmware/common/include/state.h b/firmware/common/include/state.h index dbc6e6b0..c38d45d3 100755 --- a/firmware/common/include/state.h +++ b/firmware/common/include/state.h @@ -138,6 +138,7 @@ typedef struct rt_vars_struct { uint8_t ui8_adc_lights_current_offset; uint16_t ui16_adc_battery_current; uint8_t ui8_throttle_virtual; + uint8_t ui8_torque_sensor_filter; uint8_t ui8_coast_brake_enable; battery_energy_h_km_t battery_energy_h_km; @@ -346,6 +347,7 @@ typedef struct ui_vars_struct { uint16_t ui16_adc_battery_current; uint8_t ui8_throttle_virtual; uint8_t ui8_throttle_virtual_step; + uint8_t ui8_torque_sensor_filter; uint8_t ui8_coast_brake_enable; } ui_vars_t; diff --git a/firmware/common/include/uart.h b/firmware/common/include/uart.h index 74089058..5b0c82b5 100755 --- a/firmware/common/include/uart.h +++ b/firmware/common/include/uart.h @@ -8,5 +8,5 @@ uint8_t* uart_get_tx_buffer(void); void uart_send_tx_buffer(uint8_t *tx_buffer, uint8_t ui8_len); #define UART_NUMBER_DATA_BYTES_TO_RECEIVE 29 -#define UART_NUMBER_DATA_BYTES_TO_SEND 86 +#define UART_NUMBER_DATA_BYTES_TO_SEND 87 diff --git a/firmware/common/src/configscreen.c b/firmware/common/src/configscreen.c index 874f1e33..065d1b8b 100755 --- a/firmware/common/src/configscreen.c +++ b/firmware/common/src/configscreen.c @@ -32,12 +32,13 @@ static Field motorMenus[] = { FIELD_EDITABLE_UINT(_S("Max current", "Max curren"), &ui_vars.ui8_motor_max_current, "amps", 1, 30), FIELD_EDITABLE_UINT(_S("Current ramp", "Curre ramp"), &ui_vars.ui8_ramp_up_amps_per_second_x10, "amps", 4, 100, .div_digits = 1), FIELD_EDITABLE_UINT(_S("Min current ADC step", "Min ADC st"), &ui_vars.ui8_motor_current_min_adc, "amps", 0, 13), // 13 ADC steps = 2 amps - FIELD_EDITABLE_ENUM(_S("Field weakening", "Field weak"), &ui_vars.ui8_torque_sensor_calibration_feature_enabled, "disable", "enable"), + FIELD_EDITABLE_ENUM(_S("Field weakening", "Field weak"), &ui_vars.ui8_field_weakening, "disable", "enable"), FIELD_END }; static Field torqueSensorMenus[] = { FIELD_EDITABLE_ENUM(_S("Calibration", "Calibrat"), &ui_vars.ui8_torque_sensor_calibration_feature_enabled, "disable", "enable"), + FIELD_EDITABLE_UINT(_S("Torque sensor filter", "Torq s fil"), &ui_vars.ui8_torque_sensor_filter, "", 0, 100), FIELD_EDITABLE_ENUM(_S("Start pedal ground", "Pedal grou"), &ui_vars.ui8_torque_sensor_calibration_pedal_ground, "left", "right"), FIELD_EDITABLE_UINT(_S("Left weight 1", "L weight 1"), &ui_vars.ui16_torque_sensor_calibration_table_left[0][0], "kg", 0, 200), FIELD_EDITABLE_UINT("Left ADC 1", &ui_vars.ui16_torque_sensor_calibration_table_left[0][1], "", 0, 1023), @@ -181,6 +182,8 @@ static Field displayMenus[] = FIELD_EDITABLE_ENUM("Units", &ui_vars.ui8_units_type, "SI", "Imperial"), #ifndef SW102 FIELD_READONLY_ENUM("LCD type", &g_lcd_ic_type, "ILI9481", "ST7796", "unknown"), +#else + FIELD_EDITABLE_ENUM(_S("Reset BLE connections", "Reset BLE"), &ui8_g_configuration_display_reset_bluetooth_peers, "no", "yes"), #endif FIELD_EDITABLE_ENUM(_S("Reset to defaults", "Reset def"), &ui8_g_configuration_display_reset_to_defaults, "no", "yes"), FIELD_END }; @@ -380,6 +383,7 @@ static Field configRoot = FIELD_SCROLLABLE(_S("Configurations", "Config"), topMe uint8_t ui8_g_configuration_display_reset_to_defaults = 0; uint32_t ui32_g_configuration_wh_100_percent = 0; +uint8_t ui8_g_configuration_display_reset_bluetooth_peers = 0; static void configScreenOnEnter() { // Set the font preference for this screen diff --git a/firmware/common/src/eeprom.c b/firmware/common/src/eeprom.c index 0aa68a74..1f039c9b 100755 --- a/firmware/common/src/eeprom.c +++ b/firmware/common/src/eeprom.c @@ -272,6 +272,7 @@ const eeprom_data_t m_eeprom_data_defaults = { .ui8_coast_brake_adc = DEFAULT_COAST_BRAKE_ADC, .ui8_adc_lights_current_offset = DEFAULT_ADC_LIGHTS_CURRENT_OFFSET, .ui8_throttle_virtual_step = DEFAULT_THROTTLE_VIRTUAL_STEP, + .ui8_torque_sensor_filter = DEFAULT_TORQUE_SENSOR_FILTER, .ui8_coast_brake_enable = DEFAULT_COAST_BRAKE_ENABLE, }; @@ -561,6 +562,8 @@ void eeprom_init_variables(void) { m_eeprom_data.ui8_adc_lights_current_offset; ui_vars->ui8_throttle_virtual_step = m_eeprom_data.ui8_throttle_virtual_step; + ui_vars->ui8_torque_sensor_filter = + m_eeprom_data.ui8_torque_sensor_filter; ui_vars->ui8_coast_brake_enable = m_eeprom_data.ui8_coast_brake_enable; } @@ -740,6 +743,8 @@ void eeprom_write_variables(void) { ui_vars->ui8_adc_lights_current_offset; m_eeprom_data.ui8_throttle_virtual_step = ui_vars->ui8_throttle_virtual_step; + m_eeprom_data.ui8_torque_sensor_filter = + ui_vars->ui8_torque_sensor_filter; m_eeprom_data.ui8_coast_brake_enable = ui_vars->ui8_coast_brake_enable; diff --git a/firmware/common/src/mainscreen.c b/firmware/common/src/mainscreen.c index 8fe444f4..05408eaa 100755 --- a/firmware/common/src/mainscreen.c +++ b/firmware/common/src/mainscreen.c @@ -24,6 +24,9 @@ #include "configscreen.h" #include "state.h" #include "timer.h" +#ifdef SW102 +#include "peer_manager.h" +#endif // only used on SW102, to count timeout to override the wheel speed value with assist level value static uint16_t m_assist_level_change_timeout = 0; @@ -43,7 +46,7 @@ uint8_t g_showNextScreenPreviousIndex = 0; uint16_t ui16_m_alternate_field_value; uint8_t ui8_m_alternate_field_state = 0; uint8_t ui8_m_alternate_field_timeout_cnt = 0; -uint8_t ui8_m_can_increment_decrement = 0; +uint8_t ui8_m_vthrottle_can_increment_decrement = 0; void lcd_main_screen(void); void warnings(void); @@ -57,6 +60,7 @@ void wheel_speed(void); void showNextScreen(); static bool renderWarning(FieldLayout *layout); void DisplayResetToDefaults(void); +void DisplayResetBluetoothPeers(void); void onSetConfigurationBatteryTotalWh(uint32_t v); void batteryTotalWh(void); void batteryCurrent(void); @@ -385,15 +389,15 @@ static bool onPressAlternateField(buttons_events_t events) { // start increment throttle only with UP_LONG_CLICK if ((ui8_m_alternate_field_state == 7) && (events & UP_LONG_CLICK) && - (ui8_m_can_increment_decrement == 0)) { - ui8_m_can_increment_decrement = 1; + (ui8_m_vthrottle_can_increment_decrement == 0)) { + ui8_m_vthrottle_can_increment_decrement = 1; events |= UP_CLICK; // let's increment, consider UP CLICK ui8_m_alternate_field_timeout_cnt = 50; // 50 * 20ms = 1 second } if (ui8_m_alternate_field_timeout_cnt == 0) { ui_vars.ui8_throttle_virtual = 0; - ui8_m_can_increment_decrement = 0; + ui8_m_vthrottle_can_increment_decrement = 0; } switch (ui8_m_alternate_field_state) { @@ -460,7 +464,7 @@ static bool onPressAlternateField(buttons_events_t events) { if (events & SCREENCLICK_ALTERNATE_FIELD_STOP) { ui_vars.ui8_throttle_virtual = 0; ui8_m_alternate_field_timeout_cnt = 0; - ui8_m_can_increment_decrement = 0; + ui8_m_vthrottle_can_increment_decrement = 0; ui8_m_alternate_field_state = 4; handled = true; break; @@ -469,7 +473,7 @@ static bool onPressAlternateField(buttons_events_t events) { if (events & UP_CLICK) { handled = true; - if (ui8_m_can_increment_decrement && + if (ui8_m_vthrottle_can_increment_decrement && ui_vars.ui8_assist_level) { if ((ui_vars.ui8_throttle_virtual + ui_vars.ui8_throttle_virtual_step) <= 100) { ui_vars.ui8_throttle_virtual += ui_vars.ui8_throttle_virtual_step; @@ -486,7 +490,7 @@ static bool onPressAlternateField(buttons_events_t events) { if (events & DOWN_CLICK) { handled = true; - if (ui8_m_can_increment_decrement && + if (ui8_m_vthrottle_can_increment_decrement && ui_vars.ui8_assist_level) { if (ui_vars.ui8_throttle_virtual >= ui_vars.ui8_throttle_virtual_step) { ui_vars.ui8_throttle_virtual -= ui_vars.ui8_throttle_virtual_step; @@ -699,6 +703,7 @@ void screen_clock(void) { clock_time(); #endif DisplayResetToDefaults(); + DisplayResetBluetoothPeers(); batteryTotalWh(); batteryCurrent(); batteryResistance(); @@ -964,7 +969,7 @@ void warnings(void) { } // High priorty faults in red - if(ui_vars.ui8_error_states) { + if (ui_vars.ui8_error_states) { if (ui_vars.ui8_error_states & 1) ui8_motorErrorsIndex = 1; else if (ui_vars.ui8_error_states & 2) @@ -989,20 +994,20 @@ void warnings(void) { return; } - if(motor_temp_limit && + if (motor_temp_limit && ui_vars.ui8_motor_temperature >= ui_vars.ui8_motor_temperature_max_value_to_limit) { setWarning(ColorError, _S("Temp Shutdown", "Temp Shut")); return; } // If we had a watchdog failure, show it forever - so user will report a bug - if(wd_failure_detected) { + if (wd_failure_detected) { setWarning(ColorError, "Report Bug!"); return; } // warn faults in yellow - if(motor_temp_limit && + if (motor_temp_limit && ui_vars.ui8_motor_temperature >= ui_vars.ui8_motor_temperature_min_value_to_limit) { setWarning(ColorWarning, _S("Temp Warning", "Temp Warn")); return; @@ -1010,7 +1015,7 @@ void warnings(void) { // All of the following possible 'faults' are low priority - if(ui_vars.ui8_braking) { + if (ui_vars.ui8_braking) { setWarning(ColorNormal, "BRAKE"); return; } @@ -1020,7 +1025,7 @@ void warnings(void) { return; } - if(ui_vars.ui8_lights) { + if (ui_vars.ui8_lights) { setWarning(ColorNormal, "LIGHT"); return; } @@ -1136,7 +1141,7 @@ static void handle_buttons() { if (ui8_m_alternate_field_timeout_cnt) { ui8_m_alternate_field_timeout_cnt--; } else { - ui8_m_can_increment_decrement = 0; + ui8_m_vthrottle_can_increment_decrement = 0; ui_vars.ui8_throttle_virtual = 0; } } else { @@ -1197,6 +1202,19 @@ void DisplayResetToDefaults(void) { } } +void DisplayResetBluetoothPeers(void) { +#ifdef SW102 + if (ui8_g_configuration_display_reset_bluetooth_peers) { + ui8_g_configuration_display_reset_bluetooth_peers = 0; + // TODO: fist disable any connection + // Warning: Use this (pm_peers_delete) function only when not connected or connectable. If a peer is or becomes connected + // or a PM_PEER_DATA_FUNCTIONS function is used during this procedure (until the success or failure event happens), + // the behavior is undefined. + pm_peers_delete(); + } +#endif +} + void batteryCurrent(void) { ui16_m_battery_current_filtered_x10 = ui_vars.ui16_battery_current_filtered_x5 * 2; diff --git a/firmware/common/src/state.c b/firmware/common/src/state.c index ee725f0d..39a09110 100755 --- a/firmware/common/src/state.c +++ b/firmware/common/src/state.c @@ -22,6 +22,9 @@ #include "adc.h" #include "timer.h" #include +#ifdef SW102 +#include "ble_services.h" +#endif //#define DEBUG_TSDZ2_FIRMWARE @@ -295,8 +298,9 @@ void rt_send_tx_package(frame_type_t type) { (rt_vars.ui8_coast_brake_enable << 2)); ui8_usart1_tx_buffer[81] = rt_vars.ui8_coast_brake_adc; ui8_usart1_tx_buffer[82] = rt_vars.ui8_adc_lights_current_offset; + ui8_usart1_tx_buffer[83] = rt_vars.ui8_torque_sensor_filter; - crc_len = 84; + crc_len = 85; ui8_usart1_tx_buffer[1] = crc_len; break; @@ -712,6 +716,7 @@ void copy_rt_to_ui_vars(void) { rt_vars.ui8_coast_brake_adc = ui_vars.ui8_coast_brake_adc; rt_vars.ui8_adc_lights_current_offset = ui_vars.ui8_adc_lights_current_offset; rt_vars.ui8_throttle_virtual = ui_vars.ui8_throttle_virtual; + rt_vars.ui8_torque_sensor_filter = ui_vars.ui8_torque_sensor_filter; rt_vars.ui8_coast_brake_enable = ui_vars.ui8_coast_brake_enable; } @@ -852,6 +857,10 @@ void rt_processing(void) { communications(); +#ifdef SW102 + send_bluetooth(&rt_vars); +#endif + // called here because this state machine for motor_init should run every 100ms // montor init processing must be done when exiting the configurations menu // once motor is initialized, this should take almost no processing time