|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "7259a8b6", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import rclpy as rp\n", |
| 11 | + "import jupyros.ros2 as jr2\n", |
| 12 | + "import jupyros.ros2.turtle_sim as turtle\n", |
| 13 | + "from turtlesim.srv import Spawn\n", |
| 14 | + "from turtlesim.msg import Pose\n", |
| 15 | + "import os\n", |
| 16 | + "from std_msgs.msg import String\n", |
| 17 | + "from geometry_msgs.msg import Twist\n", |
| 18 | + "from sidecar import Sidecar\n", |
| 19 | + "from time import time, sleep\n", |
| 20 | + "import math\n", |
| 21 | + "\n" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "id": "1964ecfc-67ee-47bf-aad3-824315c4418d", |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [], |
| 30 | + "source": [ |
| 31 | + "# Initialize ROS communications for a given context\n", |
| 32 | + "if(rp.ok() == False):\n", |
| 33 | + " rp.init()\n", |
| 34 | + "else:\n", |
| 35 | + " print(\"rclpy already initiated\")" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": null, |
| 41 | + "id": "47354d6d-8c92-47ce-9f85-c0c1e403d8bf", |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "superturtle = rp.create_node(\"superturtle\")\n", |
| 46 | + "moveNode = rp.create_node(\"moveNode\")" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": null, |
| 52 | + "id": "2dbf024d", |
| 53 | + "metadata": { |
| 54 | + "tags": [] |
| 55 | + }, |
| 56 | + "outputs": [], |
| 57 | + "source": [ |
| 58 | + "turtlesim = turtle.TurtleSim(background_color=\"#0000FF\")" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": null, |
| 64 | + "id": "2bfe401a-2a01-4f75-839a-411b221bac8e", |
| 65 | + "metadata": { |
| 66 | + "tags": [] |
| 67 | + }, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "display(turtlesim.canvas)" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "id": "ece1ece3-54f6-4df8-be79-42d7f37f6e08", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "**TIP:** When using JupyterLab, you can right-click on the canvas and select *Create New View from Output*" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": null, |
| 84 | + "id": "497db1e0-8c21-4ec0-b620-1607ab34d685", |
| 85 | + "metadata": { |
| 86 | + "tags": [] |
| 87 | + }, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "poses = {}\n", |
| 91 | + "\n", |
| 92 | + "for name in turtlesim.turtles.keys():\n", |
| 93 | + " poses[name] = turtlesim.turtles[name].pose\n", |
| 94 | + " \n", |
| 95 | + "print(poses[\"turtle1\"])" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "id": "ac63dbbb-b388-4b18-890c-e3bcada044a9", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "id": "cd2e66dc", |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [], |
| 112 | + "source": [ |
| 113 | + "topic_name = '/Pose'\n", |
| 114 | + "def move_turtles(msg):\n", |
| 115 | + " scale = 0.0015\n", |
| 116 | + " name = \"turtle1\"\n", |
| 117 | + " \n", |
| 118 | + " def angle_slope():\n", |
| 119 | + " d_y = (math.sin(msg.theta+1/180)*math.cos(msg.theta+1/180)-math.sin(msg.theta)*math.cos(msg.theta))\n", |
| 120 | + " d_x = (math.cos(msg.theta+1/180) - math.cos(msg.theta))\n", |
| 121 | + " return math.atan2(d_y,d_x)\n", |
| 122 | + " \n", |
| 123 | + " poses[name] = {\"x\": 1/scale * math.cos(msg.theta) + 800,\n", |
| 124 | + " \"y\": -1/scale * math.sin(msg.theta)*math.cos(msg.theta) + 800,\n", |
| 125 | + " \"theta\": angle_slope()}\n", |
| 126 | + " ##msg.theta - math.atan2((-1/scale * math.sin(msg.theta)*math.cos(msg.theta) + 800),(1/scale * math.cos(msg.theta) + 800))\n", |
| 127 | + " \n", |
| 128 | + " turtlesim.move_turtles(new_poses=poses)\n", |
| 129 | + "\n", |
| 130 | + "\n", |
| 131 | + "\n", |
| 132 | + "\n", |
| 133 | + "def cb(msg):\n", |
| 134 | + " move_turtles(msg)\n", |
| 135 | + "\n", |
| 136 | + "\n", |
| 137 | + "turtle_control = jr2.Subscriber(moveNode, Pose, topic_name, cb)\n", |
| 138 | + "turtle_control.display()" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": null, |
| 144 | + "id": "c9e44409-1f96-426f-9826-95f2ddff5119", |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "%%thread_cell2\n", |
| 149 | + "run = True\n", |
| 150 | + "i = 90\n", |
| 151 | + "pub = jr2.Publisher(moveNode, Pose, topic_name)\n", |
| 152 | + "\n", |
| 153 | + "while run:\n", |
| 154 | + " msg = Pose()\n", |
| 155 | + " msg.theta = i / 180 * math.pi\n", |
| 156 | + " pub.send_msg( msg)\n", |
| 157 | + " sleep(0.01)\n", |
| 158 | + " i += 1\n", |
| 159 | + "print(\"Done\")" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "code", |
| 164 | + "execution_count": null, |
| 165 | + "id": "0fd093f5-122e-4006-8ad8-813428356fbe", |
| 166 | + "metadata": {}, |
| 167 | + "outputs": [], |
| 168 | + "source": [] |
| 169 | + } |
| 170 | + ], |
| 171 | + "metadata": { |
| 172 | + "kernelspec": { |
| 173 | + "display_name": "Python 3 (ipykernel)", |
| 174 | + "language": "python", |
| 175 | + "name": "python3" |
| 176 | + }, |
| 177 | + "language_info": { |
| 178 | + "codemirror_mode": { |
| 179 | + "name": "ipython", |
| 180 | + "version": 3 |
| 181 | + }, |
| 182 | + "file_extension": ".py", |
| 183 | + "mimetype": "text/x-python", |
| 184 | + "name": "python", |
| 185 | + "nbconvert_exporter": "python", |
| 186 | + "pygments_lexer": "ipython3", |
| 187 | + "version": "3.8.10" |
| 188 | + } |
| 189 | + }, |
| 190 | + "nbformat": 4, |
| 191 | + "nbformat_minor": 5 |
| 192 | +} |
0 commit comments