-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathtorque_brake_control.py
More file actions
91 lines (69 loc) · 2.87 KB
/
Copy pathtorque_brake_control.py
File metadata and controls
91 lines (69 loc) · 2.87 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import time
import numpy as np
from opensourceleg.actuators.base import CONTROL_MODES
from opensourceleg.actuators.tmotor import TMotorServoActuator
from opensourceleg.logging.logger import Logger
from opensourceleg.utilities import SoftRealtimeLoop
FREQUENCY = 200
DT = 1 / FREQUENCY
MOTOR_ID = 104 # Change this to match your motor's CAN ID
def torque_control():
torque_logger = Logger(
log_path="./logs",
file_name="tmotor_torque_control",
)
# Initialize TMotor actuator
motor = TMotorServoActuator(
motor_type="AK80-9", # Change to your motor model
motor_id=MOTOR_ID,
gear_ratio=9.0,
offline=False,
)
clock = SoftRealtimeLoop(dt=DT)
with motor:
motor.update()
# Set the encoder origin first (optional)
print("Setting encoder origin...")
motor.set_origin()
# Set to current control mode for torque control
motor.set_control_mode(mode=CONTROL_MODES.CURRENT_BRAKE)
# Motor torque constant for AK80-9
MAX_OUTPUT_TORQUE = 10.0 # Maximum output torque in Nm (safety limit)
# Track torque data
torque_logger.track_function(lambda: motor.motor_torque, "Motor Torque")
torque_logger.track_function(lambda: motor.output_torque, "Output Torque")
torque_logger.track_function(lambda: command_torque_brake, "Command Output Torque")
torque_logger.track_function(lambda: motor.motor_current, "Motor Current")
torque_logger.track_function(lambda: motor.output_position, "Motor Position")
torque_logger.track_function(lambda: motor.output_velocity, "Motor Velocity")
torque_logger.track_function(lambda: time.monotonic(), "Time")
print("Starting torque control...")
# Create a torque profile
for t in clock:
# Current brake command
command_torque_brake = 5.0
# Limit output torque for safety
command_torque_brake = np.clip(command_torque_brake, 0, MAX_OUTPUT_TORQUE)
# Set output torque
motor.set_output_brake_torque(command_torque_brake)
motor.update()
torque_logger.info(
f"Time: {t:.3f}; "
f"Command Output Torque: {command_torque_brake:.3f} Nm; "
f"Output Torque: {motor.output_torque:.3f} Nm; "
f"Motor Torque: {motor.motor_torque:.3f} Nm; "
f"Current: {motor.motor_current:.2f} mA; "
f"Velocity: {motor.output_velocity:.2f} rad/s"
)
torque_logger.update()
# Run for 10 seconds
if t > 10.0:
break
print("Torque control complete")
# Stop the motor
print("Stopping motor...")
motor.set_output_brake_torque(0.0)
motor.update()
time.sleep(1.0)
if __name__ == "__main__":
torque_control()