Emakefun Encoder Motor Arduino Lib 1.1.1
Loading...
Searching...
No Matches
forward_stop_backward.ino
Go to the documentation of this file.
1/**
2 * @~Chinese
3 * @file forward_stop_backward.ino
4 * @brief 示例:电机前进后退停止
5 * @example forward_stop_backward.ino
6 * 电机前进后退停止
7 */
8/**
9 * @~English
10 * @file forward_stop_backward.ino
11 * @brief Example: Move the motor forward, stop it, and then move it backward.
12 * @example forward_stop_backward.ino
13 * Move the motor forward, stop it, and then move it backward.
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 rpm = 100;
137 static auto s_state_changed_time = millis();
138 static enum State : uint8_t {
139 kNone,
140 kForward,
141 kBackward,
142 kStateNum,
143 } s_state = kForward;
144
145 if (millis() - s_state_changed_time > 2000) {
146 switch (s_state) {
147 case kNone:
148 g_encoder_motor_0.Stop();
149 g_encoder_motor_1.Stop();
150 g_encoder_motor_2.Stop();
151 g_encoder_motor_3.Stop();
152 break;
153 case kForward:
154 g_encoder_motor_0.RunSpeed(rpm);
155 g_encoder_motor_1.RunSpeed(rpm);
156 g_encoder_motor_2.RunSpeed(rpm);
157 g_encoder_motor_3.RunSpeed(rpm);
158 break;
159 case kBackward:
160 g_encoder_motor_0.RunSpeed(-rpm);
161 g_encoder_motor_1.RunSpeed(-rpm);
162 g_encoder_motor_2.RunSpeed(-rpm);
163 g_encoder_motor_3.RunSpeed(-rpm);
164 break;
165 default:
166 break;
167 }
168 s_state = static_cast<State>((static_cast<uint8_t>(s_state) + 1) % kStateNum);
169 s_state_changed_time = millis();
170 }
171
172 printf("target speed rpm: %4" PRIi16 ", current speed rpm:[%4" PRId32 ", %4" PRId32 ", %4" PRId32 ", %4" PRId32
173 "], pwm duties:[%5 " PRIi16 ", %5" PRIi16 ", %5" PRIi16 ", %5" PRIi16 "], pulse counts:[%" PRId64 ", %" PRId64
174 ", %" PRId64 ", %" PRId64 "]\n",
175 rpm,
176 g_encoder_motor_0.SpeedRpm(),
177 g_encoder_motor_1.SpeedRpm(),
178 g_encoder_motor_2.SpeedRpm(),
179 g_encoder_motor_3.SpeedRpm(),
180 g_encoder_motor_0.PwmDuty(),
181 g_encoder_motor_1.PwmDuty(),
182 g_encoder_motor_2.PwmDuty(),
183 g_encoder_motor_3.PwmDuty(),
184 g_encoder_motor_0.EncoderPulseCount(),
185 g_encoder_motor_1.EncoderPulseCount(),
186 g_encoder_motor_2.EncoderPulseCount(),
187 g_encoder_motor_3.EncoderPulseCount());
188
189 delay(100);
190}
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.