Zap'Em Blast'Em Robots
robots that play laser tag!
Loading...
Searching...
No Matches
main.c File Reference

: Main program body More...

#include "main.h"
#include "controller_task.h"
#include "motor_driver.h"
#include "game_task.h"
#include "sound_task.h"
#include "shoot_task.h"
#include "lcd.h"
#include "photoresistor_task.h"
#include "adc_task.h"
#include "encoder_driver.h"
#include "stm32f4xx_it.h"
#include <stdint.h>

Functions

void calibration (void)
 This function runs at start to calibrate DC motors and photoresistors.
void wait_for_stall (encoder_t *encoder)
 Waits until the encoder motion stalls (no significant ticks change).
void SystemClock_Config (void)
 System Clock Configuration.
int main (void)
 The application entry point.
void HAL_GPIO_EXTI_Callback (uint16_t GPIO_Pin)
void Error_Handler (void)
 This function is executed in case of error occurrence.

Variables

ADC_HandleTypeDef hadc1
DMA_HandleTypeDef hdma_adc1
I2C_HandleTypeDef hi2c1
 External handle to use your I2C peripheral.
TIM_HandleTypeDef htim1
TIM_HandleTypeDef htim2
TIM_HandleTypeDef htim3
TIM_HandleTypeDef htim4
TIM_HandleTypeDef htim5
TIM_HandleTypeDef htim9
uint8_t red_held = 0
uint8_t blue_held = 0
uint32_t red_start = 0
uint32_t blue_start = 0
uint32_t red_elapsed = 0
uint32_t blue_elapsed = 0
const uint32_t hold_time = 2000
motor_t mred
motor_t mblue
encoder_t red_encoder
encoder_t blue_encoder
SoundTask sound_task
PhotoresistorTask red_photoresistor_task
PhotoresistorTask blue_photoresistor_task
GameTask game_task
ShootTask red_shoot_task
ShootTask blue_shoot_task
ControllerTask blue_controller_task
ControllerTask red_controller_task
ADCTask adc_task

Detailed Description

: Main program body

Attention

Copyright (c) 2025 STMicroelectronics. All rights reserved.

This software is licensed under terms that can be found in the LICENSE file in the root directory of this software component. If no LICENSE file comes with this software, it is provided AS-IS.

Function Documentation

◆ calibration()

void calibration ( void )

This function runs at start to calibrate DC motors and photoresistors.

Return values
None

◆ Error_Handler()

void Error_Handler ( void )

This function is executed in case of error occurrence.

Return values
None

◆ main()

int main ( void )

The application entry point.

Return values
int

◆ SystemClock_Config()

void SystemClock_Config ( void )

System Clock Configuration.

Return values
None

Configure the main internal regulator output voltage

Initializes the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef structure.

Initializes the CPU, AHB and APB buses clocks

◆ wait_for_stall()

void wait_for_stall ( encoder_t * p_enc)

Waits until the encoder motion stalls (no significant ticks change).

Parameters
p_encPointer to encoder_t instance

Variable Documentation

◆ adc_task

