def GetABCFromSO3Manifold(SO3dimIdx, amin, amax, W, dW, ddW): Ndim = W.shape[0] Nwaypoints = W.shape[1] duration = 1.0 / float(Nwaypoints - 1) [SO3trajstr, durationVector] = GetTrajectoryStringSO3(SO3dimIdx, W, dW, ddW) if SO3trajstr is None: a = np.zeros((Nwaypoints, 1)) b = np.zeros((Nwaypoints, 1)) c = np.zeros((Nwaypoints, 1)) return [a, b, c] #SO3traj = Trajectory.PiecewisePolynomialTrajectory.FromString(SO3trajstr) #SO3traj.Plot(0.05) #plt.show() constraintsstr = str(duration) SO3dim = len(SO3dimIdx) assert (SO3dim == 3) #for j in range(SO3dim): ### TODO: make generic amax[1] -> amax[SO3] constraintsstr += "\n" + ' '.join([str(amax[1]), str(0.0), str(0.0)]) constraintsstr += "\n" + ' '.join([str(amin[1]), str(0.0), str(0.0)]) #print constraintsstr #print "###########################" inertia = eye(3) for v in inertia: constraintsstr += "\n" + ' '.join([str(i) for i in v]) abc = TOPPbindings.RunComputeSO3Constraints(SO3trajstr, constraintsstr) a, b, c = Extractabc(abc) return [a, b, c]
def Shortcut(robot, taumax, vmax, lietraj, maxiter, expectedduration=-1, meanduration=0, upperlimit=-1, inertia=None, trackingplot=None): if trackingplot == 1: plt.axis([0, maxiter, 0, lietraj.duration]) plt.ion() plt.show() ylabel('Trajectory duration (s)') xlabel('Iteration') t_sc_start = time.time() originalduration = lietraj.duration #return shortcuted traj if upperlimit < 0: dur = lietraj.duration upperlimit = lietraj.duration else: dur = upperlimit attempt = 0 ## for shortcutting integrationtimestep = 1e-2 reparamtimestep = 1e-2 passswitchpointnsteps = 5 discrtimestep = 1e-2 constraintsstring = str(discrtimestep) constraintsstring += "\n" + ' '.join([str(v) for v in taumax]) if not (inertia is None): for v in inertia: constraintsstring += "\n" + ' '.join([str(i) for i in v]) assert (dur > 10.0 * discrtimestep) ncollision = 0 nnotretimable = 0 nnotshorter = 0 for it in range(maxiter): if trackingplot == 1: plt.scatter(it, lietraj.duration) plt.draw() if ( expectedduration > 0 ): # check, if newlietraj.duration is short enough, stop SHORTCUTING if (lietraj.duration < expectedduration): print "\033[1;32mTrajectory's duration is already shorter than expected time --> stop shortcuting\033[0m" break if (dur < discrtimestep): print "[Utils::Shortcut] trajectory duration is less than discrtimestep.\n" break ## otherwise, this will cause an error in TOPP ## select an interval for shortcutting t0 = random.rand() * dur if meanduration == 0: meanduration = dur - t0 T = random.rand() * min(meanduration, dur - t0) t1 = t0 + T while (T < 2.0 * discrtimestep): t0 = random.rand() * dur if meanduration == 0: meanduration = dur - t0 T = random.rand() * min(meanduration, dur - t0) t1 = t0 + T if t1 > upperlimit: t1 = upperlimit if (t1 < t0): temp = t0 t0 = t1 t1 = temp T = t1 - t0 # print "\n\nShortcutting iteration", it + 1 # print t0, t1, t1- t0 # interpolate from t0 to t1 R_beg = lietraj.EvalRotation(t0) R_end = lietraj.EvalRotation(t1) omega0 = lietraj.EvalOmega(t0) omega1 = lietraj.EvalOmega(t1) shortcuttraj = lie.InterpolateSO3(R_beg, R_end, omega0, omega1, T) #check feasibility only for the new portion isincollision = CheckCollisionTraj(robot, shortcuttraj, R_beg, discrtimestep) if (not isincollision): # a,b,c = lie.ComputeSO3Constraints(shortcuttraj, taumax, discrtimestep) abc = TOPPbindings.RunComputeSO3Constraints( str(shortcuttraj), constraintsstring) # discrtimestep) a, b, c = lie.Extractabc(abc) topp_inst = TOPP.QuadraticConstraints(shortcuttraj, discrtimestep, vmax, list(a), list(b), list(c)) x = topp_inst.solver ret = x.RunComputeProfiles(1, 1) if (ret == 1): x.resduration ## check whether the new one has shorter duration if (x.resduration + 0.01 < T): #skip if not shorter than 0.3 s x.ReparameterizeTrajectory() x.WriteResultTrajectory() TOPPed_shortcuttraj = Trajectory.PiecewisePolynomialTrajectory.FromString( x.restrajectorystring) newlietraj = ReplaceTrajectorySegment( lietraj, TOPPed_shortcuttraj, t0, t1) lietraj = newlietraj dur = lietraj.duration #print "*******************************************" print "Success at iteration", it + 1, ":", t0, t1, "Deta_t:", t1 - t0 - x.resduration attempt += 1 #print "T:", nnotretimable, "; S:", nnotshorter , "; C:", ncollision , "; OK:", attempt #print "*******************************************" else: # print "Not shorter" nnotshorter += 1 else: # print "Not retimable" nnotretimable += 1 else: # print "Collision" ncollision += 1 # print "T:", nnotretimable, "; S:", nnotshorter , "; C:", ncollision , "; OK:", attempt print "\033[1;32mT:", nnotretimable, "; S:", nnotshorter, "; C:", ncollision, "; OK:", attempt, "\033[0m" print "\033[1;32m", originalduration - lietraj.duration, "sec. shorter\033[0m" t_sc_end = time.time() print "\033[1;32mRunning time:", t_sc_end - t_sc_start, "sec.\033[0m" return lietraj
3 0.0 0.1 -0.159247917034 0.0789972227119 0.0 0.1 -32.7720979649 43.5627972865 0.0 0.1 0.958473557774 -1.41129807703""" traj = Trajectory.PiecewisePolynomialTrajectory.FromString(trajstr) inertia = eye(3) vmax = ones(3) accelmax = ones(3) discrtimestep = 1e-3 constraintsstr = str(discrtimestep) constraintsstr += "\n" + " ".join([str(a) for a in accelmax]) for v in inertia: constraintsstr += "\n" + " ".join([str(i) for i in v]) #When Inertia is an Identity matrix, angular accelerations are the same as torques t0 = time.time() abc = TOPPbindings.RunComputeSO3Constraints(trajstr, constraintsstr) a, b, c = Extractabc(abc) t1 = time.time() topp_inst = TOPP.QuadraticConstraints(traj, discrtimestep, vmax, list(a), list(b), list(c)) x = topp_inst.solver ret = x.RunComputeProfiles(0, 0) x.ReparameterizeTrajectory() t2 = time.time() print("Compute a,b,c:", t1 - t0) print("Run TOPP:", t2 - t1) print("Total:", t2 - t0)
# print "in collision", " ", t, "/" , lietraj.duration # time.sleep(0.01) ################################# TOPP ############################################# discrtimestep= 1e-2 constraintsstring = str(discrtimestep) constraintsstring += "\n" + ' '.join([str(v) for v in taumax]) for v in inertia: constraintsstring += "\n" + ' '.join([str(i) for i in v]) # Note that, when Inertia is an Identity matrix, angular accelerations are the same as torques print "\033[93mRunning TOPP", "\033[0m" t_topp_start = time.time() traj = Trajectory.PiecewisePolynomialTrajectory.FromString(Utils.TrajStringFromTrajList(Trajlist)) abc = TOPPbindings.RunComputeSO3Constraints(str(traj),constraintsstring) a,b,c = lie.Extractabc(abc) # a,b,c = lie.ComputeSO3Constraints(traj, taumax, discrtimestep) #This is the implementation of computing SO3Constraints in Python topp_inst = TOPP.QuadraticConstraints(traj, discrtimestep, vmax, list(a), list(b), list(c)) x = topp_inst.solver ret = x.RunComputeProfiles(0,0) if ret == 1: x.ReparameterizeTrajectory() x.WriteResultTrajectory() traj1 = Trajectory.PiecewisePolynomialTrajectory.FromString(x.restrajectorystring) t_topp_end = time.time() print "\033[1;32mRunning time:",t_topp_end-t_topp_start, "sec.\033[0m"