Emakefun Encoder Motor Arduino Lib 1.1.1
Loading...
Searching...
No Matches
detect_phase_relation.ino
Go to the documentation of this file.
1/**
2 * @~Chinese
3 * @file detect_phase_relation.ino
4 * @brief 示例:将编码电机按照示例程序中的说明接入到指定位置E0,程序运行成功后,会根据电机正转时编码器AB相的实际相位关系,
5 * 在串口打印输出是使用 @ref kAPhaseLeads 还是 @ref
6 * kBPhaseLeads,以此帮助用户确定在创建EncoderMotor对象时phase_relation参数应设置的值。
7 * @example detect_phase_relation.ino
8 * 将编码电机按照示例程序中的说明接入到指定位置E0,程序运行成功后,会根据电机正转时编码器AB相的实际相位关系,
9 * 在串口打印输出是使用 @ref kAPhaseLeads 还是 @ref
10 * kBPhaseLeads,以此帮助用户确定在创建EncoderMotor对象时phase_relation参数应设置的值。
11 */
12/**
13 * @~English
14 * @file detect_phase_relation.ino
15 * @brief
16 * Example: Connect the encoded motor to the specified positions E0 as described in the example program.
17 * After the program runs successfully, it will print out whether kAPhaseLeads or kBPhaseLeads should be used based on the
18 * actual phase relationship between the A and B phases of the encoder during forward rotation of the motor, helping the user
19 * determine the value that should be set for the phase_relation parameter when creating an EncoderMotor object.
20 * @example detect_phase_relation.ino
21 * Connect the encoded motor to the specified positions E0 as described in the example program.
22 * After the program runs successfully, it will print out whether kAPhaseLeads or kBPhaseLeads should be used based on the
23 * actual phase relationship between the A and B phases of the encoder during forward rotation of the motor, helping the user
24 * determine the value that should be set for the phase_relation parameter when creating an EncoderMotor object.
25 */
26
27#include "encoder_motor.h"
28#include "encoder_motor_lib.h"
29
30namespace {
31constexpr uint32_t kPPR = 12; // Pulses per revolution.
32constexpr uint32_t kReductionRation = 90; // Reduction ratio.
33
34#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
35em::EncoderMotor g_encoder_motor_0( // E0
36 GPIO_NUM_27, // The pin number of the motor's positive pole.
37 GPIO_NUM_13, // The pin number of the motor's negative pole.
38 GPIO_NUM_18, // The pin number of the encoder's A phase.
39 GPIO_NUM_19, // 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
46#else // The ESP32 Arduino Core Version is less than 3.0.0
47
48em::EncoderMotor g_encoder_motor_0( // E0
49 GPIO_NUM_27, // The pin number of the motor's positive pole.
50 0, // The positive pole of the motor is attached to LED Control (LEDC) Channel 0.
51 GPIO_NUM_13, // The pin number of the motor's negative pole.
52 1, // The negative pole of the motor is attached to LED Control (LEDC) Channel 1.
53 GPIO_NUM_18, // The pin number of the encoder's A phase.
54 GPIO_NUM_19, // The pin number of the encoder's B phase.
55 kPPR, // Pulses per revolution.
56 kReductionRation, // Reduction ratio.
57 em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
58 // the motor is rotating forward)
59);
60#endif
61} // namespace
62
63void setup() {
64 Serial.begin(115200);
65 printf("setting up\n");
66 printf("Emakefun Encoder Motor Library Version: %s\n", em::esp_encoder_motor_lib::Version().c_str());
67 g_encoder_motor_0.Init();
68 g_encoder_motor_0.RunPwmDuty(1023);
69 printf("setup completed\n");
70}
71
72void loop() {
73 const auto rpm = g_encoder_motor_0.SpeedRpm();
74 if (rpm > 0) {
75 printf(
76 "[%lu] RPM: %d . The phase of A leads B. Constructed with the em::EncoderMotor::PhaseRelation::kAPhaseLeads "
77 "enum.\n",
78 millis(),
79 rpm);
80 } else if (rpm < 0) {
81 printf(
82 "[%lu] RPM: %d . The phase of B leads A. Constructed with the em::EncoderMotor::PhaseRelation::kBPhaseLeads "
83 "enum.\n",
84 millis(),
85 rpm);
86 } else {
87 printf("The motor is not running currently.\n");
88 }
89 delay(200);
90}
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.