Odrive Development Notes
Record all the processes of odrive driving brushless motor from the beginning
connection
1. First of all, it's better not to use DuPont wire for wiring. It's better to use this silicone wire with interface.
2. Encoder:
The encoder selects as5047p and uses the connection method of ab. the boss of Taobao said that there is no phase i, that is, there is no index value.
spi's answer didn't get through. He resolutely answered ab.
If you want to connect spi (two encoders), cs chip selection needs to be connected to two encoders with two gpio ports. What else should be connected is what. For example, MISO, use two wires to connect to two encoders. (I saw that MOSI wanted to connect 3v3 on the forum, but someone said no, and I didn't know who to listen to)
Remember to connect the jumper on the encoder, otherwise the encoder is not powered on. Also note that at JP1 (only for as5047p encoder here), the middle channel is the selected channel, and the left and right sides are 5 and 3.3v. In other words, if the board passes 3.3v, connect the middle and right 3.3v together.
By the way, the as5047p datasheet says that when spi communication is not used, the MOSI needs to be pulled up, but I think it may not be necessary. Of course, I still took it.
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-OOE7ADqo-1627454644898)(./home/luo/Documents/Markdown/ODrive / picture 1.png)]
3. Motor:
For the motor, I don't know my model, and the number of poles is very important, but I don't quite understand the meaning of this concept for the time being. The encoder should be placed on the motor. The distance is required. My one is 5-8mm.
There is nothing particular about the wiring of the motor. It is connected directly from three phases to the top. During calibration, odrive will adjust, at least for me\
to configure
The configuration is a problem. It may need to be adjusted for some time. I don't think the official one is so clear.
The configuration mainly includes the following aspects:
The following is the configuration I summarized in many aspects, including posts on CSDN, professors of Taobao boss and official documents of odrive
It may not be applicable to everyone. Some parameters may be different. Some parameters can be set by default without setting. But I still put out some things I didn't set up\
1. Motherboard parameter configuration odrv0 config
Configure dissipation resistance Mine is 50 W 0.47RJ odrv0.config.brake_resistance = 0.47
Configure low voltage protection threshold When the voltage is lower than the protection threshold, the motor will be stopped and an error will be reported. odrv0.config.dc_bus_undervoltage_trip_level = 8.0
Configure overvoltage protection threshold When the voltage is higher than the protection threshold, the motor will be stopped and an error will be reported. This depends on odrive Marked on the board odrv0.config.dc_bus_overvoltage_trip_level = 56.0
Configure overcurrent protection threshold When the current is higher than the protection threshold, the motor will be stopped and an error will be reported. odrv0.config.dc_max_positive_current = 20.0
Configure reverse overcurrent protection threshold When the reverse current generated by motor braking on the bus is higher than this value, the motor will be stopped and an error will be reported. odrv0.config.dc_max_negative_current = -3.0
Configure recharge current value If the battery is used for power supply, it can be configured according to the recharge current borne by the battery pack. When using switching power supply, this item is configured as 0. The default value for this item is also 0. odrv0.config.max_regen_current = 0
2. Motor parameter configuration
Configure motor pole pairs The number of rotor poles divided by 2 is the number of motor poles. This parameter needs to know more about the following, and I'm not very clear. odrv0.axis0.motor.config.pole_pairs = 2
Configure the current for motor calibration This current value is used for motor parameter calibration and encoder offset calibration. odrv0.axis0.motor.config.calibration_current = 1.5
Configure the voltage for motor calibration This voltage value is used for motor parameter calibration and encoder offset calibration. odrv0.axis0.motor.config.resistance_calib_max_voltage = 2.0
Configure motor type It is configured as a high current motor here odrv0.axis0.motor.config.motor_type = MOTOR_TYPE_HIGH_CURRENT
Configure the maximum current limit for motor operation,This depends on your motor parameters odrv0.axis0.motor.config.current_lim = 3.0
Configure motor current sampling range odrv0.axis0.motor.config.requested_current_range = 10.0
3. Encoder parameter configuration
Configure encoder type The type of motor encoder configured is incremental encoder. Incremental macros are defined as 0, which can be seen in the official documents. odrv0.axis0.encoder.config.mode = ENCODER_MODE_INCREMENTAL
Configure encoder resolution odrv0.axis0.encoder.config.cpr = 4000 For my encoder as5047p It's 4000. as5047p It also depends on what base system it is. 2 and 10 base systems are also different This parameter means how much to count in a circle
Configure encoding bandwidth The default value is 1000. I don't know what to use for the moment odrv0.axis0.encoder.config.bandwidth = 1000
Configure the motor running current when the encoder is calibrated odrv0.axis0.config.calibration_lockin.current = 5
Configure the current rise time when the encoder is calibrated odrv0.axis0.config.calibration_lockin.ramp_time = 0.4
Configure the rotation distance of the motor when the current rises. This is also the default value. In fact, it can not be moved odrv0.axis0.config.calibration_lockin.ramp_distance = 3.1415927410125732
I won't write some unused ones. Most of them can use default values, because I don't know the meaning for the time being, As long as the motor can be driven first, these will be studied when necessary in the future.
4. Controller parameter configuration
Configure control mode Set to position control mode odrv0.axis0.controller.config.control_mode = CONTROL_MODE_POSITION_CONTROL More control modes are available in the official documents. I remember that the position ring is 3 and the speed ring is 2.These are macro definitions
Configure maximum motor speed circle/second odrv0.axis0.controller.config.vel_limit = 300
Configure position loop gain odrv0.axis0.controller.config.pos_gain = 30 These gain It is very important that the motor behind me vibrates continuously when it enters the closed loop, which is done by adjusting these values.
Configure speed loop gain odrv0.axis0.controller.config.vel_gain = 0.01 odrv0.axis0.controller.config.vel_integrator_gain = 0.2
Configure input mode Configure to ladder track mode odrv0.axis0.controller.config.input_mode = INPUT_MODE_TRAP_TRAJ
Configure ladder mode motor speed odrv0.axis0.trap_traj.config.vel_limit = 30
Configure ladder mode motor plus/deceleration odrv0.axis0.trap_traj.config.accel_limit = 5 odrv0.axis0.trap_traj.config.decel_limit = 5
Motor overspeed setting odrv0.axis0.controller.config.vel_limit_tolerance = 9999 #Infinity
Remember to save and restart after configuration
odrv0.save_configuration() odrv0.reboot() Also remember important instructions dump_errors(odrv0)//This is a check error. dump it For all parameter setting instructions, if you do not assign a value, you can check the current value, You can even enter it directly ENCODER_MODE_INCREMENTAL Wait, look at the values of these macros
Enter calibration test
Detect motor parameters odrv0.axis0.requested_state = AXIS_STATE_MOTOR_CALIBRATION Detect the motor parameters. About 2 seconds after executing this command, you should hear the "beep" sound of the motor.
Set pre calibration odrv0.axis0.motor.config.pre_calibrated = True Indicates that the motor has been calibrated. The results of the last calibration can be used directly after the next restart.
Encoder offset calibration odrv0.axis0.requested_state = AXIS_STATE_ENCODER_OFFSET_CALIBRATION The motor will rotate one turn in the forward direction and one turn in the reverse direction. Note: wait until the motor stops rotating, and then execute the subsequent instructions.
After the above step is completed, your calibration is completed. I use a slightly more advanced power supply, which can display the current voltage and current. During calibration, you can see the current change, which proves that my is indeed in calibration, but you can also judge whether calibration is in progress by the above, such as the beeping of the motor and whether it rotates. By the way, remember to dump error after each step to see if there is an error.
Set closed loop operation mode odrv0.axis0.requested_state = AXIS_STATE_CLOSED_LOOP_CONTROL After I enter the closed-loop mode, the current doubles. I see whether I really enter the closed-loop according to this every time. It seems that if you enter the closed-loop mode, you can't rotate your motor. This method can be used to see whether your motor has entered the closed-loop mode.
After entering the closed loop, you can directly try the following rotation command, so you can prove whether you can really drive the motor. Control motor forward rotation 50 turns odrv0.axis0.controller.input_pos = 50
Control the motor to rotate 50 turns in reverse odrv0.axis0.controller.input_pos = 0
There are no problems with the above, so we need to set the startup item:
odrv0.axis0.config.startup_motor_calibration = True //Calibration is performed automatically when starting odrv0.axis0.config.startup_encoder_index_search = False odrv0.axis0.config.startup_encoder_offset_calibration = True//Offset calibration odrv0.axis0.config.startup_closed_loop_control = True//Enter closed loop
After all the above are completed, you have completed the preliminary commissioning of odrive drive motor\
The following two operations are also provided on the official website. This is that you can save your previous settings in the form of a file, and then you can directly configure the configuration in the file, and then burn the configuration file. You can also use git to record every action you change. That's what I do. It's still very convenient. If you don't know git, you can go to Liao Xuefeng's tutorial. I'm also a novice on the road, but it's really easy to use.
To save the configuration to a file on the PC, run odrivetool backup-config my_config.json
To restore the configuration form such a file, run odrivetool restore-config my_config.json
Using python to control odrive
Introduction to motor control
Before entering python control, there are some things that must be known and clarified
Focus on the General – control structure & tuning section of the official document.
odrive: PID closed-loop control is used
Current loop, speed loop and position loop
Position ring
After the location is changed to calibrate on odrive, odrv0 axis0. controller. input_ POS = 1, this 1 is a circle. However, generally speaking, 1 represents the code disk bit. For example, if the resolution is 4096, it is 1 / 4096.
I had a question before, that is, when you set the position 50 this time, when you set the position 50 again, the motor will not rotate. Why? Shouldn't it be just 50 turns and then another 50 turns?
This is because the control of this position ring is different from what I thought. This is the position control, not the number of turns control, that is, 50 is the position I want to turn to 50, 100 is the position I want to turn to 100, and this number can be added all the time.
As mentioned earlier, 50 turns after odrive calibration is 50 turns, but generally it is the code disk position, so there will be a parameter to record that 1 will be added after a whole turn, which is phase i, but i (as5047p) don't have it here.
The position ring is very important for 3D printers and needs to return the position information all the time. I use odrive to control the motor, which is finally used in the car, so the position ring is not very important to me.
Velocity loop
I use it on the car. The speed ring is very important to me. But the speed loop is simple.
First, configure the control mode as speed loop
odrv0.axis0.controller.config.control_mode = CONTROL_MODE_VELOCITY_CONTROL
Then, after calibration, enter the closed-loop mode and set the speed directly.
odrv0.axis0.controller.input_vel = 50 After setting, you will see that your motor is rotating at this speed. This speed can be set to one limit of
In this way, there is another doubt. How do I control the rotation speed of the motor when I control the position ring?
Odrive is a PID control. The outermost ring is the position ring, then the speed ring and then the current ring. These are written in the official documents.
When you control the position loop, odrive calculates an appropriate speed according to some parameters of your trapezoidal electrical signal (including level duration, slope of bevel, etc.), as well as some other parameters. Finally, it feeds back to the speed loop, then to the current loop, and finally reaches the position you set at a speed. This is linked. So you can change the speed of the position ring by setting some parameters of the trapezoid (I won't ha ha yet)
I use it on the car, so in my control, I directly removed the position ring. This is also what the official said. When you only use a part of the inner ring, the outer ring will not participate in control. (this removal only means that you are useless, not that you need to make any adjustments in the code)