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

Use buttons on PB10 & PB11. #24

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

// ################################################################################

//#define RIGHT_BUTTONS // Use PB10=Right_sensor_board_TX and PB11=Right_sensor_board_RX as button input.
#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 (nunchuck) is used!
#define DEBUG_BAUD 115200 // UART baud rate
Expand Down Expand Up @@ -106,6 +107,14 @@ else {\

// validate settings (do not touch this):

#if defined DEBUG_SERIAL_ASCII && defined RIGHT_BUTTONS
#error DEBUG_SERIAL_ASCII and RIGHT_BUTTONS not allowed. Use only one of them.
#endif

#if defined DEBUG_SERIAL_USART3 && defined RIGHT_BUTTONS
#error DEBUG_SERIAL_USART3 and RIGHT_BUTTONS not allowed. Use only one of them.
#endif

#if defined DEBUG_SERIAL_USART2 && defined CONTROL_ADC
#error CONTROL_ADC and DEBUG_SERIAL_USART2 not allowed. use DEBUG_SERIAL_USART3 instead.
#endif
Expand Down
5 changes: 5 additions & 0 deletions Inc/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
#define BUTTON_PIN GPIO_PIN_1
#define BUTTON_PORT GPIOA

#define BUTTON_R_TX_PIN GPIO_PIN_10
#define BUTTON_R_TX_PORT GPIOB
#define BUTTON_R_RX_PIN GPIO_PIN_11
#define BUTTON_R_RX_PORT GPIOB

#define CHARGER_PIN GPIO_PIN_12
#define CHARGER_PORT GPIOA

Expand Down
2 changes: 2 additions & 0 deletions Src/comms.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void setScopeChannel(uint8_t ch, int16_t val) {
}

void consoleScope() {
#ifndef RIGHT_BUTTONS
#ifdef DEBUG_SERIAL_SERVOTERM
uart_buf[0] = 0xff;
uart_buf[1] = CLAMP(ch_buf[0]+127, 0, 255);
Expand Down Expand Up @@ -56,6 +57,7 @@ void consoleScope() {
UART_DMA_CHANNEL->CCR |= DMA_CCR_EN;
}
#endif
#endif
}

void consoleLog(char *message)
Expand Down
40 changes: 40 additions & 0 deletions Src/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,46 @@ extern I2C_HandleTypeDef hi2c2;
DMA_HandleTypeDef hdma_i2c2_rx;
DMA_HandleTypeDef hdma_i2c2_tx;

#ifdef RIGHT_BUTTONS
#define DEBOUNCE 10
uint8_t in1_0, in1_1, in2_0, in2_1;
extern uint8_t button3, button4;

// SysTick executes once each ms
void SysTick_Callback() {
if (HAL_GPIO_ReadPin(BUTTON_R_TX_PORT, BUTTON_R_TX_PIN) == 0) { //button3
in1_0++;
in1_1 = 0;
if (in1_0 >= DEBOUNCE) {
in1_0 = DEBOUNCE;
button3 = 0;
}
} else {
in1_0 = 0;
in1_1++;
if (in1_1 >= DEBOUNCE) {
in1_1 = DEBOUNCE;
button3 = 1;
}
}
if (HAL_GPIO_ReadPin(BUTTON_R_RX_PORT, BUTTON_R_RX_PIN) == 0) { //button4
in2_0++;
in2_1 = 0;
if (in2_0 >= DEBOUNCE) {
in2_0 = DEBOUNCE;
button4 = 0;
}
} else {
in2_0 = 0;
in2_1++;
if (in2_1 >= DEBOUNCE) {
in2_1 = DEBOUNCE;
button4 = 1;
}
}
}
#endif

#ifdef CONTROL_PPM
uint16_t ppm_captured_value[PPM_NUM_CHANNELS + 1] = {500, 500};
uint16_t ppm_captured_value_buffer[PPM_NUM_CHANNELS+1] = {500, 500};
Expand Down
48 changes: 36 additions & 12 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ int cmd2;
int cmd3;

uint8_t button1, button2;
#ifdef RIGHT_BUTTONS
uint8_t button3, button4;
#endif

int steer; // global variable for steering. -1000 to 1000
int speed; // global variable for speed. -1000 to 1000
Expand Down Expand Up @@ -187,7 +190,6 @@ int main(void) {
speedR = CLAMP(speed * SPEED_COEFFICIENT - steer * STEER_COEFFICIENT, -1000, 1000);
speedL = CLAMP(speed * SPEED_COEFFICIENT + steer * STEER_COEFFICIENT, -1000, 1000);


// ####### DEBUG SERIAL OUT #######
#ifdef CONTROL_ADC
setScopeChannel(0, (int)adc_buffer.l_tx2); // ADC1
Expand All @@ -200,19 +202,41 @@ int main(void) {
ADDITIONAL_CODE;
#endif

// ####### SET OUTPUTS #######
if ((speedL < lastSpeedL + 50 && speedL > lastSpeedL - 50) && (speedR < lastSpeedR + 50 && speedR > lastSpeedR - 50) && timeout < TIMEOUT) {
#ifdef INVERT_R_DIRECTION
pwmr = speedR;
#else
pwmr = -speedR;
// ####### EXAMPLE CASE FOR BUTTONS ON PB10 & PB11 #######
#ifdef RIGHT_BUTTONS
if (button3) {
} else {
}
if ((speedL < lastSpeedL + 50 && speedL > lastSpeedL - 50) && (speedR < lastSpeedR + 50 && speedR > lastSpeedR - 50) && timeout < TIMEOUT) {
if (button4) {
LEFT_TIM->BDTR &= ~TIM_BDTR_MOE; //disable left
RIGHT_TIM->BDTR |= TIM_BDTR_MOE; //enable right
pwml = 0; //pwml = -speedL;
pwmr = speedR;
} else {
LEFT_TIM->BDTR |= TIM_BDTR_MOE; //enable left
RIGHT_TIM->BDTR |= TIM_BDTR_MOE; //enable right
pwml = -speedL;
pwmr = speedR;
}
}
#endif
#ifdef INVERT_L_DIRECTION
pwml = -speedL;
#else
pwml = speedL;

// ####### SET OUTPUTS #######
#ifndef RIGHT_BUTTONS
if ((speedL < lastSpeedL + 50 && speedL > lastSpeedL - 50) && (speedR < lastSpeedR + 50 && speedR > lastSpeedR - 50) && timeout < TIMEOUT) {
#ifdef INVERT_R_DIRECTION
pwmr = speedR;
#else
pwmr = -speedR;
#endif
#ifdef INVERT_L_DIRECTION
pwml = -speedL;
#else
pwml = speedL;
#endif
}
#endif
}

lastSpeedL = speedL;
lastSpeedR = speedR;
Expand Down
8 changes: 8 additions & 0 deletions Src/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ void MX_GPIO_Init(void) {
GPIO_InitStruct.Pin = BUTTON_PIN;
HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);

#ifdef RIGHT_BUTTONS
//Use an external pullup or pulldown of 100k on pins.
GPIO_InitStruct.Pin = BUTTON_R_TX_PIN;
HAL_GPIO_Init(BUTTON_R_TX_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BUTTON_R_RX_PIN;
HAL_GPIO_Init(BUTTON_R_RX_PORT, &GPIO_InitStruct);
#endif


GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

Expand Down
7 changes: 7 additions & 0 deletions Src/stm32f1xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ void PendSV_Handler(void) {
#ifdef CONTROL_PPM
void PPM_SysTick_Callback(void);
#endif
#ifdef RIGHT_BUTTONS
void SysTick_Callback(void);
#endif

void SysTick_Handler(void) {
/* USER CODE BEGIN SysTick_IRQn 0 */

Expand All @@ -170,6 +174,9 @@ void SysTick_Handler(void) {
/* USER CODE BEGIN SysTick_IRQn 1 */
#ifdef CONTROL_PPM
PPM_SysTick_Callback();
#endif
#ifdef RIGHT_BUTTONS
SysTick_Callback();
#endif
/* USER CODE END SysTick_IRQn 1 */
}
Expand Down