My "Whimpy Robot" is in the test phase now. Things I learned through this exercise:
- Python Class structure
- Initializing class objects
- Thinking with "self"
- Threading (creating, managing, killing)
- Multi-threading with SPI to MCP3008 and MCP23S17 (critical sections, mutex variable)
- "Thinking asynchronous": (fire and forget, fire-go_away-then-resynchronize)
- Use of debugLevel to control print statements
- Maintaining state for objects and threads
- Publishing and consuming information for other objects
- Making decisions about self state and state of other objects
- Handling control-C to gracefully shutdown all objects and threads
- Designing and coding for reuse
So here is a sample run that shows:
- Whimpy Robot startup
- Whimpy is "Happy"
- Whimpy is "Bumped" (from the REAR)
- -decides escape path is straight forward
- -checks coast is clear to move
- -moves 4"
- Whimpy is "Bumped" (from the REAR)
- Whimpy is "Happy" again
- Whimpy is "Bumped" (from the FRONT)
- -decides escape path requires a 180 degree turn
- -after the turn, decides coast is not clear
- -decides to try an additional 45 degree turn
- -checks this new escape path is clear
- -moves 4"
- Whimpy is "Bumped" (from the FRONT)
- Whimpy is "Happy" again
- Control-C occurs - Whimpy shuts down gracefully
Life of a Whimpy Robot: (RWPi = Rug Warrior Pi robot)
pi@raspberrypi:~/RWPi $ ./whimpy.py
Starting Main
Robot__init__
UltrasonicDistance: readingsPerSec: 10
pollBumpers started with 0.100 interval
UltrasonicDistance: reading thread told to start
pollUltrasonicDistance started with 0.100s cycle
Motors: worker thread readingsPerSec: 20
Motors worker thread told to start 2016-07-04 09:40:04.233426
Motors: pollMotors thread started with 0.050000 at 2016-07-04 09:40:04.236642
waiting for threads to start
I'm happy now
********* RWPi STATUS *****
2016-07-04 09:40:06.258264
battery.volts(): 8.1
battery.hoursOfLifeRemaining(): 10 h 26 m
currentsensor.current_sense(): 524 mA
irDistance.inInches: 26.9
usDistance.inInches: 28.8
bumpers: NONE
I've been bumped! (REAR)
Checking if escape path is clear
Forward path is clear for: 8 inches
Moving fwd half my size
starting travel 3.5 at 1
waitForStopped or 60.0
I'm happy now
********* RWPi STATUS *****
2016-07-04 09:40:12.003888
battery.volts(): 8.1
battery.hoursOfLifeRemaining(): 10 h 25 m
currentsensor.current_sense(): 528 mA
irDistance.inInches: 28.1
usDistance.inInches: 26.9
bumpers: NONE
I've been bumped! (FRONT)
* Turning to CW180 as escape path
waitForStopped or 60.0
Checking if escape path is clear
Forward path is clear for: 2 inches
* Turning to CCW45 as escape path
waitForStopped or 60.0
Checking if escape path is clear
Forward path is clear for: 15 inches
Moving fwd half my size
starting travel 3.5 at 1
waitForStopped or 60.0
I'm happy now
********* RWPi STATUS *****
2016-07-04 09:41:13.900162
battery.volts(): 8.1
battery.hoursOfLifeRemaining(): 10 h 24 m
currentsensor.current_sense(): 508 mA
irDistance.inInches: 48.0
usDistance.inInches: 0.0
bumpers: NONE
^C
** Control-C Detected
robot.cancel() called
bumpers.cancel() called
Motors.cancel() called
Waiting for Motors.control Thread to quit
do_run went false. Stopping pollMotors thread at 2016-07-04 09:41:52.735483
UltrasonicDistance.cancel() called
Waiting for UtrasonicDistance.readThread to quit
do_run went false. Stopping pollUltrasonicDistance thread
do_run went false. Stopping pollBumpers thread
myPDALib.PiExit(): PDALib.pi.stop() called
whimpy.py says: Bye Bye
pi@raspberrypi:~/RWPi $