Skip to content

Commit 393bae8

Browse files
authored
feat(flex-stacker): add estop (#482)
1 parent b12ba27 commit 393bae8

File tree

13 files changed

+163
-12
lines changed

13 files changed

+163
-12
lines changed

stm32-modules/flex-stacker/firmware/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ static auto system_task =
6262
extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
6363
switch (GPIO_Pin) {
6464
case MOTOR_DIAG0_PIN:
65+
case N_ESTOP_PIN:
6566
static_cast<void>(aggregator.send_from_isr(
6667
messages::GPIOInterruptMessage{.pin = GPIO_Pin}));
6768
break;

stm32-modules/flex-stacker/firmware/motor_control/motor_hardware.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ typedef struct motor_hardware_struct {
5353
stepper_hardware_t motor_z;
5454
stepper_hardware_t motor_l;
5555
platform_sensor_t platform_sensors;
56+
PinConfig estop;
5657
} motor_hardware_t;
5758

5859

@@ -92,6 +93,7 @@ static motor_hardware_t _motor_hardware = {
9293
.x_minus = {PLAT_SENSE_MINUS_PORT, PLAT_SENSE_MINUS_PIN, GPIO_PIN_SET},
9394
.x_plus = {PLAT_SENSE_PLUS_PORT, PLAT_SENSE_PLUS_PIN, GPIO_PIN_SET},
9495
},
96+
.estop = {N_ESTOP_PORT, N_ESTOP_PIN, GPIO_PIN_RESET},
9597
};
9698

9799
void motor_hardware_gpio_init(void){
@@ -179,9 +181,6 @@ void motor_hardware_gpio_init(void){
179181
init.Pin = L_N_RELEASED_PIN;
180182
HAL_GPIO_Init(L_N_RELEASED_PORT, &init);
181183

182-
init.Pin = ESTOP_PIN;
183-
HAL_GPIO_Init(ESTOP_PORT, &init);
184-
185184
/*Configure GPIO pins : INPUTs IRQ */
186185
init.Mode = GPIO_MODE_IT_FALLING;
187186
init.Pull = GPIO_PULLUP;
@@ -190,6 +189,11 @@ void motor_hardware_gpio_init(void){
190189
init.Pin = MOTOR_DIAG0_PIN;
191190
HAL_GPIO_Init(MOTOR_DIAG0_PORT, &init);
192191
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 6, 0);
192+
193+
init.Pin = N_ESTOP_PIN;
194+
HAL_GPIO_Init(N_ESTOP_PORT, &init);
195+
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
196+
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
193197
}
194198

195199
// X motor timer
@@ -394,12 +398,25 @@ bool hw_read_platform_sensor(bool direction) {
394398
_motor_hardware.platform_sensors.x_minus.active_setting;
395399
}
396400

401+
bool hw_read_estop(void) {
402+
return HAL_GPIO_ReadPin(_motor_hardware.estop.port, _motor_hardware.estop.pin) ==
403+
_motor_hardware.estop.active_setting;
404+
}
405+
397406
void hw_set_diag0_irq(bool enable) {
398407
enable ?
399408
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn) :
400409
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
401410
}
402411

412+
bool hw_is_diag0_pin(uint16_t pin) {
413+
return pin == MOTOR_DIAG0_PIN;
414+
}
415+
416+
bool hw_is_estop_pin(uint16_t pin) {
417+
return pin == N_ESTOP_PIN;
418+
}
419+
403420
void TIM3_IRQHandler(void)
404421
{
405422
HAL_TIM_IRQHandler(&_motor_hardware.motor_l.timer);

stm32-modules/flex-stacker/firmware/motor_control/motor_policy.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,16 @@ auto MotorPolicy::check_platform_sensor(bool direction) -> bool {
4141
auto MotorPolicy::set_diag0_irq(bool enable) -> void {
4242
hw_set_diag0_irq(enable);
4343
}
44+
45+
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
46+
auto MotorPolicy::check_estop() -> bool { return hw_read_estop(); }
47+
48+
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
49+
auto MotorPolicy::is_diag0_pin(uint16_t pin) -> bool {
50+
return hw_is_diag0_pin(pin);
51+
}
52+
53+
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
54+
auto MotorPolicy::is_estop_pin(uint16_t pin) -> bool {
55+
return hw_is_estop_pin(pin);
56+
}

stm32-modules/flex-stacker/firmware/system/stm32g4xx_it.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,10 @@ void EXTI15_10_IRQHandler(void) {
129129
}
130130
}
131131

132+
// Estop interrupt
133+
void EXTI9_5_IRQHandler(void)
134+
{
135+
if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_6)) {
136+
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_6);
137+
}
138+
}

stm32-modules/flex-stacker/firmware/system/stm32g4xx_it.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void DebugMon_Handler(void);
3535
// void SysTick_Handler(void);
3636
void RCC_IRQHandler(void);
3737
void EXTI15_10_IRQHandler(void);
38+
void EXTI9_5_IRQHandler(void);
3839
// void DMA1_Channel1_IRQHandler(void);
3940
// void DMA1_Channel2_IRQHandler(void);
4041

