Skip to content

Commit

Permalink
use callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ahiuchingau committed Jan 5, 2024
1 parent 7040f20 commit 508010b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
13 changes: 5 additions & 8 deletions gripper/firmware/interfaces_z_motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,24 @@ static motor_handler::MotorInterruptHandler motor_interrupt(
static auto encoder_background_timer =
motor_encoder::BackgroundTimer(motor_interrupt, motor_hardware_iface);

extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
// disengage motor whenever estop is engaged
if (GPIO_Pin == ESTOP_IN_PIN) {
z_motor.motion_controller.disable_motor();
}
}

/**
* Timer callback.
*/
extern "C" void call_motor_handler(void) { motor_interrupt.run_interrupt(); }
extern "C" void call_enc_handler(int32_t direction) {
motor_hardware_iface.encoder_overflow(direction);
}
extern "C" void disengage_z_callback_glue(void) {
motor_hardware_iface.deactivate_motor();
}

void z_motor_iface::initialize() {
if (initialize_spi() != HAL_OK) {
Error_Handler();
}
initialize_hardware_z();
set_z_motor_timer_callback(call_motor_handler, call_enc_handler);
set_z_motor_timer_callback(call_motor_handler, call_enc_handler,
disengage_z_callback_glue);
encoder_background_timer.start();
}

Expand Down
4 changes: 3 additions & 1 deletion gripper/firmware/motor_hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ extern TIM_HandleTypeDef htim8;

typedef void (*motor_interrupt_callback)();
typedef void (*z_encoder_overflow_callback)(int32_t);
typedef void (*z_motor_disengage_callback)();


HAL_StatusTypeDef initialize_spi();
void initialize_hardware_z();

void set_z_motor_timer_callback(
motor_interrupt_callback callback,
z_encoder_overflow_callback enc_callback);
z_encoder_overflow_callback enc_callback,
z_motor_disengage_callback disengage_callback);


// G motor specific
Expand Down
10 changes: 9 additions & 1 deletion gripper/firmware/motor_hardware_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ static encoder_overflow_callback gripper_enc_overflow_callback = NULL;
static encoder_idle_state_callback gripper_enc_idle_state_overflow_callback =
NULL;
static stopwatch_overflow_callback gripper_force_stopwatch_overflow_callback = NULL;
static z_motor_disengage_callback disengage_z_callback = NULL;


void set_z_motor_timer_callback(
motor_interrupt_callback callback,
z_encoder_overflow_callback enc_callback) {
z_encoder_overflow_callback enc_callback,
z_motor_disengage_callback disengage_callback) {
timer_callback = callback;
z_enc_overflow_callback = enc_callback;
disengage_z_callback = disengage_callback;
}

void set_brushed_motor_timer_callback(
Expand Down Expand Up @@ -214,3 +217,8 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim) {
}


void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
if (GPIO_Pin == Z_MOT_ENABLE_PIN && disengage_z_callback) {
disengage_z_callback();
}
}
15 changes: 6 additions & 9 deletions head/firmware/main_rev1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ extern "C" void left_enc_overflow_callback_glue(int32_t direction) {
extern "C" void right_enc_overflow_callback_glue(int32_t direction) {
motor_hardware_right.encoder_overflow(direction);
}
extern "C" void motor_disengage_callback_glue() {
motor_hardware_left.deactivate_motor();
motor_hardware_right.deactivate_motor();
}

static auto psd = presence_sensing_driver::PresenceSensingHardware{
gpio::PinConfig{// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
Expand Down Expand Up @@ -411,22 +415,15 @@ class EEPromHardwareInterface
};
static auto eeprom_hw_iface = EEPromHardwareInterface();

extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
// disengage motor whenever estop is engaged
if (GPIO_Pin == pin_configurations_left.estop_in.pin) {
motor_left.motion_controller.disable_motor();
motor_right.motion_controller.disable_motor();
}
}

auto main() -> int {
HardwareInit();
RCC_Peripheral_Clock_Select();

app_update_clear_flags();

initialize_timer(motor_callback_glue, left_enc_overflow_callback_glue,
right_enc_overflow_callback_glue);
right_enc_overflow_callback_glue,
motor_disengage_callback_glue);

i2c_setup(&i2c_handles);
i2c_comms3.set_handle(i2c_handles.i2c3);
Expand Down
5 changes: 4 additions & 1 deletion head/firmware/motor_hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ extern TIM_HandleTypeDef htim3;

typedef void (*motor_interrupt_callback)();
typedef void (*encoder_overflow_callback)(int32_t);
typedef void (*disable_motor_callback)();

HAL_StatusTypeDef initialize_spi(SPI_HandleTypeDef* hspi);
void initialize_timer(motor_interrupt_callback callback,
encoder_overflow_callback left_enc_overflow_callback,
encoder_overflow_callback right_enc_overflow_callback);
encoder_overflow_callback right_enc_overflow_callback,
disable_motor_callback disengage_callback);
void initialize_rev_specific_pins();

#ifdef __cplusplus
Expand Down
13 changes: 12 additions & 1 deletion head/firmware/motor_hardware_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ motor_interrupt_callback motor_callback = NULL;
encoder_overflow_callback left_enc_overflow_callback = NULL;
encoder_overflow_callback right_enc_overflow_callback = NULL;

disable_motor_callback motor_disengage_callback = NULL;

void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
if (hspi->Instance == SPI2) {
Expand Down Expand Up @@ -334,12 +336,21 @@ void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim) {
}
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
// disengage motor whenever estop is engaged
if (GPIO_Pin == GPIO_PIN_4 && motor_disengage_callback) {
motor_disengage_callback();
}
}

void initialize_timer(motor_interrupt_callback callback,
encoder_overflow_callback l_f_callback,
encoder_overflow_callback r_f_callback) {
encoder_overflow_callback r_f_callback,
disable_motor_callback disengage_callback) {
motor_callback = callback;
left_enc_overflow_callback = l_f_callback;
right_enc_overflow_callback = r_f_callback;
motor_disengage_callback = disengage_callback;
MX_GPIO_Init();
Encoder_GPIO_Init();
encoder_init(&htim2);
Expand Down

0 comments on commit 508010b

Please sign in to comment.