Emakefun Encoder Motor Arduino Lib 1.1.1
Loading...
Searching...
No Matches
drive_dc_motor.ino
Go to the documentation of this file.
1/**
2 * @~Chinese
3 * @file drive_dc_motor.ino
4 * @brief 示例:PWM驱动直流电机正反转
5 * @example drive_dc_motor.ino
6 * PWM驱动直流电机正反转
7 */
8/**
9 * @~English
10 * @file drive_dc_motor.ino
11 * @brief Example: Use PWM to drive the DC motor to rotate forward and backward.
12 * @example drive_dc_motor.ino
13 * Use PWM to drive the DC motor to rotate forward and backward.
14 */
15
16#include "encoder_motor_lib.h"
17#include "motor.h"
18
19namespace {
20#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
21em::Motor g_motor_0( // M0
22 GPIO_NUM_27, // The pin number of the motor's positive pole.
23 GPIO_NUM_13 // The pin number of the motor's negative pole.
24);
25
26em::Motor g_motor_1( // M1
27 GPIO_NUM_4, // The pin number of the motor's positive pole.
28 GPIO_NUM_2 // The pin number of the motor's negative pole.
29);
30
31em::Motor g_motor_2( // M2
32 GPIO_NUM_17, // The pin number of the motor's positive pole.
33 GPIO_NUM_12 // The pin number of the motor's negative pole.
34);
35
36em::Motor g_motor_3( // M3
37 GPIO_NUM_15, // The pin number of the motor's positive pole.
38 GPIO_NUM_14 // The pin number of the motor's negative pole.
39);
40
41#else // The ESP32 Arduino Core Version is less than 3.0.0
42
43em::Motor g_motor_0( // M0
44 GPIO_NUM_27, // The pin number of the motor's positive pole.
45 0, // The positive pole of the motor is attached to LED Control (LEDC) Channel 0.
46 GPIO_NUM_13, // The pin number of the motor's negative pole.
47 1 // The negative pole of the motor is attached to LED Control (LEDC) Channel 1.
48);
49
50em::Motor g_motor_1( // M1
51 GPIO_NUM_4, // The pin number of the motor's positive pole.
52 2, // The positive pole of the motor is attached to LED Control (LEDC) Channel 2.
53 GPIO_NUM_2, // The pin number of the motor's negative pole.
54 3 // The negative pole of the motor is attached to LED Control (LEDC) Channel 3.
55);
56
57em::Motor g_motor_2( // M2
58 GPIO_NUM_17, // The pin number of the motor's positive pole.
59 4, // The positive pole of the motor is attached to LED Control (LEDC) Channel 4.
60 GPIO_NUM_12, // The pin number of the motor's negative pole.
61 5 // The negative pole of the motor is attached to LED Control (LEDC) Channel 5.
62);
63
64em::Motor g_motor_3( // M3
65 GPIO_NUM_15, // The pin number of the motor's positive pole.
66 6, // The positive pole of the motor is attached to LED Control (LEDC) Channel 6.
67 GPIO_NUM_14, // The pin number of the motor's negative pole.
68 7 // The negative pole of the motor is attached to LED Control (LEDC) Channel 7.
69);
70#endif
71} // namespace
72
73void setup() {
74 Serial.begin(115200);
75 printf("setting up\n");
76 printf("Emakefun Encoder Motor Library Version: %s\n", em::esp_encoder_motor_lib::Version().c_str());
77 g_motor_0.Init();
78 g_motor_1.Init();
79 g_motor_2.Init();
80 g_motor_3.Init();
81 printf("setup completed\n");
82}
83
84void loop() {
85 g_motor_0.RunPwmDuty(1023);
86 g_motor_1.RunPwmDuty(1023);
87 g_motor_2.RunPwmDuty(1023);
88 g_motor_3.RunPwmDuty(1023);
89 printf("forward\n");
90 delay(1000);
91
92 g_motor_0.RunPwmDuty(-1023);
93 g_motor_1.RunPwmDuty(-1023);
94 g_motor_2.RunPwmDuty(-1023);
95 g_motor_3.RunPwmDuty(-1023);
96 printf("backward\n");
97 delay(1000);
98}
A two-channel PWM motor driver class.
Definition motor.h:27
String Version()
Get the version number string.