Connect the encoded motor to the specified positions E0 as described in the example program. After the program runs successfully, it will print out whether kAPhaseLeads or kBPhaseLeads should be used based on the actual phase relationship between the A and B phases of the encoder during forward rotation of the motor, helping the user determine the value that should be set for the phase_relation parameter when creating an EncoderMotor object.
namespace {
constexpr uint32_t kPPR = 12;
constexpr uint32_t kReductionRation = 90;
#if defined(ARDUINO_ESP32S3_DEV)
constexpr gpio_num_t kMotorPinPos = GPIO_NUM_42;
constexpr gpio_num_t kMotorPinNeg = GPIO_NUM_41;
constexpr gpio_num_t kEncoderPinA = GPIO_NUM_38;
constexpr gpio_num_t kEncoderPinB = GPIO_NUM_37;
#elif defined(ARDUINO_ESP32_DEV)
constexpr gpio_num_t kMotorPinPos = GPIO_NUM_27;
constexpr gpio_num_t kMotorPinNeg = GPIO_NUM_13;
constexpr gpio_num_t kEncoderPinA = GPIO_NUM_18;
constexpr gpio_num_t kEncoderPinB = GPIO_NUM_19;
#else
#error "Only ESP32 and ESP32S3 are supported"
#endif
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
kMotorPinPos,
kMotorPinNeg,
kEncoderPinA,
kEncoderPinB,
kPPR,
kReductionRation,
);
#else
constexpr uint8_t kMotorAChannel = 0;
constexpr uint8_t kMotorBChannel = 1;
kMotorPinPos,
kMotorAChannel,
kMotorPinNeg,
kMotorBChannel,
kEncoderPinA,
kEncoderPinB,
kPPR,
kReductionRation,
);
#endif
}
void setup() {
Serial.begin(115200);
printf("setting up\n");
g_encoder_motor_0.Init();
g_encoder_motor_0.RunPwmDuty(1023);
printf("setup completed\n");
}
void loop() {
const auto rpm = g_encoder_motor_0.SpeedRpm();
if (rpm > 0) {
printf(
"[%lu] RPM: %ld . The phase of A leads B. Constructed with the em::EncoderMotor::PhaseRelation::kAPhaseLeads "
"enum.\n",
millis(),
rpm);
} else if (rpm < 0) {
printf(
"[%lu] RPM: %ld . The phase of B leads A. Constructed with the em::EncoderMotor::PhaseRelation::kBPhaseLeads "
"enum.\n",
millis(),
rpm);
} else {
printf("The motor is not running currently.\n");
}
delay(200);
}
@ kAPhaseLeads
Represents the situation where phase A leads phase B when the motor is rotating forward.
String Version()
Get the version number string.