diff --git a/Firmware/MotorControl/commands.c b/Firmware/MotorControl/commands.c index cc5f2359..e107aa48 100644 --- a/Firmware/MotorControl/commands.c +++ b/Firmware/MotorControl/commands.c @@ -235,17 +235,14 @@ void motor_parse_cmd(uint8_t* buffer, int len, SerialPrintf_t response_interface if (numscan == 1) { print_monitoring(limit); } - } else if (buffer[0] == 't') { - //printf("Running anti-cogging.\n"); + } else if (buffer[0] == 't') { // Run Anti-Cogging Calibration for (int i = 0; i < num_motors; i++) { + // Ensure the cogging map was correctly allocated earlier and that the motor is capable of calibrating if (motors[i].cogging_map != NULL && motors[i].error == ERROR_NO_ERROR) { - //printf("Motor %d running\n",i); if (anti_cogging_calibration(&motors[i])) { - printf("Motor %d Anti-Cogging Enabled\n", i); - set_pos_setpoint(&motors[i], 0.0f, 0.0f, 0.0f); - motors[i].use_anti_cogging = true; + set_pos_setpoint(&motors[i], 0.0f, 0.0f, 0.0f); // Send the motor home + motors[i].use_anti_cogging = true; // We're good to go, enable anti-cogging } - //printf("Motor %d ERROR\n", i); } } } diff --git a/Firmware/MotorControl/low_level.c b/Firmware/MotorControl/low_level.c index 0117439f..eea4afac 100644 --- a/Firmware/MotorControl/low_level.c +++ b/Firmware/MotorControl/low_level.c @@ -1256,6 +1256,9 @@ static void control_motor_loop(Motor_t* motor) { // Velocity control float Iq = motor->current_setpoint; + // Anti-cogging is enabled after calibration + // We get the current position and apply a current feed-forward + // ensuring that we handle negative encoder positions properly (-1 == ENCODER_CPR - 1) if(motor->use_anti_cogging){ int32_t cogPos = (int32_t)motor->encoder.pll_pos % (int32_t)ENCODER_CPR; if(cogPos < 0){