TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to...

30

Transcript of TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to...

Page 1: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

TDDD63 Project: Humanoids

Version 1.0

Jon Dybeck (jondy276)Carl Ehrenstråhle (careh585)

Erik Hansson (eriha172)Fredrik Heintz ([email protected])

October 4, 2013

Page 2: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

Contents

1 Project overview 3

1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3 The Nao robot . . . . . . . . . . . . . . . . . . . . . . . . . . 61.4 Overview of RosBots . . . . . . . . . . . . . . . . . . . . . . . 71.5 Handling Nao and guidelines for the lab . . . . . . . . . . . . 7

2 Con�guration and installation 10

2.1 Con�guring the tools . . . . . . . . . . . . . . . . . . . . . . . 102.2 Installing RosBots . . . . . . . . . . . . . . . . . . . . . . . . 112.3 Updating RosBots . . . . . . . . . . . . . . . . . . . . . . . . 11

3 Assignments 13

3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.1.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . 143.1.2 Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.1.3 Examination . . . . . . . . . . . . . . . . . . . . . . . . 143.1.4 Milestone(s) . . . . . . . . . . . . . . . . . . . . . . . . 14

3.2 Intro Phase: Assignment One � Hello world! . . . . . . . . . . 143.2.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . 153.2.2 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.2.3 Examination . . . . . . . . . . . . . . . . . . . . . . . . 153.2.4 Milestone One � Stand and sit in simulation . . . . . . 153.2.5 Milestone Two � Hello world on robot . . . . . . . . . . 183.2.6 Milestone three � Wait for touch . . . . . . . . . . . . 20

3.3 Intro Phase: Assignment Two � Basic skills . . . . . . . . . . . 233.3.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . 233.3.2 Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.3.3 Examination . . . . . . . . . . . . . . . . . . . . . . . . 233.3.4 Milestone One � Walking . . . . . . . . . . . . . . . . . 233.3.5 Milestone Two � Turning the head . . . . . . . . . . . 24

1

Page 3: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

3.3.6 Milestone Three � Using the camera . . . . . . . . . . 243.3.7 Milestone Four � Finding and tracking the ball . . . . . 253.3.8 Milestone Five � Following the ball . . . . . . . . . . . 25

3.4 Main Phase: Assignment Three � Playing soccer . . . . . . . . 253.4.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . 253.4.2 Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253.4.3 Examination . . . . . . . . . . . . . . . . . . . . . . . . 253.4.4 Milestone One � Kicking the ball . . . . . . . . . . . . 263.4.5 Milestone Two � Finding the goal . . . . . . . . . . . . 263.4.6 Milestone Three � Aligning the robot . . . . . . . . . . 263.4.7 Milestone Four � Scoring a goal . . . . . . . . . . . . . 26

3.5 Main Phase: Assignment Four � Co-operation . . . . . . . . . 263.5.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . 263.5.2 Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273.5.3 Examination . . . . . . . . . . . . . . . . . . . . . . . . 273.5.4 Milestone One � Send messages . . . . . . . . . . . . . 273.5.5 Milestone Two � Wait for clearance . . . . . . . . . . . 273.5.6 Milestone Three � Pass the ball . . . . . . . . . . . . . 283.5.7 Milestone Four (optional) � Cross pass . . . . . . . . . 28

3.6 Main Phase: Assignment Five � Elective task . . . . . . . . . 283.6.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . 283.6.2 Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283.6.3 Examination . . . . . . . . . . . . . . . . . . . . . . . . 29

2

Page 4: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

Chapter 1

Project overview

1.1 Introduction

The goal of this project is to get hands on experience with programmingreal humanoid robots. Your particular task is to program the Nao humanoidrobot to play soccer.

To achieve this you have to implement both relatively simple behaviors,such as �nding and kicking the ball, and more complex behaviors, such asscoring a goal. All the programming is done in Python.

Programming a humanoid robot to play soccer from scratch is a huge task.Therefore, you will work with a framework called RosBots, which providesa high level Python interface to the Nao. Moreover, you will get detailedinstructions on how to get started and more or less detailed descriptions ofthe most important behaviors required to make a robot play soccer. However,you still have to put in a lot of time and e�ort into the project for it to besuccessful.

The examination of the project is to complete all the assignments de-scribed in this project description on time. This includes writing a shortreport describing your �nal assignment, where you decide what you want todo.

3

Page 5: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

1.2 Background

Figure 1.1: The goalkeeper of team Bembelbots (top left), picture fromRoboCup o�cial webpage (top right) and screenshot from the simulationleague of RoboCup (bottom).

