Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bipropellant protocol #72

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Src/bipropellantProtocol"]
path = Src/bipropellantProtocol
url = https://github.com/bipropellant/hbprotocol.git
12 changes: 12 additions & 0 deletions Inc/bipropellantProtocolMachine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Define to prevent recursive inclusion
#ifndef BIPROPELLANTPROTOCOLMACHINE_H
#define BIPROPELLANTPROTOCOLMACHINE_H

#include "protocol.h"

int setup_protocol(PROTOCOL_STAT *s);

extern PROTOCOL_STAT sUSART2;
extern PROTOCOL_STAT sUSART3;

#endif
94 changes: 94 additions & 0 deletions Inc/bipropellantProtocolStructs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* This file is part of the hoverboard-firmware-hack project.
*
* Copyright (C) 2018 Simon Hailes <btsimonh@googlemail.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/>.
*/

////////////////////////////////////////////////////////////////////
// structures used in main repo which may mirror protocol structures
////////////////////////////////////////////////////////////////////

// Define to prevent recursive inclusion
#ifndef BIPROPELLANTPROTOCOLSTRUCTS_H
#define BIPROPELLANTPROTOCOLSTRUCTS_H

#include "protocol.h"

//// control structures used in firmware

// used in main
extern PROTOCOL_SPEED_DATA SpeedData;

extern PROTOCOL_PWM_DATA PWMData;


#pragma pack(push, 4) // int32_t and float are 4 byte each
typedef struct tag_HALL_DATA_STRUCT{
int32_t HallPosn; // 90 per revolution
int32_t HallSpeed; // speed part calibrated to speed demand value

float HallPosnMultiplier; // m per hall segment

int32_t HallPosn_lastread; // posn offset set via protocol in raw value
int32_t HallPosn_mm; // posn in mm
int32_t HallPosn_mm_lastread; // posn offset set via protocol in mm
int32_t HallSpeed_mm_per_s; // speed in m/s

uint32_t HallTimeDiff;
uint32_t HallSkipped;
} HALL_DATA_STRUCT;
#pragma pack(pop)

#pragma pack(push, 4) // all used types (float and int) are 4 bytes

typedef struct tag_MOTOR_ELECTRICAL{
float dcAmps;
float dcAmpsAvgAcc;
float dcAmpsAvg;
int r1;
int r2;
int q;

int dcAmpsx100;

int pwm_limiter;
int pwm_requested;
int pwm_actual;

unsigned int limiter_count;
} MOTOR_ELECTRICAL;
#pragma pack(pop)

#pragma pack(push, 4) // all used types (float and int) are 4 bytes
typedef struct tag_ELECTRICAL_PARAMS{
int bat_raw;
float batteryVoltage;

int board_temp_raw;
float board_temp_filtered;
float board_temp_deg_c;

int charging;

int dcCurLim; // amps*100
int dc_adc_limit; // limit expressed in terms of ADC units.

MOTOR_ELECTRICAL motors[2];

} ELECTRICAL_PARAMS;
#pragma pack(pop)

#endif
12 changes: 12 additions & 0 deletions Inc/bldc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef BLDC_H
#define BLDC_H

#include <stdint.h>

uint8_t bldc_getMotorsEnable(void);
void bldc_setMotorsEnable(uint8_t enable);

extern int16_t batVoltage;
extern int32_t batVoltageFixdt;

#endif
1 change: 0 additions & 1 deletion Inc/comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#ifndef COMMS_H
#define COMMS_H

#include "stm32f1xx_hal.h"

void setScopeChannel(uint8_t ch, int16_t val);
void consoleScope(void);
Expand Down
36 changes: 29 additions & 7 deletions Inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//#define VARIANT_HOVERCAR // Variant for HOVERCAR build
//#define VARIANT_HOVERBOARD // Variant for HOVERBOARD build
//#define VARIANT_TRANSPOTTER // Variant for TRANSPOTTER build https://github.com/NiklasFauth/hoverboard-firmware-hack/wiki/Build-Instruction:-TranspOtter https://hackaday.io/project/161891-transpotter-ng
//#define VARIANT_BIPROPELLANT// Variant for Bipropellant Protocol
#endif
// ########################### END OF VARIANT SELECTION ############################

Expand Down Expand Up @@ -73,7 +74,7 @@
#define BAT_BLINK_INTERVAL 80 // battery led blink interval (80 loops * 5ms ~= 400ms)
#define BAT_LVL5 (390 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Green blink: no beep
#define BAT_LVL4 (380 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Yellow: no beep
#define BAT_LVL3 (370 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Yellow blink: no beep
#define BAT_LVL3 (370 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Yellow blink: no beep
#define BAT_LVL2 (360 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Red: gently beep at this voltage level. [V*100/cell]. In this case 3.60 V/cell
#define BAT_LVL1 (350 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Red blink: fast beep. Your battery is almost empty. Charge now! [V*100/cell]. In this case 3.50 V/cell
#define BAT_DEAD (337 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // All leds off: undervoltage poweroff. (while not driving) [V*100/cell]. In this case 3.37 V/cell
Expand Down Expand Up @@ -252,6 +253,21 @@
#endif
// ######################## END OF VARIANT_USART SETTINGS #########################

