Merge branch 'master' of github.com:EmanuelFeru/hoverboard-firmware-hack-FOC into github_org

pull/1/head
JbLb 4 years ago
commit 886a86b047

@ -0,0 +1,21 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: ''
---
### Your System
- VARIANT:
- CONTROL TYPE:
- CONTROL MODE:
- What is your application?
- Hovercar, Skateboard, RC platform, etc.
Describe the bug and how we can reproduce it.
```c
// a code sample may improve communication
```

@ -0,0 +1,14 @@
---
name: Idea / Feature request
about: Suggest an idea for this project
title: ''
labels: enhancement
assignees: ''
---
**What can we do to make the firmware better?**
Consider if code examples or images would help communicate your request.
**Describe suggestions or alternatives you have considered**
A clear and concise description of any alternative solutions or features you've considered.

@ -0,0 +1,26 @@
---
name: Question about the firmware
about: How to use the firmware to...
title: ''
labels: question
assignees: ''
---
### Your System
- VARIANT:
- CONTROL TYPE:
- CONTROL MODE:
- What is your application?
- Hovercar, Skateboard, RC platform, etc.
**Before asking a question** see if it is already answered in:
* [Wiki pages](https://github.com/EmanuelFeru/hoverboard-firmware-hack-FOC/wiki)
**Indicate what you have tried:**
If applicable, indicate what you tried that _doesn't_ work:
```c
input1[inIdx].raw = adc_buffer.l_rx2;
input2[inIdx].raw = adc_buffer.l_tx2
```

@ -48,8 +48,6 @@ jobs:
- name: platformio
script: platformio run
language: python
python:
- "2.7"
install:
- pip install -U platformio
- platformio update

@ -0,0 +1,113 @@
/**
* This file is part of the hoverboard-firmware-hack project.
*
* Copyright (C) 2020-2021 Emanuel FERU <aerdronix@gmail.com>
*
* 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/>.
*/
// Define to prevent recursive inclusion
#ifndef COMMS_H
#define COMMS_H
#include "stm32f1xx_hal.h"
#if defined(DEBUG_SERIAL_PROTOCOL)
enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT};
#define typename(x) _Generic((x), \
uint8_t: UINT8_T, \
uint16_t: UINT16_T, \
uint32_t: UINT32_T, \
int8_t: INT8_T, \
int16_t: INT16_T, \
int32_t: INT32_T, \
int: INT, \
float: FLOAT)
#define PARAM_SIZE(param) sizeof(param) / sizeof(parameter_entry)
#define COMMAND_SIZE(command) sizeof(command) / sizeof(command_entry)
#define SIZEP(x) ((char*)(&(x) + 1) - (char*)&(x))
#define ADD_PARAM(var) typename(var),&var
int32_t extToInt(uint8_t index,int32_t value);
int8_t setParamValInt(uint8_t index, int32_t newValue);
int8_t setParamValExt(uint8_t index, int32_t newValue);
int32_t intToExt(uint8_t index,int32_t value);
int32_t getParamValInt(uint8_t index);
int32_t getParamValExt(uint8_t index);
int8_t initParamVal(uint8_t index);
int8_t incrParamVal(uint8_t index);
int8_t saveAllParamVal();
int16_t getParamInitInt(uint8_t index);
int32_t getParamInitExt(uint8_t index);
int8_t printCommandHelp(uint8_t index);
int8_t printParamHelp(uint8_t index);
int8_t printAllParamHelp();
int8_t printParamVal();
int8_t printParamDef(uint8_t index);
int8_t printAllParamDef();
void printError(uint8_t errornum );
int8_t watchParamVal(uint8_t index);
int8_t findCommand(uint8_t *userCommand, uint32_t len);
int8_t findParam(uint8_t *userCommand, uint32_t len);
void handle_input(uint8_t *userCommand, uint32_t len);
void process_debug();
typedef struct debug_command_struct debug_command;
struct debug_command_struct {
uint8_t semaphore;
uint8_t error;
int8_t command_index;
int8_t param_index;
int32_t param_value;
};
typedef struct command_entry_struct command_entry;
struct command_entry_struct {
const uint8_t type;
const char *name;
int8_t (*callback_function0)();
int8_t (*callback_function1)(uint8_t index);
int8_t (*callback_function2)(uint8_t index,int32_t value);
const char *help;
};
typedef struct parameter_entry_struct parameter_entry;
struct parameter_entry_struct {
const uint8_t type;
const char *name;
const uint8_t datatype;
void *valueL;
void *valueR;
const uint16_t addr;
const int32_t init;
const uint8_t initFormat;
const int32_t min;
const int32_t max;
const uint8_t div;
const uint8_t mul;
const uint8_t fix;
void (*callback_function)();
const char *help;
};
#endif // DEBUG_SERIAL_PROTOCOL
#endif // COMMS_H

@ -59,7 +59,13 @@
#define ADC_TOTAL_CONV_TIME (ADC_CLOCK_DIV * ADC_CONV_CLOCK_CYCLES) // = ((SystemCoreClock / ADC_CLOCK_HZ) * ADC_CONV_CLOCK_CYCLES), where ADC_CLOCK_HZ = SystemCoreClock/ADC_CLOCK_DIV
// ########################### END OF DO-NOT-TOUCH SETTINGS ############################
// ############################### BOARD VARIANT ###############################
/* Board Variant
* 0 - Default board type
* 1 - Alternate board type with different pin mapping for DCLINK, Buzzer and ON/OFF, Button and Charger
*/
#define BOARD_VARIANT 0 // change if board with alternate pin mapping
// ######################## END OF BOARD VARIANT ###############################
// ############################### BATTERY ###############################
/* Battery voltage calibration: connect power source.
@ -121,7 +127,7 @@
* 3. If you re-calibrate the Field Weakening please take all the safety measures! The motors can spin very fast!
Inputs:
- cmd1 and cmd2: analog normalized input values. INPUT_MIN to INPUT_MAX
- input1[inIdx].cmd and input2[inIdx].cmd: normalized input values. INPUT_MIN to INPUT_MAX
- button1 and button2: digital input values. 0 or 1
- adc_buffer.l_tx2 and adc_buffer.l_rx2: unfiltered ADC values (you do not need them). 0 to 4095
Outputs:
@ -170,7 +176,10 @@
// Default settings will be applied at the end of this config file if not set before
#define INACTIVITY_TIMEOUT 8 // Minutes of not driving until poweroff. it is not very precise.
#define BEEPS_BACKWARD 1 // 0 or 1
#define FLASH_WRITE_KEY 0x1233 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#define ADC_MARGIN 100 // ADC input margin applied on the raw ADC min and max to make sure the MIN and MAX values are reached even in the presence of noise
#define ADC_PROTECT_TIMEOUT 100 // ADC Protection: number of wrong / missing input commands before safety state is taken
#define ADC_PROTECT_THRESH 200 // ADC Protection threshold below/above the MIN/MAX ADC values
#define AUTO_CALIBRATION_ENA // Enable/Disable input auto-calibration by holding power button pressed. Un-comment this if auto-calibration is not needed.
/* FILTER is in fixdt(0,16,16): VAL_fixedPoint = VAL_floatingPoint * 2^16. In this case 6553 = 0.1 * 2^16
* Value of COEFFICIENT is in fixdt(1,16,14)
@ -186,6 +195,24 @@
// ############################## INPUT FORMAT ############################
/* ***_INPUT: TYPE, MIN, MID, MAX, DEADBAND
* -----------------------------------------
* TYPE: 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
* MIN: min ADC1-value while poti at minimum-position (0 - 4095)
* MID: mid ADC1-value while poti at mid-position (INPUT_MIN - INPUT_MAX)
* MAX: max ADC2-value while poti at maximum-position (0 - 4095)
* DEADBAND: how much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
*
* Dual-inputs
* PRI_INPUT: Primary Input. These limits will be used for the input with priority 0
* AUX_INPUT: Auxiliary Input. These limits will be used for the input with priority 1
* -----------------------------------------
*/
// ############################## END OF INPUT FORMAT ############################
// ############################## CRUISE CONTROL SETTINGS ############################
/* Cruise Control info:
* enable CRUISE_CONTROL_SUPPORT and (SUPPORT_BUTTONS_LEFT or SUPPORT_BUTTONS_RIGHT depending on which cable is the button installed)
@ -208,13 +235,13 @@
* enable DEBUG_SERIAL_USART3 or DEBUG_SERIAL_USART2
*
*
* DEBUG_SERIAL_ASCII output is:
* DEBUG ASCII output is:
* // "in1:345 in2:1337 cmdL:0 cmdR:0 BatADC:0 BatV:0 TempADC:0 Temp:0\r\n"
*
* in1: (int16_t)input1); raw input1: ADC1, UART, PWM, PPM, iBUS
* in2: (int16_t)input2); raw input2: ADC2, UART, PWM, PPM, iBUS
* cmdL: (int16_t)speedL); output command: [-1000, 1000]
* cmdR: (int16_t)speedR); output command: [-1000, 1000]
* in1: (int16_t)input1[inIdx].raw); raw input1: ADC1, UART, PWM, PPM, iBUS
* in2: (int16_t)input2[inIdx].raw); raw input2: ADC2, UART, PWM, PPM, iBUS
* cmdL: (int16_t)cmdL); output command Left: [-1000, 1000]
* cmdR: (int16_t)cmdR); output command Right: [-1000, 1000]
* BatADC: (int16_t)adc_buffer.batt1); Battery adc-value measured by mainboard
* BatV: (int16_t)(batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC)); Battery calibrated voltage multiplied by 100 for verifying battery voltage calibration
* TempADC: (int16_t)board_temp_adcFilt); for board temperature calibration
@ -224,6 +251,7 @@
// #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used!
// #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
// #define DEBUG_SERIAL_PROTOCOL // uncomment this to send user commands to the board, change parameters and print specific signals (see comms.c for the user commands)
// ########################### END OF DEBUG SERIAL ############################
@ -251,26 +279,29 @@
* Procedure:
* - connect gnd, rx and tx of a usb-uart converter in 3.3V mode to the right sensor board cable (do NOT use the red 15V wire!)
* - readout values using a serial terminal in 115200 baud rate
* - turn the potis to minimum position, write value 1 to INPUT1_MIN and value 2 to INPUT2_MIN
* - turn the potis to maximum position, write value 1 to INPUT1_MAX and value 2 to INPUT2_MAX
* - for middle resting potis: Let the potis in the middle resting position, write value 1 to INPUT1_MID and value 2 to INPUT2_MID
* - turn the potis to minimum position, write value in1 to PRI_INPUT1 MIN and value in2 to PRI_INPUT2 MIN
* - turn the potis to maximum position, write value in1 to PRI_INPUT1 MAX and value in2 to PRI_INPUT2 MAX
* - for middle resting potis: Let the potis in the middle resting position, write value in1 to PRI_INPUT1 MID and value in2 to PRI_INPUT2 MID
*/
#define CONTROL_ADC // use ADC as input. disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2!
#define ADC_PROTECT_TIMEOUT 100 // ADC Protection: number of wrong / missing input commands before safety state is taken
#define ADC_PROTECT_THRESH 200 // ADC Protection threshold below/above the MIN/MAX ADC values
#define INPUT1_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT1_MIN 0 // min ADC1-value while poti at min-position (0 - 4095)
#define INPUT1_MID 0 // mid ADC1-value while poti at mid-position (INPUT1_MIN - INPUT1_MAX)
#define INPUT1_MAX 4095 // max ADC1-value while poti at max-position (0 - 4095)
#define INPUT1_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define INPUT2_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT2_MIN 0 // min ADC2-value while poti at min-position (0 - 4095)
#define INPUT2_MID 0 // mid ADC2-value while poti at mid-position (INPUT2_MIN - INPUT2_MAX)
#define INPUT2_MAX 4095 // max ADC2-value while poti at max-position (0 - 4095)
#define INPUT2_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
#define CONTROL_ADC 0 // use ADC as input. Number indicates priority for dual-input. Disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2!
// #define DUAL_INPUTS // ADC*(Primary) + UART(Auxiliary). Uncomment this to use Dual-inputs
#define PRI_INPUT1 3, 0, 0, 4095, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 3, 0, 0, 4095, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#ifdef DUAL_INPUTS
#define FLASH_WRITE_KEY 0x1101 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
// #define SIDEBOARD_SERIAL_USART3 1
#define CONTROL_SERIAL_USART3 1 // right sensor board cable. Number indicates priority for dual-input. Disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino
#define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
#define AUX_INPUT1 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT2 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#else
#define FLASH_WRITE_KEY 0x1001 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
#endif
// #define ADC_ALTERNATE_CONNECT // use to swap ADC inputs
// #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2!
// #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3!
#endif
@ -280,25 +311,28 @@
// ############################ VARIANT_USART SETTINGS ############################
#ifdef VARIANT_USART
// #define SIDEBOARD_SERIAL_USART2
// #define CONTROL_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! For Arduino control check the hoverSerial.ino
// #define FEEDBACK_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used!
// #define SIDEBOARD_SERIAL_USART3
#define CONTROL_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino
#define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
// Min / Max values of each channel (use DEBUG to determine these values)
#define INPUT1_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT1_MIN -1000 // (-1000 - 0)
#define INPUT1_MID 0
#define INPUT1_MAX 1000 // (0 - 1000)
#define INPUT1_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
// #define SIDEBOARD_SERIAL_USART2 0
#define CONTROL_SERIAL_USART2 0 // left sensor board cable, disable if ADC or PPM is used! For Arduino control check the hoverSerial.ino
#define FEEDBACK_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used!
// #define SIDEBOARD_SERIAL_USART3 0
// #define CONTROL_SERIAL_USART3 0 // right sensor board cable. Number indicates priority for dual-input. Disable if I2C (nunchuk or lcd) is used! For Arduino control check the hoverSerial.ino
// #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
// #define DUAL_INPUTS // UART*(Primary) + SIDEBOARD(Auxiliary). Uncomment this to use Dual-inputs
#define PRI_INPUT1 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#ifdef DUAL_INPUTS
#define FLASH_WRITE_KEY 0x1102 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
// #define SIDEBOARD_SERIAL_USART2 1 // left sideboard
#define SIDEBOARD_SERIAL_USART3 1 // right sideboard
#define AUX_INPUT1 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT2 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#else
#define FLASH_WRITE_KEY 0x1002 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#endif
#define INPUT2_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT2_MIN -1000 // (-1000 - 0)
#define INPUT2_MID 0
#define INPUT2_MAX 1000 // (0 - 1000)
#define INPUT2_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
// #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2!
// #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3!
#endif
@ -344,25 +378,28 @@
* use original nunchuk. most clones does not work very well.
* Recommendation: Nunchuk Breakout Board https://github.com/Jan--Henrik/hoverboard-breakout
*/
#define CONTROL_NUNCHUK // use nunchuk as input. disable FEEDBACK_SERIAL_USART3, DEBUG_SERIAL_USART3!
// Min / Max values of each channel (use DEBUG to determine these values)
#define INPUT1_TYPE 2 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT1_MIN -1024 // (-1024 - 0)
#define INPUT1_MID 0
#define INPUT1_MAX 1024 // (0 - 1024)
#define INPUT1_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define CONTROL_NUNCHUK 0 // use nunchuk as input. Number indicates priority for dual-input. Disable FEEDBACK_SERIAL_USART3, DEBUG_SERIAL_USART3!
// #define DUAL_INPUTS // Nunchuk*(Primary) + UART(Auxiliary). Uncomment this to use Dual-inputs
#define PRI_INPUT1 2, -1024, 0, 1024, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 2, -1024, 0, 1024, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#ifdef DUAL_INPUTS
#define FLASH_WRITE_KEY 0x1103 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
// #define SIDEBOARD_SERIAL_USART2 1
#define CONTROL_SERIAL_USART2 1 // left sensor board cable, disable if ADC or PPM is used! For Arduino control check the hoverSerial.ino
#define FEEDBACK_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used!
#define AUX_INPUT1 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT2 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#else
#define FLASH_WRITE_KEY 0x1003 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#define DEBUG_SERIAL_USART2 // left sensor cable debug
#endif
#define INPUT2_TYPE 2 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT2_MIN -1024 // (-1024 - 0)
#define INPUT2_MID 0
#define INPUT2_MAX 1024 // (0 - 1024)
#define INPUT2_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
// # maybe good for ARMCHAIR #
#define FILTER 3276 // 0.05f
#define SPEED_COEFFICIENT 8192 // 0.5f
#define STEER_COEFFICIENT 62259 // -0.2f
#define DEBUG_SERIAL_USART2 // left sensor cable debug
// #define SUPPORT_BUTTONS // Define for Nunchuck buttons support
#define FILTER 3276 // 0.05f
#define SPEED_COEFFICIENT 8192 // 0.5f
#define STEER_COEFFICIENT 62259 // -0.2f
// #define SUPPORT_BUTTONS // Define for Nunchuk buttons support
#endif
// ############################# END OF VARIANT_NUNCHUK SETTINGS #########################
@ -374,29 +411,33 @@
* Right sensor board cable. Channel 1: steering, Channel 2: speed.
* https://gist.github.com/peterpoetzi/1b63a4a844162196613871767189bd05
*/
// #define CONTROL_PPM_LEFT // use PPM-Sum as input on the LEFT cable . disable CONTROL_SERIAL_USART2!
#define CONTROL_PPM_RIGHT // use PPM-Sum as input on the RIGHT cable. disable CONTROL_SERIAL_USART3!
#ifdef CONTROL_PPM_RIGHT
#define DEBUG_SERIAL_USART2 // left sensor cable debug
// #define DUAL_INPUTS // ADC*(Primary) + PPM(Auxiliary). Uncomment this to use Dual-inputs
#ifdef DUAL_INPUTS
#define FLASH_WRITE_KEY 0x1104 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#define CONTROL_ADC 0 // use ADC as input. Number indicates priority for dual-input. Disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2!
#define CONTROL_PPM_RIGHT 1 // use PPM-Sum as input on the RIGHT cable. Number indicates priority for dual-input. Disable CONTROL_SERIAL_USART3!
#define PRI_INPUT1 3, 0, 0, 4095, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 3, 0, 0, 4095, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT1 3, -1000, 0, 1000, 100 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT2 3, -1000, 0, 1000, 100 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#else
#define DEBUG_SERIAL_USART3 // right sensor cable debug
#define FLASH_WRITE_KEY 0x1004 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
// #define CONTROL_PPM_LEFT 0 // use PPM-Sum as input on the LEFT cable. Number indicates priority for dual-input. Disable CONTROL_SERIAL_USART2!
#define CONTROL_PPM_RIGHT 0 // use PPM-Sum as input on the RIGHT cable. Number indicates priority for dual-input. Disable CONTROL_SERIAL_USART3!
#define PRI_INPUT1 3, -1000, 0, 1000, 100 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 3, -1000, 0, 1000, 100 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#endif
#define PPM_NUM_CHANNELS 6 // total number of PPM channels to receive, even if they are not used.
// Min / Max values of each channel (use DEBUG to determine these values)
#define INPUT1_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT1_MIN -1000 // (-1000 - 0)
#define INPUT1_MID 0
#define INPUT1_MAX 1000 // (0 - 1000)
#define INPUT1_DEADBAND 100 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define PPM_NUM_CHANNELS 6 // total number of PPM channels to receive, even if they are not used.
#define INPUT2_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT2_MIN -1000 // (-1000 - 0)
#define INPUT2_MID 0
#define INPUT2_MAX 1000 // (0 - 1000)
#define INPUT2_DEADBAND 100 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
// #define SUPPORT_BUTTONS // Define for PPM buttons support
// #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2!
// #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3!
// #define SUPPORT_BUTTONS // Define for PPM buttons support
// #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2!
// #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3!
#if defined(CONTROL_PPM_RIGHT) && !defined(DUAL_INPUTS)
#define DEBUG_SERIAL_USART2 // left sensor cable debug
#elif defined(CONTROL_PPM_LEFT) && !defined(DUAL_INPUTS)
#define DEBUG_SERIAL_USART3 // right sensor cable debug
#endif
#endif
// ############################# END OF VARIANT_PPM SETTINGS ############################
@ -407,33 +448,36 @@
* Right sensor board cable. Connect PA2 to channel 1 and PA3 to channel 2 on receiver.
* Channel 1: steering, Channel 2: speed.
*/
// #define CONTROL_PWM_LEFT // use RC PWM as input on the LEFT cable. disable DEBUG_SERIAL_USART2!
#define CONTROL_PWM_RIGHT // use RC PWM as input on the RIGHT cable. disable DEBUG_SERIAL_USART3!
#ifdef CONTROL_PWM_RIGHT
#define DEBUG_SERIAL_USART2 // left sensor cable debug
// #define DUAL_INPUTS // ADC*(Primary) + PWM(Auxiliary). Uncomment this to use Dual-inputs
#ifdef DUAL_INPUTS
#define FLASH_WRITE_KEY 0x1105 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#define CONTROL_ADC 0 // use ADC as input. Number indicates priority for dual-input. Disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2!
#define CONTROL_PWM_RIGHT 1 // use RC PWM as input on the RIGHT cable. Number indicates priority for dual-input. Disable DEBUG_SERIAL_USART3!
#define PRI_INPUT1 3, 0, 0, 4095, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 3, 0, 0, 4095, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT1 3, -1000, 0, 1000, 100 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT2 3, -1000, 0, 1000, 100 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#else
#define DEBUG_SERIAL_USART3 // right sensor cable debug
#define FLASH_WRITE_KEY 0x1005 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
// #define CONTROL_PWM_LEFT 0 // use RC PWM as input on the LEFT cable. Number indicates priority for dual-input. Disable DEBUG_SERIAL_USART2!
#define CONTROL_PWM_RIGHT 0 // use RC PWM as input on the RIGHT cable. Number indicates priority for dual-input. Disable DEBUG_SERIAL_USART3!
#define PRI_INPUT1 3, -1000, 0, 1000, 100 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 3, -1000, 0, 1000, 100 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#endif
// Min / Max values of each channel (use DEBUG to determine these values)
#define INPUT1_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT1_MIN -1000 // (-1000 - 0)
#define INPUT1_MID 0
#define INPUT1_MAX 1000 // (0 - 1000)
#define INPUT1_DEADBAND 100 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define INPUT2_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT2_MIN -1000 // (-1000 - 0)
#define INPUT2_MID 0
#define INPUT2_MAX 1000 // (0 - 1000)
#define INPUT2_DEADBAND 100 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define FILTER 6553 // 0.1f [-] fixdt(0,16,16) lower value == softer filter [0, 65535] = [0.0 - 1.0].
#define SPEED_COEFFICIENT 16384 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14
#define STEER_COEFFICIENT 16384 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14. If you do not want any steering, set it to 0.
#define FILTER 6553 // 0.1f [-] fixdt(0,16,16) lower value == softer filter [0, 65535] = [0.0 - 1.0].
#define SPEED_COEFFICIENT 16384 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14
#define STEER_COEFFICIENT 16384 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14. If you do not want any steering, set it to 0.
// #define INVERT_R_DIRECTION
// #define INVERT_L_DIRECTION
// #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2!
// #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3!
// #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2!
// #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3!
#if defined(CONTROL_PWM_RIGHT) && !defined(DUAL_INPUTS)
#define DEBUG_SERIAL_USART2 // left sensor cable debug
#elif defined(CONTROL_PWM_LEFT) && !defined(DUAL_INPUTS)
#define DEBUG_SERIAL_USART3 // right sensor cable debug
#endif
#endif
// ############################# END OF VARIANT_PWM SETTINGS ############################
@ -444,33 +488,35 @@
/* CONTROL VIA RC REMOTE WITH FLYSKY IBUS PROTOCOL
* Connected to Right sensor board cable. Channel 1: steering, Channel 2: speed.
*/
#define CONTROL_IBUS // use IBUS as input
#define IBUS_NUM_CHANNELS 14 // total number of IBUS channels to receive, even if they are not used.
#define IBUS_LENGTH 0x20
#define IBUS_COMMAND 0x40
#undef USART3_BAUD
#define USART3_BAUD 115200
#define CONTROL_SERIAL_USART3 // right sensor board cable, disable if ADC or PPM is used!
#define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if ADC or PPM is used!
#ifdef CONTROL_SERIAL_USART3
#define DEBUG_SERIAL_USART2 // left sensor cable debug
#define CONTROL_IBUS // use IBUS as input. Number indicates priority for dual-input.
#define IBUS_NUM_CHANNELS 14 // total number of IBUS channels to receive, even if they are not used.
#define IBUS_LENGTH 0x20
#define IBUS_COMMAND 0x40
#define USART3_BAUD 115200
// #define DUAL_INPUTS // ADC*(Primary) + iBUS(Auxiliary). Uncomment this to use Dual-inputs
#ifdef DUAL_INPUTS
#define FLASH_WRITE_KEY 0x1106 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#define CONTROL_ADC 0 // use ADC as input. Number indicates priority for dual-input. Disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2!
#define CONTROL_SERIAL_USART3 1 // use RC iBUS input on the RIGHT cable. Number indicates priority for dual-input. Disable DEBUG_SERIAL_USART3!
#define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if ADC or PPM is used!
#define PRI_INPUT1 3, 0, 0, 4095, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 3, 0, 0, 4095, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT1 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT2 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#else
#define DEBUG_SERIAL_USART3 // right sensor cable debug
#define FLASH_WRITE_KEY 0x1006 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#define CONTROL_SERIAL_USART3 0 // use RC iBUS input on the RIGHT cable, disable if ADC or PPM is used!
#define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if ADC or PPM is used!
#define PRI_INPUT1 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 3, -1000, 0, 1000, 0 // TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#endif
// Min / Max values of each channel (use DEBUG to determine these values)
#define INPUT1_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT1_MIN -1000 // (-1000 - 0)
#define INPUT1_MID 0
#define INPUT1_MAX 1000 // (0 - 1000)
#define INPUT1_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define INPUT2_TYPE 3 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT2_MIN -1000 // (-1000 - 0)
#define INPUT2_MID 0
#define INPUT2_MAX 1000 // (0 - 1000)
#define INPUT2_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#if defined(CONTROL_SERIAL_USART3) && !defined(DUAL_INPUTS)
#define DEBUG_SERIAL_USART2 // left sensor cable debug
#elif defined(DEBUG_SERIAL_USART2) && !defined(DUAL_INPUTS)
#define DEBUG_SERIAL_USART3 // right sensor cable debug
#endif
#endif
// ############################# END OF VARIANT_IBUS SETTINGS ############################
@ -478,45 +524,39 @@
// ############################ VARIANT_HOVERCAR SETTINGS ############################
#ifdef VARIANT_HOVERCAR
#define FLASH_WRITE_KEY 0x1107 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#undef CTRL_MOD_REQ
#define CTRL_MOD_REQ TRQ_MODE // HOVERCAR works best in TORQUE Mode
#define CONTROL_ADC // use ADC as input. disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2!
#define ADC_PROTECT_TIMEOUT 100 // ADC Protection: number of wrong / missing input commands before safety state is taken
#define ADC_PROTECT_THRESH 200 // ADC Protection threshold below/above the MIN/MAX ADC values
#define INPUT1_TYPE 1 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT1_MIN 1000 // min ADC1-value while poti at minimum-position (0 - 4095)
#define INPUT1_MID 0
#define INPUT1_MAX 2500 // max ADC1-value while poti at maximum-position (0 - 4095)
#define INPUT1_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define INPUT2_TYPE 1 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT2_MIN 500 // min ADC2-value while poti at minimum-position (0 - 4095)
#define INPUT2_MID 0
#define INPUT2_MAX 2200 // max ADC2-value while poti at maximum-position (0 - 4095)
#define INPUT2_DEADBAND 0 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define SPEED_COEFFICIENT 16384 // 1.0f
#define STEER_COEFFICIENT 0 // 0.0f
// #define INVERT_R_DIRECTION // Invert rotation of right motor
// #define INVERT_L_DIRECTION // Invert rotation of left motor
#define SIDEBOARD_SERIAL_USART3 // Tx -> Rx of right sensor board: for LED battery indication. Comment-out if sideboard is not used!
#define FEEDBACK_SERIAL_USART3 // Rx <- Tx of right sensor board: to use photosensors as buttons. Comment-out if sideboard is not used!
// #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
#define CTRL_MOD_REQ TRQ_MODE // HOVERCAR works best in TORQUE Mode
#define CONTROL_ADC 0 // use ADC as input. Number indicates priority for dual-input. Disable CONTROL_SERIAL_USART2, FEEDBACK_SERIAL_USART2, DEBUG_SERIAL_USART2!
#define SIDEBOARD_SERIAL_USART3 1 // Rx from right sensor board: to use photosensors as buttons. Number indicates priority for dual-input. Comment-out if sideboard is not used!
#define FEEDBACK_SERIAL_USART3 // Tx to right sensor board: for LED battery indication. Comment-out if sideboard is not used!
#define DUAL_INPUTS // ADC*(Primary) + Sideboard_R(Auxiliary). Uncomment this to use Dual-inputs
#define PRI_INPUT1 1, 1000, 0, 2500, 0 // Pedal Brake TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 1, 500, 0, 2200, 0 // Pedal Accel TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT1 2, -1000, 0, 1000, 0 // Sideboard Steer TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define AUX_INPUT2 2, -1000, 0, 1000, 0 // Sideboard Speed TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define SPEED_COEFFICIENT 16384 // 1.0f
#define STEER_COEFFICIENT 8192 // 0.5f Only active in Sideboard input
// #define ADC_ALTERNATE_CONNECT // use to swap ADC inputs
// #define INVERT_R_DIRECTION // Invert rotation of right motor
// #define INVERT_L_DIRECTION // Invert rotation of left motor
// #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
// Extra functionality
// #define CRUISE_CONTROL_SUPPORT // [-] Flag to enable Cruise Control support. Activation/Deactivation is done by sideboard button or Brake pedal press.
// #define STANDSTILL_HOLD_ENABLE // [-] Flag to hold the position when standtill is reached. Only available and makes sense for VOLTAGE or TORQUE mode.
// #define ELECTRIC_BRAKE_ENABLE // [-] Flag to enable electric brake and replace the motor "freewheel" with a constant braking when the input torque request is 0. Only available and makes sense for TORQUE mode.
// #define ELECTRIC_BRAKE_MAX 100 // (0, 500) Maximum electric brake to be applied when input torque request is 0 (pedal fully released).
// #define ELECTRIC_BRAKE_THRES 120 // (0, 500) Threshold below at which the electric brake starts engaging.
// #define CRUISE_CONTROL_SUPPORT // [-] Flag to enable Cruise Control support. Activation/Deactivation is done by sideboard button or Brake pedal press.
// #define STANDSTILL_HOLD_ENABLE // [-] Flag to hold the position when standtill is reached. Only available and makes sense for VOLTAGE or TORQUE mode.
// #define ELECTRIC_BRAKE_ENABLE // [-] Flag to enable electric brake and replace the motor "freewheel" with a constant braking when the input torque request is 0. Only available and makes sense for TORQUE mode.
// #define ELECTRIC_BRAKE_MAX 100 // (0, 500) Maximum electric brake to be applied when input torque request is 0 (pedal fully released).
// #define ELECTRIC_BRAKE_THRES 120 // (0, 500) Threshold below at which the electric brake starts engaging.
#endif
// Multiple tap detection: default DOUBLE Tap on Brake pedal (4 pulses)
#define MULTIPLE_TAP_NR 2 * 2 // [-] Define tap number: MULTIPLE_TAP_NR = number_of_taps * 2, number_of_taps = 1 (for single taping), 2 (for double tapping), 3 (for triple tapping), etc...
#define MULTIPLE_TAP_HI 600 // [-] Multiple tap detection High hysteresis threshold
#define MULTIPLE_TAP_LO 200 // [-] Multiple tap detection Low hysteresis threshold
#define MULTIPLE_TAP_TIMEOUT 2000 // [ms] Multiple tap detection Timeout period. The taps need to happen within this time window to be accepted.
#define MULTIPLE_TAP_NR 2 * 2 // [-] Define tap number: MULTIPLE_TAP_NR = number_of_taps * 2, number_of_taps = 1 (for single taping), 2 (for double tapping), 3 (for triple tapping), etc...
#define MULTIPLE_TAP_HI 600 // [-] Multiple tap detection High hysteresis threshold
#define MULTIPLE_TAP_LO 200 // [-] Multiple tap detection Low hysteresis threshold
#define MULTIPLE_TAP_TIMEOUT 2000 // [ms] Multiple tap detection Timeout period. The taps need to happen within this time window to be accepted.
// ######################## END OF VARIANT_HOVERCAR SETTINGS #########################
@ -525,10 +565,18 @@
// Communication: [DONE]
// Balancing controller: [TODO]
#ifdef VARIANT_HOVERBOARD
#define SIDEBOARD_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used!
#define FLASH_WRITE_KEY 0x1008 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#define SIDEBOARD_SERIAL_USART2 1 // left sensor board cable. Number indicates priority for dual-input. Disable if ADC or PPM is used!
#define FEEDBACK_SERIAL_USART2
#define SIDEBOARD_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
#define SIDEBOARD_SERIAL_USART3 0 // right sensor board cable. Number indicates priority for dual-input. Disable if I2C (nunchuk or lcd) is used!
#define FEEDBACK_SERIAL_USART3
// If an iBUS RC receiver is connected to either Left Sideboard (AUX_INPUT) or Right Sideboard (PRI_INPUT)
// PRIMARY INPUT: TYPE, MIN, MID, MAX, DEADBAND /* TYPE: 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect */
#define PRI_INPUT1 3, -1000, 0, 1000, 0 // Priority Sideboard can be used to send commands via an iBUS Receiver connected to the sideboard
#define PRI_INPUT2 3, -1000, 0, 1000, 0 // Priority Sideboard can be used to send commands via an iBUS Receiver connected to the sideboard
#define AUX_INPUT1 3, -1000, 0, 1000, 0 // not used
#define AUX_INPUT2 3, -1000, 0, 1000, 0 // not used
#endif
// ######################## END OF VARIANT_HOVERBOARD SETTINGS #########################
@ -537,17 +585,20 @@
// ################################# VARIANT_TRANSPOTTER SETTINGS ############################
//TODO ADD VALIDATION
#ifdef VARIANT_TRANSPOTTER
#define FLASH_WRITE_KEY 0x1009 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#define CONTROL_GAMETRAK
#define SUPPORT_LCD
// #define SUPPORT_NUNCHUK
#define GAMETRAK_CONNECTION_NORMAL // for normal wiring according to the wiki instructions
//#define GAMETRAK_CONNECTION_ALTERNATE // use this define instead if you messed up the gametrak ADC wiring (steering is speed, and length of the wire is steering)
// #define GAMETRAK_CONNECTION_ALTERNATE // use this define instead if you messed up the gametrak ADC wiring (steering is speed, and length of the wire is steering)
#define ROT_P 1.2 // P coefficient for the direction controller. Positive / Negative values to invert gametrak steering direction.
// during nunchuk control (only relevant when activated)
#define SPEED_COEFFICIENT 14746 // 0.9f - higher value == stronger. 0.0 to ~2.0?
#define STEER_COEFFICIENT 8192 // 0.5f - higher value == stronger. if you do not want any steering, set it to 0.0; 0.0 to 1.0
#define INVERT_R_DIRECTION // Invert right motor
#define INVERT_L_DIRECTION // Invert left motor
#define PRI_INPUT1 2, -1000, 0, 1000, 0 // dummy input, TRANSPOTTER does not use input limitations
#define PRI_INPUT2 2, -1000, 0, 1000, 0 // dummy input, TRANSPOTTER does not use input limitations
#endif
// ############################# END OF VARIANT_TRANSPOTTER SETTINGS ########################
@ -558,37 +609,30 @@
* right sensor board cable. Connect PB10 to channel 1 and PB11 to channel 2 on receiver.
* Channel 1: steering, Channel 2: speed.
*/
#define FLASH_WRITE_KEY 0x1010 // Flash memory writing key. Change this key to ignore the input calibrations from the flash memory and use the ones in config.h
#undef CTRL_MOD_REQ
#define CTRL_MOD_REQ TRQ_MODE // SKATEBOARD works best in TORQUE Mode
//#define CONTROL_PWM_LEFT // use RC PWM as input on the LEFT cable. disable DEBUG_SERIAL_USART2!
#define CONTROL_PWM_RIGHT // use RC PWM as input on the RIGHT cable. disable DEBUG_SERIAL_USART3!
#ifdef CONTROL_PWM_RIGHT
#define DEBUG_SERIAL_USART2 // left sensor cable debug
#else
#define DEBUG_SERIAL_USART3 // right sensor cable debug
#endif
// Min / Max values of each channel (use DEBUG to determine these values)
#define INPUT1_TYPE 0 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT1_MIN -1000 // (-1000 - 0)
#define INPUT1_MID 0
#define INPUT1_MAX 1000 // (0 - 1000)
#define INPUT1_DEADBAND 100 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define CTRL_MOD_REQ TRQ_MODE // SKATEBOARD works best in TORQUE Mode
// #define CONTROL_PWM_LEFT 0 // use RC PWM as input on the LEFT cable. Number indicates priority for dual-input. Disable DEBUG_SERIAL_USART2!
#define CONTROL_PWM_RIGHT 0 // use RC PWM as input on the RIGHT cable. Number indicates priority for dual-input. Disable DEBUG_SERIAL_USART3!
#define INPUT2_TYPE 2 // 0:Disabled, 1:Normal Pot, 2:Middle Resting Pot, 3:Auto-detect
#define INPUT2_MIN -800 // (-1000 - 0)
#define INPUT2_MID 0
#define INPUT2_MAX 700 // (0 - 1000)
#define INPUT2_DEADBAND 100 // How much of the center position is considered 'center' (100 = values -100 to 100 are considered 0)
#define INPUT2_BRAKE -400 // (-1000 - 0) Change this value to adjust the braking amount
#define PRI_INPUT1 0, -1000, 0, 1000, 0 // Disabled. TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define PRI_INPUT2 2, -800, 0, 700, 100 // Active. TYPE, MIN, MID, MAX, DEADBAND. See INPUT FORMAT section
#define INPUT_BRK -400 // (-1000 - 0) Change this value to adjust the braking amount
#define FILTER 6553 // 0.1f [-] fixdt(0,16,16) lower value == softer filter [0, 65535] = [0.0 - 1.0].
#define SPEED_COEFFICIENT 16384 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14
#define STEER_COEFFICIENT 0 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14. If you do not want any steering, set it to 0.
#define FILTER 6553 // 0.1f [-] fixdt(0,16,16) lower value == softer filter [0, 65535] = [0.0 - 1.0].
#define SPEED_COEFFICIENT 16384 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14
#define STEER_COEFFICIENT 0 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14. If you do not want any steering, set it to 0.
#define INVERT_R_DIRECTION
#define INVERT_L_DIRECTION
// #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2!
// #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3!
// #define STANDSTILL_HOLD_ENABLE // [-] Flag to hold the position when standtill is reached. Only available and makes sense for VOLTAGE or TORQUE mode.
// #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2!
// #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3!
// #define STANDSTILL_HOLD_ENABLE // [-] Flag to hold the position when standtill is reached. Only available and makes sense for VOLTAGE or TORQUE mode.
#ifdef CONTROL_PWM_RIGHT
#define DEBUG_SERIAL_USART2 // left sensor cable debug
#else
#define DEBUG_SERIAL_USART3 // right sensor cable debug
#endif
#endif
// ############################# END OF VARIANT_SKATEBOARD SETTINGS ############################
@ -630,10 +674,10 @@
#ifndef STEER_COEFFICIENT
#define STEER_COEFFICIENT DEFAULT_STEER_COEFFICIENT
#endif
#ifdef CONTROL_ADC
#define INPUT_MARGIN 100 // Input margin applied on the raw ADC min and max to make sure the motor MIN and MAX values are reached even in the presence of noise
#if defined(PRI_INPUT1) && defined(PRI_INPUT2) && defined(AUX_INPUT1) && defined(AUX_INPUT2)
#define INPUTS_NR 2
#else
#define INPUT_MARGIN 0
#define INPUTS_NR 1
#endif
// ########################### END OF APPLY DEFAULT SETTING ############################
@ -647,11 +691,6 @@
// General checks
#if (defined(CONTROL_ADC) || defined(CONTROL_SERIAL_USART2) || defined(CONTROL_PPM_LEFT) || defined(CONTROL_PWM_LEFT)) && \
(defined(CONTROL_NUNCHUK) || defined(CONTROL_SERIAL_USART3) || defined(CONTROL_PPM_RIGHT) || defined(CONTROL_PWM_RIGHT))
#warning !! Multiple control input sources defined !! If NOT handled correctly, it can lead to undesired behavior!
#endif
#if defined(CONTROL_SERIAL_USART2) && defined(SIDEBOARD_SERIAL_USART2)
#error CONTROL_SERIAL_USART2 and SIDEBOARD_SERIAL_USART2 not allowed, choose one.
#endif

@ -102,28 +102,56 @@
// #define DCLINK_ADC ADC3
// #define DCLINK_CHANNEL
#if BOARD_VARIANT == 0
#define DCLINK_PIN GPIO_PIN_2
#define DCLINK_PORT GPIOC
#elif BOARD_VARIANT == 1
#define DCLINK_PIN GPIO_PIN_1
#define DCLINK_PORT GPIOA
#endif
// #define DCLINK_PULLUP 30000
// #define DCLINK_PULLDOWN 1000
#define LED_PIN GPIO_PIN_2
#define LED_PORT GPIOB
#if BOARD_VARIANT == 0
#define BUZZER_PIN GPIO_PIN_4
#define BUZZER_PORT GPIOA
#elif BOARD_VARIANT == 1
#define BUZZER_PIN GPIO_PIN_13
#define BUZZER_PORT GPIOC
#endif
#define SWITCH_PIN GPIO_PIN_1
#define SWITCH_PORT GPIOA
// UNUSED/REDUNDANT
//#define SWITCH_PIN GPIO_PIN_1
//#define SWITCH_PORT GPIOA
#if BOARD_VARIANT == 0
#define OFF_PIN GPIO_PIN_5
#define OFF_PORT GPIOA
#elif BOARD_VARIANT == 1
#define OFF_PIN GPIO_PIN_15
#define OFF_PORT GPIOC
#endif
#if BOARD_VARIANT == 0
#define BUTTON_PIN GPIO_PIN_1
#define BUTTON_PORT GPIOA
#elif BOARD_VARIANT == 1
#define BUTTON_PIN GPIO_PIN_9
#define BUTTON_PORT GPIOB
#endif
#if BOARD_VARIANT == 0
#define CHARGER_PIN GPIO_PIN_12
#define CHARGER_PORT GPIOA
#elif BOARD_VARIANT == 1
#define CHARGER_PIN GPIO_PIN_11
#define CHARGER_PORT GPIOA
#endif
#if defined(CONTROL_PPM_LEFT)
#define PPM_PIN GPIO_PIN_3
@ -222,13 +250,11 @@ void PWM_ISR_CH2_Callback(void);
#define SENSOR2_SET (0x02)
#define SENSOR_MPU (0x04)
// RC iBUS switch definitions. Flysky FS-i6S has SW1, SW4 - 2 positions; SW2, SW3 - 3 positions
#define SW1_SET (0x0100) // 0000 0001 0000 0000
#define SW2_SET1 (0x0200) // 0000 0010 0000 0000
#define SW2_SET2 (0x0400) // 0000 0100 0000 0000
#define SW3_SET1 (0x0800) // 0000 1000 0000 0000
#define SW3_SET2 (0x1000) // 0001 0000 0000 0000
#define SW4_SET (0x2000) // 0010 0000 0000 0000
// RC iBUS switch definitions. Flysky FS-i6S has [SWA, SWB, SWC, SWD] = [2, 3, 3, 2] positions switch
#define SWA_SET (0x0100) // 0000 0001 0000 0000
#define SWB_SET (0x0600) // 0000 0110 0000 0000
#define SWC_SET (0x1800) // 0001 1000 0000 0000
#define SWD_SET (0x2000) // 0010 0000 0000 0000
#endif // DEFINES_H

