-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControl.py
86 lines (73 loc) · 1.93 KB
/
Control.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
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import xarm
import ikpy.chain
import math
import matplotlib.pyplot
from mpl_toolkits.mplot3d import Axes3D
import time
# arm is the first xArm detected which is connected to USB
my_chain = ikpy.chain.Chain.from_urdf_file("xArm_with_gripper.urdf")
ax = matplotlib.pyplot.figure().add_subplot(111, projection='3d')
my_chain.plot(my_chain.inverse_kinematics([2, 2, 2]), ax)
matplotlib.pyplot.show()
def sendAngles(angles):
angles = [float(item) for item in angles]
arm = xarm.Controller('USB')
arm.setPosition(6, angles[1])
time.sleep(0.01)
arm.setPosition(2, angles[5])
time.sleep(0.01)
arm.setPosition(3, -angles[4])
time.sleep(0.01)
arm.setPosition(4, angles[3])
time.sleep(0.01)
arm.setPosition(5, angles[2])
def calcAngles(x,y,z):
res = my_chain.inverse_kinematics([x,y,z])
matplotlib.pyplot.clf()
ax = matplotlib.pyplot.figure().add_subplot(111, projection='3d')
my_chain.plot(my_chain.inverse_kinematics([x, y, z]), ax)
matplotlib.pyplot.show()
print(res)
res = [math.degrees(item) for item in res]
return res
def controlArm(x,y,z):
angles = calcAngles(x,y,z)
print (angles)
sendAngles(angles)
def grab():
arm = xarm.Controller('USB')
arm.setPosition(1,1000)
def release():
arm = xarm.Controller('USB')
arm.setPosition(1,0)
def main():
release()
controlArm(0,0,0.1)
time.sleep(1)
controlArm(0.2,0,0.1)
time.sleep(1)
controlArm(0.2,0,0.2)
time.sleep(1)
controlArm(0,0.2,0.1)
time.sleep(1)
controlArm(0,0.1,0.1)
time.sleep(1)
controlArm(-0.2,0,0.1)
time.sleep(1)
controlArm(-0.2,0,0.2)
time.sleep(1)
controlArm(0,0,0.3)
time.sleep(1)
controlArm(0,0,0.15)
time.sleep(1)
grab()
controlArm(0.15,0.15,0.1)
time.sleep(2)
controlArm(0.05,-0.1,0.1)
time.sleep(1)
main()