Move the motor forward, stop it, and then move it backward.
namespace {
constexpr uint32_t kPPR = 12;
constexpr uint32_t kReductionRation = 90;
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
GPIO_NUM_27,
GPIO_NUM_13,
GPIO_NUM_18,
GPIO_NUM_19,
kPPR,
kReductionRation,
);
GPIO_NUM_4,
GPIO_NUM_2,
GPIO_NUM_5,
GPIO_NUM_23,
kPPR,
kReductionRation,
);
GPIO_NUM_17,
GPIO_NUM_12,
GPIO_NUM_35,
GPIO_NUM_36,
kPPR,
kReductionRation,
);
GPIO_NUM_15,
GPIO_NUM_14,
GPIO_NUM_34,
GPIO_NUM_39,
kPPR,
kReductionRation,
);
#else
GPIO_NUM_27,
0,
GPIO_NUM_13,
1,
GPIO_NUM_18,
GPIO_NUM_19,
kPPR,
kReductionRation,
);
GPIO_NUM_4,
2,
GPIO_NUM_2,
3,
GPIO_NUM_5,
GPIO_NUM_23,
kPPR,
kReductionRation,
);
GPIO_NUM_17,
4,
GPIO_NUM_12,
5,
GPIO_NUM_35,
GPIO_NUM_36,
kPPR,
kReductionRation,
);
GPIO_NUM_15,
6,
GPIO_NUM_14,
7,
GPIO_NUM_34,
GPIO_NUM_39,
kPPR,
kReductionRation,
);
#endif
}
void setup() {
Serial.begin(115200);
printf("setting up\n");
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);
}
@ kAPhaseLeads
Represents the situation where phase A leads phase B when the motor is rotating forward.
String Version()
Get the version number string.