ODrive development (2) About the encoder types supported by ODrive

ODrive performs closed-loop control of brushless motors and needs to obtain the parameters of the motor and encoder in advance.

First of all, the AS5047P-SPI absolute magnetic encoder does not need to calibrate the brushless motor every time it is powered on. It can be used directly after powering on.

Using LE5012B magnetic encoder only supports ABZ mode

Dear friends, regarding the detailed selection of magnetic encoders, I will post a picture for you to take a look at.

[Selected] ODrive pitfalls (4) AS5047P-SPI absolute magnetic encoder, no need to calibrate the brushless motor every time it is powered on, it can be used directly after powering on_as5047p odrive spi-CSDN blog

I suffer from using the ABI encoder. The encoder needs to be calibrated every time it is powered on, and the motor rotates once to the left and then to the right. Not to mention wasting time, the movement process may also cause the workpiece to accidentally touch, leading to power-on accidents. If you want the device to be usable directly after power-on without encoder calibration, you may need to use an SPI absolute encoder.

ODrive supports two types of SPI absolute encoders:

CUI protocol: Compatible with AMT23xx series (AMT232A, AMT232B, AMT233A, AMT233B).
AMS protocol: Compatible with AS5047P and AS5048A.
(Note: ODrive does not support the SPI interface of TLE5012B and can only use its ABI)

1. Connect ODrive to AS5047P and install the motor

In order to facilitate the testing of AS5047P-SPI absolute encoder and the expansion of different motors, the following universal adapter board is available, supporting 2208, 2212, 3508, 5008, 6010, 6374 , 42-stepper, 57-stepper and other different motor positioning installation.

Connect to the SPI interface, SCK, MISO, MOSI correspond one to one, CS is inserted into pin 4 of ODrive

2. Connect ODrive to the computer and perform configuration before motor calibration.
Power on the system and connect ODrive to the computer.

2.1. Restore factory configuration

# Restore factory configuration
odrv0.erase_configuration()

1
22.2. Configure motherboard parameters

Limit parameters are mainly used to protect the motherboard and motor from damage, including maximum current, protection voltage and other parameters.
Adjust the size as appropriate according to the motor that suits you.
# Configure the braking resistor value on the AUX interface (common ones are 0.47, 2.0Ω), if not connected, configure it to 0
odrv0.config.brake_resistance = 0

#Configure low voltage protection threshold (V)
odrv0.config.dc_bus_undervoltage_trip_level = 8.0

# Configure high voltage protection threshold (V)
odrv0.config.dc_bus_overvoltage_trip_level = 56.0

#Configure overcurrent protection threshold (A)
odrv0.config.dc_max_positive_current = 50.0

# Configure the reverse current threshold (reverse current generated by motor braking) (A)
odrv0.config.dc_max_negative_current = -5.0

# Configure the recharge current value (according to the parameter configuration of the power supply battery, the switching power supply configuration is 0) (A)
odrv0.config.max_regen_current = 0

# Save parameters
odrv0.save_configuration()

2.3. Configure motor parameters

# Configure the number of 0-pole pairs of the motor. According to the introduction at the beginning of the blog, count the number of magnetic poles by yourself, and then /2
odrv0.axis0.motor.config.pole_pairs = 7

#Configure the limiting current of motor 0 (A)
odrv0.axis0.motor.config.current_lim = 35

#Configure the current sampling threshold of motor 0 (A)
odrv0.axis0.motor.config.requested_current_range = 60

# Configure the current threshold for motor 0 calibration (configure according to the load condition of your own motor) (A)
odrv0.axis0.motor.config.calibration_current = 10

# Configure motor 0 type.
# Currently supports two types of motors: high current motor (MOTOR_TYPE_HIGH_CURRENT) and gimbal motor (MOTOR_TYPE_GIMBAL)
odrv0.axis0.motor.config.motor_type = MOTOR_TYPE_HIGH_CURRENT

# Save parameters
odrv0.save_configuration()

2.4. Configure encoder parameters

# Configure motor 0 encoder type. ENCODER_MODE_INCREMENTAL uses the ABI orthogonal (incremental) encoder.
# Value ENCODER_MODE_SPI_ABS_AMS uses AMS magnetic encoder-AS5047/AS5048.
odrv0.axis0.encoder.config.mode = ENCODER_MODE_SPI_ABS_AMS

# Set the CSn chip select pin, select any one of ODrive J3 interface GPIO3-8 as CS, here I use GPIO4
odrv0.axis0.encoder.config.abs_spi_cs_gpio_pin = 4

# Configure motor 0 encoder CPR (encoder count per revolution), the maximum resolution of AS5047P is 14 bits
odrv0.axis0.encoder.config.cpr = 2**14

# Encoder bandwidth setting, the higher the CPR value, the higher the bandwidth setting.
odrv0.axis0.encoder.config.bandwidth = 3000

