This post is a companion post to my YouTube video on how to enable anti-cogging on your ODrive robotics High Performance Motor controller.
You can find the source coee from the video below. Note these are based on my settings from the ODrive D5065 Motor so your settings will vary from mine.
Quick disclaimer: We are playing with high currents and strong motors. take every safety precaution possible. make sure your equipment is secure, and not able to hurt anyone. I take no responsibility for you following this guide, do your own research, check my code and always test carefully.
The code is just below the video
ODrive Anti-Cogging Code
# Start the ODrive Tool
odrivetool shell
#Wipe the configuration
odrv0.erase_configuration()
#Set up some basic parameters, these work for me on my D5065
odrv0.axis0.motor.config.current_lim = 40
odrv0.axis0.controller.config.vel_limit = 15
odrv0.axis0.motor.config.calibration_current = 20
odrv0.config.brake_resistance = 2
odrv0.config.enable_brake_resistor = True
odrv0.axis0.motor.config.pole_pairs = 7
#This is the ratio of torque produced by the motor per Amp of current delivered to the motor. This should be set to 8.27 / (motor KV).
# In my case this is 8.27 / 270
odrv0.axis0.motor.config.torque_constant = (8.27 / 270)
odrv0.axis0.motor.config.motor_type = MOTOR_TYPE_HIGH_CURRENT
#Set up some encoder parameters
odrv0.axis0.encoder.config.cpr = 8192
odrv0.axis0.encoder.config.use_index =True
#Proper Tuning
odrv0.axis0.controller.config.vel_gain=0.32/2 #[Nm/(turn/s)]
odrv0.axis0.controller.config.vel_integrator_gain = (0.5 * 10 * 0.32/2 ) #[Nm/((turn/s) * s)]
odrv0.axis0.controller.config.pos_gain=15 #[(turn/s) / turn]
#Save the config
odrv0.save_configuration()
#Run the calibration
odrv0.axis0.requested_state = AXIS_STATE_FULL_CALIBRATION_SEQUENCE
#At this point you can test with the following commands to see how the motor performs, it should be stuttery
odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
odrv0.axis0.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL
odrv0.axis0.controller.input_vel = 0.2
#Idle the motor
odrv0.axis0.requested_state = AXIS_STATE_IDLE
#tell the odrive we are pr-calibrated so we don't need to recalibrate every time
odrv0.axis0.encoder.config.pre_calibrated = True
odrv0.axis0.encoder.config.use_index = True
odrv0.axis0.motor.config.pre_calibrated = True
#Save the config
and reboot
odrv0.save_configuration()
odrv0.reboot()
# When the motor comes back lets do an index search
odrv0.axis0.requested_state = AXIS_STATE_ENCODER_INDEX_SEARCH
# Anticogging Setup
# https://github.com/odriverobotics/ODrive/blob/ebd237673a483dec87b0d372dd09dfbf0bc7dd64/docs/anticogging.md
#NOTE: THE FOLLOWING COMMANDS MIGHT MAKE YOUR MOTOR SPIN WILDLY FOR A SECOND IF YOU HAVE MOVED IT SINCE TURNING IT ON
#Put the controller in position control mode
odrv0.axis0.controller.config.control_mode = CONTROL_MODE_POSITION_CONTROL
odrv0.axis0.controller.config.input_mode = INPUT_MODE_PASSTHROUGH
odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
#Temporarily increase the gain so that the calibration happens quickly
odrv0.axis0.controller.config.pos_gain = 200.0
odrv0.axis0.controller.config.vel_integrator_gain = 4
#THIS IS THE MAGIC - Start the calibration
odrv0.axis0.controller.start_anticogging_calibration()
# Wait until odrv0.axis0.controller.config.anticogging.calib_anticogging == False
# you can type
# odrv0.axis0.controller.config.anticogging.calib_anticogging
# and just up arrow and enter till it turns false
#Put the gain back to what it was before
odrv0.axis0.controller.config.vel_integrator_gain = (0.5 * 10 * 0.32/2 )
odrv0.axis0.controller.config.pos_gain=20
#Now tell the ODrive we are pre-calibrated for anti-cogging so we don't need to do it every time we power on
odrv0.axis0.controller.config.anticogging.pre_calibrated = True
#Save the new config
and reboot
#Put the odrive back in velocity control from now on (if you need this)
odrv0.axis0.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL
odrv0.save_configuration()
odrv0.reboot()
# #########################################
# Startup procedure from now on
# #########################################
# Do an index search so the controller can orient the cogging map
odrv0.axis0.requested_state = AXIS_STATE_ENCODER_INDEX_SEARCH
# or do this so this step is automatic from now on
# odrv0.axis0.config.startup_encoder_index_search = True
# enter closed loop control
odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
# or do this so this step is automatic from now on
# odrv1.axis0.config.startup_closed_loop_control = True
# Put the controller in velocity mode
odrv0.axis0.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL
#try a slow speed - it should be much smoother!
odrv0.axis0.controller.input_vel = 0.2
#Idle the motor
odrv0.axis0.requested_state = AXIS_STATE_IDLE
Code language: PHP (php)