Chapter 3 Building Robot Brains
-------------------------------

Robot Brain = Python Program

Basic structure of a Python program:

##########################
from myro import *
init("comxx")

<any other imports>
<function definitions>
def main():
  <do something>
  <do something>
  ...

main()
##########################

An example program:
##########################
# File: dance.py

from myro import *
init("com8")

def yoyo(speed, waitTime):
    forward(speed, waitTime)
    backward(speed, waitTime)

def wiggle(speed, waitTime):
    motors(-speed, speed)
    wait(waitTime)
    motors(speed, -speed)
    wait(waitTime)
    stop()

def main():
    speak("Running the dance routine...")
    yoyo(0.5, 0.5)
    wiggle(0.5, 0.5)
    yoyo(1, 1)
    wiggle(1, 1)
    speak("...Done")

main()
##########################
this program is almost the same as the previous chapter's
dance.py program - we have some print statements here and
the dance() function is being called the main() function here.

Do this:

Start IDLE
Run Module or from dance import *
Change print commands to
speak("Running the dance routine)
speak("Dude! Pardon me, would you have any Grey Poupon")
Re-run the program

What's in a name?
-----------------

A name in Python denotes something; e.g. function names, variable names,
etc.

Rule: names in Python MUST begin with a letter (a-z,A-Z) or with 
the underscore symbol (_) and can be followed by any sequence of
letters, digits, or underscores.

e.g. iRobot, yoyo, _balance, my_2_cents

Python names are case-sensitive
So, myRobot and MyRobot denote different names.

Programmer may choose any name for functions/variables - however
meaningful names are easy to read and understand.

Values
------

Names can denote functions such as yoyo, turnLeft,...
Names are also used to denote values.
e.g.  speed, waitTime, ...

Two important types of values: Numbers and Strings

Numbers: 24, -32, 2.55, 3.14, ...
Strings: "Scribby", "Jones", ...

Assignment Statement
--------------------

speed = 0.75
myFavoriteRobot = "c3po"

General form of assignment statement:

<variable name> = <expression>

Examples of (Numeric) Expressions:

5
5+3
3*4
3.2 + 4.7
10/2
x + 2
2*speed

Whole numbers are called "integers".
Numbers with fractions are called "Floating point" numbers.

Python expects users to type "expressions" at the $>>> prompt.

10/3
3

10.0/3
3.33333333

If at least one of the two operands is a floating-point number, the result
will be a floating-point number. Otherwise, we get an integer result.

String expressions:

mySchool = "GSU"
yourSchool = "GaTech"
yourSchool+mySchool

The + operation on strings is the "concatenation" operation.

A Calculating Program
---------------------

# page 54
#################################################################
# File: worldPop.py
# Purpose:
#     Estimate the world population growth in a year and
#     also per day.
#     Given than on January 1, 2008 the world's population was
#     estimated at 6,650,000,000 the estimated growth is
#     at the rate of +1.14%

def main():
    population = 6650000000
    growthRate = 1.14/100.0

    growthInOneYear = population * growthRate
    growthInADay = growthInOneYear / 365

    print "World population on January 1, 2008 is", population
    print "By Jan. 1, 2009, it will grow by", growthInOneYear
    print "An average daily increase of", growthInADay
    
main()
#################################################################

The above program is not flexible - it does a simple FIXED calculation.

Using Input in programs
-----------------------

#################################################################
# page 56

# File: worldPop.py
# Purpose:
#     Estimate the world population growth in a year and
#     also per day.
#     Given than on January 1, 2008 the world's population was
#     estimated at 6,650,000,000 the estimated growth is
#     at the rate of +1.14%

def main():
    print "This program computes population growth figures."
    
    population = input("Enter current world population: ")
    growthRate = input("Enter the growth rate: ")/100.0

    growthInOneYear = population * growthRate
    growthInADay = growthInOneYear / 365

    print "World population on January 1, 2008 is", population
    print "By Jan. 1, 2009, it will grow by", growthInOneYear
    print "An average daily increase of", growthInADay
    
main()
#################################################################

Doing Repetition in Python
--------------------------

for i in range(10):
  dance()

For-Statement (or a loop-statement) general syntax:

for <variable> in <sequence>:
  <do something>
  <do something>
  ...

loop index variable
sequence: a list of values

range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Do This:
#################################################################
# File: dance.py

#from myro import *
init("simulator")

def yoyo(speed, waitTime):
    forward(speed, waitTime)
    backward(speed, waitTime)

def wiggle(speed, waitTime):
    motors(-speed, speed)
    wait(waitTime)
    motors(speed, -speed)
    wait(waitTime)
    stop()

def dance():
    yoyo(0.5, 0.5)
    yoyo(0.5, 0.5)
    wiggle(0.5, 1)
    wiggle(0.5, 1)

def main():
    speak("Running the dance routine...")

    for danceStep in range(10):
        dance()

    speak("...Done")

main()
#################################################################

CTRL-C to stop program at any time...

While-Loops
-----------

while timeRemaining(10):
  <do-something>
  <do-something>
  ...

example:

while timeRemaining(5):
  speak("Doh!",0);

Boolean Values
--------------

True
False

will cover in next chapter

Updating Variables:

population = 650000000
growthRate = 0.12
for year in range(10):
  population = population * (1 + growthRate)