Here is the first wimpy code - notices when bumped, happy when not:
#!/usr/bin/python
#
# whimpy.py WHIMPY ROBOT
#
import PDALib
import myPDALib
import myPyLib
from bumpersClass import Bumpers
#from usDistanceClass import UltrasonicDistance
# import motors
import time
class Robot():
# class constants and vars
HAPPY=0
BUMPED=1
def __init__(self):
print "Robot__init__"
self.lastState=Robot.HAPPY # create an instance var self.lastState
self.bumpers=Bumpers() # give robot instance bumpers
# usDistance=UltrasonicDistance() # give robot instance ultrasonic sensor
# motors=Motors()
def be_wimpy(self):
while True:
while (self.bumpers.status() == Bumpers.NONE):
if (self.lastState!=Robot.HAPPY):
print "\nI'm happy now"
self.lastState=Robot.HAPPY
continue
# MUST HAVE BEEN BUMPED
if (self.lastState!=Robot.BUMPED):
print "\nI've been bumped! (%s)" % self.bumpers.toString()
self.lastState=Robot.BUMPED
def cancel(self):
print "robot.cancel() called"
self.bumpers.cancel()
#end Robot() class
# ##### MAIN ######
def main():
try:
print "Starting Main"
r=Robot()
time.sleep(1) # allow all Robot threads to start
myPyLib.set_cntl_c_handler(r.cancel) # Set CNTL-C handler
r.be_wimpy()
except SystemExit:
print "Bye Bye"
except:
print "Exception Raised"
r.cancel()
if __name__ == "__main__":
main()