2020-03-01 09:00:26 +00:00
|
|
|
/**
|
|
|
|
* 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 UTIL_H
|
|
|
|
#define UTIL_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2020-06-23 17:58:36 +00:00
|
|
|
|
|
|
|
// Rx Structures USART
|
2020-03-01 09:00:26 +00:00
|
|
|
#if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3)
|
2020-11-28 09:38:17 +00:00
|
|
|
#ifdef CONTROL_IBUS
|
2020-03-01 09:00:26 +00:00
|
|
|
typedef struct{
|
|
|
|
uint8_t start;
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t channels[IBUS_NUM_CHANNELS*2];
|
|
|
|
uint8_t checksuml;
|
2020-11-28 09:38:17 +00:00
|
|
|
uint8_t checksumh;
|
2020-06-21 21:14:46 +00:00
|
|
|
} SerialCommand;
|
2020-03-01 09:00:26 +00:00
|
|
|
#else
|
|
|
|
typedef struct{
|
2020-11-28 09:38:17 +00:00
|
|
|
uint16_t start;
|
2020-03-01 09:00:26 +00:00
|
|
|
int16_t steer;
|
|
|
|
int16_t speed;
|
2020-11-28 09:38:17 +00:00
|
|
|
uint16_t checksum;
|
2020-06-21 21:14:46 +00:00
|
|
|
} SerialCommand;
|
2020-03-01 09:00:26 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if defined(SIDEBOARD_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART3)
|
|
|
|
typedef struct{
|
2020-12-07 19:33:22 +00:00
|
|
|
uint16_t start;
|
|
|
|
int16_t pitch; // Angle
|
|
|
|
int16_t dPitch; // Angle derivative
|
|
|
|
int16_t cmd1; // RC Channel 1
|
|
|
|
int16_t cmd2; // RC Channel 2
|
|
|
|
uint16_t sensors; // RC Switches and Optical sideboard sensors
|
|
|
|
uint16_t checksum;
|
2020-03-01 09:00:26 +00:00
|
|
|
} SerialSideboard;
|
|
|
|
#endif
|
|
|
|
|
2020-12-20 09:16:31 +00:00
|
|
|
// Input Structure
|
2020-12-13 20:52:29 +00:00
|
|
|
typedef struct {
|
|
|
|
int16_t raw; // raw input
|
2020-12-20 09:16:31 +00:00
|
|
|
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
|
2020-12-13 20:52:29 +00:00
|
|
|
} InputStruct;
|
|
|
|
|
2020-03-01 09:00:26 +00:00
|
|
|
// Initialization Functions
|
|
|
|
void BLDC_Init(void);
|
|
|
|
void Input_Lim_Init(void);
|
|
|
|
void Input_Init(void);
|
2020-06-25 18:17:55 +00:00
|
|
|
void UART_DisableRxErrors(UART_HandleTypeDef *huart);
|
2020-03-01 09:00:26 +00:00
|
|
|
|
|
|
|
// General Functions
|
|
|
|
void poweronMelody(void);
|
2020-11-28 09:38:17 +00:00
|
|
|
void beepCount(uint8_t cnt, uint8_t freq, uint8_t pattern);
|
|
|
|
void beepLong(uint8_t freq);
|
|
|
|
void beepShort(uint8_t freq);
|
|
|
|
void beepShortMany(uint8_t cnt, int8_t dir);
|
2020-03-01 09:00:26 +00:00
|
|
|
void calcAvgSpeed(void);
|
|
|
|
void adcCalibLim(void);
|
|
|
|
void updateCurSpdLim(void);
|
2020-11-12 21:41:29 +00:00
|
|
|
void standstillHold(void);
|
2020-07-19 09:24:37 +00:00
|
|
|
void electricBrake(uint16_t speedBlend, uint8_t reverseDir);
|
2020-10-20 17:54:38 +00:00
|
|
|
void cruiseControl(uint8_t button);
|
2020-12-13 20:52:29 +00:00
|
|
|
int checkInputType(int16_t min, int16_t mid, int16_t max);
|
2020-03-01 09:00:26 +00:00
|
|
|
|
2020-12-20 09:16:31 +00:00
|
|
|
// Input Functions
|
|
|
|
void calcInputCmd(InputStruct *in, int16_t out_min, int16_t out_max);
|
2020-12-13 20:52:29 +00:00
|
|
|
void readInputRaw(void);
|
2020-12-20 09:16:31 +00:00
|
|
|
void handleTimeout(void);
|
2020-11-18 18:16:56 +00:00
|
|
|
void readCommand(void);
|
2020-06-21 21:14:46 +00:00
|
|
|
void usart2_rx_check(void);
|
|
|
|
void usart3_rx_check(void);
|
|
|
|
#if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3)
|
|
|
|
void usart_process_debug(uint8_t *userCommand, uint32_t len);
|
|
|
|
#endif
|
|
|
|
#if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3)
|
|
|
|
void usart_process_command(SerialCommand *command_in, SerialCommand *command_out, uint8_t usart_idx);
|
|
|
|
#endif
|
|
|
|
#if defined(SIDEBOARD_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART3)
|
|
|
|
void usart_process_sideboard(SerialSideboard *Sideboard_in, SerialSideboard *Sideboard_out, uint8_t usart_idx);
|
|
|
|
#endif
|
2020-03-01 09:00:26 +00:00
|
|
|
|
|
|
|
// Sideboard functions
|
|
|
|
void sideboardLeds(uint8_t *leds);
|
|
|
|
void sideboardSensors(uint8_t sensors);
|
|
|
|
|
2020-12-13 20:52:29 +00:00
|
|
|
// Poweroff Functions
|
|
|
|
void saveConfig(void);
|
|
|
|
void poweroff(void);
|
|
|
|
void poweroffPressCheck(void);
|
|
|
|
|
2020-03-01 09:00:26 +00:00
|
|
|
// Filtering Functions
|
|
|
|
void filtLowPass32(int32_t u, uint16_t coef, int32_t *y);
|
|
|
|
void rateLimiter16(int16_t u, int16_t rate, int16_t *y);
|
|
|
|
void mixerFcn(int16_t rtu_speed, int16_t rtu_steer, int16_t *rty_speedR, int16_t *rty_speedL);
|
|
|
|
|
|
|
|
// Multiple Tap Function
|
|
|
|
typedef struct {
|
2020-11-28 09:38:17 +00:00
|
|
|
uint32_t t_timePrev;
|
|
|
|
uint8_t z_pulseCntPrev;
|
|
|
|
uint8_t b_hysteresis;
|
|
|
|
uint8_t b_multipleTap;
|
2020-03-01 09:00:26 +00:00
|
|
|
} MultipleTap;
|
|
|
|
void multipleTapDet(int16_t u, uint32_t timeNow, MultipleTap *x);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|