forked from DAInamite/programming-humanoid-robot-in-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_client.py
97 lines (79 loc) · 3.25 KB
/
agent_client.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
'''In this file you need to implement remote procedure call (RPC) client
* The agent_server.py has to be implemented first (at least one function is implemented and exported)
* Please implement functions in ClientAgent first, which should request remote call directly
* The PostHandler can be implement in the last step, it provides non-blocking functions, e.g. agent.post.execute_keyframes
* Hints: [threading](https://docs.python.org/2/library/threading.html) may be needed for monitoring if the task is done
'''
import weakref
import xmlrpclib as rpc
import sys
import os
import threading
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'joint_control'))
from keyframes import hello
class PostHandler(object):
'''the post hander wraps function to be excuted in paralle
'''
def __init__(self, obj):
self.proxy = weakref.proxy(obj)
def set_angle(self, joint_name, angle):
'''non-blocking call of ClientAgent.set_angle'''
# YOUR CODE HERE
thread = threading.Thread(target=self.proxy.set_angle, args=(joint_name, angle))
thread.start()
def execute_keyframes(self, keyframes):
'''non-blocking call of ClientAgent.execute_keyframes'''
# YOUR CODE HERE
thread = threading.Thread(target=self.proxy.execute_keyframes, args=[keyframes])
thread.start()
def set_transform(self, effector_name, transform):
'''non-blocking call of ClientAgent.set_transform'''
# YOUR CODE HERE
thread = threading.Thread(target=self.proxy.set_transform, args=[effector_name, transform])
thread.start()
class ClientAgent(object):
'''ClientAgent request RPC service from remote server
'''
# YOUR CODE HERE
def __init__(self, rpcProxy):
self.rpcProxy = rpcProxy
self.post = PostHandler(self)
def get_angle(self, joint_name):
'''get sensor value of given joint'''
# YOUR CODE HERE
return self.rpcProxy.get_angle(joint_name)
def set_angle(self, joint_name, angle):
'''set target angle of joint for PID controller
'''
# YOUR CODE HERE
self.rpcProxy.set_angle(joint_name, angle)
return 0
def get_posture(self):
'''return current posture of robot'''
# YOUR CODE HERE
return self.rpcProxy.get_posture()
def execute_keyframes(self, keyframes):
'''excute keyframes, note this function is blocking call,
e.g. return until keyframes are executed
'''
# YOUR CODE HERE
self.rpcProxy.execute_keyframes(keyframes)
return 0
def get_transform(self, name):
'''get transform with given name
'''
# YOUR CODE HERE
return self.rpcProxy.get_transform(name)
def set_transform(self, effector_name, transform):
'''solve the inverse kinematics and control joints use the results
'''
# YOUR CODE HERE
self.post.set_transform(effector_name, transform)
return 0
if __name__ == '__main__':
rpcProxy = rpc.ServerProxy("http://localhost:8000")
agent = ClientAgent(rpcProxy)
# TEST CODE HERE
#print(agent.get_angle('LHipYawPitch'))
agent.post.set_angle("LShoulderPitch",15)
#agent.post.execute_keyframes(hello())