ADCTask adc_task
Initial value:
= {.state = 0,
.num_states = 2,
.red_contr_ptr = &red_controller_task,
.blue_contr_ptr = &blue_controller_task,
.red_photor_ptr = &red_photoresistor_task,
.blue_photor_ptr = &blue_photoresistor_task,
.hadc = &hadc1,
.state_list = {&adc_task_state_0_init,
}
void adc_task_state_0_init(ADCTask *adc_task)
Initializes the ADC task.
Definition adc_task.c:58
void adc_task_state_1_read(ADCTask *adc_task)
Reads ADC values from DMA buffer and assigns them to respective photo resistors and controllers.
Definition adc_task.c:75

◆ blue_controller_task

ControllerTask blue_controller_task
Initial value:
= {.dir = -1,
.state = 0,
.num_states = 2,
.chan1 = TIM_CHANNEL_3,
.chan2 = TIM_CHANNEL_4,
.pot_zero = 0,
.des_pos = 0,
.prev_time = 0,
.current_time = 0,
.prev_ticks = 0,
.k_p = 1,
.k_i = 0,
.adc_val = 0,
.integral_error = 0,
.htim_encoder = &htim3,
.htim_dt = &htim2,
.hadc = &hadc1,
.motor = &mblue,
.encoder = &blue_encoder,
}
void controller_task_state_1_pos(ControllerTask *controller_task)
Executes position control logic (State 1).
Definition controller_task.c:70
void controller_task_state_0_init(ControllerTask *controller_task)
Initializes the controller task (State 0).
Definition controller_task.c:50

◆ blue_encoder

encoder_t blue_encoder
Initial value:
= {.zero = 0,
.ar = 0xFFFF,
.ticks = 0,
.last_ticks = 0,
.range = 1300,
.htim = &htim3
}

◆ blue_photoresistor_task

PhotoresistorTask blue_photoresistor_task
Initial value:
= {.state = 0,
.num_states = 3,
.hit_flag = 0,
.adc_val = 0,
.thresh = 2000,
.zero = 0,
}
void photoresistor_task_state_0_init(PhotoresistorTask *photoresistor_task)
Initializes the photoresistor task (state 0).
Definition photoresistor_task.c:44
void photoresistor_task_state_1_look(PhotoresistorTask *photoresistor_task)
Checks for light detection and sets hit flag (state 1).
Definition photoresistor_task.c:58
void photoresistor_task_state_2_hit(PhotoresistorTask *photoresistor_task)
Waits for score handling and clears hit flag (state 2).
Definition photoresistor_task.c:77

◆ blue_shoot_task

ShootTask blue_shoot_task
Initial value:
= {.state = 0,
.num_states = 4,
.button = 0,
.servo_tim = &htim4,
.channel = TIM_CHANNEL_1,
.shield_val = 2900,
.unshield_val = 400,
.laser_gpio = GPIO_PIN_14,
.state_list = {&shoot_task_state_0_init,
}
void shoot_task_state_3_shoot(ShootTask *shoot_task)
Fires the laser and resets state if button is released (state 3).
Definition shoot_task.c:101
void shoot_task_state_0_init(ShootTask *shoot_task)
Initializes the shoot task (state 0).
Definition shoot_task.c:45
void shoot_task_state_2_unshield(ShootTask *shoot_task)
Lowers the servo shield before firing (state 2).
Definition shoot_task.c:81
void shoot_task_state_1_wait(ShootTask *shoot_task)
Waits for button press to initiate action (state 1).
Definition shoot_task.c:62

◆ game_task

GameTask game_task
Initial value:
= {.state = 0,
.num_states = 4,
.play_flag = 0,
.score_red = 0,
.score_blue = 0,
.score_red_prev = 0,
.score_blue_prev = 0,
.score_thresh = 5,
.num = 0,
.red_delay_start = 0,
.blue_delay_start = 0,
.red_delay_flag = 0,
.blue_delay_flag = 0,
.delay = 2000,
.end_delay = 5000,
.sound_task_ptr = &sound_task,
.red_photoresistor_task_ptr = &red_photoresistor_task,
.blue_photoresistor_task_ptr = &blue_photoresistor_task,
.mred = &mred,
.mblue = &mblue,
.i2c_handle = &hi2c1,
.state_list = {&game_task_state_0_init,
}
void game_task_state_2_play(GameTask *game_task)
Handles gameplay logic and scoring (state 2).
Definition game_task.c:88
void game_task_state_3_end(GameTask *game_task)
Displays game over screen and resets (state 3).
Definition game_task.c:179
void game_task_state_0_init(GameTask *game_task)
Initializes the game task (state 0).
Definition game_task.c:50
void game_task_state_1_home(GameTask *game_task)
Waits for player to initiate the game (state 1).
Definition game_task.c:64
I2C_HandleTypeDef hi2c1
External handle to use your I2C peripheral.
Definition main.c:57

◆ mblue

motor_t mblue
Initial value:
= {
.tim = &htim1,
.chan1 = TIM_CHANNEL_1,
.chan2 = TIM_CHANNEL_2
}

◆ mred

motor_t mred
Initial value:
= {
.tim = &htim1,
.chan1 = TIM_CHANNEL_3,
.chan2 = TIM_CHANNEL_4
}

◆ red_controller_task

ControllerTask red_controller_task
Initial value:
= {.dir = 1,
.state = 0,
.num_states = 2,
.chan1 = TIM_CHANNEL_1,
.chan2 = TIM_CHANNEL_2,
.pot_zero = 0,
.des_pos = 0,
.prev_time = 0,
.current_time = 0,
.prev_ticks = 0,
.k_p = -1,
.k_i = 0,
.adc_val = 0,
.integral_error = 0,
.htim_encoder = &htim5,
.htim_dt = &htim2,
.hadc = &hadc1,
.motor = &mred,
.encoder = &red_encoder,
}

◆ red_encoder

encoder_t red_encoder
Initial value:
= {.zero = 0,
.ar = 0xFFFFFFFF,
.ticks = 0,
.last_ticks = 0,
.range = 1300,
.htim = &htim5
}

◆ red_photoresistor_task

PhotoresistorTask red_photoresistor_task
Initial value:
= {.state = 0,
.num_states = 3,
.hit_flag = 0,
.adc_val = 0,
.thresh = 2000,
.zero = 0,
}

◆ red_shoot_task

ShootTask red_shoot_task
Initial value:
= {.state = 0,
.num_states = 4,
.button = 0,
.servo_tim = &htim4,
.channel = TIM_CHANNEL_2,
.shield_val = 2900,
.unshield_val = 400,
.laser_gpio = GPIO_PIN_15,
.state_list = {&shoot_task_state_0_init,
}

◆ sound_task

SoundTask sound_task
Initial value:
= {.state = 0,
.num_states = 7,
.laser_snd = 0,
.hit_snd = 0,
.start_snd = 0,
.win_snd = 0,
.htim = &htim2,
.chan = TIM_CHANNEL_1,
.state_list = {&sound_task_state_0_init,
}
void sound_task_state_3_hit(SoundTask *sound_task)
Starts the interrupts and sets the array pointer for the hit sound(state 3).
Definition sound_task.c:126
void sound_task_state_2_laser(SoundTask *sound_task)
Starts the interrupts and sets the array pointer for the laser sound(state 2).
Definition sound_task.c:97
void sound_task_state_5_start(SoundTask *sound_task)
Starts the interrupts and sets the array pointer for the start sound(state 5).
Definition sound_task.c:190
void sound_task_state_0_init(SoundTask *sound_task)
Initializes the sound task (state 0).
Definition sound_task.c:57
void sound_task_state_6_stop(SoundTask *sound_task)
Stops the interrupts and PWM for speaker after sound played (state 6)
Definition sound_task.c:221
void sound_task_state_1_wait(SoundTask *sound_task)
Waits for sound flags (state 1).
Definition sound_task.c:71
void sound_task_state_4_win(SoundTask *sound_task)
Starts the interrupts and sets the array pointer for the win sound(state 4).
Definition sound_task.c:158