uav_trajectory.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import numpy as np
3 
4 def normalize(v):
5  norm = np.linalg.norm(v)
6  assert norm > 0
7  return v / norm
8 
9 
10 class Polynomial:
11  def __init__(self, p):
12  self.p = p
13 
14  # evaluate a polynomial using horner's rule
15  def eval(self, t):
16  assert t >= 0
17  x = 0.0
18  for i in range(0, len(self.p)):
19  x = x * t + self.p[len(self.p) - 1 - i]
20  return x
21 
22  # compute and return derivative
23  def derivative(self):
24  return Polynomial([(i+1) * self.p[i+1] for i in range(0, len(self.p) - 1)])
25 
26 
28  def __init__(self):
29  self.pos = None # position [m]
30  self.vel = None # velocity [m/s]
31  self.acc = None # acceleration [m/s^2]
32  self.omega = None # angular velocity [rad/s]
33  self.yaw = None # yaw angle [rad]
34 
35 
36 # 4d single polynomial piece for x-y-z-yaw, includes duration.
38  def __init__(self, duration, px, py, pz, pyaw):
39  self.duration = duration
40  self.px = Polynomial(px)
41  self.py = Polynomial(py)
42  self.pz = Polynomial(pz)
43  self.pyaw = Polynomial(pyaw)
44 
45  # compute and return derivative
46  def derivative(self):
47  return Polynomial4D(
48  self.duration,
49  self.px.derivative().p,
50  self.py.derivative().p,
51  self.pz.derivative().p,
52  self.pyaw.derivative().p)
53 
54  def eval(self, t):
55  result = TrajectoryOutput()
56  # flat variables
57  result.pos = np.array([self.px.eval(t), self.py.eval(t), self.pz.eval(t)])
58  result.yaw = self.pyaw.eval(t)
59 
60  # 1st derivative
61  derivative = self.derivative()
62  result.vel = np.array([derivative.px.eval(t), derivative.py.eval(t), derivative.pz.eval(t)])
63  dyaw = derivative.pyaw.eval(t)
64 
65  # 2nd derivative
66  derivative2 = derivative.derivative()
67  result.acc = np.array([derivative2.px.eval(t), derivative2.py.eval(t), derivative2.pz.eval(t)])
68 
69  # 3rd derivative
70  derivative3 = derivative2.derivative()
71  jerk = np.array([derivative3.px.eval(t), derivative3.py.eval(t), derivative3.pz.eval(t)])
72 
73  thrust = result.acc + np.array([0, 0, 9.81]) # add gravity
74 
75  z_body = normalize(thrust)
76  x_world = np.array([np.cos(result.yaw), np.sin(result.yaw), 0])
77  y_body = normalize(np.cross(z_body, x_world))
78  x_body = np.cross(y_body, z_body)
79 
80  jerk_orth_zbody = jerk - (np.dot(jerk, z_body) * z_body)
81  h_w = jerk_orth_zbody / np.linalg.norm(thrust)
82 
83  result.omega = np.array([-np.dot(h_w, y_body), np.dot(h_w, x_body), z_body[2] * dyaw])
84  return result
85 
86 
87 class Trajectory:
88  def __init__(self):
89  self.polynomials = None
90  self.duration = None
91 
92  def loadcsv(self, filename):
93  data = np.loadtxt(filename, delimiter=",", skiprows=1, usecols=range(33))
94  self.polynomials = [Polynomial4D(row[0], row[1:9], row[9:17], row[17:25], row[25:33]) for row in data]
95  self.duration = np.sum(data[:,0])
96 
97  def eval(self, t):
98  assert t >= 0
99  assert t <= self.duration
100 
101  current_t = 0.0
102  for p in self.polynomials:
103  if t < current_t + p.duration:
104  return p.eval(t - current_t)
105  current_t = current_t + p.duration
def loadcsv(self, filename)
def __init__(self, duration, px, py, pz, pyaw)


crazyflie_demo
Author(s): Wolfgang Hoenig
autogenerated on Mon Sep 28 2020 03:40:12