Hello Odrive community,
I have written this code for position control of Odrive motor D6374 150 KV using Odrive controller v.3.6 56V. But there seems to be some error. Could someone guide and help me fix it please. I am running this python script in Spyder IDE. I am new to Spyder too. So any help would mean a lot to me.
Thanks.
import odrive
from odrive.enums import *
import time
import numpy as np
from matplotlib import pyplot as plt
from odrive.utils import start_liveplotter
Connect to ODrive
odrv0 = odrive.find_any()
print(str(odrv0.vbus_voltage))
#configuring prarameters
odrv0.axis0.motor.config.current_lim = 10
odrv0.axis0.controller.config.vel_limit = 8
odrv0.config.enable_brake_resistor = True
odrv0.config.brake_resistance = 2
odrv0.axis0.motor.config.pole_pairs = 7
odrv0.axis0.motor.config.torque_constant = 8.27/150
odrv0.axis0.motor.config.motor_type = MOTOR_TYPE_HIGH_CURRENT
odrv0.axis0.encoder.config.cpr = 16384
#save=odrv0.save_configuration()
#calibration
odrv0.axis0.requested_state = AXIS_STATE_FULL_CALIBRATION_SEQUENCE
time.sleep(0.1)
#necessary for plotting?
odrv0.axis0.requested_state = AXIS_STATE_ENCODER_OFFSET_CALIBRATION
odrv0.axis0.encoder.config.pre_calibrated = True
odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
odrv0.axis0.controller.config.control_mode = CONTROL_MODE_POSITION_CONTROL
odrv0.axis0.trap_traj.config.vel_limit = 10.0
odrv0.axis0.trap_traj.config.accel_limit = 20.0
odrv0.axis0.trap_traj.config.decel_limit = 20.0
odrv0.axis0.controller.config.input_mode = INPUT_MODE_TRAP_TRAJ
odrv0.axis0.controller.config.pos_gain = 2
#varibales for plotting
t = np.linspace(-np.pi, np.pi, 12)
#w=(2*np.pi)/t
t_arr = np.array([t])
estimates =
for i in range (len(t)): #cannot take the entire array just the values using array index
setpoint = np.sin(t[i]) #should it be 2piT?
odrv0.axis0.controller.input_pos = setpoint
#plt.plot(t, x)
#plt.show()
#setpoint = np.array()
while True:
estimate = odrv0.axis0.encoder.pos_estimate
if np.abs(estimate - setpoint) < 0.001:
break
time.sleep(0.001)
estimates.append(estimate)
#odrv0.axis0.controller.move_incremental(pos_increment, from_goal_point)
#starting liveplotter
#graph = start_liveplotter(lambda:[estimate,setpoint])
odrv0.axis0.controller.input_pos = 0
plt.plot(t, np.sin(t), label=âSetpointâ)
plt.plot(t, estimates, label=âEstimated valueâ)
plt.legend()
plt.show()
quit()