-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathArmKeyboardController.py
executable file
·87 lines (78 loc) · 2.9 KB
/
ArmKeyboardController.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
#!/usr/bin/env python
#coding: utf-8
from evdev import InputDevice
from select import select
import numpy as np
import rospy
from std_msgs.msg import Float64MultiArray
key_left,key_right,key_up,key_down = 105, 106,103,108
key_w,key_s,key_a,key_d=17,31,30,32
curState = np.array([[0,0,-40,120],[50,50,50,50],[1,1,1,1]])
graspStep = np.array([[5,0,0,0],[0,0,0,0],[0,0,0,0]])
rotateStep = np.array([[0,5,0,0],[0,0,0,0],[0,0,0,0]])
bigarmStep = np.array([[0,0,0,5],[0,0,0,0],[0,0,0,0]])
smallarmStep = np.array([[0,0,5,0],[0,0,0,0],[0,0,0,0]])
def UpdateStateByInputKey():
global curState
dev = InputDevice('/dev/input/event2')
select([dev], [], [])
for event in dev.read():
if event.value == 1 and event.code != 0:
#print("Key: %s Status: %s" % (event.code, "pressed" if event.value else "release"))
if event.code==key_left:
curState[2]=np.array([0,1,0,0])
curState -= rotateStep
elif event.code==key_right:
curState[2]=np.array([0,1,0,0])
curState += rotateStep
elif event.code==key_up:
curState[2]=np.array([0,0,1,0])
curState += smallarmStep
elif event.code == key_down:
curState[2]=np.array([0,0,1,0])
curState -= smallarmStep
elif event.code==key_w:
curState[2]=np.array([0,0,0,1])
curState += bigarmStep
elif event.code==key_s:
curState[2]=np.array([0,0,0,1])
curState -= bigarmStep
elif event.code==key_a:
curState[2]=np.array([1,0,0,0])
curState += graspStep
elif event.code==key_d:
curState[2]=np.array([1,0,0,0])
curState -= graspStep
def arm_controller():
pub = rospy.Publisher('arm_input_4x3', Float64MultiArray, queue_size=10)
rospy.init_node('arm_controller', anonymous=True)
rate = rospy.Rate(100) # 10hz
while not rospy.is_shutdown():
UpdateStateByInputKey()
#hello_str = "hello world %s" % rospy.get_time()
#dim = [MultiArrayDimension(label="arm_input",size=12,stride=12)]
#layout=MultiArrayLayout(dim,0)
#data = reduce(lambda a,b:a+b, [list(v) for v in curState])
msg = Float64MultiArray(data = reduce(lambda a,b:a+b, [list(v) for v in curState]))
rospy.loginfo(msg)
pub.publish(msg)
rate.sleep()
def main():
try:
arm_controller()
except rospy.ROSInterruptException:
pass
from AI.ROSEnvironment import ArmControlPlateform
from config import DQNConfig
from time import sleep
def test():
config = DQNConfig
env = ArmControlPlateform(config)
env.reset()
for i in range(30):
sleep(0.1)
action = env.action_space.sample()
ob,re,ter = env.step(action)
print(ob,re,ter)
if __name__ == '__main__':
main()