Emakefun Encoder Motor Arduino Lib 1.1.1
Loading...
Searching...
No Matches
run_speed.ino
Go to the documentation of this file.
1/**
2 * @~Chinese
3 * @file run_speed.ino
4 * @brief 示例:以指定的速度(单位RPM)驱动电机。
5 * @example run_speed.ino
6 * 以指定的速度(单位RPM)驱动电机。
7 */
8/**
9 * @~English
10 * @file run_speed.ino
11 * @brief Example: Rotate the motor at the specified speed (RPM).
12 * @example run_speed.ino
13 * Rotate the motor at the specified speed (RPM).
14 */
15
16#include "encoder_motor.h"
17#include "encoder_motor_lib.h"
18
19namespace {
20constexpr uint32_t kPPR = 12; // Pulses per revolution.
21constexpr uint32_t kReductionRation = 90; // Reduction ratio.
22
23#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
24em::EncoderMotor g_encoder_motor_0( // E0
25 GPIO_NUM_27, // The pin number of the motor's positive pole.
26 GPIO_NUM_13, // The pin number of the motor's negative pole.
27 GPIO_NUM_18, // The pin number of the encoder's A phase.
28 GPIO_NUM_19, // The pin number of the encoder's B phase.
29 kPPR, // Pulses per revolution.
30 kReductionRation, // Reduction ratio.
31 em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
32 // the motor is rotating forward)
33);
34
35em::EncoderMotor g_encoder_motor_1( // E1
36 GPIO_NUM_4, // The pin number of the motor's positive pole.
37 GPIO_NUM_2, // The pin number of the motor's negative pole.
38 GPIO_NUM_5, // The pin number of the encoder's A phase.
39 GPIO_NUM_23, // The pin number of the encoder's B phase.
40 kPPR, // Pulses per revolution.
41 kReductionRation, // Reduction ratio.
42 em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
43 // the motor is rotating forward)
44);
45
46em::EncoderMotor g_encoder_motor_2( // E2
47 GPIO_NUM_17, // The pin number of the motor's positive pole.
48 GPIO_NUM_12, // The pin number of the motor's negative pole.
49 GPIO_NUM_35, // The pin number of the encoder's A phase.
50 GPIO_NUM_36, // The pin number of the encoder's B phase.
51 kPPR, // Pulses per revolution.
52 kReductionRation, // Reduction ratio.
53 em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
54 // the motor is rotating forward)
55);
56
57em::EncoderMotor g_encoder_motor_3( // E3
58 GPIO_NUM_15, // The pin number of the motor's positive pole.
59 GPIO_NUM_14, // The pin number of the motor's negative pole.
60 GPIO_NUM_34, // The pin number of the encoder's A phase.
61 GPIO_NUM_39, // The pin number of the encoder's B phase.
62 kPPR, // Pulses per revolution.
63 kReductionRation, // Reduction ratio.
64 em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
65 // the motor is rotating forward)
66);
67
68#else // The ESP32 Arduino Core Version is less than 3.0.0
69
70em::EncoderMotor g_encoder_motor_0( // E0
71 GPIO_NUM_27, // The pin number of the motor's positive pole.
72 0, // The positive pole of the motor is attached to LED Control (LEDC) Channel 0.
73 GPIO_NUM_13, // The pin number of the motor's negative pole.
74 1, // The negative pole of the motor is attached to LED Control (LEDC) Channel 1.
75 GPIO_NUM_18, // The pin number of the encoder's A phase.
76 GPIO_NUM_19, // The pin number of the encoder's B phase.
77 kPPR, // Pulses per revolution.
78 kReductionRation, // Reduction ratio.
79 em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
80 // the motor is rotating forward)
81);
82
83em::EncoderMotor g_encoder_motor_1( // E1
84 GPIO_NUM_4, // The pin number of the motor's positive pole.
85 2, // The positive pole of the motor is attached to LED Control (LEDC) Channel 2.
86 GPIO_NUM_2, // The pin number of the motor's negative pole.
87 3, // The negative pole of the motor is attached to LED Control (LEDC) Channel 3.
88 GPIO_NUM_5, // The pin number of the encoder's A phase.
89 GPIO_NUM_23, // The pin number of the encoder's B phase.
90 kPPR, // Pulses per revolution.
91 kReductionRation, // Reduction ratio.
92 em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
93 // the motor is rotating forward)
94);
95
96em::EncoderMotor g_encoder_motor_2( // E2
97 GPIO_NUM_17, // The pin number of the motor's positive pole.
98 4, // The positive pole of the motor is attached to LED Control (LEDC) Channel 4.
99 GPIO_NUM_12, // The pin number of the motor's negative pole.
100 5, // The negative pole of the motor is attached to LED Control (LEDC) Channel 5.
101 GPIO_NUM_35, // The pin number of the encoder's A phase.
102 GPIO_NUM_36, // The pin number of the encoder's B phase.
103 kPPR, // Pulses per revolution.
104 kReductionRation, // Reduction ratio.
105 em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
106 // the motor is rotating forward)
107);
108
109em::EncoderMotor g_encoder_motor_3( // E3
110 GPIO_NUM_15, // The pin number of the motor's positive pole.
111 6, // The positive pole of the motor is attached to LED Control (LEDC) Channel 6.
112 GPIO_NUM_14, // The pin number of the motor's negative pole.
113 7, // The negative pole of the motor is attached to LED Control (LEDC) Channel 7.
114 GPIO_NUM_34, // The pin number of the encoder's A phase.
115 GPIO_NUM_39, // The pin number of the encoder's B phase.
116 kPPR, // Pulses per revolution.
117 kReductionRation, // Reduction ratio.
118 em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
119 // the motor is rotating forward)
120);
121#endif
122} // namespace
123
124void setup() {
125 Serial.begin(115200);
126 printf("setting up\n");
127 printf("Emakefun Encoder Motor Library Version: %s\n", em::esp_encoder_motor_lib::Version().c_str());
128 g_encoder_motor_0.Init();
129 g_encoder_motor_1.Init();
130 g_encoder_motor_2.Init();
131 g_encoder_motor_3.Init();
132 printf("setup completed\n");
133}
134
135void loop() {
136 const int16_t speed_rpm = 100;
137
138 g_encoder_motor_0.RunSpeed(speed_rpm);
139 g_encoder_motor_1.RunSpeed(speed_rpm);
140 g_encoder_motor_2.RunSpeed(speed_rpm);
141 g_encoder_motor_3.RunSpeed(speed_rpm);
142
143 printf("target speed rpm: %" PRIi16 ", current speed rpm: [%4" PRId32 ", %4" PRId32 ", %4" PRId32 ", %4" PRId32
144 "], pwm duties: [%5 " PRIi16 ", %5" PRIi16 ", %5" PRIi16 ", %5" PRIi16 "], pulse counts: [%" PRId64 ", %" PRId64
145 ", %" PRId64 ", %" PRId64 "]\n",
146 speed_rpm,
147 g_encoder_motor_0.SpeedRpm(),
148 g_encoder_motor_1.SpeedRpm(),
149 g_encoder_motor_2.SpeedRpm(),
150 g_encoder_motor_3.SpeedRpm(),
151 g_encoder_motor_0.PwmDuty(),
152 g_encoder_motor_1.PwmDuty(),
153 g_encoder_motor_2.PwmDuty(),
154 g_encoder_motor_3.PwmDuty(),
155 g_encoder_motor_0.EncoderPulseCount(),
156 g_encoder_motor_1.EncoderPulseCount(),
157 g_encoder_motor_2.EncoderPulseCount(),
158 g_encoder_motor_3.EncoderPulseCount());
159
160 delay(100);
161}
Encoder Motor Class.
@ kAPhaseLeads
Represents the situation where phase A leads phase B when the motor is rotating forward.
String Version()
Get the version number string.