Instantly share code, notes, and snippets.

Embed
What would you like to do?
import odrive
from odrive.enums import *
import time
print("Finding an odrive...")
odrv = odrive.find_any()
axes = [odrv.axis0, odrv.axis1];
# axes = [odrv.axis1];
print("Setting config...")
odrv.config.dc_bus_overvoltage_trip_level = 14.8
odrv.config.dc_bus_undervoltage_trip_level = 8.0
odrv.config.brake_resistance = 0
for ax in axes:
ax.motor.config.requested_current_range = 25
ax.motor.config.calibration_current = 10
ax.motor.config.current_lim = 10
ax.motor.config.resistance_calib_max_voltage = 4
ax.motor.config.pole_pairs = 10
ax.encoder.config.cpr = 4096
ax.encoder.config.use_index = True
ax.encoder.config.find_idx_on_lockin = True
ax.encoder.config.idx_search_unidirectional = True
ax.controller.config.control_mode = CTRL_MODE_VELOCITY_CONTROL
ax.controller.config.vel_limit = 10000
ax.controller.config.vel_gain = 0.002205736003816127
ax.controller.config.vel_integrator_gain = 0.022057360038161278
ax.controller.config.pos_gain = 26
ax.config.lockin_current = 10
ax.config.lockin_vel = 15
ax.config.lockin_accel = 10
ax.config.lockin_accel = 10
ax.config.lockin_finish_distance = 30
def wait_and_exit_on_error(ax):
while ax.current_state != AXIS_STATE_IDLE:
time.sleep(0.1)
if ax.error != AXIS_ERROR_NONE:
print("Axis Error: {:#x}".format(ax.error))
quit()
for axnum, ax in enumerate(axes):
print("Calibrating motor {}...".format(axnum))
ax.requested_state = AXIS_STATE_MOTOR_CALIBRATION
wait_and_exit_on_error(ax)
print("Checking motor {} direction...".format(axnum))
ax.requested_state = AXIS_STATE_ENCODER_DIR_FIND
wait_and_exit_on_error(ax)
print("Searching for index on motor {}...".format(axnum))
ax.requested_state = AXIS_STATE_ENCODER_INDEX_SEARCH
wait_and_exit_on_error(ax)
print("Calibrating encoder offset on motor {}...".format(axnum))
ax.requested_state = AXIS_STATE_ENCODER_OFFSET_CALIBRATION
wait_and_exit_on_error(ax)
#If we get here there were no errors, so let's commit the values
ax.motor.config.pre_calibrated = True
ax.encoder.config.pre_calibrated = True
#Everything should be good to go here, so let's save and reboot
print("All operations successful!")
odrv.save_configuration()
odrv.reboot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment