-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_force_control.py
More file actions
116 lines (94 loc) · 4.34 KB
/
Copy pathtest_force_control.py
File metadata and controls
116 lines (94 loc) · 4.34 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
"""
Test force control for realistic servo behavior.
"""
import mujoco
import numpy as np
import os
# Load model
model_path = os.path.join(os.path.dirname(__file__), 'ezgripper.xml')
model = mujoco.MjModel.from_xml_path(model_path)
data = mujoco.MjData(model)
print("="*80)
print("FORCE CONTROL TEST")
print("="*80)
# Get IDs
f1_palm_id = model.joint('F1_palm_knuckle').id
f1_tip_id = model.joint('F1_knuckle_tip').id
f2_palm_id = model.joint('F2_palm_knuckle').id
f2_tip_id = model.joint('F2_knuckle_tip').id
f1_actuator_id = model.actuator('gripper_actuator_f1').id
f2_actuator_id = model.actuator('gripper_actuator_f2').id
print(f"\nTesting force control:")
print(f"Force range: -50N (open) to +50N (close)")
# Start from open position
mujoco.mj_resetData(model, data)
data.ctrl[f1_actuator_id] = 0.0 # No force initially
data.ctrl[f2_actuator_id] = 0.0
# Open the gripper with negative force
print(f"\nOpening gripper with -150N force:")
for step in range(200):
data.ctrl[f1_actuator_id] = -150.0 # Opening force
data.ctrl[f2_actuator_id] = -150.0
mujoco.mj_step(model, data)
if step % 50 == 0:
f1_palm = np.degrees(data.qpos[f1_palm_id])
f1_tip = np.degrees(data.qpos[f1_tip_id])
f2_palm = np.degrees(data.qpos[f2_palm_id])
f2_tip = np.degrees(data.qpos[f2_tip_id])
print(f" Step {step:3d}: F1({f1_palm:5.1f}°,{f1_tip:5.1f}°) F2({f2_palm:5.1f}°,{f2_tip:5.1f}°) Contacts={data.ncon}")
# Close the gripper with positive force
print(f"\nClosing gripper with +100N force:")
for step in range(300):
data.ctrl[f1_actuator_id] = 100.0 # Closing force
data.ctrl[f2_actuator_id] = 100.0
mujoco.mj_step(model, data)
if step % 25 == 0:
f1_palm = np.degrees(data.qpos[f1_palm_id])
f1_tip = np.degrees(data.qpos[f1_tip_id])
f2_palm = np.degrees(data.qpos[f2_palm_id])
f2_tip = np.degrees(data.qpos[f2_tip_id])
print(f" Step {step:3d}: F1({f1_palm:5.1f}°,{f1_tip:5.1f}°) F2({f2_palm:5.1f}°,{f2_tip:5.1f}°) Contacts={data.ncon}")
if data.ncon > 0:
print(f" CONTACT DETECTED!")
for i in range(min(data.ncon, 3)):
contact = data.contact[i]
geom1_name = mujoco.mj_id2name(model, mujoco.mjtObj.mjOBJ_GEOM, contact.geom1)
geom2_name = mujoco.mj_id2name(model, mujoco.mjtObj.mjOBJ_GEOM, contact.geom2)
force = np.linalg.norm(data.efc_force[contact.efc_address:contact.efc_address+6])
print(f" {i}: {geom1_name} <-> {geom2_name}, force={force:.1f}N")
break
# Continue applying force after contact
print(f"\nApplying more force after contact (+40N):")
for step in range(200):
data.ctrl[f1_actuator_id] = 40.0 # Higher closing force
data.ctrl[f2_actuator_id] = 40.0
mujoco.mj_step(model, data)
if step % 25 == 0:
f1_palm = np.degrees(data.qpos[f1_palm_id])
f1_tip = np.degrees(data.qpos[f1_tip_id])
f2_palm = np.degrees(data.qpos[f2_palm_id])
f2_tip = np.degrees(data.qpos[f2_tip_id])
print(f" Step {step:3d}: F1({f1_palm:5.1f}°,{f1_tip:5.1f}°) F2({f2_palm:5.1f}°,{f2_tip:5.1f}°) Contacts={data.ncon}")
if data.ncon > 0:
for i in range(min(data.ncon, 2)):
contact = data.contact[i]
geom1_name = mujoco.mj_id2name(model, mujoco.mjtObj.mjOBJ_GEOM, contact.geom1)
geom2_name = mujoco.mj_id2name(model, mujoco.mjtObj.mjOBJ_GEOM, contact.geom2)
force = np.linalg.norm(data.efc_force[contact.efc_address:contact.efc_address+6])
print(f" {i}: {geom1_name} <-> {geom2_name}, force={force:.1f}N")
# Test maximum force
print(f"\nApplying maximum force (+50N):")
for step in range(100):
data.ctrl[f1_actuator_id] = 50.0 # Maximum force
data.ctrl[f2_actuator_id] = 50.0
mujoco.mj_step(model, data)
if step % 25 == 0:
f1_palm = np.degrees(data.qpos[f1_palm_id])
f1_tip = np.degrees(data.qpos[f1_tip_id])
f2_palm = np.degrees(data.qpos[f2_palm_id])
f2_tip = np.degrees(data.qpos[f2_tip_id])
print(f" Step {step:3d}: F1({f1_palm:5.1f}°,{f1_tip:5.1f}°) F2({f2_palm:5.1f}°,{f2_tip:5.1f}°) Contacts={data.ncon}")
print("\n" + "="*80)
print("FORCE CONTROL TEST COMPLETE")
print("="*80)