summaryrefslogtreecommitdiff
path: root/PVCM/cama/en/ma25 Drones -- Exercice.ipynb
blob: e6055c3ba3e8291d0e0b0a5dd3e3b06704bfd977 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
{
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5,
 "cells": [
  {
   "cell_type": "markdown",
   "id": "899494b3",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "# Drone show\n",
    "\n",
    "<img src=\"images/drone_boat.png\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ada5357c",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "The goal is to make a nice night show.\n",
    "\n",
    "We have a set of 100 drones that form the image of a boat in 3D. For us it is a set of 3D points (center of gravity of each drone) that we store in a Numpy 3 by 100 table. The boat fits in a 20x20x20 cube.\n",
    "\n",
    "Initially\n",
    "\n",
    "*all the drones are centered at (0,0,0) so if you want to position the boat at (10, 20, 200) you have to translate the same amount over all the drones.* the main axis of the boat is the axis of x i.e. the front of the boat is at x=10 and the back at x=-10."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "58dcf259",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-25T09:43:59.562566Z",
     "start_time": "2024-03-25T09:43:59.487939Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-20.,  20.,   0.,   0.,   0.,   0.,   0.],\n",
       "       [  0.,   0.,   0.,   0.,   0., -10.,  10.],\n",
       "       [  0.,   0.,  -5.,   5.,   0.,   0.,   0.]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "#boat = np.random.randint(-100, 100, size=(3,10)) / 10   # with a little luck...\n",
    "\n",
    "# A formation that allows for easier debugging\n",
    "\n",
    "boat = np.array([[-20.,   0.,   0.],\n",
    "                 [ 20.,   0.,   0.],\n",
    "                 [  0.,   0.,  -5.],\n",
    "                 [  0.,   0.,   5.],\n",
    "                 [  0.,   0.,   0.],\n",
    "                 [  0., -10.,   0.],\n",
    "                 [  0.,  10.,   0.],\n",
    "                ])\n",
    "boat = boat.T\n",
    "boat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ab4a847",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "We want to move the boat in the sky by making it do figures (the units used are the meter and the second). All movements are broken down second by second i.e. we give the position of all the drones every second (another program manages to move the drones to their next position).\n",
    "\n",
    "### Figure 1\n",
    "\n",
    "Rotate the boat (therefore all the drones) horizontally around the point (0,0,80) at a distance of 100 m. It makes a full turn in 5 minutes.\n",
    "\n",
    "Care should be taken that the orientation of the boat follows the movement, i.e. that the front of the boat is always well oriented."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4818926d",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def circle(boat, t):\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35cc29f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Code to display\n",
    "# Do not touch this unless you know what you are doing\n",
    "import matplotlib.animation\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.patches import Circle\n",
    "from matplotlib.transforms import Affine2D\n",
    "import mpl_toolkits.mplot3d.art3d as art3d\n",
    "matplotlib.rcParams['animation.embed_limit'] = 256 # 256mb max animation size\n",
    "\n",
    "plt.rcParams[\"animation.html\"] = \"jshtml\"\n",
    "plt.rcParams['figure.dpi'] = 100  \n",
    "plt.ioff()\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(projection='3d')\n",
    "\n",
    "# 5min -> 300 sec;\n",
    "FRAMEPERSEC = 1.\n",
    "\n",
    "def animate(t):\n",
    "    boat_prime = circle(boat_start, t/FRAMEPERSEC)\n",
    "    plt.cla()\n",
    "    p = Circle((0, 0), 100, fill=False)\n",
    "    ax.add_patch(p)\n",
    "    art3d.pathpatch_2d_to_3d(p, z=80, zdir=\"z\")\n",
    "    plt.plot(boat_prime[0,:], boat_prime[1,:], boat_prime[2,:], '.b')\n",
    "    ax.set_xlim([-120, 120])\n",
    "    ax.set_ylim([-120, 120])\n",
    "    ax.set_zlim([0, 120]) # alternative\n",
    "    ax.set_xlabel(\"x\")\n",
    "    ax.set_ylabel(\"y\")\n",
    "    ax.set_zlabel(\"z\")\n",
    "    ax.set_title(f\"t: {t} sec\")\n",
    "\n",
    "\n",
    "matplotlib.animation.FuncAnimation(fig, animate, frames=int(300*FRAMEPERSEC))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9ca0600",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "### Figure 2\n",
    "\n",
    "We want to rotate the boat on its main axis (the x axis) until it returns to the original position. In aviation, this is called a barrel roll (rotation of 360 on the roll axis). Write the `barrel_roll(boat, T, t)` function that returns the position of each drone at time `t` given that a complete roll take `T` seconds."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bbdb81d",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def barrel_rool(boat, T, t):\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3861ca65",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Code to display\n",
    "# You can change this\n",
    "Tcomplete = 20\n",
    "\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(projection='3d')\n",
    "\n",
    "# Do not touch this unless you know what you are doing\n",
    "def animate2(t):\n",
    "    boat_prime = barrel_roll(boat, Tcomplete, t)\n",
    "    plt.cla()\n",
    "    plt.plot(boat_prime[0,:], boat_prime[1,:], boat_prime[2,:], '.b')\n",
    "    ax.set_xlim([-60, 60])\n",
    "    ax.set_ylim([-60, 60])\n",
    "    ax.set_zlim([-60, 60])\n",
    "    ax.set_xlabel(\"x\")\n",
    "    ax.set_ylabel(\"y\")\n",
    "    ax.set_zlabel(\"z\")\n",
    "    ax.set_title(f\"t: {t} sec\")\n",
    "\n",
    "# One frame per second for 2 complete rolls\n",
    "matplotlib.animation.FuncAnimation(fig, animate2, frames=2*Tcomplete)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51d00ff1",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "### Figure 3\n",
    "\n",
    "Combine figures 1 and 2 so that the boat rolls in 10 seconds every 30 seconds."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc7aed8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def combined_move(boat, t):\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2265fd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Code to display\n",
    "# Do not touch this unless you know what you are doing\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(projection='3d')\n",
    "\n",
    "def animate3(t):\n",
    "    boat_prime = combined_move(boat, t/FRAMEPERSEC)\n",
    "    plt.cla()\n",
    "    p = Circle((0, 0), 100, fill=False)\n",
    "    ax.add_patch(p)\n",
    "    art3d.pathpatch_2d_to_3d(p, z=80, zdir=\"z\")\n",
    "    plt.plot(boat_prime[0,:], boat_prime[1,:], boat_prime[2,:], '.b')\n",
    "    ax.set_xlim([-120, 120])\n",
    "    ax.set_ylim([-120, 120])\n",
    "    ax.set_zlim([0, 120])\n",
    "    ax.set_xlabel(\"x\")\n",
    "    ax.set_ylabel(\"y\")\n",
    "    ax.set_zlabel(\"z\")\n",
    "    ax.set_title(f\"t: {t} sec\")\n",
    "\n",
    "# 5min -> 300 sec\n",
    "matplotlib.animation.FuncAnimation(fig, animate3, frames=int(300*FRAMEPERSEC))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c2da47f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f9122e6-899e-4afa-a49a-992e48e99647",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}