# Encoder accuracy, type is [float], unit is [circumferential angle∠] (this value can be appropriately larger to avoid environmental interference)
# The maximum allowed error between the actual rotation angle of the motor and the open-loop movement distance. If this error is exceeded, the error ERROR_CPR_OUT_OF_RANGE will be reported.
odrv0.axis0.encoder.config.calib_range = 10

# Save parameters
odrv0.save_configuration()

If synchronization is caused by encoder accuracy error, ODrive will report an error and not run.

It is recommended to increase the odrv0.axis0.encoder.config.calib_range, especially for magnetic encoders, to avoid errors due to environmental interference. (The AS5047P manual describes the accuracy of the device as ±0.1°, so the minimum value of odrv0.axis0.encoder.config.calib_range cannot be less than 0.1)

2.5. Configure controller parameters (position closed-loop mode, configure PID parameters)

# Configure motor 0 control mode, which is position closed-loop control
odrv0.axis0.controller.config.control_mode = CONTROL_MODE_POSITION_CONTROL

# Configure the maximum speed of motor 0 (rev/second) (motor kv value * voltage / 60)
odrv0.axis0.controller.config.vel_limit = 120

#Configure position loop gain: 20
odrv0.axis0.controller.config.pos_gain = 20

# Configure speed loop gain: 0.05
odrv0.axis0.controller.config.vel_gain = 0.05

# Configure integral gain: 0.02
odrv0.axis0.controller.config.vel_integrator_gain = 0.02

# Configure the input mode as: trapezoidal trajectory mode
odrv0.axis0.controller.config.input_mode = INPUT_MODE_TRAP_TRAJ

# Configure the motor speed threshold in trapezoidal mode (rev/s)
odrv0.axis0.trap_traj.config.vel_limit = 50

# Configure acceleration in trapezoidal motion mode
# The size of the value affects the action following effect. If it is large, it will follow quickly; if it is small, it will follow slowly.
odrv0.axis0.trap_traj.config.accel_limit = 10

# Configure deceleration acceleration in trapezoidal motion mode
# The size of the value affects the action following effect. If it is large, it will follow quickly; if it is small, it will follow slowly.
odrv0.axis0.trap_traj.config.decel_limit = 10

# Save parameters
odrv0.save_configuration()

# Reboot the drive
odrv0.reboot()

Encoder resolution (wire harness)
The encoder I use is TLE5012B-E1000, which is a magnetic encoder that supports two output modes: SPI interface and ABI interface. The connection to ODrive uses the ABI interface.

When the output mode of TLE5012B-E1000 is ABI, its default resolution is 12 bits, that is, 4096 lines/turn.

Wiring and powering on
The motor encoder is connected to the interface on the right, and the three motor wires are connected to the M0 position.

Plug in the mains power supply and connect to the computer via USB. The braking resistor on the AUX port does not need to be connected.

cmd opens the command line window, starts odrivetool, and confirms the connection.

1. Configuration before motor calibration
1.1. Restore factory configuration
I asked the seller of the drive and found that the factory firmware is v0.5.1 and has parameters. When adapting your own motor, you need to restore the factory configuration first.

#Restore factory configuration
odrv0.erase_configuration()
1
2
During this period, ODrive will disconnect once and automatically reconnect to the computer.


1.2. Configure motherboard parameters
Limiting parameters are mainly used to protect the motherboard and motor from damage, including parameters such as maximum current and protection voltage.
Adjust the size as appropriate according to the motor that suits you.
# Configure the braking resistor value on the AUX interface (common ones are 0.47, 2.0Ω), if not connected, configure it to 0
odrv0.config.brake_resistance = 0

#Configure low voltage protection threshold (V)
odrv0.config.dc_bus_undervoltage_trip_level = 8.0

# Configure high voltage protection threshold (V)
odrv0.config.dc_bus_overvoltage_trip_level = 56.0

#Configure overcurrent protection threshold (A)
odrv0.config.dc_max_positive_current = 50.0

# Configure the reverse current threshold (reverse current generated by motor braking) (A)
odrv0.config.dc_max_negative_current = -5.0

# Configure the recharge current value (according to the parameter configuration of the power supply battery, the switching power supply configuration is 0) (A)
odrv0.config.max_regen_current = 0

# Save parameters
odrv0.save_configuration()


1.3. Configure motor parameters
# Configure the number of 0 pole pairs of the motor. According to the introduction at the beginning of the blog, count the number of magnetic poles by yourself, and then /2
odrv0.axis0.motor.config.pole_pairs = 7

#Configure the limiting current of motor 0 (A)
odrv0.axis0.motor.config.current_lim = 35

#Configure the current sampling threshold of motor 0 (A)
odrv0.axis0.motor.config.requested_current_range = 60

# Configure the current threshold for motor 0 calibration (configure as appropriate according to the load condition of your own motor) (A)
odrv0.axis0.motor.config.calibration_current = 10

# Configure motor 0 type.
# Currently supports two types of motors: high current motor (MOTOR_TYPE_HIGH_CURRENT) and gimbal motor (MOTOR_TYPE_GIMBAL)
odrv0.axis0.motor.config.motor_type = MOTOR_TYPE_HIGH_CURRENT

