|
| 1 | +# Copyright (c) 2024. |
| 2 | +# ProrokLab (https://www.proroklab.org/) |
| 3 | +# All rights reserved. |
| 4 | + |
| 5 | +from typing import Union |
| 6 | + |
| 7 | +import torch |
| 8 | +from torch import Tensor |
| 9 | + |
| 10 | +import vmas.simulator.core |
| 11 | +import vmas.simulator.utils |
| 12 | +from vmas.simulator.dynamics.common import Dynamics |
| 13 | + |
| 14 | + |
| 15 | +class Drone(Dynamics): |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + world: vmas.simulator.core.World, |
| 19 | + I_xx: float = 8.1e-3, |
| 20 | + I_yy: float = 8.1e-3, |
| 21 | + I_zz: float = 14.2e-3, |
| 22 | + integration: str = "rk4", |
| 23 | + ): |
| 24 | + super().__init__() |
| 25 | + |
| 26 | + assert integration in ( |
| 27 | + "rk4", |
| 28 | + "euler", |
| 29 | + ) |
| 30 | + |
| 31 | + self.integration = integration |
| 32 | + self.I_xx = I_xx |
| 33 | + self.I_yy = I_yy |
| 34 | + self.I_zz = I_zz |
| 35 | + self.world = world |
| 36 | + self.g = 9.81 |
| 37 | + self.dt = world.dt |
| 38 | + self.reset() |
| 39 | + |
| 40 | + def reset(self, index: Union[Tensor, int] = None): |
| 41 | + if index is None: |
| 42 | + # Drone state: phi(roll), theta (pitch), psi (yaw), |
| 43 | + # p (roll_rate), q (pitch_rate), r (yaw_rate), |
| 44 | + # x_dot (vel_x), y_dot (vel_y), z_dot (vel_z), |
| 45 | + # x (pos_x), y (pos_y), z (pos_z) |
| 46 | + self.drone_state = torch.zeros( |
| 47 | + self.world.batch_dim, |
| 48 | + 12, |
| 49 | + device=self.world.device, |
| 50 | + ) |
| 51 | + else: |
| 52 | + self.drone_state[index] = 0.0 |
| 53 | + |
| 54 | + def needs_reset(self) -> Tensor: |
| 55 | + # Constraint roll and pitch within +-30 degrees |
| 56 | + return torch.any(self.drone_state[:, :2].abs() > 30 * (torch.pi / 180), dim=-1) |
| 57 | + |
| 58 | + def euler(self, f, state): |
| 59 | + return state + self.dt * f(state) |
| 60 | + |
| 61 | + def runge_kutta(self, f, state): |
| 62 | + k1 = f(state) |
| 63 | + k2 = f(state + self.dt * k1 / 2) |
| 64 | + k3 = f(state + self.dt * k2 / 2) |
| 65 | + k4 = f(state + self.dt * k3) |
| 66 | + return state + (self.dt / 6) * (k1 + 2 * k2 + 2 * k3 + k4) |
| 67 | + |
| 68 | + @property |
| 69 | + def needed_action_size(self) -> int: |
| 70 | + return 4 |
| 71 | + |
| 72 | + def process_action(self): |
| 73 | + u = self.agent.action.u |
| 74 | + thrust = u[:, 0] # Thrust, sum of all propeller thrusts |
| 75 | + torque = u[:, 1:4] # Torque in x, y, z direction |
| 76 | + |
| 77 | + thrust += self.agent.mass * self.g # Ensure the drone is not falling |
| 78 | + |
| 79 | + self.drone_state[:, 9] = self.agent.state.pos[:, 0] # x |
| 80 | + self.drone_state[:, 10] = self.agent.state.pos[:, 1] # y |
| 81 | + self.drone_state[:, 2] = self.agent.state.rot[:, 0] # psi (yaw) |
| 82 | + |
| 83 | + def f(state): |
| 84 | + phi = state[:, 0] |
| 85 | + theta = state[:, 1] |
| 86 | + psi = state[:, 2] |
| 87 | + p = state[:, 3] |
| 88 | + q = state[:, 4] |
| 89 | + r = state[:, 5] |
| 90 | + x_dot = state[:, 6] |
| 91 | + y_dot = state[:, 7] |
| 92 | + z_dot = state[:, 8] |
| 93 | + |
| 94 | + c_phi = torch.cos(phi) |
| 95 | + s_phi = torch.sin(phi) |
| 96 | + c_theta = torch.cos(theta) |
| 97 | + s_theta = torch.sin(theta) |
| 98 | + c_psi = torch.cos(psi) |
| 99 | + s_psi = torch.sin(psi) |
| 100 | + |
| 101 | + # Postion Dynamics |
| 102 | + x_ddot = ( |
| 103 | + (c_phi * s_theta * c_psi + s_phi * s_psi) * thrust / self.agent.mass |
| 104 | + ) |
| 105 | + y_ddot = ( |
| 106 | + (c_phi * s_theta * s_psi - s_phi * c_psi) * thrust / self.agent.mass |
| 107 | + ) |
| 108 | + z_ddot = (c_phi * c_theta) * thrust / self.agent.mass - self.g |
| 109 | + # Angular velocity dynamics |
| 110 | + p_dot = (torque[:, 0] - (self.I_yy - self.I_zz) * q * r) / self.I_xx |
| 111 | + q_dot = (torque[:, 1] - (self.I_zz - self.I_xx) * p * r) / self.I_yy |
| 112 | + r_dot = (torque[:, 2] - (self.I_xx - self.I_yy) * p * q) / self.I_zz |
| 113 | + |
| 114 | + return torch.stack( |
| 115 | + [ |
| 116 | + p, |
| 117 | + q, |
| 118 | + r, |
| 119 | + p_dot, |
| 120 | + q_dot, |
| 121 | + r_dot, |
| 122 | + x_ddot, |
| 123 | + y_ddot, |
| 124 | + z_ddot, |
| 125 | + x_dot, |
| 126 | + y_dot, |
| 127 | + z_dot, |
| 128 | + ], |
| 129 | + dim=-1, |
| 130 | + ) |
| 131 | + |
| 132 | + if self.integration == "euler": |
| 133 | + new_drone_state = self.euler(f, self.drone_state) |
| 134 | + else: |
| 135 | + new_drone_state = self.runge_kutta(f, self.drone_state) |
| 136 | + |
| 137 | + # Calculate the change in state |
| 138 | + delta_state = new_drone_state - self.drone_state |
| 139 | + self.drone_state = new_drone_state |
| 140 | + |
| 141 | + # Calculate the accelerations required to achieve the change in state |
| 142 | + acceleration_x = delta_state[:, 6] / self.dt |
| 143 | + acceleration_y = delta_state[:, 7] / self.dt |
| 144 | + angular_acceleration = delta_state[:, 5] / self.dt |
| 145 | + |
| 146 | + # Calculate the forces required for the linear accelerations |
| 147 | + force_x = self.agent.mass * acceleration_x |
| 148 | + force_y = self.agent.mass * acceleration_y |
| 149 | + |
| 150 | + # Calculate the torque required for the angular acceleration |
| 151 | + torque_yaw = self.agent.moment_of_inertia * angular_acceleration |
| 152 | + |
| 153 | + # Update the physical force and torque required for the user inputs |
| 154 | + self.agent.state.force[:, vmas.simulator.utils.X] = force_x |
| 155 | + self.agent.state.force[:, vmas.simulator.utils.Y] = force_y |
| 156 | + self.agent.state.torque = torque_yaw.unsqueeze(-1) |
0 commit comments