Skip to content

Commit

Permalink
Weapon
Browse files Browse the repository at this point in the history
  • Loading branch information
Candas1 committed Jun 24, 2024
1 parent f32af99 commit f59c408
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Inc/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@
// #define TANK_STEERING // use for tank steering, each input controls each wheel

#if defined(CONTROL_SERIAL_USART3) && !defined(DUAL_INPUTS)
#define DEBUG_SERIAL_USART2 // left sensor cable debug
//#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
Expand Down
1 change: 1 addition & 0 deletions Inc/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

void MX_GPIO_Init(void);
void MX_TIM_Init(void);
void MX_TIM2_Init(void);
void MX_ADC1_Init(void);
void MX_ADC2_Init(void);
void UART2_Init(void);
Expand Down
37 changes: 33 additions & 4 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ 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 int16_t weapon;
static int16_t weaponRateFixdt; // local fixed-point variable for weapon rate limiter
static int32_t weaponFixdt; // local fixed-point variable for weapon low-pass filter


static uint32_t buzzerTimer_prev = 0;
static uint32_t inactivity_timeout_counter;
static MultipleTap MultipleTapBrake; // define multiple tap functionality for the Brake pedal
Expand Down Expand Up @@ -205,6 +210,7 @@ int main(void) {
__HAL_RCC_DMA1_CLK_DISABLE();
MX_GPIO_Init();
MX_TIM_Init();
MX_TIM2_Init();
MX_ADC1_Init();
MX_ADC2_Init();
BLDC_Init(); // BLDC Controller Init
Expand Down Expand Up @@ -271,23 +277,46 @@ int main(void) {
swa){
beepShort(6); // make 2 beeps indicating the motor enable
beepShort(4); HAL_Delay(100);
steerFixdt = speedFixdt = 0; // reset filters
weaponFixdt = steerFixdt = speedFixdt = 0; // reset filters
enable = 1; // enable motors

#if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3)
printf("-- Motors enabled --\r\n");
#endif
}

if (enable == 1 && !swa){
if (enable == 1 && !swa){
beepShort(4); // make 2 beeps indicating the motor enable
beepShort(6); HAL_Delay(100);
steerFixdt = speedFixdt = 0; // reset filters
weaponFixdt = steerFixdt = speedFixdt = 0; // reset filters
enable = 0; // enable motors

#if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3)
printf("-- Motors disabled --\r\n");
#endif
}

if (swa && swd){
weapon = vra;
}else{
weapon = 0;
}

rateLimiter16(weapon, 10, &weaponRateFixdt);
filtLowPass32(weaponRateFixdt >> 4, FILTER, &weaponFixdt);

TIM2->CCR3 = (int16_t)(weaponFixdt >> 16) * 10;

if (TIM2->CCR3){
// Weapon PWM ON
TIM2->BDTR |= TIM_BDTR_MOE;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
}
else{
// Weapon PWM OFF
TIM2->BDTR &= ~TIM_BDTR_MOE;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
}

// ####### VARIANT_HOVERCAR #######
#if defined(VARIANT_HOVERCAR) || defined(VARIANT_SKATEBOARD) || defined(ELECTRIC_BRAKE_ENABLE)
Expand Down
45 changes: 41 additions & 4 deletions Src/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pb10 usart3 dma1 channel2/3

TIM_HandleTypeDef htim_right;
TIM_HandleTypeDef htim_left;
TIM_HandleTypeDef htim_out;
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
I2C_HandleTypeDef hi2c2;
Expand Down Expand Up @@ -415,6 +416,8 @@ void MX_GPIO_Init(void) {
GPIO_InitStruct.Pin = OFF_PIN;
HAL_GPIO_Init(OFF_PORT, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

Expand All @@ -441,10 +444,10 @@ void MX_GPIO_Init(void) {

//Analog in
#if !defined(SUPPORT_BUTTONS_LEFT)
GPIO_InitStruct.Pin = GPIO_PIN_3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//GPIO_InitStruct.Pin = GPIO_PIN_3;
//HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//GPIO_InitStruct.Pin = GPIO_PIN_2;
//HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
Expand Down Expand Up @@ -484,6 +487,40 @@ void MX_GPIO_Init(void) {

GPIO_InitStruct.Pin = RIGHT_TIM_WL_PIN;
HAL_GPIO_Init(RIGHT_TIM_WL_PORT, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

void MX_TIM2_Init(void) {
__HAL_RCC_TIM2_CLK_ENABLE();

TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;

htim_out.Instance = TIM2;
htim_out.Init.Prescaler = 0;
htim_out.Init.CounterMode = TIM_COUNTERMODE_UP;
htim_out.Init.Period = 64000000 / 6400;
htim_out.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim_out.Init.RepetitionCounter = 0;
htim_out.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_TIM_PWM_Init(&htim_out);

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim_out, &sMasterConfig);

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim_out, &sConfigOC, TIM_CHANNEL_3);

TIM2->BDTR &= ~TIM_BDTR_MOE;

HAL_TIM_PWM_Start(&htim_out, TIM_CHANNEL_3);
__HAL_TIM_ENABLE(&htim_out);
}

void MX_TIM_Init(void) {
Expand Down

0 comments on commit f59c408

Please sign in to comment.