Chapter 4 Sensing From Within
-----------------------------

External Sensors (exteroceptors) - allows robot to sense external
conditions (e.g. light, temperaturs, distance to other objects, etc)

Internal Sensors (proprioceptors) - allows robot to sense internal
conditions (e.g. battery level etc)

Scribbler's Internal Sensors (time, stall, battery level)
---------------------------------------------------------
(1) Time
All computers come built-in with an internal clock.
Scribbler can use the computer's clock to sense time.
e.g. timeRemaining(5), wait(5)

currentTime()

returns current time (in seconds elapsed from an earlier time!!)
(See: http://en.wikipedia.org/wiki/System_time)

Here are different ways to make Scribbler move forward for 3 seconds

forward(1.0,3.0)

forward(1.0)
wait(3.0)
stop()

while timeRemaining(3.0):
  forward(1.0)
stop()

startTime = currentTime()   # record current time
while (currentTime() - startTime) < 3.0:
  forward(1.0)
stop()

General form of while-loop
--------------------------

while <some condition is true>:
  <do something>

To make a robotic vacuum cleaner clean a room for 1 hour:

startTime = currentTime()   # record current time
while (currentTime() - startTime)/60.0 < 60.0:
  cleanRoom()

Writing Conditions
------------------

All conditions when evaluated will result in True or False
Comparison of two values using <, <=, >, >=, ==, !=

42 > 23
42 < 23
42 == 23
42 != 23
(42+23) < 100
True == 1
False == 0

"Hello" == "Good Bye"
"Hello" != "Good Bye"
"Elmore" < "Elvis"
"A" < "B"
"a" < "B"

More complex conditions can be written using and/or/not logical
operations

(5 < 7) and (8 > 3)
not ((5 < 7) and (8 > 3))
(6 > 7) or (3 > 4)

Some logical equivalences

(A or True) == True
(not (not A)) == A
(A or (B and C)) == ((A or B) and (A or C))
(A and (B or C)) == ((A and B) or (A and C))
(not (A or B)) == ((not A) and (not B))
(not (A and B)) == ((not A) or (not B))

(2) Stall
Scribbler has the ability to detect if it has stalled while trying
to move (e.g. facing an obstruction in the environment)

getStall()

returns True if robot has stalled, False otherwise.

while not getStall():
  <do something>

Do this:

while not getStall():
  forward(1.0)
stop()
speak("Ouch! I think I bumped into something")

Show to your non-CS friends!!


(3) Battery Level
Scribbler runs on 6 AA batteries. 

getBattery()

returns current voltage (between 0 and 9 volts) being supplied 
by the batteries.

Two battery power indicators - one on scribbler (red LED) and the
other on the fluke. These flash when battery is low!

Can use following code that senses battery:

while (getBattery() >= 5.0) and timeRemaining(duration):
  <do something>

Random Walks
------------

from random import *

random()
 returns a random number between 0.0 and 1.0

randint(A,B)
  returns a random integer between A and B

Example Program: Generate a 4-digit number between 1000 and 9999
in which there are no repeating digits. For example, 1219 is not
valid because digit 1 appears twice, but 2397 is OK.

############################################################
from random import *

def noRepeatingDigits(num):
  d3 = (num / 1000) % 10
  d2 = (num / 100) % 10
  d1 = (num / 10) % 10
  d0 = (num / 1) % 10
  return ((d3 != d2) and (d3 != d1) and (d3 != d0) and
          (d2 != d1) and (d2 != d0) and 
          (d1 != d0))

def produceTarget():
  while True:
    num = randint(1000,9999)
    if (noRepeatingDigits(num)):
      break
  return num

############################################################

Asking Questions
----------------

askQuestion("Are you ready?")

pops up a dialogue window with message and "Yes" and "No"
buttons.

the value of the functions is the string "Yes" or "No" whichever
was clicked by the user.

askQuestion("Change pen to different color and press OK",["OK"])

second parameter is a list of options shown as buttons; if second
parameter is not specified then "Yes" and "No" are used by default.

askQuestion("What is your favorite ice cream flavor?",
            ["Vanilla","Chocolate","Mango","Other"])

if-statement
------------

if <condition>:
  <statement>
  <statement>
  ...

if <condition>:
  <statement>
  <statement>
  ...
else:
  <statement>
  <statement>
  ...
  
Example:

x = input("enter a number between 1 and 100: ")
if (x%2 == 0):
  print "The number you entered is EVEN"
else:
  print "The number you entered is ODD"

Example:

x = input("enter a month number (1-12): ")
if x==1:
  print "January"
elif x==2:
  print "February"
elif x==3:
  print "March"
elif x==4:
  print "April"
elif x==5:
  print "May"
elif x==6:
  print "June"
elif x==7:
  print "July"
elif x==8:
  print "August"
elif x==9:
  print "September"
elif x==10:
  print "October"
elif x==11:
  print "November"
else:
  print "December"