2018-02-09 07:53:25 +00:00
# pragma once
# include "stm32f1xx_hal.h"
2018-04-09 19:43:59 +00:00
// ################################################################################
2018-04-09 20:03:19 +00:00
# define PWM_FREQ 16000 // PWM frequency in Hz
# define DEAD_TIME 32 // PWM deadtime
2018-02-09 07:53:25 +00:00
2018-05-27 20:16:52 +00:00
# define DC_CUR_LIMIT 15 // Motor DC current limit in amps. it does not disable motors, it is a soft current limit.
2018-02-09 07:53:25 +00:00
2018-05-27 20:16:52 +00:00
# define BAT_LOW_LVL1 36.0 // gently beeps at this voltage level. ~3.6V/cell
# define BAT_LOW_LVL2 33.0 // your battery is almost empty. Charge now! ~3.3V/cell
# define BAT_LOW_DEAD 31.0 // undervoltage lockout. ~3.1V/cell
2018-04-09 20:03:19 +00:00
2018-04-09 19:43:59 +00:00
// ################################################################################
2018-06-11 12:29:39 +00:00
# define DEBUG_SERIAL_USART3 // left sensor board cable, disable if ADC or PPM is used!
2018-05-27 20:16:52 +00:00
//#define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuck) is used!
2018-04-09 20:03:19 +00:00
# define DEBUG_BAUD 115200 // UART baud rate
2018-04-09 19:43:59 +00:00
//#define DEBUG_SERIAL_SERVOTERM
2018-05-27 20:16:52 +00:00
# define DEBUG_SERIAL_ASCII // human readable output. i.e. "345;1337;0;0\n\r"
2018-02-09 08:39:47 +00:00
2018-06-11 12:29:39 +00:00
# define CONTROL_SERIAL_USART2
2018-06-11 12:48:58 +00:00
# define CONTROL_BAUD 9600 // control via usart from eg an Arduino or raspberry
// for Arduino, use: while(1) {Serial.write((int16_t) steer); Serial.write((int16_t) speed); delay(10);} or so
2018-06-11 12:29:39 +00:00
2018-05-27 20:16:52 +00:00
//#define DEBUG_I2C_LCD // standard 16x2 or larger text-lcd via i2c-converter on right sensor board cable
# define TIMEOUT 5 // number of wrong / missing commands before emergency off
2018-05-06 18:11:53 +00:00
2018-04-09 19:43:59 +00:00
// ################################################################################
// ###### CONTROL VIA RC REMOTE ######
2018-06-11 12:29:39 +00:00
// left sensor board cable. Channel 1: steering, Channel 2: speed.
2018-05-27 20:16:52 +00:00
//#define CONTROL_PPM // use PPM-Sum as input. disable DEBUG_SERIAL_USART2!
//#define PPM_NUM_CHANNELS 6 // total number of PPM channels to receive, even if they are not used.
2018-04-07 22:03:35 +00:00
2018-04-09 19:43:59 +00:00
// ###### CONTROL VIA TWO POTENTIOMETERS ######
2018-05-27 20:16:52 +00:00
// ADC-calibration to cover the full poti-range: connect potis to left sensor board cable (0 to 3.3V), watch UART on the right sensor board cable. the first 2 values are ADC1 and ADC2. write minimum and maximum poti position-values to ADC?_MIN and ADC?_MAX.
//#define CONTROL_ADC // use ADC as input. disable DEBUG_SERIAL_USART2!
//#define ADC1_MIN 0 // min ADC1-value while poti at minimum-position (0 - 4095)
//#define ADC1_MAX 4095 // max ADC1-value while poti at maximum-position (0 - 4095)
//#define ADC2_MIN 0 // min ADC2-value while poti at minimum-position (0 - 4095)
//#define ADC2_MAX 4095 // max ADC2-value while poti at maximum-position (0 - 4095)
2018-04-09 19:43:59 +00:00
// ###### CONTROL VIA NINTENDO NUNCHUCK ######
2018-05-27 20:16:52 +00:00
// left sensor board cable. keep cable short, use shielded cable, use ferrits, stabalize voltage in nunchuck, use the right one of the 2 types of nunchucks, add i2c pullups.
2018-06-11 12:29:39 +00:00
//#define CONTROL_NUNCHUCK // use nunchuck as input. disable DEBUG_SERIAL_USART3!
2018-04-07 22:03:35 +00:00
2018-04-09 19:43:59 +00:00
// ################################################################################
// ###### DRIVING BEHAVIOR ######
2018-05-27 20:16:52 +00:00
// inputs:
// - cmd1 and cmd2: analog normalized input values. -1000 to 1000
// - 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:
// - speedR and speedL: normal driving -1000 to 1000
// - weakr and weakl: field weakening for extra boost at high speed (speedR > 700 and speedL > 700). 0 to ~400
# define FILTER 0.1 // lower value == softer filter. do not use values <0.01, you will get float precision issues.
# define SPEED_COEFFICIENT 0.5 // higher value == stronger. 0.0 to 1.0
# define STEER_COEFFICIENT 0.5 // higher value == stronger. if you do not want any steering, set it to 0.0; 0.0 to 1.0
//#define INVERT_R_DIRECTION
//#define INVERT_L_DIRECTION
//Turbo boost at high speeds while button1 is pressed:
2018-05-08 11:06:10 +00:00
/ / # define ADDITIONAL_CODE \
2018-05-27 20:16:52 +00:00
if ( button1 & & speedR > 700 ) { /* field weakening at high speeds */ \
2018-05-08 11:06:10 +00:00
weakl = cmd1 - 700 ; /* weak should never exceed 400 or 450 MAX!! */ \
weakr = cmd1 - 700 ; } \
else { \
weakl = 0 ; \
weakr = 0 ; }
2018-04-09 19:43:59 +00:00
// ###### BOBBYCAR ######
// #define FILTER 0.1
2018-04-09 20:03:19 +00:00
// #define SPEED_COEFFICIENT -1
2018-04-09 19:43:59 +00:00
// #define STEER_COEFFICIENT 0
2018-04-25 17:27:15 +00:00
/ / # define ADDITIONAL_CODE \
2018-05-27 20:16:52 +00:00
if ( button1 & & speedR < 300 ) { /* drive backwards */ \
2018-04-25 17:27:15 +00:00
speedR = speedR * - 0.2f ; \
speedL = speedL * - 0.2f ; } \
else { \
direction = 1 ; } \
if ( button1 & & speedR > 700 ) { /* field weakening at high speeds */ \
weakl = speedR - 600 ; /* weak should never exceed 400 or 450 MAX!! */ \
weakr = speedR - 600 ; } \
else { \
weakl = 0 ; \
weakr = 0 ; }
2018-04-09 19:43:59 +00:00
// ###### ARMCHAIR ######
// #define FILTER 0.05
// #define SPEED_COEFFICIENT 0.5
2018-04-09 20:03:19 +00:00
// #define STEER_COEFFICIENT -0.2
2018-04-09 19:43:59 +00:00
2018-05-11 20:04:12 +00:00
/ / # define ADDITIONAL_CODE if ( button1 & & scale > 0.8 ) { /* field weakening at high speeds */ \
weakl = speedL - 600 ; /* weak should never exceed 400 or 450 MAX!! */ \
weakr = speedR - 600 ; } \
else { \
weakl = 0 ; \
weakr = 0 ;
2018-04-07 22:03:35 +00:00
// #define BEEPS_BACKWARD
2018-05-27 20:16:52 +00:00
// ################################################################################
// validate settings (do not touch this):
# if defined DEBUG_SERIAL_USART2 && defined CONTROL_ADC
# error CONTROL_ADC and DEBUG_SERIAL_USART2 not allowed. use DEBUG_SERIAL_USART3 instead.
# endif
# if defined DEBUG_SERIAL_USART2 && defined CONTROL_PPM
# error CONTROL_PPM and DEBUG_SERIAL_USART2 not allowed. use DEBUG_SERIAL_USART3 instead.
# endif
# if defined DEBUG_SERIAL_USART3 && defined CONTROL_NUNCHUCK
# error CONTROL_NUNCHUCK and DEBUG_SERIAL_USART3 not allowed. use DEBUG_SERIAL_USART2 instead.
# endif
# if defined CONTROL_PPM && defined CONTROL_ADC && defined CONTROL_NUNCHUCK || defined CONTROL_PPM && defined CONTROL_ADC || defined CONTROL_ADC && defined CONTROL_NUNCHUCK || defined CONTROL_PPM && defined CONTROL_NUNCHUCK
# error only 1 input method allowed. use CONTROL_PPM or CONTROL_ADC or CONTROL_NUNCHUCK.
# endif