{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Record, Save, and Play Moves on a Poppy Creature" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**This notebook is still work in progress! [Feedbacks are welcomed](https://github.com/poppy-project/pypot/labels/Notebooks)!**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this tutorial we will show how to:\n", "* record moves by direct demonstration on a Poppy Creature\n", "* save them to the disk - and re-load them\n", "* play, and re-play the best moves" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To follow this notebook, you should already have installed everything needed to control a Poppy Creature. The examples below used a Poppy Ergo but then can be easily transposed to a Poppy Humanoid or to any other creatures." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Connect to your Poppy Creature" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, connect to your Poppy Creature and put it in its \"base\" position so you can easily record motions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we use a Poppy Ergo but you can replace it by a Poppy Humanoid." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from pypot.creatures import PoppyErgo\n", "\n", "poppy = PoppyErgo()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for m in poppy.motors:\n", " m.compliant = False\n", " m.goal_position = 0.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import the Move, Recorder and Player" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Import everything you need for recording, playing, saving, and loading Moves\n", "\n", "# Move: object used to represent a movement\n", "# MoveRecorder: object used to record a Move\n", "# MovePlayer: object used to play (and re-play) a Move\n", "\n", "from pypot.primitive.move import Move, MoveRecorder, MovePlayer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a Recorder for the robot Poppy" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "record_frequency = 50.0 # This means that a new position will be recorded 50 times per second.\n", "recorded_motors = [poppy.m4, poppy.m5, poppy.m6] # We will record the position of the 3 last motors of the Ergo\n", "\n", "# You can also use alias for the recorded_motors\n", "# e.g. recorder = MoveRecorder(poppy, record_frequency, poppy.tip)\n", "# or even to record all motors position\n", "# recorder = MoveRecorder(poppy, record_frequency, poppy.motors)\n", "\n", "recorder = MoveRecorder(poppy, record_frequency, recorded_motors)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Start the recording" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, turn the recorded motors compliant, so you can freely move them:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for m in recorded_motors:\n", " m.compliant = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Starts the recording when you are ready!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "recorder.start()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stop the recording" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stop it when you are done demonstrating the movement." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "recorder.stop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Turn back off the compliance." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for m in recorded_motors:\n", " m.compliant = False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get the recorder Move and store it on the disk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the recorded move on the text file named 'mymove.json'." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "recorded_move = recorder.move\n", "\n", "with open('mymove.json', 'w') as f:\n", " recorded_move.save(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load a saved Move" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Re-load it from the file jsut as an example purpose." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open('mymove.json') as f:\n", " loaded_move = Move.load(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a Move Player and Play Back a Recorded Move" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, create the object used to re-play a recorded Move." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "player = MovePlayer(poppy, loaded_move)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can start the play back whenever you want:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "player.start()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can play your move as many times as you want. Note, that we use the *wait_to_stop* method to wait for the first play abck to end before running it again." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for _ in range(3):\n", " player.start()\n", " player.wait_to_stop()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }