-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw3_astar.py
100 lines (72 loc) · 3.28 KB
/
hw3_astar.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
96
97
98
99
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import openravepy
#### YOUR IMPORTS GO HERE ####
from myastarfunctions import *
#### END OF YOUR IMPORTS ####
if not __openravepy_build_doc__:
from openravepy import *
from numpy import *
def waitrobot(robot):
"""busy wait for robot completion"""
while not robot.GetController().IsDone():
time.sleep(0.01)
def tuckarms(env,robot):
with env:
jointnames = ['l_shoulder_lift_joint','l_elbow_flex_joint','l_wrist_flex_joint','r_shoulder_lift_joint','r_elbow_flex_joint','r_wrist_flex_joint']
robot.SetActiveDOFs([robot.GetJoint(name).GetDOFIndex() for name in jointnames])
robot.SetActiveDOFValues([1.29023451,-2.32099996,-0.69800004,1.27843491,-2.32100002,-0.69799996]);
robot.GetController().SetDesired(robot.GetDOFValues());
waitrobot(robot)
def ConvertPathToTrajectory(robot,path=[]):
#Path should be of the form path = [q_1, q_2, q_3,...], where q_i = [x_i, y_i, theta_i]
if not path:
return None
# Initialize trajectory
traj = RaveCreateTrajectory(env,'')
traj.Init(robot.GetActiveConfigurationSpecification())
for i in range(0,len(path)):
traj.Insert(i,numpy.array(path[i]))
# Move Robot Through Trajectory
planningutils.RetimeAffineTrajectory(traj,maxvelocities=ones(3),maxaccelerations=5*ones(3))
return traj
if __name__ == "__main__":
env = Environment()
env.SetViewer('qtcoin')
collisionChecker = RaveCreateCollisionChecker(env,'ode')
env.SetCollisionChecker(collisionChecker)
env.Reset()
# load a scene from ProjectRoom environment XML file
env.Load('data/pr2test2.env.xml')
time.sleep(0.1)
# 1) get the 1st robot that is inside the loaded scene
# 2) assign it to the variable named 'robot'
robot = env.GetRobots()[0]
# tuck in the PR2's arms for driving
tuckarms(env,robot);
with env:
# the active DOF are translation in X and Y and rotation about the Z axis of the base of the robot.
robot.SetActiveDOFs([],DOFAffine.X|DOFAffine.Y|DOFAffine.RotationAxis,[0,0,1])
goalconfig = [2.6,-1.3,-pi/2]
start = time.clock()
#### YOUR CODE HERE ####
#### Implement your algorithm to compute a path for the robot's base starting from the current configuration of the robot and ending at goalconfig. The robot's base DOF have already been set as active. It may be easier to implement this as a function in a separate file and call it here.
neighbors = 8
path_nodes = astar(env,robot,goalconfig,neighbors)
path_cost = 0
for i in range(1,len(path_nodes)):
path_cost += np.linalg.norm(path_nodes[i]-path_nodes[i-1])
print "Path_cost: ", path_cost
#### Draw the X and Y components of the configurations explored by your algorithm
path = path_nodes #put your final path in this variable
#### END OF YOUR CODE ###
end = time.clock()
print "Time: ", end - start
# Now that you have computed a path, convert it to an openrave trajectory
traj = ConvertPathToTrajectory(robot, path)
# Execute the trajectory on the robot.
if traj != None:
robot.GetController().SetPath(traj)
waitrobot(robot)
raw_input("Press enter to exit...")