My goal is to regulate the stepper motors' speed based on angular velocity. When I input 2 * Pi as an argument, which corresponds to one revolution per second, the motor rotates at approximately one-eighth of the expected speed for some reason. The function is called from main loop without any delay function so there has to be something wrong with the code or maybe inherent delay with the controller. I'm hoping to get some feedback on what to improve. Thanks!
void Stepper::step(double angular_velocity) {
// Angular velocity takes rad/s
long us_halfstep = (long)abs(us_adjustment * 1.0 / (2 * SPR * angular_velocity / (2.0 * PI) / 1000000.0));
// set direction
int dir;
if (angular_velocity > 0) {
dir = CCW;
} else if (angular_velocity < 0) {
dir = CW;
} else {
return;
}
gpio_put(this->pin_d, dir);
// step
uint64_t curr_usec = time_us_64();
if (curr_usec - this->inst_usec >= us_halfstep) {
gpio_put(this->pin_s, !this->prev_step_state);
this->prev_step_state = !this->prev_step_state;
this->inst_usec = curr_usec;
}
}
For now, I'm using variable us_adjustment with value of 1/8 to adjust this delay.
I checked my driver board and both MS1 and MS2 were set to low which indicates it was running on 8 microstepping. I further checked the data sheet and apparently there was no configuration for full stepping when it comes to TMC2208/2209 drivers. So basically no fix was necessary for the slow rotation issue.