@ -209,7 +209,7 @@
#define PAGE_FULL ((uint8_t)0x80)
/* Variables' number */
#define NB_OF_VAR ((uint8_t)0x0B)
#define NB_OF_VAR ((uint8_t)0x13) /* 19 Variables */
/* Exported types ------------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/

@ -55,6 +55,18 @@
} SerialSideboard;
#endif
// Input Structure
typedef struct {
int16_t raw; // raw input
int16_t cmd; // command
uint8_t typ; // type
uint8_t typDef; // type Defined
int16_t min; // minimum
int16_t mid; // middle
int16_t max; // maximum
int16_t dband; // deadband
} InputStruct;
// Initialization Functions
void BLDC_Init(void);
void Input_Lim_Init(void);
@ -68,21 +80,17 @@ void beepLong(uint8_t freq);
void beepShort(uint8_t freq);
void beepShortMany(uint8_t cnt, int8_t dir);
void calcAvgSpeed(void);
int addDeadBand(int16_t u, int16_t type, int16_t deadBand, int16_t in_min, int16_t in_mid, int16_t in_max, int16_t out_min, int16_t out_max);
int checkInputType(int16_t min, int16_t mid, int16_t max);
void adcCalibLim(void);
void updateCurSpdLim(void);
void saveConfig(void);
void standstillHold(void);
void electricBrake(uint16_t speedBlend, uint8_t reverseDir);
void cruiseControl(uint8_t button);
int checkInputType(int16_t min, int16_t mid, int16_t max);
// Poweroff Functions
void poweroff(void);
void poweroffPressCheck(void);
// Read Functions
void readInput(void);
// Input Functions
void calcInputCmd(InputStruct *in, int16_t out_min, int16_t out_max);
void readInputRaw(void);
void handleTimeout(void);
void readCommand(void);
void usart2_rx_check(void);
void usart3_rx_check(void);
@ -100,6 +108,11 @@ void usart_process_sideboard(SerialSideboard *Sideboard_in, SerialSideboard *Sid
void sideboardLeds(uint8_t *leds);
void sideboardSensors(uint8_t sensors);
// Poweroff Functions
void saveConfig(void);
void poweroff(void);
void poweroffPressCheck(void);
// Filtering Functions
void filtLowPass32(int32_t u, uint16_t coef, int32_t *y);
void rateLimiter16(int16_t u, int16_t rate, int16_t *y);

@ -75,7 +75,7 @@
<OPTFL>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<IsCurrentTarget>0</IsCurrentTarget>
<IsCurrentTarget>1</IsCurrentTarget>
</OPTFL>
<CpuCode>18</CpuCode>
<DebugOpt>
@ -1452,7 +1452,7 @@
<OPTFL>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<IsCurrentTarget>1</IsCurrentTarget>
<IsCurrentTarget>0</IsCurrentTarget>
</OPTFL>
<CpuCode>18</CpuCode>
<DebugOpt>
@ -1721,6 +1721,18 @@
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>2</GroupNumber>
<FileNumber>14</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>..\Src\comms.c</PathWithFileName>
<FilenameWithoutPath>comms.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>
<Group>
@ -1731,7 +1743,7 @@
<RteFlg>0</RteFlg>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>14</FileNumber>
<FileNumber>15</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1743,7 +1755,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>15</FileNumber>
<FileNumber>16</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1755,7 +1767,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>16</FileNumber>
<FileNumber>17</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1767,7 +1779,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>17</FileNumber>
<FileNumber>18</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1779,7 +1791,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>18</FileNumber>
<FileNumber>19</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1791,7 +1803,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>19</FileNumber>
<FileNumber>20</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1803,7 +1815,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>20</FileNumber>
<FileNumber>21</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1815,7 +1827,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>21</FileNumber>
<FileNumber>22</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1827,7 +1839,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>22</FileNumber>
<FileNumber>23</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1839,7 +1851,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>23</FileNumber>
<FileNumber>24</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1851,7 +1863,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>24</FileNumber>
<FileNumber>25</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1863,7 +1875,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>25</FileNumber>
<FileNumber>26</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1875,7 +1887,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>26</FileNumber>
<FileNumber>27</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1887,7 +1899,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>27</FileNumber>
<FileNumber>28</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1899,7 +1911,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>28</FileNumber>
<FileNumber>29</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1911,7 +1923,7 @@
</File>
<File>
<GroupNumber>3</GroupNumber>
<FileNumber>29</FileNumber>
<FileNumber>30</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
@ -1931,7 +1943,7 @@
<RteFlg>0</RteFlg>
<File>
<GroupNumber>4</GroupNumber>
<FileNumber>30</FileNumber>
<FileNumber>31</FileNumber>
<FileType>1</FileType>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>

@ -449,6 +449,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -994,6 +999,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -1607,6 +1617,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -2220,6 +2235,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -2833,6 +2853,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -3446,6 +3471,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -4059,6 +4089,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -4672,6 +4707,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -5217,6 +5257,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>
@ -5762,6 +5807,11 @@
<FileType>5</FileType>
<FilePath>..\Inc\config.h</FilePath>
</File>
<File>
<FileName>comms.c</FileName>
<FileType>1</FileType>
<FilePath>..\Src\comms.c</FilePath>
</File>
</Files>
</Group>
<Group>

@ -38,6 +38,7 @@ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \
Src/system_stm32f1xx.c \
Src/setup.c \
Src/control.c \
Src/comms.c \
Src/util.c \
Src/main.c \
Src/bldc.c \

@ -14,7 +14,8 @@ Table of Contents
* [Hardware](#hardware)
* [FOC Firmware](#foc-firmware)
* [Example Variants ](#example-variants)
* [Example Variants](#example-variants)
* [Dual Inputs](#dual-inputs)
* [Flashing](#flashing)
* [Troubleshooting](#troubleshooting)
* [Diagnostics](#diagnostics)
@ -59,22 +60,12 @@ Typically, the mainboard brain is an [STM32F103RCT6](/docs/literature/[10]_STM32
In this firmware 3 control types are available:
- Commutation
- SIN (Sinusoidal)
<<<<<<< HEAD
- FOC (Field Oriented Control)
![Schematic representation of the available control methods](https://mygit.jblb.net/jerome/hoverboard-firmware-hack-FOC/raw/branch/master/01_Matlab/02_Figures/control_methods.png)
Demo videos:
[►Video: Commutation vs Advanced control (constant speed)](https://drive.google.com/open?id=1vC_kEkp2LE2lAaMCJcmK4z2m3jrPUoBD)
=======
- FOC (Field Oriented Control) with the following 3 control modes:
- **VOLTAGE MODE**: in this mode the controller applies a constant Voltage to the motors. Recommended for robotics applications or applications where a fast motor response is required.
- **SPEED MODE**: in this mode a closed-loop controller realizes the input speed target by rejecting any of the disturbance (resistive load) applied to the motor. Recommended for robotics applications or constant speed applications.
- **TORQUE MODE**: in this mode the input torque target is realized. This mode enables motor "freewheeling" when the torque target is `0`. Recommended for most applications with a sitting human driver.
#### Comparison between different control methods
>>>>>>> 3fcae4fc8bb60bc6d26a7466a681183c1965cad1
|Control method| Complexity | Efficiency | Smoothness | Field Weakening | Freewheeling | Standstill hold |
|--|--|--|--|--|--|--|
@ -84,11 +75,9 @@ Demo videos:
|FOC SPEED| +++ | +++ | + | ++ | n.a. | +++ |
|FOC TORQUE| +++ | +++ | +++ | ++ | +++<sup>(1)</sup> | n.a<sup>(2)</sup> |
<sup>(1)</sup> By enabling `ELECTRIC_BRAKE_ENABLE` in `config.h`, the freewheeling amount can be adjusted using the `ELECTRIC_BRAKE_MAX` parameter.
<sup>(1)</sup> By enabling `ELECTRIC_BRAKE_ENABLE` in `config.h`, the freewheeling amount can be adjusted using the `ELECTRIC_BRAKE_MAX` parameter.<br/>
<sup>(2)</sup> The standstill hold functionality can be forced by enabling `STANDSTILL_HOLD_ENABLE` in `config.h`.
In all FOC control modes, the controller features maximum motor speed and maximum motor current protection. This brings great advantages to fulfil the needs of many robotic applications while maintaining safe operation.
@ -120,7 +109,7 @@ This firmware offers currently these variants (selectable in [platformio.ini](/p
- **VARIANT_PPM**: RC remote control with PPM Sum signal.
- **VARIANT_PWM**: RC remote control with PWM signal.
- **VARIANT_IBUS**: RC remote control with Flysky iBUS protocol connected to the Left sensor cable.
- **VARIANT_HOVERCAR**: The motors are controlled by two pedals brake and throttle. Reverse is engaged by double tapping on the brake pedal at standstill. See [HOVERCAR video](https://www.youtube.com/watch?v=IgHCcj0NgWQ&t=).
- **VARIANT_HOVERCAR**: The motors are controlled by two pedals brake and throttle. Reverse is engaged by double tapping on the brake pedal at standstill. See [HOVERCAR wiki](https://github.com/EmanuelFeru/hoverboard-firmware-hack-FOC/wiki/Variant-HOVERCAR).
- **VARIANT_HOVERBOARD**: The mainboard reads the two sideboards data. The sideboards need to be flashed with the hacked version. The balancing controller is **not** yet implemented.
- **VARIANT_TRANSPOTTER**: This is for transpotter build, which is a hoverboard based transportation system. For more details on how to build it check [here](https://github.com/NiklasFauth/hoverboard-firmware-hack/wiki/Build-Instruction:-TranspOtter) and [here](https://hackaday.io/project/161891-transpotter-ng).
- **VARIANT_SKATEBOARD**: This is for skateboard build, controlled using an RC remote with PWM signal connected to the right sensor cable.
@ -128,6 +117,29 @@ This firmware offers currently these variants (selectable in [platformio.ini](/p
Of course the firmware can be further customized for other needs or projects.
---
## Dual Inputs
The firmware supports the input to be provided from two different sources connected to the Left and Right cable, respectively. To enable dual-inputs functionality uncomment `#define DUAL_INPUTS` in config.h for the respective variant. Various dual-inputs combinations can be realized as illustrated in the following table:
| Left | Right | Availability |
| --- | --- | --- |
| ADC<sup>(0)</sup> | UART<sup>(1)</sup> | VARIANT_ADC |
| ADC<sup>(0)</sup> | {PPM,PWM,iBUS}<sup>(1)</sup> | VARIANT_{PPM,PWM,IBUS} |
| ADC<sup>(0)</sup> | Sideboard<sup></sup><sup>(1*)</sup> | VARIANT_HOVERCAR |
| UART<sup>(0)</sup> | Sideboard<sup>(1*)</sup> | VARIANT_UART |
| UART<sup>(1)</sup> | Nunchuk<sup>(0)</sup> | VARIANT_NUNCHUK |
<sup>(0)</sup> Primary input: this input is used when the Auxilliary input is not available or not connected.<br/>
<sup>(1)</sup> Auxilliary input: this inputs is used when connected or enabled by a switch<sup>(*)</sup>. If the Auxilliary input is disconnected, the firmware will automatically switch to the Primary input. Timeout is reported **only** on the Primary input.
With slight modifications in config.h, other dual-inputs combinations can be realized as:
| Left | Right | Possibility |
| --- | --- | --- |
| Sideboard<sup>(1*)</sup> | UART<sup>(0)</sup> | VARIANT_UART |
| UART<sup>(0)</sup> | {PPM,PWM,iBUS}<sup>(1)</sup> | VARIANT_{PPM,PWM,IBUS} |
| {PPM,PWM,iBUS}<sup>(1)</sup> | Nunchuk<sup>(0)</sup> | VARIANT_{PPM,PWM,IBUS} |
---
## Flashing
@ -237,7 +249,7 @@ The errors reported by the board are in the form of audible beeps:
- **1 beep (low pitch)**: Motor error (see [possible causes](https://github.com/EmanuelFeru/bldc-motor-control-FOC#diagnostics))
- **2 beeps (low pitch)**: ADC timeout
- **3 beeps (low pitch)**: Serial communication timeout
- **4 beeps (low pitch)**: General timeout (PPM, PWM, Nunchuck)
- **4 beeps (low pitch)**: General timeout (PPM, PWM, Nunchuk)
- **5 beeps (low pitch)**: Mainboard temperature warning
- **1 beep slow (medium pitch)**: Low battery voltage < 36V
- **1 beep fast (medium pitch)**: Low battery voltage < 35V
@ -249,7 +261,8 @@ For a more detailed troubleshooting connect an [FTDI Serial adapter](https://s.c
## Projects and Links
- **Original firmware:** [https://github.com/NiklasFauth/hoverboard-firmware-hack](https://github.com/NiklasFauth/hoverboard-firmware-hack)
- **[Candas](https://github.com/Candas1/) Hoverboard Web Serial Control:** [https://candas1.github.io/Hoverboard-Web-Serial-Control/](https://candas1.github.io/Hoverboard-Web-Serial-Control/)
- **[Candas](https://github.com/Candas1/) Hoverboard Web Serial Control:** [https://github.com/Candas1/Hoverboard-Web-Serial-Control](https://github.com/Candas1/Hoverboard-Web-Serial-Control)
- **[RoboDurden's](https://github.com/RoboDurden) online compiler:** [https://pionierland.de/hoverhack/](https://pionierland.de/hoverhack/)
- **Hoverboard hack for AT32F403RCT6 mainboards:** [https://github.com/cloidnerux/hoverboard-firmware-hack](https://github.com/cloidnerux/hoverboard-firmware-hack)
- **Hoverboard hack for split mainboards:** [https://github.com/flo199213/Hoverboard-Firmware-Hack-Gen2](https://github.com/flo199213/Hoverboard-Firmware-Hack-Gen2)
@ -258,7 +271,7 @@ For a more detailed troubleshooting connect an [FTDI Serial adapter](https://s.c
<a/>
- **Bobbycar** [https://github.com/larsmm/hoverboard-firmware-hack-bbcar](https://github.com/larsmm/hoverboard-firmware-hack-bbcar)
- **Bobbycar** [https://github.com/larsmm/hoverboard-firmware-hack-FOC-bbcar](https://github.com/larsmm/hoverboard-firmware-hack-FOC-bbcar)
- **Wheel chair:** [https://github.com/Lahorde/steer_speed_ctrl](https://github.com/Lahorde/steer_speed_ctrl)
- **TranspOtterNG:** [https://github.com/Jan--Henrik/transpOtterNG](https://github.com/Jan--Henrik/transpOtterNG)
- **ST Community:** [Custom FOC motor control](https://community.st.com/s/question/0D50X0000B28qTDSQY/custom-foc-control-current-measurement-dma-timer-interrupt-needs-review)
@ -267,6 +280,10 @@ For a more detailed troubleshooting connect an [FTDI Serial adapter](https://s.c
- **Telegram Community:** If you are an enthusiast join our [Hooover Telegram Group](https://t.me/joinchat/BHWO_RKu2LT5ZxEkvUB8uw)
---
## Stargazers
[![Stargazers over time](https://starchart.cc/EmanuelFeru/hoverboard-firmware-hack-FOC.svg)](https://starchart.cc/EmanuelFeru/hoverboard-firmware-hack-FOC)
---
## Contributions
@ -277,6 +294,4 @@ If you want to donate to keep this firmware updated, please use the link below:
[![paypal](https://www.paypalobjects.com/en_US/NL/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=CU2SWN2XV9SCY&currency_code=EUR&source=url)
---

@ -39,13 +39,14 @@ extern RT_MODEL *const rtM_Right;
extern DW rtDW_Left; /* Observable states */
extern ExtU rtU_Left; /* External inputs */
extern ExtY rtY_Left; /* External outputs */
extern P rtP_Left;
extern DW rtDW_Right; /* Observable states */
extern ExtU rtU_Right; /* External inputs */
extern ExtY rtY_Right; /* External outputs */
// ###############################################################################
static int16_t pwm_margin = 110; /* This margin allows to always have a window in the PWM signal for proper Phase currents measurement */
static int16_t pwm_margin; /* This margin allows to have a window in the PWM signal for proper FOC Phase currents measurement */
extern uint8_t ctrlModReq;
static int16_t curDC_max = (I_DC_MAX * A2BIT_CONV);
@ -60,7 +61,7 @@ extern volatile adc_buf_t adc_buffer;
uint8_t buzzerFreq = 0;
uint8_t buzzerPattern = 0;
uint8_t buzzerCount = 0;
static uint32_t buzzerTimer = 0;
volatile uint32_t buzzerTimer = 0;
static uint8_t buzzerPrev = 0;
static uint8_t buzzerIdx = 0;
@ -146,6 +147,13 @@ void DMA1_Channel1_IRQHandler(void) {
buzzerPrev = 0;
}
// Adjust pwm_margin depending on the selected Control Type
if (rtP_Left.z_ctrlTypSel == FOC_CTRL) {
pwm_margin = 110;
} else {
pwm_margin = 0;
}
// ############################### MOTOR CONTROL ###############################
int ul, vl, wl;