stm32-modules/flex-stacker/src/errors.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const char* const UNHANDLED_GCODE = "ERR003:unhandled gcode\n";
99
const char* const GCODE_CACHE_FULL = "ERR004:gcode cache full\n";
1010
const char* const BAD_MESSAGE_ACKNOWLEDGEMENT =
1111
"ERR005:bad message acknowledgement\n";
12+
const char* const ESTOP_TRIGGERED = "ERR006:estop triggered\n";
1213
const char* const SYSTEM_SERIAL_NUMBER_INVALID =
1314
"ERR301:system:serial number invalid format\n";
1415
const char* const SYSTEM_SERIAL_NUMBER_HAL_ERROR =
@@ -44,6 +45,7 @@ auto errors::errorstring(ErrorCode code) -> const char* {
4445
HANDLE_CASE(UNHANDLED_GCODE);
4546
HANDLE_CASE(GCODE_CACHE_FULL);
4647
HANDLE_CASE(BAD_MESSAGE_ACKNOWLEDGEMENT);
48+
HANDLE_CASE(ESTOP_TRIGGERED);
4749
HANDLE_CASE(SYSTEM_SERIAL_NUMBER_INVALID);
4850
HANDLE_CASE(SYSTEM_SERIAL_NUMBER_HAL_ERROR);
4951
HANDLE_CASE(SYSTEM_EEPROM_ERROR);

stm32-modules/include/flex-stacker/firmware/motor_hardware.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ void hw_set_direction(MotorID, bool direction);
2222
bool hw_read_limit_switch(MotorID motor_id, bool direction);
2323
void hw_set_diag0_irq(bool enable);
2424
bool hw_read_platform_sensor(bool direction);
25+
bool hw_read_estop(void);
26+
bool hw_is_diag0_pin(uint16_t pin);
27+
bool hw_is_estop_pin(uint16_t pin);
2528

2629
#ifdef __cplusplus
2730
} // extern "C"
@@ -95,7 +98,7 @@ bool hw_read_platform_sensor(bool direction);
9598
#define L_N_RELEASED_PORT (GPIOC)
9699

97100
/**************** COMMON ********************/
98-
#define ESTOP_PIN (GPIO_PIN_6)
99-
#define ESTOP_PORT (GPIOB)
101+
#define N_ESTOP_PIN (GPIO_PIN_6)
102+
#define N_ESTOP_PORT (GPIOB)
100103
#define MOTOR_DIAG0_PIN (GPIO_PIN_12)
101104
#define MOTOR_DIAG0_PORT (GPIOB)

stm32-modules/include/flex-stacker/firmware/motor_policy.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class MotorPolicy {
1616
auto check_limit_switch(MotorID motor_id, bool direction) -> bool;
1717
auto set_diag0_irq(bool enable) -> void;
1818
auto check_platform_sensor(bool direction) -> bool;
19+
auto check_estop() -> bool;
20+
auto is_diag0_pin(uint16_t pin) -> bool;
21+
auto is_estop_pin(uint16_t pin) -> bool;
1922
};
2023

2124
} // namespace motor_policy

stm32-modules/include/flex-stacker/flex-stacker/errors.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum class ErrorCode {
1414
UNHANDLED_GCODE = 3,
1515
GCODE_CACHE_FULL = 4,
1616
BAD_MESSAGE_ACKNOWLEDGEMENT = 5,
17+
ESTOP_TRIGGERED = 6,
1718
// 3xx - System General
1819
SYSTEM_SERIAL_NUMBER_INVALID = 301,
1920
SYSTEM_SERIAL_NUMBER_HAL_ERROR = 302,

stm32-modules/include/flex-stacker/flex-stacker/gcodes_motor.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,4 +976,34 @@ struct GetPlatformSensors {
976976
}
977977
};
978978

979+
struct GetEstopStatus {
980+
using ParseResult = std::optional<GetEstopStatus>;
981+
static constexpr auto prefix = std::array{'M', '1', '1', '2'};
982+
983+
template <typename InputIt, typename Limit>
984+
requires std::forward_iterator<InputIt> &&
985+
std::sized_sentinel_for<Limit, InputIt>
986+
static auto parse(const InputIt& input, Limit limit)
987+
-> std::pair<ParseResult, InputIt> {
988+
auto working = prefix_matches(input, limit, prefix);
989+
if (working == input) {
990+
return std::make_pair(ParseResult(), input);
991+
}
992+
return std::make_pair(ParseResult(GetEstopStatus()), working);
993+
}
994+
995+
template <typename InputIt, typename InLimit>
996+
requires std::forward_iterator<InputIt> &&
997+
std::sized_sentinel_for<InputIt, InLimit>
998+
static auto write_response_into(InputIt buf, InLimit limit, int triggered)
999+
-> InputIt {
1000+
int res = 0;
1001+
res = snprintf(&*buf, (limit - buf), "M112 %i OK", triggered);
1002+
if (res <= 0) {
1003+
return buf;
1004+
}
1005+
return buf + res;
1006+
}
1007+
};
1008+
9791009
} // namespace gcode

0 commit comments

Comments
 (0)