Skip to content

Commit 4d1fe6f

Browse files
committed
get motor driver task up
1 parent 42fcbcd commit 4d1fe6f

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ auto run(tasks::FirmwareTasks::QueueAggregator* aggregator) -> void {
2626
aggregator->register_queue(_queue);
2727
_top_task.provide_aggregator(aggregator);
2828

29-
// motor_hardware_init();
29+
motor_hardware_init();
3030

31-
// auto policy = motor_policy::MotorPolicy();
31+
auto policy = motor_policy::MotorPolicy();
3232
while (true) {
33-
vTaskDelay(1000);
34-
// _top_task.run_once(policy);
33+
//vTaskDelay(1000);
34+
_top_task.run_once(policy);
3535
}
3636
}
3737

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

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#define Z_DIR_PORT (GPIOC)
1010
#define Z_EN_PIN (GPIO_PIN_3)
1111
#define Z_EN_PORT (GPIOA)
12-
#define Z_N_BRAKE_PIN(GPIO_PIN_7)
13-
#define Z_N_BRAKE_PORT(GPIOB)
12+
#define Z_N_BRAKE_PIN (GPIO_PIN_7)
13+
#define Z_N_BRAKE_PORT (GPIOB)
1414

1515
/** Limit switches **/
1616
/* Note: Photointerrupters limit switches */
@@ -29,8 +29,8 @@
2929
#define X_DIR_PORT (GPIOA)
3030
#define X_EN_PIN (GPIO_PIN_4)
3131
#define X_EN_PORT (GPIOA)
32-
#define X_N_BRAKE_PIN(GPIO_PIN_9)
33-
#define X_N_BRAKE_PORT(GPIOB)
32+
#define X_N_BRAKE_PIN (GPIO_PIN_9)
33+
#define X_N_BRAKE_PORT (GPIOB)
3434

3535

3636
/** Limit switches **/
@@ -48,8 +48,8 @@
4848
#define L_STEP_PORT (GPIOB)
4949
#define L_DIR_PIN (GPIO_PIN_0)
5050
#define L_DIR_PORT (GPIOB)
51-
#define L_EN_PIN (GPIO_PIN_4)
52-
#define L_EN_PORT (GPIOA)
51+
#define L_EN_PIN (GPIO_PIN_5)
52+
#define L_EN_PORT (GPIOC)
5353

5454

5555
/** Limit switches **/
@@ -60,3 +60,64 @@
6060
#define L_N_RELEASED_PORT (GPIO_PIN_C)
6161

6262

63+
void motor_hardware_init(void){
64+
65+
GPIO_InitTypeDef GPIO_InitStruct = {0};
66+
/* USER CODE BEGIN MX_GPIO_Init_1 */
67+
/* USER CODE END MX_GPIO_Init_1 */
68+
69+
/* GPIO Ports Clock Enable */
70+
__HAL_RCC_GPIOC_CLK_ENABLE();
71+
__HAL_RCC_GPIOF_CLK_ENABLE();
72+
__HAL_RCC_GPIOA_CLK_ENABLE();
73+
__HAL_RCC_GPIOB_CLK_ENABLE();
74+
75+
/*Configure GPIO pin Output Level */
76+
HAL_GPIO_WritePin(GPIOC, Z_DIR_PIN|L_N_HELD_PIN, GPIO_PIN_RESET);
77+
78+
/*Configure GPIO pin Output Level */
79+
HAL_GPIO_WritePin(GPIOA, Z_EN_PIN|X_EN_PIN|X_DIR_PIN, GPIO_PIN_RESET);
80+
81+
/*Configure GPIO pin Output Level */
82+
HAL_GPIO_WritePin(GPIOB, L_DIR_PIN, GPIO_PIN_RESET);
83+
84+
/*Configure GPIO pins : Z_MINUS_LIMIT_PIN */
85+
GPIO_InitStruct.Pin = Z_MINUS_LIMIT_PIN;
86+
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
87+
GPIO_InitStruct.Pull = GPIO_NOPULL;
88+
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
89+
90+
/*Configure GPIO pins : Z_DIR_PIN L_EN_PIN */
91+
GPIO_InitStruct.Pin = Z_DIR_PIN|L_EN_PIN;
92+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
93+
GPIO_InitStruct.Pull = GPIO_NOPULL;
94+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
95+
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
96+
97+
/*Configure GPIO pins : Z_PLUS_LIMIT_PIN LIMIT_X__Pin LIMIT_X_A2_Pin */
98+
GPIO_InitStruct.Pin = Z_PLUS_LIMIT_PIN|X_MINUS_LIMIT_PIN|X_PLUS_LIMIT_PIN;
99+
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
100+
GPIO_InitStruct.Pull = GPIO_NOPULL;
101+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
102+
103+
/*Configure GPIO pins : Z_EN_PIN X_EN_PIN X_DIR_PIN */
104+
GPIO_InitStruct.Pin = Z_EN_PIN|X_EN_PIN|X_DIR_PIN;
105+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
106+
GPIO_InitStruct.Pull = GPIO_NOPULL;
107+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
108+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
109+
110+
/*Configure GPIO pins : L_DIR_PIN Z_N_BRAKE_PIN X_N_BRAKE_PIN*/
111+
GPIO_InitStruct.Pin = L_DIR_PIN | Z_N_BRAKE_PIN | X_N_BRAKE_PIN;
112+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
113+
GPIO_InitStruct.Pull = GPIO_NOPULL;
114+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
115+
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
116+
117+
/*Configure GPIO pins : L_N_HELD_PIN */
118+
GPIO_InitStruct.Pin = L_N_HELD_PIN;
119+
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
120+
GPIO_InitStruct.Pull = GPIO_NOPULL;
121+
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
122+
}
123+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace motor_policy {
88

9+
910
class MotorPolicy {
1011
public:
1112
auto enable_motor(MotorID motor_id) -> void;

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,25 @@
1010
#include "core/version.hpp"
1111
#include "flex-stacker/messages.hpp"
1212
#include "flex-stacker/tasks.hpp"
13+
#include "firmware/motor_policy.hpp"
1314
#include "flex-stacker/tmc2160_registers.hpp"
1415
#include "hal/message_queue.hpp"
1516

1617
namespace motor_task {
1718

19+
template <typename P>
20+
concept MotorControlPolicy = requires(P p, MotorID motor_id) {
21+
{
22+
p.enable_motor(motor_id)
23+
} -> std::same_as<void>;
24+
{
25+
p.disable_motor(motor_id)
26+
} -> std::same_as<void>;
27+
{
28+
p.set_motor_speed(motor_id, double{0.0})
29+
} -> std::same_as<bool>;
30+
};
31+
1832
using Message = messages::MotorMessage;
1933

2034
template <template <class> class QueueImpl>
@@ -38,7 +52,9 @@ class MotorTask {
3852
_task_registry = aggregator;
3953
}
4054

41-
auto run_once() -> void {
55+
56+
template <MotorControlPolicy Policy>
57+
auto run_once(Policy& policy) -> void {
4258
if (!_task_registry) {
4359
return;
4460
}

0 commit comments

Comments
 (0)