forked from DAInamite/programming-humanoid-robot-in-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_server.py
78 lines (62 loc) · 2.28 KB
/
agent_server.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
'''In this file you need to implement remote procedure call (RPC) server
* There are different RPC libraries for python, such as xmlrpclib, json-rpc. You are free to choose.
* The following functions have to be implemented and exported:
* get_angle
* set_angle
* get_posture
* execute_keyframes
* get_transform
* set_transform
* You can test RPC server with ipython before implementing agent_client.py
'''
# add PYTHONPATH
import os
import sys
import threading
from xmlrpc.server import SimpleXMLRPCServer
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'kinematics'))
from inverse_kinematics import InverseKinematicsAgent
class ServerAgent(InverseKinematicsAgent):
'''ServerAgent provides RPC service
'''
# YOUR CODE HERE
def __init__(self):
super(ServerAgent, self).__init__()
server = SimpleXMLRPCServer(('localhost', 9000), logRequests=True, allow_none=True)
server.register_instance(self)
server.register_introspection_functions()
server.register_multicall_functions()
thread = threading.Thread(target=server.serve_forever)
thread.start()
def get_angle(self, joint_name):
'''get sensor value of given joint'''
# YOUR CODE HERE
return self.perception.joint[joint_name]
def set_angle(self, joint_name, angle):
'''set target angle of joint for PID controller
'''
# YOUR CODE HERE
self.target_joints[joint_name] = angle
def get_posture(self):
'''return current posture of robot'''
# YOUR CODE HERE
return self.recognize_posture(self.perception)
def execute_keyframes(self, keyframes):
'''excute keyframes, note this function is blocking call,
e.g. return until keyframes are executed
'''
# YOUR CODE HERE
self.keyframes = keyframes
def get_transform(self, name):
'''get transform with given name
'''
# YOUR CODE HERE
return self.transforms[name]
def set_transform(self, effector_name, transform):
'''solve the inverse kinematics and control joints use the results
'''
# YOUR CODE HERE
self.set_transforms(effector_name, transform)
if __name__ == '__main__':
agent = ServerAgent()
agent.run()