@ -0,0 +1,650 @@
/**
* This file is part of the hoverboard-firmware-hack project.
*
* Copyright (C) 2020-2021 Emanuel FERU <aerdronix@gmail.com>
*
* 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/>.
*/
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32f1xx_hal.h"
#include "config.h"
#include "defines.h"
#include "eeprom.h"
#include "BLDC_controller.h"
#include "util.h"
#include "comms.h"
#if defined(DEBUG_SERIAL_PROTOCOL)
#if defined(DEBUG_SERIAL_PROTOCOL) && (defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3))
#ifdef CONTROL_ADC
#define RAW_MIN 0
#define RAW_MAX 4095
#else
#define RAW_MIN -1000
#define RAW_MAX 1000
#endif
#define MAX_PARAM_WATCH 15
extern ExtY rtY_Left; /* External outputs */
extern ExtU rtU_Left; /* External inputs */
extern P rtP_Left;
extern ExtY rtY_Right; /* External outputs */
extern ExtU rtU_Right; /* External inputs */
extern P rtP_Right;
extern InputStruct input1[]; // input structure
extern InputStruct input2[]; // input structure
extern uint16_t VirtAddVarTab[NB_OF_VAR];
extern int16_t speedAvg; // average measured speed
extern int16_t speedAvgAbs; // average measured speed in absolute
extern uint8_t ctrlModReqRaw;
extern int16_t batVoltageCalib;
extern int16_t board_temp_deg_c;
extern int16_t left_dc_curr;
extern int16_t right_dc_curr;
extern int16_t dc_curr;
extern int16_t cmdL;
extern int16_t cmdR;
enum commandTypes {READ,WRITE};
// Function0 - Function with 0 parameter
// Function1 - Function with 1 parameter (e.g. GET PARAM)
// Function2 - Function with 2 parameter (e.g. SET PARAM XXXX)
const command_entry commands[] = {
// Type ,Name ,Function0 ,Function1 ,Function2 ,Help
{READ ,"GET" ,printAllParamDef ,printParamDef ,NULL ,"Get Parameter/Variable"},
{READ ,"HELP" ,printAllParamHelp ,printParamHelp ,NULL ,"Command/Parameter/Variable Help"},
{READ ,"WATCH" ,NULL ,watchParamVal ,NULL ,"Toggle Parameter/Variable Watch"},
{WRITE ,"SET" ,NULL ,NULL ,setParamValExt ,"Set Parameter"},
{WRITE ,"INIT" ,NULL ,initParamVal ,NULL ,"Init Parameter from EEPROM or CONFIG.H"},
{WRITE ,"SAVE" ,saveAllParamVal ,NULL ,NULL ,"Save Parameters to EEPROM"},
};
enum paramTypes {PARAMETER,VARIABLE};
const parameter_entry params[] = {
// CONTROL PARAMETERS
// Type ,Name ,Datatype ,ValueL ptr ,ValueR ,EEPRM Addr ,Init Int/Ext ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text
{PARAMETER ,"CTRL_MOD" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,0 ,1 ,3 ,0 ,0 ,0 ,NULL ,"Ctrl mode 1:VLT 2:SPD 3:TRQ"},
{PARAMETER ,"CTRL_TYP" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,0 ,2 ,0 ,0 ,0 ,NULL ,"Ctrl type 0:COM 1:SIN 2:FOC"},
{PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,1 ,1 ,40 ,A2BIT_CONV ,0 ,4 ,NULL ,"Max phase current A"},
{PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,1 ,10 ,2000 ,0 ,0 ,4 ,NULL ,"Max motor RPM"},
{PARAMETER ,"FI_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,0 ,1 ,0 ,0 ,0 ,NULL ,"Enable field weak"},
{PARAMETER ,"FI_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,1 ,0 ,1500 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak high RPM"},
{PARAMETER ,"FI_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,1 ,0 ,1000 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak low RPM"},
{PARAMETER ,"FI_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,1 ,0 ,20 ,A2BIT_CONV ,0 ,4 ,NULL ,"Field weak max current A(FOC)"},
{PARAMETER ,"PHA_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,1 ,0 ,55 ,0 ,0 ,4 ,NULL ,"Max Phase Adv angle Deg(SIN)"},
// INPUT PARAMETERS
// Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init Int/Ext ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text
{VARIABLE ,"IN1_RAW" ,ADD_PARAM(input1[0].raw) ,NULL ,0 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input1 raw"},
{PARAMETER ,"IN1_TYP" ,ADD_PARAM(input1[0].typ) ,NULL ,3 ,0 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input1 type"},
{PARAMETER ,"IN1_MIN" ,ADD_PARAM(input1[0].min) ,NULL ,4 ,RAW_MIN ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input1 min"},
{PARAMETER ,"IN1_MID" ,ADD_PARAM(input1[0].mid) ,NULL ,5 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input1 mid"},
{PARAMETER ,"IN1_MAX" ,ADD_PARAM(input1[0].max) ,NULL ,6 ,RAW_MAX ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input1 max"},
{VARIABLE ,"IN1_CMD" ,ADD_PARAM(input1[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 cmd"},
{VARIABLE ,"IN2_RAW" ,ADD_PARAM(input2[0].raw) ,NULL ,0 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input2 raw"},
{PARAMETER ,"IN2_TYP" ,ADD_PARAM(input2[0].typ) ,NULL ,7 ,0 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input2 type"},
{PARAMETER ,"IN2_MIN" ,ADD_PARAM(input2[0].min) ,NULL ,8 ,RAW_MIN ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input2 min"},
{PARAMETER ,"IN2_MID" ,ADD_PARAM(input2[0].mid) ,NULL ,9 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input2 mid"},
{PARAMETER ,"IN2_MAX" ,ADD_PARAM(input2[0].max) ,NULL ,10 ,RAW_MAX ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input2 max"},
{VARIABLE ,"IN2_CMD" ,ADD_PARAM(input2[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 cmd"},
#if defined(PRI_INPUT1) && defined(PRI_INPUT2) && defined(AUX_INPUT1) && defined(AUX_INPUT2)
// Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init Int/Ext ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text
{VARIABLE ,"AUX_IN1_RAW" ,ADD_PARAM(input1[1].raw) ,NULL ,0 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input1 raw"},
{PARAMETER ,"AUX_IN1_TYP" ,ADD_PARAM(input1[1].typ) ,NULL ,11 ,0 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input1 type"},
{PARAMETER ,"AUX_IN1_MIN" ,ADD_PARAM(input1[1].min) ,NULL ,12 ,RAW_MIN ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input1 min"},
{PARAMETER ,"AUX_IN1_MID" ,ADD_PARAM(input1[1].mid) ,NULL ,13 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input1 mid"},
{PARAMETER ,"AUX_IN1_MAX" ,ADD_PARAM(input1[1].max) ,NULL ,14 ,RAW_MAX ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input1 max"},
{VARIABLE ,"AUX_IN1_CMD" ,ADD_PARAM(input1[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 cmd"},
{VARIABLE ,"AUX_IN2_RAW" ,ADD_PARAM(input2[1].raw) ,NULL ,0 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input2 raw"},
{PARAMETER ,"AUX_IN2_TYP" ,ADD_PARAM(input2[1].typ) ,NULL ,15 ,0 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input2 type"},
{PARAMETER ,"AUX_IN2_MIN" ,ADD_PARAM(input2[1].min) ,NULL ,16 ,RAW_MIN ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input2 min"},
{PARAMETER ,"AUX_IN2_MID" ,ADD_PARAM(input2[1].mid) ,NULL ,17 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input2 mid"},
{PARAMETER ,"AUX_IN2_MAX" ,ADD_PARAM(input2[1].max) ,NULL ,18 ,RAW_MAX ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input2 max"},
{VARIABLE ,"AUX_IN2_CMD" ,ADD_PARAM(input2[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 cmd"},
#endif
// FEEDBACK
// Type ,Name ,Datatype, ValueL ptr ,ValueR ,EEPRM Addr ,Init Int/Ext ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text
{VARIABLE ,"DC_CURR" ,ADD_PARAM(dc_curr) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Total DC Link current A *100"},
{VARIABLE ,"RDC_CURR" ,ADD_PARAM(right_dc_curr) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Right DC Link current A *100"},
{VARIABLE ,"LDC_CURR" ,ADD_PARAM(left_dc_curr) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Left DC Link current A *100"},
{VARIABLE ,"CMDL" ,ADD_PARAM(cmdL) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Left Motor Command"},
{VARIABLE ,"CMDR" ,ADD_PARAM(cmdR) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Right Motor Command"},
{VARIABLE ,"SPD_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Motor Measured Avg RPM"},
{VARIABLE ,"SPDL" ,ADD_PARAM(rtY_Left.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Left Motor Measured RPM"},
{VARIABLE ,"SPDR" ,ADD_PARAM(rtY_Right.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Right Motor Measured RPM"},
{VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,0 ,0 ,4 ,NULL ,"Rate *10"},
{VARIABLE ,"SPD_COEF" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,0 ,10 ,14 ,NULL ,"Speed Coefficient *10"},
{VARIABLE ,"STR_COEF" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,0 ,10 ,14 ,NULL ,"Steer Coefficient *10"},
{VARIABLE ,"BATV" ,ADD_PARAM(batVoltageCalib) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Calibrated Battery voltage *100"},
{VARIABLE ,"TEMP" ,ADD_PARAM(board_temp_deg_c) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Calibrated Temperature °C *10"},
};
const char *errors[9] = {
"Command not found", // Err1
"Parameter not found", // Err2
"This command cannot be used with a Variable", // Err3
"Value not in range", // Err4
"Value expected", // Err5
"Start of line expected", // Err6
"End of line expected", // Err7
"Parameter expected", // Err8
"Uncaught error" // Err9
"Watch list is full" // Err10
};
debug_command command;
int8_t watchParamList[MAX_PARAM_WATCH] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
// Set Param with Value from external format
int8_t setParamValExt(uint8_t index, int32_t value) {
int8_t ret = 0;
// check min and max before conversion to internal values
if (IN_RANGE(value,params[index].min,params[index].max)){
ret = setParamValInt(index,extToInt(index,value));
printParamDef(index);
}else{
printError(4); // Error - Value out of range
}
return ret;
}
// Set Param with value from internal format
int8_t setParamValInt(uint8_t index, int32_t newValue) {
int32_t oldValue = getParamValInt(index);
if (oldValue != newValue){
// if value is different, beep, cast and assign new value
switch (params[index].datatype){
case UINT8_T:
if (params[index].valueL != NULL) *(uint8_t*)params[index].valueL = newValue;
if (params[index].valueR != NULL) *(uint8_t*)params[index].valueR = newValue;
break;
case UINT16_T:
if (params[index].valueL != NULL) *(uint16_t*)params[index].valueL = newValue;
if (params[index].valueR != NULL) *(uint16_t*)params[index].valueR = newValue;
break;
case UINT32_T:
if (params[index].valueL != NULL) *(uint32_t*)params[index].valueL = newValue;
if (params[index].valueR != NULL) *(uint32_t*)params[index].valueR = newValue;
break;
case INT8_T:
if (params[index].valueL != NULL) *(int8_t*)params[index].valueL = newValue;
if (params[index].valueR != NULL) *(int8_t*)params[index].valueR = newValue;
break;
case INT16_T:
if (params[index].valueL != NULL) *(int16_t*)params[index].valueL = newValue;
if (params[index].valueR != NULL) *(int16_t*)params[index].valueR = newValue;
break;
case INT32_T:
if (params[index].valueL != NULL) *(int32_t*)params[index].valueL = newValue;
if (params[index].valueR != NULL) *(int32_t*)params[index].valueR = newValue;
break;
}
// Beep if value was modified
beepShort(5);
}
// Run callback function if assigned
if (params[index].callback_function) (*params[index].callback_function)();
return 1;
}
// Get Parameter Internal value and translate to external
int32_t getParamValExt(uint8_t index) {
return intToExt(index,getParamValInt(index));
}
// Get Parameter Internal Value
int32_t getParamValInt(uint8_t index) {
int32_t value = 0;
int8_t countVar = 0;
if (params[index].valueL != NULL) countVar++;
if (params[index].valueR != NULL) countVar++;
if (countVar > 0){
// Read Left and Right values and calculate average
// If left and right have to be summed up, DIV field could be adapted to multiply by 2
// Cast to parameter datatype
switch (params[index].datatype){
case UINT8_T:
if (params[index].valueL != NULL) value += *(uint8_t*)params[index].valueL;
if (params[index].valueR != NULL) value += *(uint8_t*)params[index].valueR;
break;
case UINT16_T:
if (params[index].valueL != NULL) value += *(uint16_t*)params[index].valueL;
if (params[index].valueR != NULL) value += *(uint16_t*)params[index].valueR;
break;
case UINT32_T:
if (params[index].valueL != NULL) value += *(uint32_t*)params[index].valueL;
if (params[index].valueR != NULL) value += *(uint32_t*)params[index].valueR;
break;
case INT8_T:
if (params[index].valueL != NULL) value += *(int8_t*)params[index].valueL;
if (params[index].valueR != NULL) value += *(int8_t*)params[index].valueR;
break;
case INT16_T:
if (params[index].valueL != NULL) value += *(int16_t*)params[index].valueL;
if (params[index].valueR != NULL) value += *(int16_t*)params[index].valueR;
break;
case INT32_T:
if (params[index].valueL != NULL) value += *(int32_t*)params[index].valueL;
if (params[index].valueR != NULL) value += *(int32_t*)params[index].valueR;
break;
default:
value = 0;
}
// Divide by number of values provided for the parameter
value /= countVar;
}else{
// No variable was provided, return init value that might contain a macro
value = params[index].init;
}
return value;
}
// Add or remove parameter from watch list
int8_t watchParamVal(uint8_t index){
int8_t i,found = 0;
for(i=0;i < MAX_PARAM_WATCH && watchParamList[i]>-1;i++){
if (watchParamList[i] == index) found = 1;
if (found) watchParamList[i] = (i < MAX_PARAM_WATCH-1)?watchParamList[i+1]:-1;
}
if (!found){
if (watchParamList[i] == -1){
watchParamList[i] = index;
return 1;
}
printError(10);
return 0;
}
return 1;
}
// Print value for all parameters with watch flag
int8_t printParamVal(){
int8_t i = 0;
for(i=0;i < MAX_PARAM_WATCH && watchParamList[i]>-1;i++){
printf("%s:%li ",params[watchParamList[i]].name,getParamValExt(watchParamList[i]));
}
if (i>0) printf("\r\n");
return 1;
}
// Print help for Command
int8_t printCommandHelp(uint8_t index){
printf("? %s:\"%s\"\r\n",commands[index].name,commands[index].help);
return 1;
}
// Print help for parameter
int8_t printParamHelp(uint8_t index){
printf("? %s:\"%s\" ",params[index].name,params[index].help);
if (params[index].type == PARAMETER) printf("[min:%li max:%li]",params[index].min,params[index].max);
printf("\r\n");
return 1;
}
// Print help for all parameters
int8_t printAllParamHelp(){
printf("? Commands\r\n");
for(int i=0;i<COMMAND_SIZE(commands);i++)
printCommandHelp(i);
printf("?\r\n");
printf("? Parameters\r\n");
for(int i=0;i<PARAM_SIZE(params);i++){
if (params[i].type == PARAMETER) printParamHelp(i);
}
printf("?\r\n");
printf("? Variables\r\n");
for(int i=0;i<PARAM_SIZE(params);i++){
if (params[i].type == VARIABLE) printParamHelp(i);
}
printf("?\r\n");
return 1;
}
// Print definition(name,value,initial value, min, max) for parameter
int8_t printParamDef(uint8_t index){
printf("# name:\"%s\" value:%li init:%li min:%li max:%li\r\n",
params[index].name, // Parameter Name
getParamValExt(index), // Parameter Value translated to external format
getParamInitExt(index), // Parameter Init Value translated to external format
params[index].min, // Parameter Min Value with External format
params[index].max); // Parameter Max Value with External format
return 1;
}
// Print definition(name,value,initial value, min, max) for all parameters
int8_t printAllParamDef(){
for(int i=0;i<PARAM_SIZE(params);i++) printParamDef(i);
return 1;
}
void printError(uint8_t errornum ){
printf("! Err%i:\"%s\"\r\n",errornum,errors[errornum-1]);
}
// Function to increment a value
// Get Parameter in External format, check max value, increment, set Parameter
// Not used in the protocol yet
int8_t incrParamVal(uint8_t index) {
// This should be used only if min and max values are known
if (params[index].min == params[index].max) return 0;
uint32_t value = getParamValExt(index);
if (value < params[index].max){
return setParamValExt(index,value + 1);
}else{
return setParamValExt(index,(int32_t) params[index].min);
}
}
// Get internal Parameter value and save it to EEprom for all paraemeter with an address assigned
int8_t saveAllParamVal() {
HAL_FLASH_Unlock();
EE_WriteVariable(VirtAddVarTab[0] , (uint16_t)FLASH_WRITE_KEY);
for(int i=0;i<PARAM_SIZE(params);i++){
// Only Parameters with eeprom address can be saved
if (params[i].addr){
EE_WriteVariable(VirtAddVarTab[params[i].addr] , (uint16_t)getParamValInt(i));
}
}
HAL_FLASH_Lock();
return 1;
}
// Translate from Internal to External format
int32_t intToExt(uint8_t index,int32_t value){
// Multiply for small number
if(params[index].mul) value *= params[index].mul;
// Divide to translate to external format
if(params[index].div) value /= params[index].div;
// Shift to translate to external format
if(params[index].fix) value >>= params[index].fix;
return value;
}
// Translate from External to Internal Format
int32_t extToInt(uint8_t index,int32_t value){
// Multiply to translate to internal format
if(params[index].div) value *= params[index].div;
// Shift to translate to internal format
if (params[index].fix) value <<= params[index].fix;
// Divide for small number
if(params[index].mul) value /= params[index].mul;
return value;
}
// Get Parameter Init value(EEPROM or init/config.h) and translate to external format
int32_t getParamInitExt(uint8_t index) {
return intToExt(index,getParamInitInt(index));
}
// Get Parameter value with EEprom data if address is avalaible, init/config.h value otherwise
int16_t getParamInitInt(uint8_t index){
if (params[index].addr){
// if EEPROM address is specified, init from EEPROM address
uint16_t writeCheck, readVal;
HAL_FLASH_Unlock();
EE_ReadVariable(VirtAddVarTab[0], &writeCheck);
EE_ReadVariable(VirtAddVarTab[params[index].addr] , &readVal);
HAL_FLASH_Lock();
// EEPROM was written, use stored value
if (writeCheck == FLASH_WRITE_KEY){
return readVal;
}else{
// Use init value from array
if (params[index].initFormat){
// Init Value is in External format (e.g. PHA_ADV_MAX is 25 deg)
return extToInt(index,params[index].init);
}else{
return params[index].init;
}
}
}else{
if (params[index].initFormat){
// Init Value is in External format (e.g. PHA_ADV_MAX is 25 deg)
return extToInt(index,params[index].init);
}else{
return params[index].init;
}
}
}
// initialize Parameter value with EEprom data if address is avalaible, init/config.h value otherwise
int8_t initParamVal(uint8_t index) {
int8_t ret = 0;
ret = setParamValInt(index,(int32_t) getParamInitInt(index));
printParamDef(index);
return ret;
}
// Find command in commands array and return index
int8_t findCommand(uint8_t *userCommand, uint32_t len){
for(int index=0;index<COMMAND_SIZE(commands);index++){
uint8_t command_len = strlen(commands[index].name);
if (command_len < len){
if (memcmp(userCommand,commands[index].name,command_len)==0){
return index;
}
}
}
return -1; // Not found
}
// Find parameter in params array and return index
int8_t findParam(uint8_t *userCommand, uint32_t len){
for(int index=0;index<PARAM_SIZE(params);index++){
uint8_t param_len = strlen(params[index].name);
if (param_len < len){
if (memcmp(userCommand,params[index].name,param_len)==0){
return index;
}
}
}
return -1; // Not found
}
// Parse and save the command to be executed
void handle_input(uint8_t *userCommand, uint32_t len)
{
// If there is already an unprocessed command, exit
if (command.semaphore == 1) return;
// Check end of line
userCommand+=len-1; // Go to last char
if (*userCommand != '\n' && *userCommand != '\r'){
command.error = 7; // Error - End of line expected
return;
}
userCommand-=len-1; // Come back
int8_t cindex = -1;
int8_t pindex = -1;
uint8_t size = 0;
// Find Command
cindex = findCommand(userCommand,len);
if (cindex == -1){
// Error - Command not found
command.error = 1;
return;
}
// Skip command characters
size = strlen(commands[cindex].name);
{len-=size;userCommand+=size;}
// Skip if space
if (*userCommand == 0x20){len-=1;userCommand+=1;}
if (*userCommand == '\n' || *userCommand == '\r'){
if (commands[cindex].callback_function0 != NULL){
// Command without parameter
command.semaphore = 1;
command.command_index = cindex;
command.param_index = -1;
command.param_value = 0;
}else{
command.error = 8; // Error - Parameter expected
}
return;
}
// Find parameter
pindex = findParam(userCommand,len);
if (pindex == -1){
// Error - Parameter not found
command.error = 2;
return;
}
// Skip parameter characters
size = strlen(params[pindex].name);
{len-=size;userCommand+=size;}
// Skip if space
if (*userCommand == 0x20){len-=1;userCommand+=1;}
if (commands[cindex].type == WRITE && params[pindex].type == VARIABLE){
// Error - This command cannot be used with a Variable
command.error = 3;
return;
}
if (commands[cindex].callback_function1 != NULL){
if (*userCommand == '\n' || *userCommand == '\r'){
// Command with parameter
command.semaphore = 1;
command.command_index = cindex;
command.param_index = pindex;
command.param_value = 0;
}else{
command.error = 7; // Error - End of line expected
}
return;
}
int32_t value = 0;
int8_t sign = 1;
int8_t count = 0;
// Read sign
if (*userCommand == '-'){len-=1;userCommand+=1;sign =-1;}
// Read value
for (value=0; (unsigned)*userCommand-'0'<10; userCommand++){
value = 10*value+(*userCommand-'0');
count++;
// Error - Value out of range
if (value>MAX_int16_T){command.error = 4;return;}
}
if (count == 0){
// Error - Value required
command.error = 5;
return;
}
// Apply sign
value*= sign;
// Command with parameter and value
if (commands[cindex].callback_function2 != NULL){
if (*userCommand == '\n' || *userCommand == '\r'){
command.semaphore = 1;
command.command_index = cindex;
command.param_index = pindex;
command.param_value = value;
}else{
command.error = 7; // Error - End of line expected
}
return;
}
// Uncaught error
command.error = 9;
}
void process_debug()
{
// Print parameters from watch list
printParamVal();
// Show Error if any
if(command.error> 0){
printError(command.error);
command.error = 0;
return;
}
// Nothing to do
if (command.semaphore == 0) return;
int8_t ret = 0;
if (commands[command.command_index].callback_function0 != NULL &&
command.param_index == -1){
// This function needs no parameter
ret = (*commands[command.command_index].callback_function0)();
if (ret==1){printf("OK\r\n");}
command.semaphore = 0;
return;
}
if (commands[command.command_index].callback_function1 != NULL &&
command.param_index != -1){
// This function needs only a parameter
ret = (*commands[command.command_index].callback_function1)(command.param_index);
if (ret==1){printf("OK\r\n");}
command.semaphore = 0;
return;
}
if (commands[command.command_index].callback_function2 != NULL &&
command.param_index != -1){
// This function needs an additional parameter
ret = (*commands[command.command_index].callback_function2)(command.param_index,command.param_value);
if (ret==1){printf("OK\r\n");}
command.semaphore = 0;
}
}
#endif
#endif // DEBUG_SERIAL_PROTOCOL

@ -8,10 +8,11 @@
TIM_HandleTypeDef TimHandle;
TIM_HandleTypeDef TimHandle2;
uint8_t ppm_count = 0;
uint8_t pwm_count = 0;
uint32_t timeoutCnt = 0;
uint8_t nunchuk_data[6] = {0};
uint8_t ppm_count = 0;
uint8_t pwm_count = 0;
uint32_t timeoutCntGen = TIMEOUT;
uint8_t timeoutFlgGen = 0;
uint8_t nunchuk_data[6] = {0};
uint8_t i2cBuffer[2];
@ -34,7 +35,8 @@ void PPM_ISR_Callback(void) {
if (rc_delay > 3000) {
if (ppm_valid && ppm_count == PPM_NUM_CHANNELS) {
ppm_timeout = 0;
timeoutCnt = 0;
timeoutCntGen = 0;
timeoutFlgGen = 0;
memcpy(ppm_captured_value, ppm_captured_value_buffer, sizeof(ppm_captured_value));
}
ppm_valid = true;
@ -122,7 +124,8 @@ void PWM_ISR_CH1_Callback(void) {
} else { // Falling Edge interrupt -> measure pulse duration
uint16_t rc_signal = TIM2->CNT - pwm_CNT_prev_ch1;
if (IN_RANGE(rc_signal, 900, 2100)){
timeoutCnt = 0;
timeoutCntGen = 0;
timeoutFlgGen = 0;
pwm_timeout_ch1 = 0;
pwm_captured_ch1_value = CLAMP(rc_signal, 1000, 2000) - 1000;
}
@ -141,7 +144,8 @@ void PWM_ISR_CH2_Callback(void) {
} else { // Falling Edge interrupt -> measure pulse duration
uint16_t rc_signal = TIM2->CNT - pwm_CNT_prev_ch2;
if (IN_RANGE(rc_signal, 900, 2100)){
timeoutCnt = 0;
timeoutCntGen = 0;
timeoutFlgGen = 0;
pwm_timeout_ch2 = 0;
pwm_captured_ch2_value = CLAMP(rc_signal, 1000, 2000) - 1000;
}
@ -237,11 +241,12 @@ void Nunchuk_Read(void) {
HAL_I2C_Master_Transmit(&hi2c2,0xA4,(uint8_t*)i2cBuffer, 1, 10);
HAL_Delay(3);
if (HAL_I2C_Master_Receive(&hi2c2,0xA4,(uint8_t*)nunchuk_data, 6, 10) == HAL_OK) {
timeoutCnt = 0;
timeoutCntGen = 0;
timeoutFlgGen = 0;
}
#ifndef TRANSPOTTER
if (timeoutCnt > 3) {
if (timeoutCntGen > 3) {
HAL_Delay(50);
Nunchuk_Init();
}

@ -29,6 +29,7 @@
#include "util.h"
#include "BLDC_controller.h" /* BLDC's header file */
#include "rtwtypes.h"
#include "comms.h"
#if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD)
#include "hd44780.h"
@ -60,18 +61,21 @@ extern P rtP_Left; /* Block parameters (auto storage) */
extern P rtP_Right; /* Block parameters (auto storage) */
extern ExtY rtY_Left; /* External outputs */
extern ExtY rtY_Right; /* External outputs */
extern ExtU rtU_Left; /* External inputs */
extern ExtU rtU_Right; /* External inputs */
//---------------
extern int16_t cmd1; // normalized input value. -1000 to 1000
extern int16_t cmd2; // normalized input value. -1000 to 1000
extern int16_t input1; // Non normalized input value
extern int16_t input2; // Non normalized input value
extern uint8_t inIdx; // input index used for dual-inputs
extern uint8_t inIdx_prev;
extern InputStruct input1[]; // input structure
extern InputStruct input2[]; // input structure
extern int16_t speedAvg; // Average measured speed
extern int16_t speedAvgAbs; // Average measured speed in absolute
extern volatile uint32_t timeoutCnt; // Timeout counter for the General timeout (PPM, PWM, Nunchuck)
extern uint8_t timeoutFlagADC; // Timeout Flag for for ADC Protection: 0 = OK, 1 = Problem detected (line disconnected or wrong ADC data)
extern uint8_t timeoutFlagSerial; // Timeout Flag for Rx Serial command: 0 = OK, 1 = Problem detected (line disconnected or wrong Rx data)
extern volatile uint32_t timeoutCntGen; // Timeout counter for the General timeout (PPM, PWM, Nunchuk)
extern volatile uint8_t timeoutFlgGen; // Timeout Flag for the General timeout (PPM, PWM, Nunchuk)
extern uint8_t timeoutFlgADC; // Timeout Flag for for ADC Protection: 0 = OK, 1 = Problem detected (line disconnected or wrong ADC data)
extern uint8_t timeoutFlgSerial; // Timeout Flag for Rx Serial command: 0 = OK, 1 = Problem detected (line disconnected or wrong Rx data)
extern volatile int pwml; // global variable for pwm left. -1000 to 1000
extern volatile int pwmr; // global variable for pwm right. -1000 to 1000
@ -99,7 +103,15 @@ extern volatile uint16_t pwm_captured_ch2_value;
// Global variables set here in main.c
//------------------------------------------------------------------------
uint8_t backwardDrive;
extern volatile uint32_t buzzerTimer;
volatile uint32_t main_loop_counter;
int16_t batVoltageCalib; // global variable for calibrated battery voltage
int16_t board_temp_deg_c; // global variable for calibrated temperature in degrees Celsius
int16_t left_dc_curr; // global variable for Left DC Link current
int16_t right_dc_curr; // global variable for Right DC Link current
int16_t dc_curr; // global variable for Total DC Link current
int16_t cmdL; // global variable for Left Command
int16_t cmdR; // global variable for Right Command
//------------------------------------------------------------------------
// Local variables
@ -146,10 +158,10 @@ static int16_t speed; // local variable for speed. -1000 to 10
static int32_t speedFixdt; // local fixed-point variable for speed low-pass filter
#endif
static uint32_t buzzerTimer_prev = 0;
static uint32_t inactivity_timeout_counter;
static MultipleTap MultipleTapBrake; // define multiple tap functionality for the Brake pedal
int main(void) {
HAL_Init();
@ -189,24 +201,22 @@ int main(void) {
poweronMelody();
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
int16_t cmdL = 0, cmdR = 0;
int16_t lastCmdL = 0, lastCmdR = 0;
int32_t board_temp_adcFixdt = adc_buffer.temp << 16; // Fixed-point filter output initialized with current ADC converted to fixed-point
int16_t board_temp_adcFilt = adc_buffer.temp;
int16_t board_temp_deg_c;
// Loop until button is released
while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { HAL_Delay(10); }
while(1) {
HAL_Delay(DELAY_IN_MAIN_LOOP); //delay in ms
if (buzzerTimer - buzzerTimer_prev > 16*DELAY_IN_MAIN_LOOP) { // 1 ms = 16 ticks buzzerTimer
readCommand(); // Read Command: cmd1, cmd2
readCommand(); // Read Command: input1[inIdx].cmd, input2[inIdx].cmd
calcAvgSpeed(); // Calculate average measured speed: speedAvg, speedAvgAbs
#ifndef VARIANT_TRANSPOTTER
// ####### MOTOR ENABLING: Only if the initial input is very small (for SAFETY) #######
if (enable == 0 && (!rtY_Left.z_errCode && !rtY_Right.z_errCode) && (cmd1 > -50 && cmd1 < 50) && (cmd2 > -50 && cmd2 < 50)){
if (enable == 0 && (!rtY_Left.z_errCode && !rtY_Right.z_errCode) && (input1[inIdx].cmd > -50 && input1[inIdx].cmd < 50) && (input2[inIdx].cmd > -50 && input2[inIdx].cmd < 50)){
beepShort(6); // make 2 beeps indicating the motor enable
beepShort(4); HAL_Delay(100);
steerFixdt = speedFixdt = 0; // reset filters
@ -216,7 +226,7 @@ int main(void) {
#endif
}
// ####### VARIANT_HOVERCAR #######
// ####### VARIANT_HOVERCAR #######
#if defined(VARIANT_HOVERCAR) || defined(VARIANT_SKATEBOARD) || defined(ELECTRIC_BRAKE_ENABLE)
uint16_t speedBlend; // Calculate speed Blend, a number between [0, 1] in fixdt(0,16,15)
speedBlend = (uint16_t)(((CLAMP(speedAvgAbs,10,60) - 10) << 15) / 50); // speedBlend [0,1] is within [10 rpm, 60rpm]
@ -226,115 +236,119 @@ int main(void) {
standstillHold(); // Apply Standstill Hold functionality. Only available and makes sense for VOLTAGE or TORQUE Mode
#endif
#ifdef VARIANT_HOVERCAR
#ifdef VARIANT_HOVERCAR
if (inIdx == CONTROL_ADC) { // Only use use implementation below if pedals are in use (ADC input)
if (speedAvgAbs < 60) { // Check if Hovercar is physically close to standstill to enable Double tap detection on Brake pedal for Reverse functionality
multipleTapDet(cmd1, HAL_GetTick(), &MultipleTapBrake); // Brake pedal in this case is "cmd1" variable
multipleTapDet(input1[inIdx].cmd, HAL_GetTick(), &MultipleTapBrake); // Brake pedal in this case is "input1" variable
}
if (cmd1 > 30) { // If Brake pedal (cmd1) is pressed, bring to 0 also the Throttle pedal (cmd2) to avoid "Double pedal" driving
cmd2 = (int16_t)((cmd2 * speedBlend) >> 15);
if (input1[inIdx].cmd > 30) { // If Brake pedal (input1) is pressed, bring to 0 also the Throttle pedal (input2) to avoid "Double pedal" driving
input2[inIdx].cmd = (int16_t)((input2[inIdx].cmd * speedBlend) >> 15);
cruiseControl((uint8_t)rtP_Left.b_cruiseCtrlEna); // Cruise control deactivated by Brake pedal if it was active
}
}
#endif
#ifdef ELECTRIC_BRAKE_ENABLE
#ifdef ELECTRIC_BRAKE_ENABLE
electricBrake(speedBlend, MultipleTapBrake.b_multipleTap); // Apply Electric Brake. Only available and makes sense for TORQUE Mode
#endif
#ifdef VARIANT_HOVERCAR
#ifdef VARIANT_HOVERCAR
if (inIdx == CONTROL_ADC) { // Only use use implementation below if pedals are in use (ADC input)
if (speedAvg > 0) { // Make sure the Brake pedal is opposite to the direction of motion AND it goes to 0 as we reach standstill (to avoid Reverse driving by Brake pedal)
cmd1 = (int16_t)((-cmd1 * speedBlend) >> 15);
input1[inIdx].cmd = (int16_t)((-input1[inIdx].cmd * speedBlend) >> 15);
} else {
cmd1 = (int16_t)(( cmd1 * speedBlend) >> 15);
input1[inIdx].cmd = (int16_t)(( input1[inIdx].cmd * speedBlend) >> 15);
}
}
#endif
#ifdef VARIANT_SKATEBOARD
if (cmd2 < 0) { // When Throttle is negative, it acts as brake. This condition is to make sure it goes to 0 as we reach standstill (to avoid Reverse driving)
if (speedAvg > 0) { // Make sure the braking is opposite to the direction of motion
cmd2 = (int16_t)(( cmd2 * speedBlend) >> 15);
if (input2[inIdx].cmd < 0) { // When Throttle is negative, it acts as brake. This condition is to make sure it goes to 0 as we reach standstill (to avoid Reverse driving)
if (speedAvg > 0) { // Make sure the braking is opposite to the direction of motion
input2[inIdx].cmd = (int16_t)(( input2[inIdx].cmd * speedBlend) >> 15);
} else {
cmd2 = (int16_t)((-cmd2 * speedBlend) >> 15);
input2[inIdx].cmd = (int16_t)((-input2[inIdx].cmd * speedBlend) >> 15);
}
}
#endif
// ####### LOW-PASS FILTER #######
rateLimiter16(cmd1, RATE, &steerRateFixdt);
rateLimiter16(cmd2, RATE, &speedRateFixdt);
rateLimiter16(input1[inIdx].cmd , RATE, &steerRateFixdt);
rateLimiter16(input2[inIdx].cmd , RATE, &speedRateFixdt);
filtLowPass32(steerRateFixdt >> 4, FILTER, &steerFixdt);
filtLowPass32(speedRateFixdt >> 4, FILTER, &speedFixdt);
steer = (int16_t)(steerFixdt >> 16); // convert fixed-point to integer
speed = (int16_t)(speedFixdt >> 16); // convert fixed-point to integer
// ####### VARIANT_HOVERCAR #######
#ifdef VARIANT_HOVERCAR
#ifdef VARIANT_HOVERCAR
if (inIdx == CONTROL_ADC) { // Only use use implementation below if pedals are in use (ADC input)
if (!MultipleTapBrake.b_multipleTap) { // Check driving direction
speed = steer + speed; // Forward driving: in this case steer = Brake, speed = Throttle
} else {
speed = steer - speed; // Reverse driving: in this case steer = Brake, speed = Throttle
}
steer = 0; // Do not apply steering to avoid side effects if STEER_COEFFICIENT is NOT 0
}
#endif
// ####### MIXER #######
// cmdR = CLAMP((int)(speed * SPEED_COEFFICIENT - steer * STEER_COEFFICIENT), INPUT_MIN, INPUT_MA);
// cmdL = CLAMP((int)(speed * SPEED_COEFFICIENT + steer * STEER_COEFFICIENT), INPUT_MIN, INPUT_MA);
// cmdR = CLAMP((int)(speed * SPEED_COEFFICIENT - steer * STEER_COEFFICIENT), INPUT_MIN, INPUT_MAX);
// cmdL = CLAMP((int)(speed * SPEED_COEFFICIENT + steer * STEER_COEFFICIENT), INPUT_MIN, INPUT_MAX);
mixerFcn(speed << 4, steer << 4, &cmdR, &cmdL); // This function implements the equations above
// ####### SET OUTPUTS (if the target change is less than +/- 100) #######
if ((cmdL > lastCmdL-100 && cmdL < lastCmdL+100) && (cmdR > lastCmdR-100 && cmdR < lastCmdR+100)) {
#ifdef INVERT_R_DIRECTION
pwmr = cmdR;
#else
pwmr = -cmdR;
#endif
#ifdef INVERT_L_DIRECTION
pwml = -cmdL;
#else
pwml = cmdL;
#endif
}
#ifdef INVERT_R_DIRECTION
pwmr = cmdR;
#else
pwmr = -cmdR;
#endif
#ifdef INVERT_L_DIRECTION
pwml = -cmdL;
#else
pwml = cmdL;
#endif
#endif
#ifdef VARIANT_TRANSPOTTER
distance = CLAMP(cmd1 - 180, 0, 4095);
steering = (cmd2 - 2048) / 2048.0;
distance = CLAMP(input1[inIdx].cmd - 180, 0, 4095);
steering = (input2[inIdx].cmd - 2048) / 2048.0;
distanceErr = distance - (int)(setDistance * 1345);
if (nunchuk_connected == 0) {
cmdL = cmdL * 0.8f + (CLAMP(distanceErr + (steering*((float)MAX(ABS(distanceErr), 50)) * ROT_P), -850, 850) * -0.2f);
cmdR = cmdR * 0.8f + (CLAMP(distanceErr - (steering*((float)MAX(ABS(distanceErr), 50)) * ROT_P), -850, 850) * -0.2f);
if ((cmdL < lastCmdL + 50 && cmdL > lastCmdL - 50) && (cmdR < lastCmdR + 50 && cmdR > lastCmdR - 50)) {
if (distanceErr > 0) {
enable = 1;
}
if (distanceErr > -300) {
#ifdef INVERT_R_DIRECTION
pwmr = cmdR;
#else
pwmr = -cmdR;
#endif
#ifdef INVERT_L_DIRECTION
pwml = -cmdL;
#else
pwml = cmdL;
#endif
if (checkRemote) {
if (!HAL_GPIO_ReadPin(LED_PORT, LED_PIN)) {
//enable = 1;
} else {
enable = 0;
}
if (distanceErr > 0) {
enable = 1;
}
if (distanceErr > -300) {
#ifdef INVERT_R_DIRECTION
pwmr = cmdR;
#else
pwmr = -cmdR;
#endif
#ifdef INVERT_L_DIRECTION
pwml = -cmdL;
#else
pwml = cmdL;
#endif
if (checkRemote) {
if (!HAL_GPIO_ReadPin(LED_PORT, LED_PIN)) {
//enable = 1;
} else {
enable = 0;
}
} else {
enable = 0;
}
} else {
enable = 0;
}
timeoutCnt = 0;
timeoutCntGen = 0;
timeoutFlgGen = 0;
}
if (timeoutCnt > TIMEOUT) {
if (timeoutFlgGen) {
pwml = 0;
pwmr = 0;
enable = 0;
@ -368,7 +382,8 @@ int main(void) {
#ifdef SUPPORT_LCD
LCD_SetLocation(&lcd, 0, 0); LCD_WriteString(&lcd, "Nunchuk Control");
#endif
timeoutCnt = 0;
timeoutCntGen = 0;
timeoutFlgGen = 0;
HAL_Delay(1000);
nunchuk_connected = 1;
}
@ -394,11 +409,11 @@ int main(void) {
#endif
// ####### SIDEBOARDS HANDLING #######
#if defined(SIDEBOARD_SERIAL_USART2)
#if defined(SIDEBOARD_SERIAL_USART2) && defined(FEEDBACK_SERIAL_USART2)
sideboardLeds(&sideboard_leds_L);
sideboardSensors((uint8_t)Sideboard_L.sensors);
#endif
#if defined(SIDEBOARD_SERIAL_USART3)
#if defined(SIDEBOARD_SERIAL_USART3) && defined(FEEDBACK_SERIAL_USART3)
sideboardLeds(&sideboard_leds_R);
sideboardSensors((uint8_t)Sideboard_R.sensors);
#endif
@ -408,18 +423,30 @@ int main(void) {
board_temp_adcFilt = (int16_t)(board_temp_adcFixdt >> 16); // convert fixed-point to integer
board_temp_deg_c = (TEMP_CAL_HIGH_DEG_C - TEMP_CAL_LOW_DEG_C) * (board_temp_adcFilt - TEMP_CAL_LOW_ADC) / (TEMP_CAL_HIGH_ADC - TEMP_CAL_LOW_ADC) + TEMP_CAL_LOW_DEG_C;
// ####### CALC CALIBRATED BATTERY VOLTAGE #######
batVoltageCalib = batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC;
// ####### CALC DC LINK CURRENT #######
left_dc_curr = -(rtU_Left.i_DCLink * 100) / A2BIT_CONV; // Left DC Link Current * 100
right_dc_curr = -(rtU_Right.i_DCLink * 100) / A2BIT_CONV; // Right DC Link Current * 100
dc_curr = left_dc_curr + right_dc_curr; // Total DC Link Current * 100
// ####### DEBUG SERIAL OUT #######
#if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3)
if (main_loop_counter % 25 == 0) { // Send data periodically every 125 ms
printf("in1:%i in2:%i cmdL:%i cmdR:%i BatADC:%i BatV:%i TempADC:%i Temp:%i\r\n",
input1, // 1: INPUT1
input2, // 2: INPUT2
cmdL, // 3: output command: [-1000, 1000]
cmdR, // 4: output command: [-1000, 1000]
adc_buffer.batt1, // 5: for battery voltage calibration
batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC, // 6: for verifying battery voltage calibration
board_temp_adcFilt, // 7: for board temperature calibration
board_temp_deg_c); // 8: for verifying board temperature calibration
if (main_loop_counter % 25 == 0) { // Send data periodically every 125 ms
#if defined(DEBUG_SERIAL_PROTOCOL)
process_debug();
#else
printf("in1:%i in2:%i cmdL:%i cmdR:%i BatADC:%i BatV:%i TempADC:%i Temp:%i\r\n",
input1[inIdx].raw, // 1: INPUT1
input2[inIdx].raw, // 2: INPUT2
cmdL, // 3: output command: [-1000, 1000]
cmdR, // 4: output command: [-1000, 1000]
adc_buffer.batt1, // 5: for battery voltage calibration
batVoltageCalib, // 6: for verifying battery voltage calibration
board_temp_adcFilt, // 7: for board temperature calibration
board_temp_deg_c); // 8: for verifying board temperature calibration
#endif
}
#endif
@ -427,31 +454,31 @@ int main(void) {
#if defined(FEEDBACK_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART3)
if (main_loop_counter % 2 == 0) { // Send data periodically every 10 ms
Feedback.start = (uint16_t)SERIAL_START_FRAME;
Feedback.cmd1 = (int16_t)cmd1;
Feedback.cmd2 = (int16_t)cmd2;
Feedback.cmd1 = (int16_t)input1[inIdx].cmd;
Feedback.cmd2 = (int16_t)input2[inIdx].cmd;
Feedback.speedR_meas = (int16_t)rtY_Right.n_mot;
Feedback.speedL_meas = (int16_t)rtY_Left.n_mot;
Feedback.batVoltage = (int16_t)(batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC);
Feedback.batVoltage = (int16_t)batVoltageCalib;
Feedback.boardTemp = (int16_t)board_temp_deg_c;
#if defined(FEEDBACK_SERIAL_USART2)
if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) {
Feedback.cmdLed = (uint16_t)sideboard_leds_L;
Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedR_meas ^ Feedback.speedL_meas
^ Feedback.batVoltage ^ Feedback.boardTemp ^ Feedback.cmdLed);
Feedback.cmdLed = (uint16_t)sideboard_leds_L;
Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedR_meas ^ Feedback.speedL_meas
^ Feedback.batVoltage ^ Feedback.boardTemp ^ Feedback.cmdLed);
HAL_UART_Transmit_DMA(&huart2, (uint8_t *)&Feedback, sizeof(Feedback));
}
#endif
#if defined(FEEDBACK_SERIAL_USART3)
if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) {
Feedback.cmdLed = (uint16_t)sideboard_leds_R;
Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedR_meas ^ Feedback.speedL_meas
^ Feedback.batVoltage ^ Feedback.boardTemp ^ Feedback.cmdLed);
Feedback.cmdLed = (uint16_t)sideboard_leds_R;
Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedR_meas ^ Feedback.speedL_meas
^ Feedback.batVoltage ^ Feedback.boardTemp ^ Feedback.cmdLed);
HAL_UART_Transmit_DMA(&huart3, (uint8_t *)&Feedback, sizeof(Feedback));
}
#endif
#endif
}
#endif
@ -464,11 +491,11 @@ int main(void) {
} else if (rtY_Left.z_errCode || rtY_Right.z_errCode) { // 1 beep (low pitch): Motor error, disable motors
enable = 0;
beepCount(1, 24, 1);
} else if (timeoutFlagADC) { // 2 beeps (low pitch): ADC timeout
} else if (timeoutFlgADC) { // 2 beeps (low pitch): ADC timeout
beepCount(2, 24, 1);
} else if (timeoutFlagSerial) { // 3 beeps (low pitch): Serial timeout
} else if (timeoutFlgSerial) { // 3 beeps (low pitch): Serial timeout
beepCount(3, 24, 1);
} else if (timeoutCnt > TIMEOUT) { // 4 beeps (low pitch): General timeout (PPM, PWM, Nunchuck)
} else if (timeoutFlgGen) { // 4 beeps (low pitch): General timeout (PPM, PWM, Nunchuk)
beepCount(4, 24, 1);
} else if (TEMP_WARNING_ENABLE && board_temp_deg_c >= TEMP_WARNING) { // 5 beeps (low pitch): Mainboard temperature warning
beepCount(5, 24, 1);
@ -495,12 +522,13 @@ int main(void) {
poweroff();
}
// HAL_GPIO_TogglePin(LED_PORT, LED_PIN); // This is to measure the main() loop duration with an oscilloscope connected to LED_PIN
// Update main loop states
lastCmdL = cmdL;
lastCmdR = cmdR;
// Update states
inIdx_prev = inIdx;
buzzerTimer_prev = buzzerTimer;
main_loop_counter++;
timeoutCnt++;
}
}
}

@ -638,7 +638,11 @@ void MX_ADC1_Init(void) {
sConfig.Rank = 3;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
#if BOARD_VARIANT == 0
sConfig.Channel = ADC_CHANNEL_12; // pc2 vbat
#elif BOARD_VARIANT == 1
sConfig.Channel = ADC_CHANNEL_1; // pa1 vbat
#endif
sConfig.Rank = 4;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);

File diff suppressed because it is too large Load Diff

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 467 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 234 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 574 KiB

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

Before

Width:  |  Height:  |  Size: 44 KiB

After

Width:  |  Height:  |  Size: 44 KiB

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 49 KiB

Loading…
Cancel
Save