1 /* RMT example -- step motor */
2
3 /*
4 * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
5 *
6 * SPDX-License-Identifier: CC0-1.0
7 */
8
9 #include "sdkconfig.h"
10 #include "freertos/FreeRTOS.h"
11 #include "freertos/task.h"
12 #include "esp_log.h"
13 #include "driver/rmt.h"
14 #include "step_motor.h"
15 #include "step_motor_driver_io_a4988.h"
16
17 // GPIO configuration
18 #define STEP_MOTOR_DIRECTION_PIN GPIO_NUM_18
19 #define STEP_MOTOR_STEP_PIN GPIO_NUM_17
20 #define STEP_MOTOR_SLEEP_PIN GPIO_NUM_16
21 #define STEP_MOTOR_RESET_PIN GPIO_NUM_15
22 #define STEP_MOTOR_MS3_PIN GPIO_NUM_7
23 #define STEP_MOTOR_MS2_PIN GPIO_NUM_6
24 #define STEP_MOTOR_MS1_PIN GPIO_NUM_5
25 #define STEP_MOTOR_ENABLE_PIN GPIO_NUM_4
26
27 #define RMT_TX_CHANNEL RMT_CHANNEL_0
28
29 static const char *TAG = "step_motor";
30
app_main(void)31 void app_main(void)
32 {
33 // Apply default RMT configuration
34 rmt_config_t dev_config = RMT_DEFAULT_CONFIG_TX(STEP_MOTOR_STEP_PIN, RMT_TX_CHANNEL);
35
36 step_motor_io_a4988_conf_t a4988_conf = {
37 .direction_pin = STEP_MOTOR_DIRECTION_PIN,
38 .sleep_pin = STEP_MOTOR_SLEEP_PIN,
39 .reset_pin = STEP_MOTOR_RESET_PIN,
40 .ms3_pin = STEP_MOTOR_MS3_PIN,
41 .ms2_pin = STEP_MOTOR_MS2_PIN,
42 .ms1_pin = STEP_MOTOR_MS1_PIN,
43 .enable_pin = STEP_MOTOR_ENABLE_PIN,
44 };
45
46 // Install low part driver
47 step_motor_driver_io_t *a4988_io;
48 ESP_ERROR_CHECK(step_motor_new_a4988_io_driver(&a4988_conf, &a4988_io));
49
50 // Install rmt driver
51 step_motor_t *motor = NULL;
52 ESP_ERROR_CHECK(step_motor_create_rmt(a4988_io, &dev_config, &motor));
53
54 step_motor_init(motor);
55 ESP_LOGI(TAG, "init");
56
57 ESP_LOGI(TAG, "set_step");
58 // configure Microstep to Full Step
59 step_motor_set_step(motor, 1, STEP_MOTOR_DIRECTION_POSITIVE);
60 vTaskDelay(pdMS_TO_TICKS(1000));
61
62 ESP_LOGI(TAG, "step 10 @ 1000/s");
63 step_motor_step(motor, 10, 1000);
64 vTaskDelay(pdMS_TO_TICKS(1000));
65 ESP_LOGI(TAG, "step 100 @ 1000/s");
66 step_motor_step(motor, 100, 1000);
67 vTaskDelay(pdMS_TO_TICKS(1000));
68 ESP_LOGI(TAG, "step 1000 @ 1200/s");
69 step_motor_step(motor, 1000, 1200);
70 vTaskDelay(pdMS_TO_TICKS(1000));
71 ESP_LOGI(TAG, "step 5000 @ 1400/s");
72 step_motor_step(motor, 5000, 1400);
73 vTaskDelay(pdMS_TO_TICKS(1000));
74
75 ESP_LOGI(TAG, "smoothstep start 5000 steps @ 500~1400/s");
76 step_motor_smooth_step(motor, 5000, 1000, 500, 1400);
77 ESP_LOGI(TAG, "smoothstep finish");
78 vTaskDelay(pdMS_TO_TICKS(1000));
79
80 ESP_LOGI(TAG, "continuous running for 5s");
81 step_motor_step(motor, UINT32_MAX, 1000);
82 vTaskDelay(pdMS_TO_TICKS(5000));
83 ESP_LOGI(TAG, "stop");
84 step_motor_step(motor, 0, 1000);
85
86 vTaskDelay(pdMS_TO_TICKS(1000));
87 step_motor_deinit(motor);
88 ESP_LOGI(TAG, "deinit");
89 ESP_ERROR_CHECK(step_motor_delete_rmt(motor));
90
91 ESP_ERROR_CHECK(step_motor_delete_a4988_io_driver(a4988_io));
92 }
93