-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
126 lines (102 loc) · 4.22 KB
/
main.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
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
117
118
119
120
121
122
123
124
125
126
import sys
import warnings
import time
import keyboard
from sympy import *
import IK
import IK_SceneSetup
import KeyEvent
from Robot import Robot
from Scripts import Script
sys.path.append("C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\programming\zmqRemoteApi\clients\python")
from zmqRemoteApi import RemoteAPIClient
init_printing(wrap_line=False)
try:
client = RemoteAPIClient()
sim = client.getObject('sim')
simIK = client.getObject('simIK')
print("""
DEMO
Welcome to the Coppeliasim interface demo - Inverse Kinematics
------------------------------------------------------------------
preparing the scene...\n
""")
Robot.import_robot('Franka')
franka = Robot('/Franka[0]')
bot = franka.get_name()
bot_handle = franka.get_bot()
end_effector = franka.get_end_effector()
# TIP AND TARGET
try:
simBase = franka.get_bot()
simTip = sim.getObject('/tip', {'noError': False})
simTarget = sim.getObject('/target', {'noError': False})
except Exception as e:
endPosition = sim.getObjectPosition(end_effector, bot_handle)
if not sim.isHandle(sim.getObjectHandle('/tip@silentError')) and not sim.isHandle(
sim.getObjectHandle('/target@silentError')):
tip = IK_SceneSetup.setup_tip(franka, endPosition)
target_dummy = IK_SceneSetup.setup_target(franka, endPosition)
IK_SceneSetup.link(tip, target_dummy)
elif sim.isHandle(sim.getObject('/tip@silentError')):
tip = sim.getObject('/tip')
target_dummy = IK_SceneSetup.setup_target(franka, endPosition)
IK_SceneSetup.link(tip, target_dummy)
elif sim.isHandle(sim.getObject('/target@silentError')):
target = sim.getObject('/target')
tip = IK_SceneSetup.setup_tip(franka, endPosition)
IK_SceneSetup.link(tip, target)
else:
warnings.warn("Error was detected:" + str(e) + "\nError couldn't be resolved. ")
finally:
simBase = bot_handle
simTip = sim.getObject('/tip', {'noError': False})
simTarget = sim.getObject('/target', {'noError': False})
# SCRIPTS
sim.removeScript(sim.getScript(sim.scripttype_childscript, bot_handle)) # remove old script
childHandle = Script('child', bot_handle)
childHandle.set_code("childScript_IK.txt")
graphHandle = Script('custom', bot_handle)
graphHandle.set_code("LUA_graph.txt")
print("\nSetup Complete\n")
print("Final Step requires manual interference. Please create 3 graphs in your current scene named:"
"CartesianPoints, JointAngles, EulerAngles\n\n"
"Once complete, press Enter")
while not keyboard.is_pressed('enter'): # making a loop
pass
# MAIN FUNCTIONS
quitDemo = False
while not quitDemo:
print("""\n\n
1. Move the end-effector to an inputted cartesian position
2. Move the end_effector with Mouse
3. Control Panda with the keyboard\n
4. Quit Demo
""")
try:
opt = int(input("Out of the options above, which one do you want to try?\n--"))
if opt < 1 or opt > 4:
raise ValueError('Invalid input: please enter a number between 1 and 4')
except ValueError as e:
print(f"Error: {e}")
continue
graphHandle.init_script()
if opt == 1:
IK.move_to_cartesian_point(simBase, simTip, simTarget)
elif opt == 2:
manip_sphere = IK_SceneSetup.visible_sphere(simTarget)
print("Use the Mouse to drag the sphere to control robot Pose")
sim.startSimulation()
while not keyboard.is_pressed('esc'): # making a loop
pass
sim.stopSimulation()
print("Mouse Drag control has stopped")
IK_SceneSetup.invisible_sphere(manip_sphere)
elif opt == 3:
KeyEvent.keyboard_control(simTip, simTarget)
elif opt == 4:
print("Thank you for trying the Demo")
quitDemo = True
except Exception as e:
print(f"Error: {e}")
sys.exit(1)