Emakefun Encoder Motor Arduino Lib 1.1.1
Loading...
Searching...
No Matches
forward_stop_backward.ino

Move the motor forward, stop it, and then move it backward.

/**
* @~Chinese
* @file forward_stop_backward.ino
* @brief 示例:电机前进后退停止
* @example forward_stop_backward.ino
* 电机前进后退停止
*/
/**
* @~English
* @file forward_stop_backward.ino
* @brief Example: Move the motor forward, stop it, and then move it backward.
* @example forward_stop_backward.ino
* Move the motor forward, stop it, and then move it backward.
*/
#include "encoder_motor.h"
namespace {
constexpr uint32_t kPPR = 12; // Pulses per revolution.
constexpr uint32_t kReductionRation = 90; // Reduction ratio.
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
em::EncoderMotor g_encoder_motor_0( // E0
GPIO_NUM_27, // The pin number of the motor's positive pole.
GPIO_NUM_13, // The pin number of the motor's negative pole.
GPIO_NUM_18, // The pin number of the encoder's A phase.
GPIO_NUM_19, // The pin number of the encoder's B phase.
kPPR, // Pulses per revolution.
kReductionRation, // Reduction ratio.
em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
// the motor is rotating forward)
);
em::EncoderMotor g_encoder_motor_1( // E1
GPIO_NUM_4, // The pin number of the motor's positive pole.
GPIO_NUM_2, // The pin number of the motor's negative pole.
GPIO_NUM_5, // The pin number of the encoder's A phase.
GPIO_NUM_23, // The pin number of the encoder's B phase.
kPPR, // Pulses per revolution.
kReductionRation, // Reduction ratio.
em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
// the motor is rotating forward)
);
em::EncoderMotor g_encoder_motor_2( // E2
GPIO_NUM_17, // The pin number of the motor's positive pole.
GPIO_NUM_12, // The pin number of the motor's negative pole.
GPIO_NUM_35, // The pin number of the encoder's A phase.
GPIO_NUM_36, // The pin number of the encoder's B phase.
kPPR, // Pulses per revolution.
kReductionRation, // Reduction ratio.
em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
// the motor is rotating forward)
);
em::EncoderMotor g_encoder_motor_3( // E3
GPIO_NUM_15, // The pin number of the motor's positive pole.
GPIO_NUM_14, // The pin number of the motor's negative pole.
GPIO_NUM_34, // The pin number of the encoder's A phase.
GPIO_NUM_39, // The pin number of the encoder's B phase.
kPPR, // Pulses per revolution.
kReductionRation, // Reduction ratio.
em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
// the motor is rotating forward)
);
#else // The ESP32 Arduino Core Version is less than 3.0.0
em::EncoderMotor g_encoder_motor_0( // E0
GPIO_NUM_27, // The pin number of the motor's positive pole.
0, // The positive pole of the motor is attached to LED Control (LEDC) Channel 0.
GPIO_NUM_13, // The pin number of the motor's negative pole.
1, // The negative pole of the motor is attached to LED Control (LEDC) Channel 1.
GPIO_NUM_18, // The pin number of the encoder's A phase.
GPIO_NUM_19, // The pin number of the encoder's B phase.
kPPR, // Pulses per revolution.
kReductionRation, // Reduction ratio.
em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
// the motor is rotating forward)
);
em::EncoderMotor g_encoder_motor_1( // E1
GPIO_NUM_4, // The pin number of the motor's positive pole.
2, // The positive pole of the motor is attached to LED Control (LEDC) Channel 2.
GPIO_NUM_2, // The pin number of the motor's negative pole.
3, // The negative pole of the motor is attached to LED Control (LEDC) Channel 3.
GPIO_NUM_5, // The pin number of the encoder's A phase.
GPIO_NUM_23, // The pin number of the encoder's B phase.
kPPR, // Pulses per revolution.
kReductionRation, // Reduction ratio.
em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
// the motor is rotating forward)
);
em::EncoderMotor g_encoder_motor_2( // E2
GPIO_NUM_17, // The pin number of the motor's positive pole.
4, // The positive pole of the motor is attached to LED Control (LEDC) Channel 4.
GPIO_NUM_12, // The pin number of the motor's negative pole.
5, // The negative pole of the motor is attached to LED Control (LEDC) Channel 5.
GPIO_NUM_35, // The pin number of the encoder's A phase.
GPIO_NUM_36, // The pin number of the encoder's B phase.
kPPR, // Pulses per revolution.
kReductionRation, // Reduction ratio.
em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
// the motor is rotating forward)
);
em::EncoderMotor g_encoder_motor_3( // E3
GPIO_NUM_15, // The pin number of the motor's positive pole.
6, // The positive pole of the motor is attached to LED Control (LEDC) Channel 6.
GPIO_NUM_14, // The pin number of the motor's negative pole.
7, // The negative pole of the motor is attached to LED Control (LEDC) Channel 7.
GPIO_NUM_34, // The pin number of the encoder's A phase.
GPIO_NUM_39, // The pin number of the encoder's B phase.
kPPR, // Pulses per revolution.
kReductionRation, // Reduction ratio.
em::EncoderMotor::kAPhaseLeads // Phase relationship (A phase leads or B phase leads, referring to the situation when
// the motor is rotating forward)
);
#endif
} // namespace
void setup() {
Serial.begin(115200);
printf("setting up\n");
printf("Emakefun Encoder Motor Library Version: %s\n", em::esp_encoder_motor_lib::Version().c_str());
g_encoder_motor_0.Init();
g_encoder_motor_1.Init();
g_encoder_motor_2.Init();
g_encoder_motor_3.Init();
printf("setup completed\n");
}
void loop() {
const int16_t rpm = 100;
static auto s_state_changed_time = millis();
static enum State : uint8_t {
kNone,
kForward,
kBackward,
kStateNum,
} s_state = kForward;
if (millis() - s_state_changed_time > 2000) {
switch (s_state) {
case kNone:
g_encoder_motor_0.Stop();
g_encoder_motor_1.Stop();
g_encoder_motor_2.Stop();
g_encoder_motor_3.Stop();
break;
case kForward:
g_encoder_motor_0.RunSpeed(rpm);
g_encoder_motor_1.RunSpeed(rpm);
g_encoder_motor_2.RunSpeed(rpm);
g_encoder_motor_3.RunSpeed(rpm);
break;
case kBackward:
g_encoder_motor_0.RunSpeed(-rpm);
g_encoder_motor_1.RunSpeed(-rpm);
g_encoder_motor_2.RunSpeed(-rpm);
g_encoder_motor_3.RunSpeed(-rpm);
break;
default:
break;
}
s_state = static_cast<State>((static_cast<uint8_t>(s_state) + 1) % kStateNum);
s_state_changed_time = millis();
}
printf("target speed rpm: %4" PRIi16 ", current speed rpm:[%4" PRId32 ", %4" PRId32 ", %4" PRId32 ", %4" PRId32
"], pwm duties:[%5 " PRIi16 ", %5" PRIi16 ", %5" PRIi16 ", %5" PRIi16 "], pulse counts:[%" PRId64 ", %" PRId64
", %" PRId64 ", %" PRId64 "]\n",
rpm,
g_encoder_motor_0.SpeedRpm(),
g_encoder_motor_1.SpeedRpm(),
g_encoder_motor_2.SpeedRpm(),
g_encoder_motor_3.SpeedRpm(),
g_encoder_motor_0.PwmDuty(),
g_encoder_motor_1.PwmDuty(),
g_encoder_motor_2.PwmDuty(),
g_encoder_motor_3.PwmDuty(),
g_encoder_motor_0.EncoderPulseCount(),
g_encoder_motor_1.EncoderPulseCount(),
g_encoder_motor_2.EncoderPulseCount(),
g_encoder_motor_3.EncoderPulseCount());
delay(100);
}
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.