forked from DAInamite/programming-humanoid-robot-in-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangle_interpolation.py
74 lines (62 loc) · 3.28 KB
/
angle_interpolation.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
'''In this exercise you need to implement an angle interploation function which makes NAO executes keyframe motion
* Tasks:
1. complete the code in `AngleInterpolationAgent.angle_interpolation`,
you are free to use splines interploation or Bezier interploation,
but the keyframes provided are for Bezier curves, you can simply ignore some data for splines interploation,
please refer data format below for details.
2. try different keyframes from `keyframes` folder
* Keyframe data format:
keyframe := (names, times, keys)
names := [str, ...] # list of joint names
times := [[float, float, ...], [float, float, ...], ...]
# times is a matrix of floats: Each line corresponding to a joint, and column element to a key.
keys := [[float, [int, float, float], [int, float, float]], ...]
# keys is a list of angles in radians or an array of arrays each containing [float angle, Handle1, Handle2],
# where Handle is [int InterpolationType, float dTime, float dAngle] describing the handle offsets relative
# to the angle and time of the point. The first Bezier param describes the handle that controls the curve
# preceding the point, the second describes the curve following the point.
'''
from pid import PIDAgent
from keyframes import hello
class AngleInterpolationAgent(PIDAgent):
def __init__(self, simspark_ip='localhost',
simspark_port=3100,
teamname='DAInamite',
player_id=0,
sync_mode=True):
super(AngleInterpolationAgent, self).__init__(simspark_ip, simspark_port, teamname, player_id, sync_mode)
self.keyframes = ([], [], [])
self.start_time = self.perception.time
def think(self, perception):
target_joints = self.angle_interpolation(self.keyframes, perception)
self.target_joints.update(target_joints)
return super(AngleInterpolationAgent, self).think(perception)
def angle_interpolation(self, keyframes, perception):
target_joints = {}
# YOUR CODE HERE
current_time = perception.time - self.start_time
names, times, keys = keyframes
for i, name in enumerate(names):
if name in self.joint_names:
for i_time in range(len(times[i]) - 1):
if current_time < times[i][0]:
p0 = self.perception.joint[name]
p3 = keys[i][0][0]
p1 = p3 + keys[i][0][1][2]
p2 = p1
t = current_time / times[i][0]
elif (times[i][i_time] < current_time < times[i][len(times[i])-1]):
p0 = keys[i][i_time][0]
p3 = keys[i][i_time + 1][0]
p1 = p0 + keys[i][i_time][2][2]
p2 = p3 + keys[i][i_time][1][2]
t = (current_time - times[i][i_time]) / (times[i][i_time + 1] - times[i][i_time])
try:
target_joints[name] = ((1-t)**3*p0 + 3*(1-t)**2*t*p1 + 3*(1-t)*t**2*p2 + t**3*p3)
except:
break
return target_joints
if __name__ == '__main__':
agent = AngleInterpolationAgent()
agent.keyframes = hello() # CHANGE DIFFERENT KEYFRAMES
agent.run()