The history of robots playing soccer dates back to 1992. At that time it wasmainly a subject of some papers, workshops on challenges in arti�cial intelli-gence and discussions. However, this led to the creation of RoboCup, whichis an international robot soccer competition held every year starting in 1997in Japan. Sony CSL has more information about the history of RoboCupand some of the original papers if you are interested in reading more (http://www.sonycsl.co.jp/person/kitano/RoboCup/RoboCup-old.html).

RoboCup has many di�erent leagues, such as the simulation league, thesmall size league, the standard platform league, and the kid size humanoid

4

Page 6: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

league. In the simulation league teams of 11 simulated robots and 1 simulatedcoach compete against each other, while in the other leagues teams consistof real robots.

The Nao humanoid robot is currently the robot used in the standardplatform league (SPL). This means that all the teams use the same hardware,the di�erence is the software. In orange street hockey ball. A game is 2 ×10 minutes. For more information see the SPL home page (http://www.tzi.de/spl) and the SPL rules (http://www.tzi.de/spl/pub/Website/Downloads/Rules2013.pdf).

As the interest for RoboCup and robots in general has increased, moreand more types of competitions have been introduced such as rescue anddance.

The main goal of RoboCup is to beat the FIFA (human) world championsin soccer by 2050. Of course, this has to be done on human terms, whichmeans that the robots are limited to humanoids that may not harm peo-ple and with human-like sensors. To achieve this, many breakthroughs arerequired. For example, the robots need signi�cantly better hardware thatmakes them faster and more robust while still being safe to play against forhumans. They also need much better software to control the actuators andsensors on the robots, implement behaviors such as dribbling like Maradonnaor Messi and making the right decisions at the right time.

More information about RoboCup can be found at their website: http:

//www.robocup.org/.

5

Page 7: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

1.3 The Nao robot

Figure 1.2: The Nao humanoid robot with its sensors and actuators.

In this project you work with a robot called Nao, developed by theFrench company Aldebaran Robotics (http://www.aldebaran-robotics.com/en/). It is a humanoid robot with 25 degrees of freedom (DOF). DOFis a measurement of independent motions, for example a robot with one jointthat can turn in one direction has one DOF. For the Nao, the 25 DOF are inthe form of 25 motors and actuators. In addition, it has several sensors: Twocameras, four microphones, a sonar range �nder, two infrared (IR) emit-ters and receivers, one inertial measurement unit and nine tactile sensors.Furthermore, it is �tted with eight high-�delity speakers and several LEDs.

The Nao is equipped with an Intel ATOM 1.6 GHz CPU that runs a

6

Page 8: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

Linux kernel and controls the mechanical hardware. It also has a middlewarecalled NAOqi developed by Aldebaran Robotics. NAOqi provides a lot ofprede�ned functionality. It also possible for users to add their own modulesto NAOqi. However, one drawback is that the prede�ned functions are notalways optimized. Therefore, most teams in the SPL uses their own code forhigher level functionality such as walk engines, motions and image processing.However, this new functionality still uses the NAOqi to control each joint andto fetch data from the sensors.

The technical documentation for the Nao can be downloaded from Alde-barans website (http://www.aldebaran-robotics.com/Downloads/Download-document/192-Datasheet-NAO-Humanoid.html).

1.4 Overview of RosBots

RosBots is a software system developed at Linköpings University. It consistsof a �nite state machine (hereafter referred to as FSM) model, a functionalwrapper around NAOqi, and a ROS layer which provides communicationbetween di�erent parts of the system, called nodes in ROS. Some parts ofthe ROS layer is open source code provided by the University of Freiburg.Furthermore, RosBots also has a communication layer between the FSMmodule and the ROS layer.

In this project you only have to work with the FSM model. You are al-lowed to work with the other layers later in the project as well, but youmust

consult an assistant or supervisor before testing the code on the robts. Fur-thermore, we encourage you to share good modi�cations that you do by send-ing a pull request to the RosBots bitbucket repository (https://bitbucket.org/jondy276/rosbots). See http://githup-scm.com for more informa-tion about how to send one.

You can read more about RosBots in the documentation (RosBots - Doc-umentation). You are also welcome to ask an assistant if you have anyquestions regarding the project.

1.5 Handling Nao and guidelines for the lab

There are, to avoid any unnecessary damages to the robot, rules for how tohandle the Nao humanoid robot:

• You should under no circumstances run any code on the robot unlesssomeone is on the �eld and is prepared to catch the robot if somethingis about to happen to it.

7

Page 9: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

• When you are lifting the robot you should have a �rm grip around itswaist with both your hands (see the picture below).

• Always test new behaviors in the simulator before you try it on the realrobot.

• Never run experimental code that is not using the RosBots systemwithout consulting an assistant or supervisor in advance.

• Take a new robot if one is available and yours has run out of battery.In the case that all other robots are taken you can change batteries toa reserve battery by following the instructions at the table.

• Do not leave the robots around on the �oor in the case of someonefalling.

• Contact an assistant or supervisor immediately and mark the robot ifsomething has broken on the robot.

Figure 1.3: How to hold the Nao robot (left), Nao in sitting position (right)

In addition to the rules for how to handle the Nao humanoid robot there area few guidelines for the lab:

• You should never wear shoes when you are on the �eld. We want tokeep it as clean as possible!

8

Page 10: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

• The computers in the lab is under the same rules as for the rest of thecomputers at the university. This means that it is, for example, notallowed to play games on them.

• Share the robots between each other in a fair way since there is a limitednumber of robots available.

9

Page 11: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

Chapter 2

Con�guration and installation

2.1 Con�guring the tools

Before you can start on the �rst assignment you have to con�gure youraccount on the workstation in the lab. All the workstations share the sameaccounts so it does not matter which of them you use.

After you have logged in to one of the workstations we strongly recommendthat you con�gure the tools you will use. You are free to use all the programsthat are installed and we will not require that you use a certain con�guration.However, we strongly recommend that you use a con�guration that will helpyou to keep tab on the length of your lines in your code since you should followthe standard for Python, pep8, This includes a maximum of 80 charactersper line, see below for instructions on how to get this functionality in Emacs.Furthermore, we recommend that you con�gure the indentation, accordingto pep-8, to four spaces. This saves you the trouble of having di�erentindentation when writing code with others. Moreover, it will make sure thatyou will not get any problems in the case that one of the assistants decidesto test your code on their own accounts.

Put these three lines in the .emacs �le in your home folder to graphicallydisplay the lines that are longer than 80 character as well as show unnecessarywhite spaces and tab characters.

(require 'whitespace)

(setq whitespace-style '(face empty tabs lines-tail trailing ))

(global-whitespace-mode t)

Lastly, we strongly recommend you to use svn or a similar software to handleyour code. Partly because it will make it easier for you to work on the samecode, from di�erent computers and partly because it will be easier to sendin the code to the assistants. SVN is a version control and source code

10

Page 12: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

management tool. That means that it allows you to distribute your code toothers, merging �les that more than one person has modi�ed, reverting backto older versions of the code, etc.

2.2 Installing RosBots

RosBots is not installed on the workstations, unlike the rest of the programsthat you will use. Therefore, you have to install it manually. This is donein two steps: First, download the source code. Second, run the RosBotsinstallation script. The script sets up all the required system variables forthe programs that you use (e.g. Python and ROS) and compiles the RosBotscode.

To download the RosBots source code you have to use git since it is storein a git repository. Follow the following two steps to get the code. First,open a terminal and change directories to the folder where you want tostore the RosBots code (it will be stored in a newly created folder calledRosBots in your current directory). Second, run the command: git clone

https://bitbucket.org/jondy276/rosbots. You should now have the sourcecode stored in the folder of your choice.

The next step after downloading the source code to your account is toinstall RosBots. This is done by moving to the folder rosbots/scripts andexecuting the script rosbots_installation. The script sets up all the sys-tem variables and compiles the program. Finally, you have to source your.bashrc �le, alternatively restart the shell, before RosBots is ready for use.

2.3 Updating RosBots

It is higly likely that RosBots will be updated during the course. However,do not worry about having to check for updates. If an update is released youwill be noti�ed trough one of the assistants. However, you will have to installthe update on your own. This is done by opening a terminal and changingdirectories to the RosBots folder or any of its sub folders. Thereafter, run thecommand git pull, which will update your source code to the most recent.To compile the new source code run the command rosbots_update. Notethat this command only works if you have installed RosBots before. Whenthe command is �nished, RosBots will be updated.

Note that if you have modi�ed the RosBots source code you will have torun the update command as well. Furthermore, there is a small risk thatyou can encounter so called merge errors (see http://git-scm.com for moreinformation) when you fetch the latest update if you have modi�ed the �les

11

Page 13: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

that have been updated.

12

Page 14: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

Chapter 3

Assignments

3.1 Introduction

The project consist of two phases, the introductory phase and the main phase.The introductory phase consists of two assignments and should be �nishedby the end of the �rst period. The main phase consists of three assignmentsand should be �nished by the end of the course. This chapter describes allthe assignments. It is recommended that you do them in the order they arepresented since they in many cases build upon previous assignments. Thisallows you to reuse the code that you developed in previous assignments.Furthermore, we expect that you follow the time schedule below. It is de�nedto make sure that you will �nish the project on time.

Assignment Expected week for completion

Intro phase: Assignment 1 week 43Intro phase: Assignment 2 week 45Main phase: Assignment 3 week 47Main phase: Assignment 4 week 48Main phase: Assignment 5 week 50

Main phase: Technical report week 51

Table 3.1: Project time schedule

Assignments are presented in the same way as this introduction, i.e. an in-troduction and then the following subsections.

13

Page 15: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

3.1.1 Purpose

A short description of what you are supposed to learn from the assignmentand a very brief description on how it will be learned.

3.1.2 Task

A description on what you are supposed to do in the assignment.

3.1.3 Examination

A detailed description on what you have to do to pass the assignment.

3.1.4 Milestone(s)

For every milestone of an assignment there is a detailed description.

3.2 Intro Phase: Assignment One � Hello world!

This assignment gets you started programming the Nao humanoid robot.Since this is the �rst assignment, you get highly detailed descriptions on howto complete each milestone. However, keep in mind that you will not getequally detailed descriptions in the other assignments.

Before we begin with the assignment we start with a short descriptionof what a simulator is and which one we use in this project. A simulatorsimulates something, which can be �gured out from its name. In our case,it simulates the Nao humanoid robot in an environment that is similar toour room. The simulator we use is called Webots and simulates the Naohumanoid robot and the environment using a physics engine called ODE.The physics engine simulates the laws of physics and the e�ect of those onthe simulated objects. Furthermore, Webots launches a version of NAOqithat runs locally on as part of the simulation. This NAOqi works in thesame way as the one which runs on the real robots. However, instead ofsending commands to the servos and fetching data from the sensors thisversion communicates with the physics engine to send commands and fetchsensor values.

When using a simulator you should always beware of the fact that it is asimulation. That means that after all the simulator will not behave exactly asin the real world. For example, the physics engine in Webots do not behaveexactly as the physical laws of the real world. Hence, you will not get exactlythe same sensor data in the simulation as in the real world. Furthermore,the robot might behave slightly di�erent due to the fact that some part ofthe robot might be slightly worn while the simulated robot stays in perfect

14

Page 16: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

condition. Therefore, the simulation should be used for coarse adjustmentsand to make sure that the code will not hurt the robot while �ne tuning isdone on the real robot.

3.2.1 Purpose

The purpose of this assignment is that you should learn the basics of pro-gramming the Nao humanoid robot. Through the assignment you will beguided on how to use the Nao, RosBots and other related software.

3.2.2 Tasks

Your task is to complete the three milestones of the assignment. This includessimulating some simple motions in the simulator, Webots, and using thesemotions and the text to speech module to run your �rst program on therobot. Finally, you have to read data from the sensors and access it throughthe world model module which contains all the robot's information about theworld.

3.2.3 Examination

To pass this assignment you have to:1. Complete all the milestones.2. Demonstrate your programs for each of the milestones of the assignment

to an assistant.3. Demonstrate an understanding of the code and the system by individ-

ually answering questions regarding your code.

3.2.4 Milestone One � Stand and sit in simulation

The �rst milestone of the assignment is to run an FSM in the simulator. Thisinvolves three steps:

1. Create a state diagram.2. Write the code for the FSM.3. Run the code on the simulated robot.

It is important to create a state diagram before implementing the FSM incode because it allows you to �nd errors in the FSM, before you have writtenlarge amounts of code.

In this milestone, you should create a small FSM that makes the robotstand, then sit and lastly relax its actuators before it shutdown. You needfour states to create an FSM that does this: One for standing up, one forsitting down, one for relaxing, and one for terminating the behavior.

15

Page 17: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

It is worth mentioning that the robot will start in a standing position inthe simulator. However, the real robot starts in a sitting position. Therefore,it is strongly recommended that the program always makes the robot standbefore anything else.

Figure 3.1: State diagram describing the stand and sit program.

3.2.4.1 Writing the program

The FSM interface (fsm.functions) contains all the functions you need tocreate an FSM using RosBots. Descriptions of the functions in the interfacecan be found in the RosBots documentation.

# Import the FSM functions

from fsm.functions import (

createState , createFSM ,

addTransition , addStates ,

setInitialState , setMainFSM)

All the primitives in RosBots are de�ned in the pubapi module. In thisexample we use the sit, stand and rest primitives.

# Import primitive robot behaviors

from api.pubapi import sit , stand , rest , shutdown

The next part of writing an FSM is to create the states. This is done bycalling the function createState with a name as the �rst argument and afunction as the second argument. There is one limitation on the functionthat is given as an argument, it can only take the world model or nothingas a parameter. The reason is that the FSM module will call the functionduring run-time with the world model as the only argument. Normally, thiswould require that the function always takes the world model as an argument.However, the framework checks whether the function takes any argumentswhen it is created which allows states to be created that does not take theworld model as an argument.

Note: The state shutdownState takes a new function created from theshutdown function together with a set of given parameters. Since shutdown

takes an argument which is not the world model, it is not possible to use itin createState directly.

16

Page 18: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

# Create states

sitState = createState("sitState", sit)

standState = createState("standState", stand)

restState = createState("restState", rest)

shutdownState = createState("shutdownState",

lambda : shutdown("Final state reached"))

By calling the function addTransition we add a transition between two states.The parameters are the originating state, a function returning True when thecondition for transition is satis�ed and the goal state of the transition.

# Add transitions between states

addTransition(standState , lambda wm: True , sitState)

addTransition(sitState , lambda wm: True , restState)

addTransition(restState , lambda wm: True , shutdownState)

When we have created the states and the transitions, we can create the FSM.This is done by calling createFSM with the name of the FSM as a string,adding the states to it using addStates and then setting the initial stateof the FSM using setInitialState. In the example, you can see that the �rstargument to addStates is the FSM to add states to. The rest of the argumentsare the states to add to the FSM, in this case the four states created above.For setInitialStates the �rst argument is an FSM and the second argument isa state. In this example, the initial state is the standState.

# Create the FSM and add the states created above

myFSM = createFSM("myFSM")

addStates(myFSM , standState , sitState , restState , shutdownState)

# Set the initial state to standState

setInitialState(myFSM , standState)

To make the new FSM ready to run, you have to tell RosBots that it is themain FSM. The main FSM is the FSM which is executed by the framework.It is possible to have multiple FSMs, but only one of them can be the mainFSM. This is done by calling setMainFSM.

# Make myFSM the main FSM

setMainFSM(myFSM)

3.2.4.2 Running the program

To launch a robot program, such as the program written in the previoussection, in simulation execute the script run_simulation. The script takesup to four arguments with the following syntax:

<�rst, second, third, fourth>=<path to the program>

17

Page 19: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

Note: There are simulated worlds for 1�4 robots. Simply add one argumentfor each robot you want to simulate.

As an example, to run a program called helloworld.py located in yourhome directory on the �rst simulated robot assuming your user name is "user"would be (on a Linux system):

run_simulation �rst=/home/user/helloworld.py

To run two di�erent programs, called helloworld.py respectively standandsit.pyin the home directory of the same account, on two robots while standing inthat directory would be done with the following command:

run_simulation �rst=helloworld.py second=standandsit.py

3.2.5 Milestone Two � Hello world on robot

In this milestone you will extend your program from milestone one to makethe robot say "Hello world" after it has stood up. To do this you need toadd one more state to the FSM. Thereafter, you should run your code on therobot. Do not forget to run the code in the simulation �rst! However, do notworry about the lack of sound, Webots does not support sound.

Figure 3.2: State diagram describing the hello world program.

3.2.5.1 Writing the program

To transform the state diagram into an executable program controlling theNao the following code snippets have to be written.

To begin with you need to import the FSM functions and the robot prim-itives. These are the same as in the previous milestone, except the newprimitive say. This primitive is a text-to-speech function that is included inthe NAOqi. You should be relatively careful when using say since it is imple-

18

Page 20: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

mented as a queue and messages can not be removed from the queue. Thismeans that you can fairly easily stack hundreds of messages in the queuewithout any possibility to abort.

# Import the FSM functions

from fsm.functions import (

createState , createFSM ,

addTransition , addStates ,

setInitialState , setMainFSM)

# Import primitive robot behaviors

from api.pubapi import sit , stand , rest , say , shutdown

Create the states for standing, sitting, relaxing and shutting down like in theprevious task.

# Create states for standing , sitting and resting

sitState = createState("sitState", sit)

standState = createState("standState", stand)

restState = createState("restState", rest)

# Create a state which shuts down the FSM

shutdownState = createState("shutdownState",

lambda: shutdown("Final state reached"))

Create a new state saying "Hello world!". Since say takes the string to sayas an argument, it is necessary to create a new function with no argumentsthat calls say with a particular string.

# Create a state which commands the robot to say "Hello World !"

sayState = createState("sayState", lambda : say("Hello World"))

Create the transitions according to the state diagram. This is done in thesame way as in the previous milestone.

# Add transitions according to the state diagram.

# Each transition takes place as soon as possible.

addTransition(standState , lambda wm: True , sayState)

addTransition(sayState , lambda wm: True , sitState)

addTransition(sitState , lambda wm: True , restState)

addTransition(restState , lambda wm: True , shutdownState)

Create the FSM, set the initial state and make it the main FSM. This isalmost the same code as in the previous milestone, with the addition ofadding the new state sayState.

# Create the FSM and add the states created above

myFSM = createFSM("fsm")

addStates(myFSM , standState , sayState ,

sitState , restState , shutdownState)

19

Page 21: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

# Set the initial state to standState

setInitialState(myFSM , standState)

# Make myFSM the main FSM

setMainFSM(myFSM)

3.2.5.2 Running the program

To run the program on a robot use the command run_robot. The commandtakes up to four arguments with the following syntax:

<robotname>=<path to the program>

The four Nao robots in the lab have the following names and numbers:1. goliath2. pi�3. pu�4. david

For example, running a program called helloworld.py located in your homedirectory on pi� would look like this for user "user" (on a Linux based sys-tem):

run_robot pi�=/home/user/helloworld.py

3.2.6 Milestone three � Wait for touch

In this milestone you will extend your program to make the robot wait untilits middle tactile sensor is touched before it stands. Furthermore, the robotshould wait with sitting down until its middle tactile sensor has been touchedagain. Moreover, it should say "goodbye" before it sits down.

This requires that you add some more states to your FSM, see the diagrambelow.

20

Page 22: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

Figure 3.3: State diagram describing the touch program. The text on thearrows describes the condition for the transition. As you might have noticed,none of the previous �gures have had any text on the arrows. That is becausethe lack of text indicates a transition that happens as soon as possible.

3.2.6.1 Writing the code

To implement these extensions you need to add a few states and change someof the transitions used in the previous milestone. However, the major newthing is that the program requires information from the robot. All infor-mation that the robot has about itself and its surrounding is located in theworld model. To access this information, import the function readWM fromfsm.functions. Therefore, you have to update the imports to the following:

# Import the FSM functions

from fsm.functions import (

addStates , addTransition , setInitialState ,

setMainFSM , createState , createFSM , readWM)

# Import primitive robot behaviors

from api.pubapi import sit , stand , rest , say , shutdown

Now that you can access the world model, you need to create a functionwhich detects touch so that it can be used in the transitions:

# Define the event function which detects touch

def detectTouch(WM):

return readWM(WM , "tactile", "middle")

21

Page 23: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

The function readWM takes the argument world model (WM ) and any num-ber of keys. These keys identify what data to fetch from the world model.Use the key "tactile" and "middle" to access the middle tactile sensor. Thiskey has two possible return values, True and False, where True indicates thatthe button is pressed and False that it is not. A description of all the data inthe world model and their keys can be found in the documentation for theworld model (see the RosBots documentation).

Note: It is easy to modify the code to accept a touch of any of the tactilesensors.

# Create states which command the robot to say hello and goodbye

sayHelloState = createState("sayHelloState",

lambda : say("Hello World!"))

sayGoodbyeState = createState("sayGoodbyeState",

lambda : say("Goodbye World!"))

Note that we have changed the name of the sayState and added a new stategoodbyeState since we want two di�erent states where something is said.

We also need to add two new states for waiting until the tactile sensor hasbeen touched:

# Create states for waiting for touch

waitSittingState = createState("waitSittingState", lambda : None)

waitStandingState = createState("waitStandingState", lambda : None)

Note that these two states does exactly the same thing. However, two statesare needed since we want them to have di�erent transitions.

Then we need to update the transitions and add new ones:

# Add transitions according to the state diagram

addTransition(waitSittingState , detectTouch , standState)

addTransition(standState , lambda wm: True , sayHelloState)

addTransition(sayHelloState , lambda wm: True , waitStandingState)

addTransition(waitStandingState , detectTouch , sayGoodbyeState)

addTransition(sayGoodbyeState , lambda wm: True , sitState)

addTransition(sitState , lambda wm: True , restState)

addTransition(restState , lambda wm: True , shutdownState)

Do not forget to set the transition function to detectTouch, described above.Note: With a minor modi�cation you can make to robot wait for a new

touch instead of shutting down after it has said goodbye.Lastly, all the states has to be added to the FSM, set the initial state and

the main FSM.

# Set the initial state to standState

setInitialState(myFSM , waitSittingState)

22

Page 24: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

# Make myFSM the main FSM

setMainFSM(myFSM)

3.2.6.2 Running the code

Follow the instructions for the previous milestone to run the FSM on therobot.

3.3 Intro Phase: Assignment Two � Basic skills

3.3.1 Purpose

The purpose of this assignment is for you to get a deeper understanding ofthe FSM. This is achieved through developing the basic skills that a robotneeds for playing football.

3.3.2 Task

The task of this assignment is to make the robot �nd the ball, even if it isnot in its line of sight, and then follow it.

3.3.3 Examination

To pass Assignment Two you have to:• Complete all the milestones.• Present a diagram over the FSM (hand drawn or computer made).• Present a diagram over the FSM (hand drawn or computer made) forthe basic skills.

• Divide the code in a logical manner between di�erent �les.• Demonstrate a working ball tracker.• Individually explain how the code works.

3.3.4 Milestone One � Walking

The �rst milestone of the assignment is to write an FSM that makes therobot walk. First, it should walk forward for a while; then it should turnroughly ninety degrees on the spot; �nally, it should walk in an arc so that itis close to face the same direction as it started in (basically doing a u-turn).

As mentioned before you should start by drawing a state diagram for thebehavior. Remember that FSMs in RosBots are Moore machines. Therefore,the action associated with the current state is executed each tick and noactions are associated with the transitions. For more information see theRosBots documentation.

23

Page 25: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

When you have an initial state diagram, the next step is to implement itin Python. Thereafter, you should test the program in the simulator. Finally,when the FSM is running without problem in simulation, you may executeit on the robot.

Note: You can �nd all the primitives that are available in the RosBotsdocumentation.

Note: Writing a script or alias for starting your program will make itfaster to launch the FSM in the simulator and on the robot.

Tip: Do not �ne tune parameters by running your program in simulation.After all, it is a perfect world without the same quirks as the real world.

3.3.5 Milestone Two � Turning the head

In the second milestone of the assignment you will write a new FSM thatturns the head of the robot. The FSM should make the robot turn its headin a motion similar to one used when searching for something, the ball forexample.

To �nish this assignment it is recommended that you follow the sameprocedure as described in the previous milestone.

3.3.6 Milestone Three � Using the camera

The focus in the third milestone is to make use of the robot's camera. Thisshould be done by detecting the ball as well as reacting to �nding the balland loosing it. However, you must be aware that there is a risk that therobot will not recognize the ball in front of it. Therefore, you must take thisinto account since it is not acceptable if the robot thinks it has lost the ballwhen it is in front of the robot and has been lost for a couple of frames.

The public API has a function for fetching the relevant data for searchingfor balls, goals and lines from the world model. This data is made availablein the world model through algorithms that are running on the robot itself.We will not provide an explanation on how the processing is done in thisdocument. However, there is a description of the algorithms in the RosBotsdocumentation.

Note: The say function is really good for troubleshooting while runningan FSM on the robot. However, you have to be restrictive with the use of itsince it is, as mentioned before, implemented as a queue.

Note: Nao has two cameras, one facing forwards and the other facingdownwards. Both of them can not be active at the same time, instead youhave to swap between them. Furthermore, the top camera can not see thefeet of the robot. Therefore, you have to use both cameras to keep the ballin sight.

24

Page 26: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

3.3.7 Milestone Four � Finding and tracking the ball

To achieve this milestone, the robot should be able to �nd the ball when itis within a two meter radius of the robot (it does not have to �nd the ball ifit is located in an impossible spot, such as between its feet). This milestonecombines the last two milestones.

Note: Remember that a state in an FSM can be another FSM.

3.3.8 Milestone Five � Following the ball

In the �nal milestone of this assignment you should make the robot �nd theball and then follow it as it is pushed around the �eld (within reasonablelimits). As in milestone four it can be very useful to create an FSM withother FSMs as states.

3.4 Main Phase: Assignment Three � Playing

soccer

3.4.1 Purpose

The purpose of this assignment is to expand your code so that the robot isable to play football and not just mastering basic skills. To achieve this,you need to use more complex FSMs than in the previous assignments. Youwill most likely also have to improve the basic skills that you have started todevelop in the previous assignment.

3.4.2 Task

The task is divided into four milestones that incrementally creates an FSMthat can �nd the ball and kick it into the goal. As usual, we strongly recom-mend you to re-use the code you have written in previous assignments.

3.4.3 Examination

To pass Assignment Three you have to:• Complete all the milestones.• Present a diagram over the FSM.• Demonstrate how the robot can �nd the ball, position itself and thenscore a goal.

• Be prepared to indivdually explain how the code works.

25

Page 27: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

3.4.4 Milestone One � Kicking the ball

First out is one more basic skill, which is to kick the ball. By now you shouldbe familiar with how FSMs and the system works and you should be able toimplement this behavior without much problem. Note that you are supposedto make the robot position itself in front of the ball as well as kick it.

Tip: You will soon notice that you need di�erent parameters for the sim-ulation and for the real robot. To handle this, you should use con�guration�les, one for simulation and for the robot. Then you can import the appro-priate �le depending on the platform.

3.4.5 Milestone Two � Finding the goal

Finding and kicking the ball is quite useless unless you can �nd the opponent'sgoal or your own position on the �eld. Therefore, the next milestone of thisassignment is to locate the goal.

Note: Remember that the two goals have di�erent colors.

3.4.6 Milestone Three � Aligning the robot

We are now ready to start with slightly more complex tasks related to makinga robot play football. First, aligning it in relation to the goal and the ball inorder to kick the ball in the right direction.

Note: Can your code keep control of the ball while aligning the robot?Note: Can the robot realize that it has lost the ball?

3.4.7 Milestone Four � Scoring a goal

As the �nal step in this assignment your robot should actually be able toscore a goal. There are many ways to achieve this task and you are free toexplore whichever of these that you want. However, your program should beable to score a goal from di�erent positions around the �eld.

3.5 Main Phase: Assignment Four � Co-operation

3.5.1 Purpose

In a soccer game with multiple players communication and co-operation isessential to winning. Until now your robot has been incapable of communi-cating with other robots. The purpose of this assignment is to teach you thebasics of co-operation.

26

Page 28: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

3.5.2 Task

In this assignment you will be introduced to the communication part of theAPI. Furthermore, you will create an FSM that allows two or more robotsto co-operate in order to solve a couple of task. The �rst task consists ofmaking one robot react to sensor inputs to another. Following this is a taskto make one of the robot direct the other robot through giving permissionfor shooting. The �nal task is to make one robot pass the ball to the other.

3.5.3 Examination

To pass Assignment Three you have to:• Complete at least the three �rst milestones.• Present a diagram of the state machines.• Demonstrate two robots passing the ball between themselves.• Be prepared to individually explain how the code works.

3.5.4 Milestone One � Send messages

It is now time to introduce communication between agents. The basic prim-itive for communication is communicate which sends a string to a namedrobot. The string sent can then be extracted using the function readWM.See below for an example.

sendReadyStatus = createState(

lambda : communicate("goliath", "Ready"))

An example state that will send the string �Ready� to Goliath.

messages = readWM(WM, "comms")

An example of how to extract the available messages.Your task is now to modify the code from the last milestone of the �rst

assignment so that touching the tactile sensor of one robot makes anotherrobot react.

3.5.5 Milestone Two � Wait for clearance

One of the fundamentals of passing is that the receiving robot should beprepared. Therefore, it is necessary for the robots to communicate to makea successful pass.

Your task in this milestone is to make one of the robots �nd the ball, waituntil it gets clearance from the other robot and then kick the ball.

27

Page 29: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

3.5.6 Milestone Three � Pass the ball

In the previous milestone the robot kicked as soon as it got clearance. How-ever, kicking a ball in a random direction does not count as a pass. A passhas to be directed towards one of the robot's teammates. To make a perfectpass is a complex task and therefore your task is to make one robot passthe ball in the general direction of the other robot. When the ball has beenpassed the second robot should take control of it.

3.5.7 Milestone Four (optional) � Cross pass

This milestone is an extension of the previous, with some more conditions:• The �rst robot should be located on your side of the �eld.• The second robot should be located on the opponent's side of the �eld.• The robots may not cross the middle line (look at the line detectionfunctions).

• The second robot should score a goal after it has gotten control of theball.

3.6 Main Phase: Assignment Five � Elective

task

3.6.1 Purpose

Robots are used for a huge variety of tasks, not just playing football. Thereare always new ways of completing a task and new ideas on what could besolved by robots. This assignment gives you the opportunity to try your ownideas for what a robot can be used for.

3.6.2 Task

You are relatively free to de�ne the task for this assignment. However, it hasto be approved by one of the assistants or the supervisor.

These are some examples of what you can do:• Program two robots to co-operate and play against two other robots.If there is another group that does the same, why not challenge themto a match?

• Program a goalkeeper.• Make a throw-in.*• Make two robots hand the ball to each other.*

* These tasks require functionality that is not part of the API. However,

28

Page 30: TDDD63 Project: Humanoids Version 1TDDD63/projects/2013/humanoids/...Programming a humanoid robot to play soccer from scratch is a huge task. Therefore, you will work with a framework

they can be implemented by only using Python and you are very welcometo ask an assistant on how to do so.

3.6.3 Examination

The examination of this assignment is to de�ne a task, make a serious e�ortto complete the task and write a short report. The report should have thefollowing outline:

• Front page.• Table of content.• Introduction � a short description of the task.• Implementation � a description of how you implemented the task.• Results � how well did you code work? Pictures are very nice anduseful.

• Discussion � why did you get the results?• Conclusion � what could have been improved? What would the nextstep be?

• References � include any references following a well know standard,such as the Harvard or Oxford reference system.

29