|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Cheatsheet\n", |
| 8 | + "\n", |
| 9 | + "*License: CC-BY-NC-SA 4.0*\n", |
| 10 | + "\n", |
| 11 | + "*Author: Murilo M. Marinho (murilo.marinho@manchester.ac.uk)*\n", |
| 12 | + "\n", |
| 13 | + "## Installation" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "%%capture\n", |
| 23 | + "%pip install numpy\n", |
| 24 | + "%pip install roboticstoolbox-python\n", |
| 25 | + "%pip install numpy --break-system-packages\n", |
| 26 | + "%pip install roboticstoolbox-python --break-system-packages" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "metadata": {}, |
| 32 | + "source": [ |
| 33 | + "## Imports" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": 37, |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "import numpy as np\n", |
| 43 | + "import spatialmath as sm\n", |
| 44 | + "from math import pi" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "## Converting from `spatialmath` to `numpy`\n", |
| 52 | + "\n", |
| 53 | + "### SE(2)" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 38, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [ |
| 61 | + { |
| 62 | + "name": "stdout", |
| 63 | + "output_type": "stream", |
| 64 | + "text": [ |
| 65 | + " \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m-1 \u001b[0m \u001b[38;5;4m 1 \u001b[0m \u001b[0m\n", |
| 66 | + " \u001b[38;5;1m 1 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;4m 2 \u001b[0m \u001b[0m\n", |
| 67 | + " \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 1 \u001b[0m \u001b[0m\n", |
| 68 | + "\n", |
| 69 | + "The original SE(2) object was \n", |
| 70 | + " \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m-1 \u001b[0m \u001b[38;5;4m 1 \u001b[0m \u001b[0m\n", |
| 71 | + " \u001b[38;5;1m 1 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;4m 2 \u001b[0m \u001b[0m\n", |
| 72 | + " \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 1 \u001b[0m \u001b[0m\n", |
| 73 | + "\n", |
| 74 | + " and can be converted to: \n", |
| 75 | + "\tA rotation matrix \n", |
| 76 | + "[[ 6.123234e-17 -1.000000e+00]\n", |
| 77 | + " [ 1.000000e+00 6.123234e-17]]\n", |
| 78 | + " that has size = (2, 2).\n", |
| 79 | + "\tA position vector \n", |
| 80 | + "[1. 2.]\n", |
| 81 | + " that has size = (2,).\n", |
| 82 | + "\tA homogenous transformation matrix \n", |
| 83 | + "[[ 6.123234e-17 -1.000000e+00 1.000000e+00]\n", |
| 84 | + " [ 1.000000e+00 6.123234e-17 2.000000e+00]\n", |
| 85 | + " [ 0.000000e+00 0.000000e+00 1.000000e+00]]\n", |
| 86 | + " that has size = (3, 3).\n" |
| 87 | + ] |
| 88 | + } |
| 89 | + ], |
| 90 | + "source": [ |
| 91 | + "import numpy as np\n", |
| 92 | + "import spatialmath as sm\n", |
| 93 | + "from math import pi\n", |
| 94 | + "\n", |
| 95 | + "# SE(2)\n", |
| 96 | + "HA = sm.SE2(1,2,pi/2)\n", |
| 97 | + "print(HA)\n", |
| 98 | + "\n", |
| 99 | + "# Useful conversions\n", |
| 100 | + "# Rotation\n", |
| 101 | + "RA_np = HA.R\n", |
| 102 | + "# Position\n", |
| 103 | + "pA_np = HA.t\n", |
| 104 | + "# (full) Transformation\n", |
| 105 | + "HA_np = HA.A\n", |
| 106 | + "\n", |
| 107 | + "print(f\"The original SE(2) object was \\n{HA}\\n and can be converted to: \")\n", |
| 108 | + "print(f\"\\tA rotation matrix \\n{RA_np}\\n that has size = {RA_np.shape}.\")\n", |
| 109 | + "print(f\"\\tA position vector \\n{pA_np}\\n that has size = {pA_np.shape}.\")\n", |
| 110 | + "print(f\"\\tA homogenous transformation matrix \\n{HA_np}\\n that has size = {HA_np.shape}.\")\n", |
| 111 | + "\n" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 118 | + "### SE(3)" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": 39, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [ |
| 126 | + { |
| 127 | + "name": "stdout", |
| 128 | + "output_type": "stream", |
| 129 | + "text": [ |
| 130 | + " \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m-1 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;4m 1 \u001b[0m \u001b[0m\n", |
| 131 | + " \u001b[38;5;1m 1 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;4m 2 \u001b[0m \u001b[0m\n", |
| 132 | + " \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m 1 \u001b[0m \u001b[38;5;4m 3 \u001b[0m \u001b[0m\n", |
| 133 | + " \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 1 \u001b[0m \u001b[0m\n", |
| 134 | + "\n", |
| 135 | + "The original SE(2) object was \n", |
| 136 | + " \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m-1 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;4m 1 \u001b[0m \u001b[0m\n", |
| 137 | + " \u001b[38;5;1m 1 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;4m 2 \u001b[0m \u001b[0m\n", |
| 138 | + " \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m 0 \u001b[0m \u001b[38;5;1m 1 \u001b[0m \u001b[38;5;4m 3 \u001b[0m \u001b[0m\n", |
| 139 | + " \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 0 \u001b[0m \u001b[38;5;244m 1 \u001b[0m \u001b[0m\n", |
| 140 | + "\n", |
| 141 | + " and can be converted to: \n", |
| 142 | + "\tA rotation matrix \n", |
| 143 | + "[[ 6.123234e-17 -1.000000e+00 0.000000e+00]\n", |
| 144 | + " [ 1.000000e+00 6.123234e-17 0.000000e+00]\n", |
| 145 | + " [ 0.000000e+00 0.000000e+00 1.000000e+00]]\n", |
| 146 | + " that has size = (3, 3).\n", |
| 147 | + "\tA position vector \n", |
| 148 | + "[1. 2. 3.]\n", |
| 149 | + " that has size = (3,).\n", |
| 150 | + "\tA homogenous transformation matrix \n", |
| 151 | + "[[ 6.123234e-17 -1.000000e+00 0.000000e+00 1.000000e+00]\n", |
| 152 | + " [ 1.000000e+00 6.123234e-17 0.000000e+00 2.000000e+00]\n", |
| 153 | + " [ 0.000000e+00 0.000000e+00 1.000000e+00 3.000000e+00]\n", |
| 154 | + " [ 0.000000e+00 0.000000e+00 0.000000e+00 1.000000e+00]]\n", |
| 155 | + " that has size = (4, 4).\n" |
| 156 | + ] |
| 157 | + } |
| 158 | + ], |
| 159 | + "source": [ |
| 160 | + "import numpy as np\n", |
| 161 | + "import spatialmath as sm\n", |
| 162 | + "from math import pi\n", |
| 163 | + "\n", |
| 164 | + "# SE(3)\n", |
| 165 | + "HB = sm.SE3(1,2,3) * sm.SE3().Rz(pi/2)\n", |
| 166 | + "print(HB)\n", |
| 167 | + "\n", |
| 168 | + "# Useful conversions\n", |
| 169 | + "# Rotation\n", |
| 170 | + "RB_np = HB.R\n", |
| 171 | + "# Position\n", |
| 172 | + "pB_np = HB.t\n", |
| 173 | + "# (full) Transformation\n", |
| 174 | + "HB_np = HB.A\n", |
| 175 | + "\n", |
| 176 | + "print(f\"The original SE(2) object was \\n{HB}\\n and can be converted to: \")\n", |
| 177 | + "print(f\"\\tA rotation matrix \\n{RB_np}\\n that has size = {RB_np.shape}.\")\n", |
| 178 | + "print(f\"\\tA position vector \\n{pB_np}\\n that has size = {pB_np.shape}.\")\n", |
| 179 | + "print(f\"\\tA homogenous transformation matrix \\n{HB_np}\\n that has size = {HB_np.shape}.\")" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "markdown", |
| 184 | + "metadata": {}, |
| 185 | + "source": [ |
| 186 | + "## Comparing `numpy` matrices\n", |
| 187 | + "\n", |
| 188 | + "Owing to floating point limitations of computers, use, in general, `numpy.allclose`." |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "code", |
| 193 | + "execution_count": 40, |
| 194 | + "metadata": {}, |
| 195 | + "outputs": [ |
| 196 | + { |
| 197 | + "name": "stdout", |
| 198 | + "output_type": "stream", |
| 199 | + "text": [ |
| 200 | + "True\n" |
| 201 | + ] |
| 202 | + } |
| 203 | + ], |
| 204 | + "source": [ |
| 205 | + "import numpy as np\n", |
| 206 | + "import spatialmath as sm\n", |
| 207 | + "from math import pi, sin, cos\n", |
| 208 | + "\n", |
| 209 | + "# SE(2) with spatialmath\n", |
| 210 | + "HC = sm.SE2(1,2,pi/2)\n", |
| 211 | + "\n", |
| 212 | + "# SE(2) directly from numpy\n", |
| 213 | + "HC_np = np.array(\n", |
| 214 | + " [[cos(pi/2), -sin(pi/2), 1],\n", |
| 215 | + " [sin(pi/2), cos(pi/2), 2],\n", |
| 216 | + " [0, 0, 1]]\n", |
| 217 | + ")\n", |
| 218 | + "\n", |
| 219 | + "# Compare HC.A and HC_np\n", |
| 220 | + "print(np.allclose(HC.A, HC_np))" |
| 221 | + ] |
| 222 | + } |
| 223 | + ], |
| 224 | + "metadata": { |
| 225 | + "kernelspec": { |
| 226 | + "display_name": "Python 3", |
| 227 | + "language": "python", |
| 228 | + "name": "python3" |
| 229 | + }, |
| 230 | + "language_info": { |
| 231 | + "codemirror_mode": { |
| 232 | + "name": "ipython", |
| 233 | + "version": 3 |
| 234 | + }, |
| 235 | + "file_extension": ".py", |
| 236 | + "mimetype": "text/x-python", |
| 237 | + "name": "python", |
| 238 | + "nbconvert_exporter": "python", |
| 239 | + "pygments_lexer": "ipython3", |
| 240 | + "version": "3.12.9" |
| 241 | + } |
| 242 | + }, |
| 243 | + "nbformat": 4, |
| 244 | + "nbformat_minor": 2 |
| 245 | +} |
0 commit comments