Skip to content

Commit

Permalink
Revert "fix(motor-control): Revert motor driver chip errors (#777)"
Browse files Browse the repository at this point in the history
This reverts commit 30ff770.
  • Loading branch information
pmoegenburg committed Jul 22, 2024
1 parent ab1b7d1 commit 71758bd
Show file tree
Hide file tree
Showing 103 changed files with 1,447 additions and 375 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
build-and-test:
name: Run all tests
runs-on: "ubuntu-20.04"
timeout-minutes: 10
timeout-minutes: 20
steps:
- name: Checkout ot3-firmware repo
uses: actions/checkout@v4
Expand Down
24 changes: 20 additions & 4 deletions gantry/core/tasks_proto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ static auto eeprom_data_rev_update_builder =
/**
* Start gantry tasks.
*/
void gantry::tasks::start_tasks(
auto gantry::tasks::start_tasks(
can::bus::CanBus& can_bus,
motion_controller::MotionController<lms::BeltConfig>& motion_controller,
spi::hardware::SpiDeviceBase& spi_device,
tmc2130::configs::TMC2130DriverConfig& driver_configs,
motor_hardware_task::MotorHardwareTask& mh_tsk,
i2c::hardware::I2CBase& i2c2,
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hw_iface) {
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hw_iface)
-> interfaces::diag0_handler {
auto& can_writer = can_task::start_writer(can_bus);
can_task::start_reader(can_bus);
auto& motion = mc_task_builder.start(5, "motion controller",
motion_controller, ::queues, ::queues);
auto& motion =
mc_task_builder.start(5, "motion controller", motion_controller,
::queues, ::queues, ::queues, ::queues);
auto& tmc2130_driver = motor_driver_task_builder.start(
5, "tmc2130 driver", driver_configs, ::queues, spi_task_client);
auto& move_group =
Expand Down Expand Up @@ -116,6 +118,15 @@ void gantry::tasks::start_tasks(
::queues.usage_storage_queue = &usage_storage_task.get_queue();

mh_tsk.start_task();

return gantry::tasks::call_run_diag0_interrupt;
}

void gantry::tasks::call_run_diag0_interrupt() {
if (gantry::tasks::get_tasks().motion_controller) {
return gantry::tasks::get_tasks()
.motion_controller->run_diag0_interrupt();
}
}

gantry::queues::QueueClient::QueueClient(can::ids::NodeId this_fw)
Expand All @@ -131,6 +142,11 @@ void gantry::queues::QueueClient::send_motor_driver_queue(
motor_driver_queue->try_write(m);
}

void gantry::queues::QueueClient::send_motor_driver_queue_isr(
const tmc2130::tasks::TaskMessage& m) {
static_cast<void>(motor_driver_queue->try_write_isr(m));
}

void gantry::queues::QueueClient::send_move_group_queue(
const move_group_task::TaskMessage& m) {
move_group_queue->try_write(m);
Expand Down
24 changes: 20 additions & 4 deletions gantry/core/tasks_rev1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,20 @@ static auto tail_accessor = eeprom::dev_data::DevDataTailAccessor{queues};
/**
* Start gantry ::tasks.
*/
void gantry::tasks::start_tasks(
auto gantry::tasks::start_tasks(
can::bus::CanBus& can_bus,
motion_controller::MotionController<lms::BeltConfig>& motion_controller,
spi::hardware::SpiDeviceBase& spi_device,
tmc2160::configs::TMC2160DriverConfig& driver_configs,
motor_hardware_task::MotorHardwareTask& mh_tsk,
i2c::hardware::I2CBase& i2c2,
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hw_iface) {
eeprom::hardware_iface::EEPromHardwareIface& eeprom_hw_iface)
-> interfaces::diag0_handler {
auto& can_writer = can_task::start_writer(can_bus);
can_task::start_reader(can_bus);
auto& motion = mc_task_builder.start(5, "motion controller",
motion_controller, ::queues, ::queues);
auto& motion =
mc_task_builder.start(5, "motion controller", motion_controller,
::queues, ::queues, ::queues, ::queues);
auto& tmc2160_driver = motor_driver_task_builder.start(
5, "tmc2160 driver", driver_configs, ::queues, spi_task_client);
auto& move_group =
Expand Down Expand Up @@ -115,6 +117,15 @@ void gantry::tasks::start_tasks(
::queues.usage_storage_queue = &usage_storage_task.get_queue();

mh_tsk.start_task();

return gantry::tasks::call_run_diag0_interrupt;
}

void gantry::tasks::call_run_diag0_interrupt() {
if (gantry::tasks::get_tasks().motion_controller) {
return gantry::tasks::get_tasks()
.motion_controller->run_diag0_interrupt();
}
}

gantry::queues::QueueClient::QueueClient(can::ids::NodeId this_fw)
Expand All @@ -130,6 +141,11 @@ void gantry::queues::QueueClient::send_motor_driver_queue(
motor_driver_queue->try_write(m);
}

void gantry::queues::QueueClient::send_motor_driver_queue_isr(
const tmc2160::tasks::TaskMessage& m) {
static_cast<void>(motor_driver_queue->try_write_isr(m));
}

void gantry::queues::QueueClient::send_move_group_queue(
const move_group_task::TaskMessage& m) {
move_group_queue->try_write(m);
Expand Down
37 changes: 25 additions & 12 deletions gantry/firmware/interfaces_proto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ struct motion_controller::HardwareConfig motor_pins_x {
.port = GPIOB,
.pin = GPIO_PIN_7,
.active_setting = GPIO_PIN_RESET},
.estop_in = {
.estop_in =
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = GPIOA,
.pin = GPIO_PIN_10,
.active_setting = GPIO_PIN_RESET},
.diag0 = {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = GPIOA,
.pin = GPIO_PIN_10,
.port = GPIOC,
.pin = GPIO_PIN_5,
.active_setting = GPIO_PIN_RESET}
};

Expand Down Expand Up @@ -125,15 +131,21 @@ struct motion_controller::HardwareConfig motor_pins_y {
.port = GPIOB,
.pin = GPIO_PIN_5,
.active_setting = GPIO_PIN_RESET},
.estop_in = {
.estop_in =
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = GPIOA,
.pin = GPIO_PIN_10,
.active_setting = GPIO_PIN_RESET},
.diag0 = {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = GPIOA,
.pin = GPIO_PIN_10,
.port = GPIOC,
.pin = GPIO_PIN_5,
.active_setting = GPIO_PIN_RESET}
};

static tmc2130::configs::TMC2130DriverConfig gantry_x_driver_configs{
.registers = {.gconfig = {.en_pwm_mode = 1},
.registers = {.gconfig = {.en_pwm_mode = 1, .diag0_error = 1},
.ihold_irun = {.hold_current = 0x2,
.run_current = 0x18,
.hold_current_delay = 0x7},
Expand All @@ -156,7 +168,7 @@ static tmc2130::configs::TMC2130DriverConfig gantry_x_driver_configs{
}};

static tmc2130::configs::TMC2130DriverConfig gantry_y_driver_configs{
.registers = {.gconfig = {.en_pwm_mode = 1},
.registers = {.gconfig = {.en_pwm_mode = 1, .diag0_error = 1},
.ihold_irun = {.hold_current = 0x2,
.run_current = 0x18,
.hold_current_delay = 0x7},
Expand Down Expand Up @@ -232,8 +244,8 @@ static stall_check::StallCheck stallcheck(
* Handler of motor interrupts.
*/
static motor_handler::MotorInterruptHandler motor_interrupt(
motor_queue, gantry::queues::get_queues(), motor_hardware_iface, stallcheck,
update_position_queue);
motor_queue, gantry::queues::get_queues(), gantry::queues::get_queues(),
motor_hardware_iface, stallcheck, update_position_queue);

static auto encoder_background_timer =
motor_encoder::BackgroundTimer(motor_interrupt, motor_hardware_iface);
Expand Down Expand Up @@ -269,13 +281,14 @@ static constexpr auto can_bit_timings =
can::bit_timings::BitTimings<170 * can::bit_timings::MHZ, 100,
500 * can::bit_timings::KHZ, 800>{};

void interfaces::initialize() {
void interfaces::initialize(diag0_handler* call_diag0_handler) {
// Initialize SPI
if (initialize_spi(get_axis_type()) != HAL_OK) {
Error_Handler();
}

initialize_timer(call_motor_handler, enc_overflow_callback);
initialize_timer(call_motor_handler, call_diag0_handler,
enc_overflow_callback);

// Start the can bus
canbus.start(can_bit_timings);
Expand Down
39 changes: 27 additions & 12 deletions gantry/firmware/interfaces_rev1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ struct motion_controller::HardwareConfig motor_pins_x {
.port = GPIOB,
.pin = GPIO_PIN_7,
.active_setting = GPIO_PIN_RESET},
.estop_in = {
.estop_in =
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = GPIOA,
.pin = GPIO_PIN_10,
.active_setting = GPIO_PIN_RESET},
.diag0 = {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = GPIOA,
.pin = GPIO_PIN_10,
.port = GPIOC,
.pin = GPIO_PIN_5,
.active_setting = GPIO_PIN_RESET}
};

Expand Down Expand Up @@ -125,15 +131,21 @@ struct motion_controller::HardwareConfig motor_pins_y {
.port = GPIOB,
.pin = GPIO_PIN_7,
.active_setting = GPIO_PIN_RESET},
.estop_in = {
.estop_in =
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = GPIOA,
.pin = GPIO_PIN_10,
.active_setting = GPIO_PIN_RESET},
.diag0 = {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
.port = GPIOA,
.pin = GPIO_PIN_10,
.port = GPIOC,
.pin = GPIO_PIN_5,
.active_setting = GPIO_PIN_RESET}
};

static tmc2160::configs::TMC2160DriverConfig motor_driver_config_x{
.registers = {.gconfig = {.en_pwm_mode = 0},
.registers = {.gconfig = {.en_pwm_mode = 0, .diag0_error = 1},
.ihold_irun = {.hold_current = 16,
.run_current = 31,
.hold_current_delay = 0x7},
Expand All @@ -159,6 +171,7 @@ static tmc2160::configs::TMC2160DriverConfig motor_driver_config_x{
.freewheel = 0,
.pwm_reg = 0x7,
.pwm_lim = 0xC},
.drvconf = {.ot_select = 0},
.glob_scale = {.global_scaler = 0xA7}},
.current_config =
{
Expand All @@ -172,7 +185,7 @@ static tmc2160::configs::TMC2160DriverConfig motor_driver_config_x{
}};

static tmc2160::configs::TMC2160DriverConfig motor_driver_config_y{
.registers = {.gconfig = {.en_pwm_mode = 0},
.registers = {.gconfig = {.en_pwm_mode = 0, .diag0_error = 1},
.ihold_irun = {.hold_current = 16,
.run_current = 31,
.hold_current_delay = 0x7},
Expand All @@ -198,6 +211,7 @@ static tmc2160::configs::TMC2160DriverConfig motor_driver_config_y{
.freewheel = 0,
.pwm_reg = 0x7,
.pwm_lim = 0xC},
.drvconf = {.ot_select = 0},
.glob_scale = {.global_scaler = 0xA7}},
.current_config =
{
Expand Down Expand Up @@ -257,8 +271,8 @@ static stall_check::StallCheck stallcheck(
* Handler of motor interrupts.
*/
static motor_handler::MotorInterruptHandler motor_interrupt(
motor_queue, gantry::queues::get_queues(), motor_hardware_iface, stallcheck,
update_position_queue);
motor_queue, gantry::queues::get_queues(), gantry::queues::get_queues(),
motor_hardware_iface, stallcheck, update_position_queue);

static auto encoder_background_timer =
motor_encoder::BackgroundTimer(motor_interrupt, motor_hardware_iface);
Expand Down Expand Up @@ -295,13 +309,14 @@ static constexpr auto can_bit_timings =
can::bit_timings::BitTimings<170 * can::bit_timings::MHZ, 100,
500 * can::bit_timings::KHZ, 800>{};

void interfaces::initialize() {
void interfaces::initialize(diag0_handler* call_diag0_handler) {
// Initialize SPI
if (initialize_spi(get_axis_type()) != HAL_OK) {
Error_Handler();
}

initialize_timer(call_motor_handler, enc_overflow_callback);
initialize_timer(call_motor_handler, call_diag0_handler,
enc_overflow_callback);

// Start the can bus
canbus.start(can_bit_timings);
Expand Down
6 changes: 4 additions & 2 deletions gantry/firmware/main_proto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "gantry/core/tasks_proto.hpp"
#include "i2c/firmware/i2c_comms.hpp"

static interfaces::diag0_handler call_diag0_handler = nullptr;

static auto i2c_comms2 = i2c::hardware::I2C();
static auto i2c_handles = I2CHandlerStruct{};

Expand Down Expand Up @@ -44,11 +46,11 @@ auto main() -> int {

app_update_clear_flags();

interfaces::initialize();
interfaces::initialize(&call_diag0_handler);
i2c_setup(&i2c_handles);
i2c_comms2.set_handle(i2c_handles.i2c2);

gantry::tasks::start_tasks(
call_diag0_handler = gantry::tasks::start_tasks(
interfaces::get_can_bus(), interfaces::get_motor().motion_controller,
interfaces::get_spi(), interfaces::get_driver_config(),
interfaces::get_motor_hardware_task(), i2c_comms2, eeprom_hw_iface);
Expand Down
6 changes: 4 additions & 2 deletions gantry/firmware/main_rev1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "gantry/core/tasks_rev1.hpp"
#include "i2c/firmware/i2c_comms.hpp"

static interfaces::diag0_handler call_diag0_handler = nullptr;

static auto i2c_comms2 = i2c::hardware::I2C();
static auto i2c_handles = I2CHandlerStruct{};

Expand Down Expand Up @@ -44,11 +46,11 @@ auto main() -> int {

app_update_clear_flags();

interfaces::initialize();
interfaces::initialize(&call_diag0_handler);
i2c_setup(&i2c_handles);
i2c_comms2.set_handle(i2c_handles.i2c2);

gantry::tasks::start_tasks(
call_diag0_handler = gantry::tasks::start_tasks(
interfaces::get_can_bus(), interfaces::get_motor().motion_controller,
interfaces::get_spi(), interfaces::get_driver_config(),
interfaces::get_motor_hardware_task(), i2c_comms2, eeprom_hw_iface);
Expand Down
Loading

0 comments on commit 71758bd

Please sign in to comment.