-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeyboard.py
152 lines (134 loc) · 4.19 KB
/
keyboard.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
__author__ = "Luis Pacheco"
__copyright__ = "Copyright 2023, Luis Pacheco"
__contributors__ = ""
__license__ = "GPL"
__version__ = "0.0.1"
__maintainer__ = "Luis Pacheco"
__email__ = "[email protected]"
__status__ = "Alpha"
"""
Description: Keyboard/ Joystick Jog for xarm
"""
import os
import sys
import time
sys.path.append(os.path.join(os.path.dirname(__file__), '../../..'))
from pynput.keyboard import Key, Listener
from xarm.wrapper import XArmAPI
if len(sys.argv) >= 2:
ip = sys.argv[1]
else:
try:
from configparser import ConfigParser
parser = ConfigParser()
parser.read('../robot.conf')
ip = parser.get('xArm', 'ip')
except:
ip = input('Please input the xArm ip address:')
if not ip:
print('input error, exit')
sys.exit(1)
########################################################
arm = XArmAPI(ip)
arm.motion_enable(enable=True)
arm.set_mode(0)
arm.set_state(state=0)
arm.reset(wait=True)
arm.set_position(*[200, 0, 200, 180, 0, 0], wait=True)
arm.set_mode(1)
arm.set_state(0)
time.sleep(0.1)
step = 5
astep = 1
speed = 200
aspeed = 100
accel = 1000
def show(key):
currentPos = arm.get_position()
x=currentPos[1][0]
y=currentPos[1][1]
a=currentPos[1][3]
b=currentPos[1][4]
#mvpose = [x, y, 200, a, b, 0]
#ret = arm.set_servo_cartesian(mvpose, speed=speed, mvacc=accel)
print(currentPos)
if key == Key.up:
print("up")
x=x+step
mvpose = [x, y, 200, a, b, 0]
ret = arm.set_servo_cartesian(mvpose, speed=speed, mvacc=accel)
#print('set_servo_cartesian, ret={}'.format(ret))
time.sleep(0.01)
if key == Key.down:
x=x-step
print("up")
print(x)
mvpose = [x, y, 200, a, b, 0]
ret = arm.set_servo_cartesian(mvpose, speed=speed, mvacc=accel)
#print('set_servo_cartesian, ret={}'.format(ret))
time.sleep(0.01)
if key == Key.left:
y=y+step
print("left")
print(y)
mvpose = [x, y, 200, a, b, 0]
ret = arm.set_servo_cartesian(mvpose, speed=speed, mvacc=accel)
#print('set_servo_cartesian, ret={}'.format(ret))
time.sleep(0.01)
if key == Key.right:
y=y-step
print("right")
print(y)
mvpose = [x, y, 200, a, b, 0]
ret = arm.set_servo_cartesian(mvpose, speed=speed, mvacc=accel)
#print('set_servo_cartesian, ret={}'.format(ret))
time.sleep(0.01)
try:
if key.char == "a":
a=a-astep
print("roll-")
mvpose = [x, y, 200, a, b, 0]
ret = arm.set_servo_cartesian(mvpose, speed=aspeed, mvacc=accel)
#print('set_servo_cartesian, ret={}'.format(ret))
time.sleep(0.01)
except AttributeError:
print('special key {0} pressed'.format(key))
try:
if key.char == "d":
a=a+astep
print("roll+")
mvpose = [x, y, 200, a, b, 0]
ret = arm.set_servo_cartesian(mvpose, speed=aspeed, mvacc=accel)
#print('set_servo_cartesian, ret={}'.format(ret))
time.sleep(0.01)
except AttributeError:
print('special key {0} pressed'.format(key))
try:
if key.char == "s":
b=b-astep
print("roll-")
mvpose = [x, y, 200, a, b, 0]
ret = arm.set_servo_cartesian(mvpose, speed=aspeed, mvacc=accel)
#print('set_servo_cartesian, ret={}'.format(ret))
time.sleep(0.01)
except AttributeError:
print('special key {0} pressed'.format(key))
try:
if key.char == "w":
b=b+astep
print("roll+")
mvpose = [x, y, 200, a, b, 0]
ret = arm.set_servo_cartesian(mvpose, speed=aspeed, mvacc=accel)
#print('set_servo_cartesian, ret={}'.format(ret))
time.sleep(0.01)
except AttributeError:
print('special key {0} pressed'.format(key))
# by pressing 'delete' button
# you can terminate the loop
if key == Key.delete:
return False
# Collect all event until released
with Listener(on_press = show) as listener:
listener.join()
arm.disconnect()# Write your code here :-)