{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1a6262b92f424c8ab4bd12827396ad3d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Playback(children=(HBox(children=(Button(description='Play', icon='play', style=ButtonStyle(), tooltip='Start …" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8dcda618ac964d159f61c1635de4f8d9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "KlamptWidgetAdaptor(rpc={'type': 'multiple', 'calls': [{'y': 5, 'type': 'add_text', 'x': 5, 'name': 'HUD1', 't…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#%load_ext wurlitzer\n", "#^^^ used to capture C/C++ output to be displayed in the notebook. Does not work on Windows.\n", "\n", "#Python 2/3 compatibility\n", "from __future__ import print_function,division\n", "\n", "import time\n", "from klampt import *\n", "from IPython.display import clear_output\n", "from klampt import vis\n", "from klampt.vis.ipython import Playback\n", "\n", "world = WorldModel()\n", "if world.loadFile(\"../data/tx90scenario0.xml\"):\n", " sim = Simulator(world)\n", " \n", " #If you'd like to show the print output, comment out this line\n", " clear_output()\n", " \n", " vis.add(\"world\",world)\n", " vis.addText(\"HUD1\",\"0\",position=(5,5))\n", " playback_widget = Playback(vis.scene())\n", " \n", " display(playback_widget)\n", " vis.show()\n", "else:\n", " print(\"There was a problem loading the world file\")\n", "\n", "#Controls:\n", "#left mouse click to rotate the view\n", "#right click or ctrl+click to pan the view\n", "#mouse wheel or shift+click to zoom the view" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#define your visualization and controller code here\n", "import random\n", "\n", "next_move_time = 2.0\n", "move_delay = 5.0\n", "robot = world.robot(0) \n", " \n", "def control_loop(t,controller):\n", " global next_move_time\n", " vis.addText(\"HUD1\",\"%.2f\"%(t,),position=(5,5))\n", " if t >= next_move_time:\n", " qmin,qmax = robot.getJointLimits()\n", " q = [random.uniform(a,b) for (a,b) in zip(qmin,qmax)]\n", " controller.setMilestone(q)\n", " next_move_time += move_delay\n", " pass\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#this code runs the simulation and binds your code to the playback buttons\n", "dt = 0.02\n", "\n", "\n", "def do_reset():\n", " sim.reset()\n", " sim.updateWorld()\n", " global next_move_time\n", " next_move_time = 2.0\n", " vis.addText(\"HUD1\",\"0\",position=(5,5))\n", "\n", "def do_advance():\n", " global world,sim\n", " if world.numRobots() > 0:\n", " control_loop(sim.getTime(),sim.controller(0)) #call code in the above cell\n", " #print(\"Simulating...\",dt)\n", " sim.simulate(dt)\n", " sim.updateWorld()\n", " #print(\"Done.\")\n", "\n", "#this binds the playback widget buttons\n", "playback_widget.advance = do_advance\n", "playback_widget.reset = do_reset\n", "playback_widget.quiet = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.2" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }