-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOdriveTEST.py
68 lines (62 loc) · 2.47 KB
/
OdriveTEST.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import odrive
from odrive.enums import *
import time
import keyboard
import matplotlib.pyplot as plt
#----------program for testing odrive---------#
#----wait motor calibrate, it will auto run---#
#---parameter---#
MAX_SPEED = 100
KP_VALUE = 5
KI_VALUE = 0.25
BANDWIDTH = 2500
RAMP_RATE = 100
POS_GAIN = 35
#-------------setup-------------#
print("finding odrive...")
address_motor = odrive.find_any()
print("odrive found! reset position...")
odrive.utils.start_liveplotter(lambda:[address_motor.axis0.encoder.shadow_count])
address_motor.axis0.motor.config.current_control_bandwidth = BANDWIDTH
address_motor.axis0.controller.config.vel_gain = KP_VALUE
address_motor.axis0.controller.config.vel_integrator_gain = KI_VALUE
address_motor.axis0.controller.config.vel_ramp_rate = RAMP_RATE
address_motor.axis0.controller.config.vel_limit = MAX_SPEED
address_motor.axis0.controller.config.pos_gain = POS_GAIN
address_motor.axis0.controller.config.control_mode = ControlMode.POSITION_CONTROL
address_motor.axis0.controller.input_pos = 0
time.sleep(2)
print("changing mode...")
address_motor.axis0.controller.config.input_mode = InputMode.VEL_RAMP
address_motor.axis0.controller.config.control_mode = ControlMode.VELOCITY_CONTROL
address_motor.axis0.requested_state = AxisState.CLOSED_LOOP_CONTROL
time.sleep(1)
print("SETUP DONE!")
time.sleep(1)
#--starting speed--#
velocity_motor = 10
#---run---#
while True:
if keyboard.is_pressed("h"): # PRESS KEYBOARD BUTTON 'h' FOR TURNING OFF ODRIVE MOTOR
velocity_motor = 0
elif keyboard.is_pressed("p"): # PRESS KEYBOARD BUTTON 'p' FOR TURNING ON ODRIVE MOTOR CW
velocity_motor = 50
elif keyboard.is_pressed("o"): # PRESS KEYBOARD BUTTON 'o' FOR TURNING ON ODRIVE MOTOR CCW
velocity_motor = -50
elif keyboard.is_pressed("l"): # PRESS KEYBOARD BUTTON 'l' FOR TURNING ON ODRIVE MOTOR CW SEQUENTIAL
velocity_motor = 0
while velocity_motor < 50:
velocity_motor += 1
time.sleep(1)
elif keyboard.is_pressed("k"): # PRESS KEYBOARD BUTTON 'k' FOR TURNING ON ODRIVE MOTOR CCW SEQUENTIAL
velocity_motor = 0
while velocity_motor > -50:
velocity_motor -= 1
time.sleep(1)
if address_motor.axis0.current_state == 1:
print("error detected!!! reseting motor...")
address_motor.clear_errors()
address_motor.axis0.requested_state = 8
else:
print("starting motor...")
address_motor.axis0.controller.input_vel = velocity_motor