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