// ############################ VARIANT_BIPROPELLANT SETTINGS ############################
#ifdef VARIANT_BIPROPELLANT
// #define USART2_ENABLE
#define USART3_ENABLE
#define USART2_BAUD 115200
#define USART3_BAUD 115200
#undef TIMEOUT
#define TIMEOUT 30 // number of wrong / missing input commands before emergency off
#undef BEEPS_BACKWARD
#define BEEPS_BACKWARD 0 // 0 or 1
#define SERIAL_BUFFER_SIZE 64 // [bytes] Size of Serial Rx buffer. Make sure it is always larger than the structure size
#define USART2_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B
#define USART3_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B
#endif
// ######################## END OF VARIANT_BIPROPELLANT SETTINGS #########################


// ################################# VARIANT_NUNCHUK SETTINGS ############################
Expand Down Expand Up @@ -318,7 +334,7 @@
#define PWM_CH1_MAX 1000 // (0 - 1000)
#define PWM_CH1_MIN -1000 // (-1000 - 0)
#define PWM_CH2_MAX 1000 // (0 - 1000)
#define PWM_CH2_MIN -1000 // (-1000 - 0)
#define PWM_CH2_MIN -1000 // (-1000 - 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.
Expand All @@ -333,7 +349,7 @@

// ################################# VARIANT_IBUS SETTINGS ##############################
#ifdef VARIANT_IBUS
/* CONTROL VIA RC REMOTE WITH FLYSKY IBUS PROTOCOL
/* CONTROL VIA RC REMOTE WITH FLYSKY IBUS PROTOCOL
* Connected to Left sensor board cable. Channel 1: steering, Channel 2: speed.
*/
#define CONTROL_IBUS // use IBUS as input
Expand Down Expand Up @@ -384,10 +400,10 @@
// Communication: [DONE]
// Balancing controller: [TODO]
#ifdef VARIANT_HOVERBOARD
#define SIDEBOARD_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used!
#define SIDEBOARD_SERIAL_USART2 // left sensor board cable, 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 FEEDBACK_SERIAL_USART3
#define SIDEBOARD_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used!
#define FEEDBACK_SERIAL_USART3
#endif
// ######################## END OF VARIANT_HOVERBOARD SETTINGS #########################

Expand Down Expand Up @@ -429,6 +445,12 @@
#define USART3_BAUD 38400 // UART3 baud rate (short wired cable)
#define USART3_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B
#endif
#if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2)
#define USART2_ENABLE
#endif
#if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3)
#define USART3_ENABLE
#endif
// ########################### UART SETIINGS ############################


Expand All @@ -452,7 +474,7 @@

// ############################### VALIDATE SETTINGS ###############################
#if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) && !defined(VARIANT_PWM) && \
!defined(VARIANT_IBUS) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_HOVERBOARD) && !defined(VARIANT_TRANSPOTTER)
!defined(VARIANT_IBUS) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_HOVERBOARD) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_BIPROPELLANT)
#error Variant not defined! Please check platformio.ini or Inc/config.h for available variants.
#endif

Expand Down
3 changes: 3 additions & 0 deletions Inc/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define SETUP_H

#include "stm32f1xx_hal.h"
#include "defines.h"

void MX_GPIO_Init(void);
void MX_TIM_Init(void);
Expand All @@ -32,5 +33,7 @@ void MX_ADC2_Init(void);
void UART2_Init(void);
void UART3_Init(void);

extern volatile adc_buf_t adc_buffer;

#endif

21 changes: 7 additions & 14 deletions Inc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@

// Rx Structures USART
#if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3)
#ifdef CONTROL_IBUS
#ifdef CONTROL_IBUS
typedef struct{
uint8_t start;
uint8_t type;
uint8_t type;
uint8_t channels[IBUS_NUM_CHANNELS*2];
uint8_t checksuml;
uint8_t checksumh;
uint8_t checksumh;
} SerialCommand;
#else
typedef struct{
uint16_t start;
uint16_t start;
int16_t steer;
int16_t speed;
uint16_t checksum;
uint16_t checksum;
} SerialCommand;
#endif
#endif
Expand Down Expand Up @@ -79,15 +79,6 @@ void poweroffPressCheck(void);
void readCommand(void);
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

// Sideboard functions
void sideboardLeds(uint8_t *leds);
Expand All @@ -107,5 +98,7 @@ typedef struct {
} MultipleTap;
void multipleTapDet(int16_t u, uint32_t timeNow, MultipleTap *x);

extern uint8_t ctrlModReq;

#endif

7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Src/hd44780.c \
Src/pcf8574.c \
Src/comms.c \
Src/stm32f1xx_it.c \
Src/bipropellantProtocol/protocol.c \
Src/bipropellantProtocol/machine_protocol.c \
Src/bipropellantProtocol/ascii_protocol.c \
Src/bipropellantProtocol/cobsr.c \
Src/bipropellantProtocolASCII.c \
Src/bipropellantProtocolMachine.c \
Src/BLDC_controller_data.c \
Src/BLDC_controller.c

Expand Down Expand Up @@ -97,6 +103,7 @@ AS_INCLUDES =
# C includes
C_INCLUDES = \
-IInc \
-ISrc/bipropellantProtocol \
-IDrivers/STM32F1xx_HAL_Driver/Inc \
-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \
Expand Down
1 change: 1 addition & 0 deletions Src/bipropellantProtocol
Submodule bipropellantProtocol added at fa5e75
Loading