{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# QuickStart: Playing with a Poppy Ergo (or a PoppyErgoJr)"
   ]
  },
  {
   "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 get started with your [PoppyErgo](https://www.poppy-project.org/project/mathematics-a-beautiful-elsewhere/) creature. You can use a [PoppyErgoJr](https://github.com/poppy-project/poppy-ergo-jr) instead."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/poppy-project/poppy-ergo-jr/master/doc/img/poppy-ergo-jr.jpg\" alt=\"Poppy Ergo Jr\" style=\"height: 500px;\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To run the code in this notebook, you will need:\n",
    "* a poppy ergo creature (or a Jr)\n",
    "* the [pypot](https://github.com/poppy-project/pypot) library\n",
    "* the [poppy-ergo](https://github.com/pierre-rouanet/poppy-ergo) library (or use the [poppy-ergo-jr](https://github.com/poppy-project/poppy-ergo-jr) library instead)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can install those libraries with the pip tool (see [here]() if you don't know how to run this):\n",
    "```bash\n",
    "\n",
    "pip install pypot poppy-ergo\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to your robot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a PoppyErgo:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pypot.creatures import PoppyErgo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ergo = PoppyErgo()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a PoppyErgoJr:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pypot.creatures import PoppyErgoJr\n",
    "\n",
    "ergo = PoppyErgoJr()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get robot current status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Robot motors=[<DxlMotor name=m1 id=11 pos=-1.69>, <DxlMotor name=m2 id=12 pos=-97.13>, <DxlMotor name=m3 id=13 pos=89.96>, <DxlMotor name=m4 id=14 pos=0.65>, <DxlMotor name=m5 id=15 pos=41.5>, <DxlMotor name=m6 id=16 pos=22.14>]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ergo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<DxlMotor name=m2 id=12 pos=-97.43>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ergo.m2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-97.13"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ergo.m2.present_position"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ergo.m2.present_temperature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Motor \"m1\" current position = -1.69\n",
      "Motor \"m2\" current position = -97.13\n",
      "Motor \"m3\" current position = 89.96\n",
      "Motor \"m4\" current position = 0.65\n",
      "Motor \"m5\" current position = 41.5\n",
      "Motor \"m6\" current position = 22.14\n"
     ]
    }
   ],
   "source": [
    "for m in ergo.motors:\n",
    "    print 'Motor \"{}\" current position = {}'.format(m.name, m.present_position)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Turn on/off the compliancy of a motor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ergo.m3.compliant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ergo.m6.compliant = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Go to the zero position"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ergo.m6.goal_position = 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for m in ergo.motors:\n",
    "    m.compliant = False\n",
    "    \n",
    "    # Goes to the position 0 in 2s\n",
    "    m.goto_position(0, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# You can also change the maximum speed of the motors\n",
    "# Warning! Goto position also change the maximum speed.\n",
    "\n",
    "for m in ergo.motors:\n",
    "    m.moving_speed = 50"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Make a simple dance movement"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On a single motor:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "ergo.m4.goal_position = 30\n",
    "time.sleep(1.)\n",
    "ergo.m4.goal_position = -30"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On multiple motors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ergo.m4.goal_position = 30\n",
    "ergo.m5.goal_position = 20\n",
    "ergo.m6.goal_position = -20\n",
    "\n",
    "time.sleep(1.)\n",
    "\n",
    "ergo.m4.goal_position = -30\n",
    "ergo.m5.goal_position = -20\n",
    "ergo.m6.goal_position = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wrap it inside a function for convenience:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def dance():\n",
    "    ergo.m4.goal_position = 30\n",
    "    ergo.m5.goal_position = 20\n",
    "    ergo.m6.goal_position = -20\n",
    "\n",
    "    time.sleep(1.)\n",
    "\n",
    "    ergo.m4.goal_position = -30\n",
    "    ergo.m5.goal_position = -20\n",
    "    ergo.m6.goal_position = 20\n",
    "    \n",
    "    time.sleep(1.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dance()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for _ in range(4):\n",
    "    dance()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using goto position instead:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def dance2():\n",
    "    ergo.goto_position({'m4': 30, 'm5': 20, 'm6': -20}, 1., wait=True)\n",
    "    ergo.goto_position({'m4': -30, 'm5': -20, 'm6': 20}, 1., wait=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for _ in range(4):\n",
    "    dance2()"
   ]
  }
 ],
 "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
}
