forked from DAInamite/programming-humanoid-robot-in-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_client.py
More file actions
100 lines (79 loc) · 3.32 KB
/
Copy pathagent_client.py
File metadata and controls
100 lines (79 loc) · 3.32 KB
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
'''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 threading
import os
import sys
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'kinematics'))
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 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()
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()
#elf.proxy.set_angle(joint_name,angle)
class ClientAgent(object):
'''ClientAgent request RPC service from remote server
'''
# YOUR CODE HERE
def __init__(self, proxy):
self.proxy = proxy
self.post_handler = PostHandler(self)
def get_angle(self, joint_name):
'''get sensor value of given joint'''
# YOUR CODE HERE
return self.proxy.get_angle(joint_name)
def set_angle(self, joint_name, angle):
'''set target angle of joint for PID controller
'''
# YOUR CODE HERE
self.proxy.set_angle(joint_name,angle)
def get_posture(self):
'''return current posture of robot'''
# YOUR CODE HERE
return self.proxy.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.proxy.execute_keyframes(keyframes)
def get_transform(self, name):
'''get transform with given name
'''
# YOUR CODE HERE
return self.proxy.get_transform(name)
def set_transform(self, effector_name, transform):
'''solve the inverse kinematics and control joints use the results
'''
# YOUR CODE HERE
self.proxy.set_transform(effector_name,transform)
if __name__ == '__main__':
proxy = rpc.ServerProxy("http://localhost:8000")
agent = ClientAgent(proxy)
# TEST CODE HERE
#print proxy.system.listMethods()
keyframes = hello()
agent.post_handler.execute_keyframes(keyframes)
#print agent.get_angle("LShoulderPitch")
#agent.post_handler.set_angle("LShoulderPitch", 5)