Tutorials/CloudSim/IntroductionToiPyNotebook


 * 1) iPython Notebook

This notebook web app is part of the iPython project. You can use it to: More details can be found here: iPython Notebook.
 * Edit and run interactive python programs.
 * Run shell commands without having to open a Terminal window.


 * 1) Launching the iPython Notebook

Before we can launch the iPython Notebook, we need to create and start a task.

Note: If you're still running a task (the blue light next to the task would be solid or blinking blue), then you need to first stop that task by clicking the `Stop current task...` button.

Similar to the  Introduction to gzweb tutorial, create a task in the CloudSim console with the following parameters:


 * Task title : `Atlas world`
 * ROS package : `drcsim_gazebo`
 * Launch file : `atlas_sandia_hands.launch`

Launch the task you created and start the `WebGL interface` "3d view" when it's ready. Open the `Python notebook` link in a new tab and you should be able to see a page like the one below:



This is the iPython Notebook Dashboard. Let's create a new notebook by clicking on `New Notebook`, and a new tab should open with a few menu items at the top and an empty `cell` in the body. Cells are where you type commands and run python code.




 * 1) Shell Commands

As mentioned earlier, you can run shell commands within the iPython Notebook. There are a few built-in commands, for example `ls`, `cd`, `pwd`, etc. Try typing these commands into a cell, followed by a
 * `CTRL` + `ENTER` to execute the current cell in place, or
 * `SHIFT` + `ENTER` to execute the current cell and jump to the next one.

To execute other system commands, simply prefix the command with `!`. For example, type in a cell the following command to get a list of ROS topics.

!rostopic list

You should see a list similar to below:



The iPython quick reference sheet provides useful shortcuts and tips. Or, you can enter `%quickref` in the iPython notebook cell.


 * 1) Python Scripts

Python code can be directly entered into a cell in the iPython Notebook and executed in the same way by pressing `SHIFT` + `ENTER`. This part of the tutorial will demonstrate how to write a python program that sends commands to Atlas in the simulation which we started earlier. To get started, we need to load a couple of python modules:
 * `rospy` (ROS Python) module ,
 * `atlas_msgs` module for high level Atlas commands

The code below does the initialization. Copy and paste it into an empty cell and run it:

print "load Atlas library" import rospy from atlas_msgs.msg import WalkDemoAction from std_msgs.msg import String

Then set up a ROS node and publishers for sending messages to Atlas. Copy the python script below to a cell and run it.

print "init Atlas library" rospy.init_node('Atlas', anonymous=True)

control_mode = rospy.Publisher('/atlas/control_mode', String, None, False, True, None) mode = rospy.Publisher('/atlas/mode', String, None, False, True, None)

rospy.sleep(5.0) # connection print "good to go"

In the above cell, we create two publishers. The `control_mode` publisher provides a channel to send high level commands to the robot while `mode` allows you to send development aid commands.

Before we send the actual walking commands, we need to set Atlas to a standing mode. This can also be used to reset the robot to the standing pose after a fall. Launch the `WebGL interface` if you haven't already done so, then execute the code below in a cell to see Atlas rests its arms down in the `WebGL interface`.

print "Set the robot to a standing pose"

mode.publish("harnessed") control_mode.publish("Freeze") control_mode.publish("StandPrep") rospy.sleep(2.0) mode.publish("nominal") rospy.sleep(0.3) control_mode.publish("Stand")

Finally, send the walking command below and see Atlas walk.

print "Make the robot walk for 5 steps" control_mode.publish("Walk")

Another method to make Atlas walk is by sending ROS terminal commands within a cell. !rostopic pub --once /atlas/control_mode std_msgs/String -- "Walk"
 * 1) make the robot walk.



Stopping the iPython Notebook Kernel ===

A long running python command can produce a `kernel busy` message in the upper right corner of the notebook and prevent you from executing scripts from other cells. If that happens, interrupt the current cell by pressing the `Interrupt` icon below the menu bar or under `Kernel`->`Interrupt` to interrupt the computation.

Another handy tool is the kernel restart option that allows you to reset your notebook session. Select `Kernel` from the menu and choose `Restart`.

Notebook: ROS camcorder
 * 1) Next ##