summaryrefslogtreecommitdiff
path: root/PVCM/cama/fr/ma25 Drones -- Exercice.ipynb
blob: 019ce93471b351a0e26e22f4a853c4615f456eb9 (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
295
{
 "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": "3235ec05",
   "metadata": {
    "lang": "fr"
   },
   "source": [
    "# Spectacle de drones\n",
    "\n",
    "<img src=\"images/drone_boat.png\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "630fb2b1",
   "metadata": {
    "lang": "fr"
   },
   "source": [
    "Le but est de faire un joli spectacle nocturne. \n",
    "\n",
    "On a un ensemble de 100 drones qui forment l'image d'un bateau en 3D. Pour nous il s'agit d'un ensemble de points 3D (centre de gravité de chaque drone) qu'on range dans un tableau Numpy 3 par 100. Le bateau tient dans un cube de 20x20x20. \n",
    "\n",
    "Initialement \n",
    "\n",
    "* l'ensemble des drones est centré en (0,0,0) donc si on veut positionner le bateau en (10, 20, 200) il faut faire une translation d'autant sur l'ensemble des drones.\n",
    "* l'axe principal du bateau est l'axe de x c.a.d que l'avant du bateau est en x=10 et l'arrière en 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": "311abaaa",
   "metadata": {
    "lang": "fr"
   },
   "source": [
    "On désire déplacer le bateau dans le ciel en lui faisant faire des figures (les unités utilisées sont le mètre et la seconde). Tous les mouvements sont décomposés seconde par seconde c.a.d. qu'on donne la position de tous les drones à chaque seconde (un autre programme se débrouille pour déplacer les drones à leur position suivante).\n",
    "\n",
    "### Figure 1\n",
    "\n",
    "Faire tourner le bateau (donc l'ensemble des drones) à l'horizontal autour du point (0,0,80) à une distance de 100 m. Il fait un tour complet en 5 minutes. \n",
    "\n",
    "On fera attention à ce que l'orientation du bateau suive le mouvement, c.a.d. que l'avant du bateau soit toujours bien orientée. \n"
   ]
  },
  {
   "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": "2318d7f5",
   "metadata": {
    "lang": "fr"
   },
   "source": [
    "### Figure 2\n",
    "\n",
    "On désire faire tourner le bateau sur son axe principal (l'axe des x) jusqu'à revenir à la position d'origine. En aviation on appelle cela faire un tonneau (rotation de 360 sur l'axe du roulis). Écrire la fonction `barrel_roll(boat, T, t)` qui retourne la position de chaque drone, à l'instant `t` si un tour complet dure `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": "65cfd841",
   "metadata": {
    "lang": "fr"
   },
   "source": [
    "### Figure 3\n",
    "\n",
    "Combiner les figures 1 et 2 pour que le bateau fasse un tonneau en 10 secondes toutes les 30 secondes."
   ]
  },
  {
   "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": []
  }
 ]
}