|
| 1 | + |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from underactuated import PyPlotVisualizer |
| 5 | +from pydrake.systems.framework import BasicVector_, LeafSystem_, PortDataType |
| 6 | +from pydrake.systems.scalar_conversion import TemplateSystem |
| 7 | + |
| 8 | + |
| 9 | +# Note: In order to use the Python system with drake's autodiff features, we |
| 10 | +# have to add a little "TemplateSystem" boilerplate (for now). For details, |
| 11 | +# see https://drake.mit.edu/pydrake/pydrake.systems.scalar_conversion.html |
| 12 | + |
| 13 | +# TODO(russt): Clean this up pending any resolutions on |
| 14 | +# https://github.com/RobotLocomotion/drake/issues/10745 |
| 15 | +@TemplateSystem.define("Quadrotor2D_") |
| 16 | +def Quadrotor2D_(T): |
| 17 | + |
| 18 | + class Impl(LeafSystem_[T]): |
| 19 | + def _construct(self, converter=None): |
| 20 | + LeafSystem_[T].__init__(self, converter) |
| 21 | + # two inputs (thrust) |
| 22 | + self._DeclareVectorInputPort("u", BasicVector_[T](2)) |
| 23 | + # six outputs (full state) |
| 24 | + self._DeclareVectorOutputPort("x", BasicVector_[T](6), |
| 25 | + self._CopyStateOut) |
| 26 | + # three positions, three velocities |
| 27 | + self._DeclareContinuousState(3, 3, 0) |
| 28 | + |
| 29 | + # parameters based on [Bouadi, Bouchoucha, Tadjine, 2007] |
| 30 | + self.length = 0.25 # length of rotor arm |
| 31 | + self.mass = 0.486 # mass of quadrotor |
| 32 | + self.inertia = 0.00383 # moment of inertia |
| 33 | + self.gravity = 9.81 # gravity |
| 34 | + |
| 35 | + def _construct_copy(self, other, converter=None): |
| 36 | + Impl._construct(self, converter=converter) |
| 37 | + |
| 38 | + def _CopyStateOut(self, context, output): |
| 39 | + x = context.get_continuous_state_vector().CopyToVector() |
| 40 | + y = output.SetFromVector(x) |
| 41 | + |
| 42 | + def _DoCalcTimeDerivatives(self, context, derivatives): |
| 43 | + x = context.get_continuous_state_vector().CopyToVector() |
| 44 | + u = self.EvalVectorInput(context, 0).CopyToVector() |
| 45 | + q = x[:3] |
| 46 | + qdot = x[3:] |
| 47 | + qddot = np.array([-np.sin(q[2])/self.mass*(u[0] + u[1]), |
| 48 | + np.cos(x[2])/self.mass*(u[0] + u[1]) |
| 49 | + - self.gravity, |
| 50 | + self.length/self.inertia*(-u[0] + u[1])]) |
| 51 | + derivatives.get_mutable_vector().SetFromVector( |
| 52 | + np.concatenate((qdot, qddot))) |
| 53 | + |
| 54 | + return Impl |
| 55 | + |
| 56 | + |
| 57 | +Quadrotor2D = Quadrotor2D_[None] # Default instantiation |
| 58 | + |
| 59 | + |
| 60 | +class Quadrotor2DVisualizer(PyPlotVisualizer): |
| 61 | + def __init__(self): |
| 62 | + PyPlotVisualizer.__init__(self) |
| 63 | + self._DeclareInputPort(PortDataType.kVectorValued, 6) |
| 64 | + self.ax.set_aspect('equal') |
| 65 | + self.ax.set_xlim(-2, 2) |
| 66 | + self.ax.set_ylim(-1, 1) |
| 67 | + |
| 68 | + self.length = .25 # moment arm (meters) |
| 69 | + |
| 70 | + self.base = np.vstack((1.2*self.length*np.array([1, -1, -1, 1, 1]), |
| 71 | + 0.025*np.array([1, 1, -1, -1, 1]))) |
| 72 | + self.pin = np.vstack((0.005*np.array([1, 1, -1, -1, 1]), |
| 73 | + .1*np.array([1, 0, 0, 1, 1]))) |
| 74 | + a = np.linspace(0, 2*np.pi, 50) |
| 75 | + self.prop = np.vstack((self.length/1.5*np.cos(a), .1+.02*np.sin(2*a))) |
| 76 | + |
| 77 | + self.base_fill = self.ax.fill(self.base[0, :], self.base[1, :], |
| 78 | + zorder=1, edgecolor='k', |
| 79 | + facecolor=[.6, .6, .6]) |
| 80 | + self.left_pin_fill = self.ax.fill(self.pin[0, :], self.pin[1, :], |
| 81 | + zorder=0, edgecolor='k', |
| 82 | + facecolor=[0, 0, 0]) |
| 83 | + self.right_pin_fill = self.ax.fill(self.pin[0, :], self.pin[1, :], |
| 84 | + zorder=0, edgecolor='k', |
| 85 | + facecolor=[0, 0, 0]) |
| 86 | + self.left_prop_fill = self.ax.fill(self.prop[0, :], self.prop[0, :], |
| 87 | + zorder=0, edgecolor='k', |
| 88 | + facecolor=[0, 0, 1]) |
| 89 | + self.right_prop_fill = self.ax.fill(self.prop[0, :], self.prop[0, :], |
| 90 | + zorder=0, edgecolor='k', |
| 91 | + facecolor=[0, 0, 1]) |
| 92 | + |
| 93 | + def draw(self, context): |
| 94 | + x = self.EvalVectorInput(context, 0).CopyToVector() |
| 95 | + R = np.array([[np.cos(x[2]), -np.sin(x[2])], |
| 96 | + [np.sin(x[2]), np.cos(x[2])]]) |
| 97 | + |
| 98 | + p = np.dot(R, self.base) |
| 99 | + self.base_fill[0].get_path().vertices[:, 0] = x[0] + p[0, :] |
| 100 | + self.base_fill[0].get_path().vertices[:, 1] = x[1] + p[1, :] |
| 101 | + |
| 102 | + p = np.dot(R, np.vstack((-self.length + self.pin[0, :], |
| 103 | + self.pin[1, :]))) |
| 104 | + self.left_pin_fill[0].get_path().vertices[:, 0] = x[0] + p[0, :] |
| 105 | + self.left_pin_fill[0].get_path().vertices[:, 1] = x[1] + p[1, :] |
| 106 | + p = np.dot(R, np.vstack((self.length + self.pin[0, :], |
| 107 | + self.pin[1, :]))) |
| 108 | + self.right_pin_fill[0].get_path().vertices[:, 0] = x[0] + p[0, :] |
| 109 | + self.right_pin_fill[0].get_path().vertices[:, 1] = x[1] + p[1, :] |
| 110 | + |
| 111 | + p = np.dot(R, np.vstack((-self.length + self.prop[0, :], |
| 112 | + self.prop[1, :]))) |
| 113 | + self.left_prop_fill[0].get_path().vertices[:, 0] = x[0] + p[0, :] |
| 114 | + self.left_prop_fill[0].get_path().vertices[:, 1] = x[1] + p[1, :] |
| 115 | + |
| 116 | + p = np.dot(R, np.vstack((self.length + self.prop[0, :], |
| 117 | + self.prop[1, :]))) |
| 118 | + self.right_prop_fill[0].get_path().vertices[:, 0] = x[0] + p[0, :] |
| 119 | + self.right_prop_fill[0].get_path().vertices[:, 1] = x[1] + p[1, :] |
| 120 | + |
| 121 | + self.ax.set_title('t = ' + str(context.get_time())) |
0 commit comments