# Save parameters
odrv0.save_configuration()




1.4. Configure encoder parameters
# Configure the motor 0 encoder type. Currently using the ABI orthogonal (incremental) encoder.
odrv0.axis0.encoder.config.mode = ENCODER_MODE_INCREMENTAL

# Configure motor 0 encoder CPR (encoder count per revolution), which is the encoder harness * 4, as mentioned at the beginning of the blog
odrv0.axis0.encoder.config.cpr = 16384

# Save parameters
odrv0.save_configuration()

1.5. Configure controller parameters (position closed-loop mode, configure PID parameters)
# Configure motor 0 control mode, which is position closed-loop control
odrv0.axis0.controller.config.control_mode = CONTROL_MODE_POSITION_CONTROL

# Configure the maximum speed of motor 0 (rev/second) (motor kv value * voltage / 60)
odrv0.axis0.controller.config.vel_limit = 120

#Configure position loop gain: 20
odrv0.axis0.controller.config.pos_gain = 20

# Configure speed loop gain: 0.05
odrv0.axis0.controller.config.vel_gain = 0.05

# Configure integral gain: 0.02
odrv0.axis0.controller.config.vel_integrator_gain = 0.02

# Configure the input mode as: trapezoidal trajectory mode
odrv0.axis0.controller.config.input_mode = INPUT_MODE_TRAP_TRAJ

# Configure the motor speed threshold in trapezoidal mode (rev/s)
odrv0.axis0.trap_traj.config.vel_limit = 50

# Configure acceleration in trapezoidal motion mode
# The size of the value affects the action following effect. If it is large, it will follow quickly; if it is small, it will follow slowly.
odrv0.axis0.trap_traj.config.accel_limit = 10

# Configure deceleration acceleration in trapezoidal motion mode
# The size of the value affects the action following effect. If it is large, it will follow quickly; if it is small, it will follow slowly.
odrv0.axis0.trap_traj.config.decel_limit = 10

# Save parameters
odrv0.save_configuration()

# Reboot the drive
odrv0.reboot()


After the above configuration is completed, the controller will be restarted and pay attention to the last command.


2. Motor and encoder calibration
Motor and encoder calibration can be done separately in two instructions, or together in one instruction.

Do it separately: (To do it separately or together, just choose one group to operate)

# Carry out motor parameter calibration (the motor will beep ~ after running)
odrv0.axis0.requested_state = AXIS_STATE_MOTOR_CALIBRATION

#Set motor pre-calibration. (No need to beep every time when powering on)
# The driver will save this calibration value to avoid automatic calibration after power-on to speed up startup.
odrv0.axis0.motor.config.pre_calibrated = True

# Perform encoder calibration (after operation, the motor will rotate forward and then reverse)
odrv0.axis0.requested_state = AXIS_STATE_ENCODER_OFFSET_CALIBRATION

Do together:

# Calibrate the motor parameters and encoder (the motor will make a beep after operation, and the motor will rotate forward and then reverse)
odrv0.axis0.requested_state = AXIS_STATE_FULL_CALIBRATION_SEQUENCE

#Set motor pre-calibration. (No need to beep every time when powering on)
# The driver will save this calibration value to avoid automatic calibration after power-on to speed up startup.
odrv0.axis0.motor.config.pre_calibrated = True

Then enter closed-loop mode:

#Configure the motor in closed loop mode
odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL
1
2
After entering the closed-loop mode, the motor will have a slight sizzling current sound. When you twist the motor by hand, the motor will produce a resisting torque. After you let go, the motor will return to its original position.

If the motor does not return to the original position after hand dialing, or moves repeatedly in the original position, or overshoot is clearly felt, the PID parameters in Chapter 1.5 need to be readjusted.


Finally, it is set to automatically calibrate on power-on and automatically enter the closed loop.

#Set ODrive to automatically calibrate the encoder when it is powered on.
odrv0.axis0.config.startup_encoder_offset_calibration = True

# Set ODrive to automatically enter closed-loop mode when it is powered on.
odrv0.axis0.config.startup_closed_loop_control = True

# Save parameters
odrv0.save_configuration()

# Reboot the drive
odrv0.reboot()

After restarting, the motor will automatically perform encoder calibration, rotate forward and return once, and automatically enter closed-loop mode.

Control instructions can then be sent directly to make the motor move.


3. Issue control instructions and control motor operation
Send control instructions to control the rotation position of the motor.

# Control the motor to run to the 100-turn position
odrv0.axis0.controller.input_pos = 100

# Control the motor to run to the 0-turn position
odrv0.axis0.controller.input_pos = 0

# Control the motor to run to the position of -100 turns
odrv0.axis0.controller.input_pos = -100

# Release the motor (after releasing, the motor exits the closed-loop mode, and external force can easily move the motor)
odrv0.axis0.requested_state = AXIS_STATE_IDLE

#Configure the motor in closed loop mode
odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL