Thanks,
I just updated the post with a link to the report (with Mikronauts.com Pi Droid Alpha mention) on element14.
Once the bot "has a brain", I want to investigate driving onto one of the Qi wireless charging pads.
Alan
Moderator: Steve
alanmcdonley wrote:Thanks,
I just updated the post with a link to the report (with Mikronauts.com Pi Droid Alpha mention) on element14.
Once the bot "has a brain", I want to investigate driving onto one of the Qi wireless charging pads.
Alan
#
# myPyLib.py SUPPLIMENTAL PYTHON FUNCTIONS
#
# v0.1 19June2016
import time
import sys
import signal
# ######### CNTL-C #####
# Callback and setup to catch control-C and quit program
_funcToRun=None
def signal_handler(signal, frame):
print '\n** Control-C Detected'
if (_funcToRun != None):
_funcToRun()
sys.exit(0)
# Setup the callback to catch control-C
def set_cntl_c_handler(toRun):
global _funcToRun
_funcToRun = toRun
signal.signal(signal.SIGINT, signal_handler)
#!/usr/bin/python
#
# bumbersClass.py BUMPERS SENSOR CLASS
#
import PDALib
import myPDALib
import myPyLib
import time
import sys
import threading
class Bumpers():
# CLASS VARS (Avail to all instances)
# Access as Bumpers.class_var_name
pollThreadHandle=None # the SINGLE read sensor thread for the Bumpers class
tSleep=0.033 # time for read_sensor thread to sleep after each read op
# Bumpers are on the Pi Droid Alpha MCP23S17 DIO expander
# Wired to use internal pull-up power of the MCP23S17
# Bumper value is negative logic - 0 means bumper activated, normal 1
LeftBumperDIO=18
RightBumperDIO=17
RearBumperDIO=16
# Allowable Bumpers.state values
NONE = 0
# Single bumpers
LEFT = 1
RIGHT = 2
REAR = 4
# Combinations
FRONT = 3
LEFTREAR = 5
RIGHTREAR= 6
ALL = 7
# Not possible
UNKNOWN = 8
# THE STATE OF EACH BUMPER and the combined BUMPERS state
# (class vars because there are only one physical bumper)
# note: can get rid of left(), right(), rear() methods by using these vars direct
leftState= UNKNOWN
rightState=UNKNOWN
rearState= UNKNOWN
state= UNKNOWN #0,1=L,2=R,3=L+R(front),4=Rear,...
# use to print Bumper.state var
bumperStrings=["NONE", "LEFT", "RIGHT", "FRONT", "REAR",
"LEFTREAR", "RIGHTREAR", "ALL", "UNKNOWN"]
# end of class vars definition
def __init__(self):
# SINGLETON TEST
if (Bumpers.pollThreadHandle!=None):
print "Second Bumpers Class Object, not starting pollingThread"
return None
# Set Bumper DIO channels as input for now
PDALib.pinMode(Bumpers.LeftBumperDIO, PDALib.INPUT)
PDALib.pinMode(Bumpers.RightBumperDIO,PDALib.INPUT)
PDALib.pinMode(Bumpers.RearBumperDIO, PDALib.INPUT)
# Set internal pull-ups on bumper channels
PDALib.setDioBit( PDALib.DIO_GPPU, 8 ) # set LeftBumper pin 16 pull-up
PDALib.setDioBit( PDALib.DIO_GPPU, 9 ) # set RightBumper pin 17 pull-up
PDALib.setDioBit( PDALib.DIO_GPPU, 10 ) # set RearBumper pin 18 pull-up
# threading target must be an instance
Bumpers.pollThreadHandle = threading.Thread( target=self.pollBumpers,
args=(Bumpers.tSleep,))
Bumpers.pollThreadHandle.start()
#end init()
# BUMPER THREAD WORKER METHOD TO READ BUMPERS
def pollBumpers(self,tSleep=0.01):
print "pollBumpers started with %f" % tSleep
t = threading.currentThread() # get handle to self (pollingBumpers thread)
while getattr(t, "dorun", True): # check the dorun thread attribute
self.read()
time.sleep(tSleep)
print("dorun went false. Stopping pollBumpers thread")
def read(self): #READ THE BUMPERS - can be used as poll or directly
Bumpers.leftState= Bumpers.LEFT - Bumpers.LEFT * \
PDALib.digitalRead(Bumpers.LeftBumperDIO)
Bumpers.rightState= Bumpers.RIGHT - Bumpers.RIGHT * \
PDALib.digitalRead(Bumpers.RightBumperDIO)
Bumpers.rearState= Bumpers.REAR - Bumpers.REAR * \
PDALib.digitalRead(Bumpers.RearBumperDIO)
Bumpers.state = Bumpers.leftState + Bumpers.rightState + Bumpers.rearState
return Bumpers.state
def status(self):
return Bumpers.state
def left(self):
return Bumpers.leftState
def right(self):
return Bumpers.rightState
def rear(self):
return Bumpers.rearState
def toString(self,bumperState=UNKNOWN):
if (bumperState==Bumpers.UNKNOWN):
bumperState= Bumpers.state
return Bumpers.bumperStrings[bumperState]
def cancel(self):
print "bumpers.cancel() called"
self.pollThreadHandle.dorun = False
myPDALib.PiExit()
# ##### BUMPER CLASS TEST METHOD ######
# creates two instances, only the first should start the read() thread
# the first time through the main() while loop, the sensors may not have been read yet
# so bumpers.status() and each bumper may have a value of 8/UNKNOWN
def main():
# note: lowercase bumpers is object, uppercase Bumpers is class (everywhere in code)
bumpers=Bumpers() #create an instance which starts the read bumpers thread
bumpersNoThreadStart=Bumpers() # Test a second instance of class
myPyLib.set_cntl_c_handler(bumpers.cancel) # Set CNTL-C handler
while True:
print "\n"
print "bumpers.state: %d %s" % (bumpers.status(), bumpers.toString())
print "left():%d rear():%d right():%d" % (
bumpers.left(),
bumpers.rear(),
bumpers.right() )
time.sleep(1)
#end while
if __name__ == "__main__":
main()
#!/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()
pi@raspberrypi:~/RWPi $ ./whimpy.py
Starting Main
Robot__init__
readingsPerSec: 10
pollBumpers started with 0.100000
reading thread told to start
pollUltrasonicDistance started with 0.100000
Motors worker thread readingsPerSec: 20
Motors worker thread told to start 2016-07-03 13:57:04.322502
pollMotors thread started with 0.050000 at 2016-07-03 13:57:04.325529
waiting for threads to start
I'm happy now
I've been bumped! (REAR)
Checking if escape path is clear
Forward path is clear for: 23 inches
Moving fwd half my size
starting travel 3.5 at 1
waitForStopped or 60.0
I'm happy now
I've been bumped! (REAR)
Checking if escape path is clear
Forward path is clear for: 21 inches
Moving fwd half my size
starting travel 3.5 at 1
waitForStopped or 60.0
I'm happy now
I've been bumped! (REAR)
Checking if escape path is clear
Forward path is clear for: 17 inches
Moving fwd half my size
starting travel 3.5 at 1
waitForStopped or 60.0
File "/home/pi/RWPi/PDALib.py", line 324, in writeDio
r = spi.xfer([0x40,reg,val&255,val>>8])
IOError: [Errno 9] Bad file descriptor
I'm happy now
I've been bumped! (REAR)
Checking if escape path is clear
Forward path is clear for: 14 inches
Moving fwd half my size
starting travel 3.5 at 1
waitForStopped or 60.0
Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/pi/RWPi/motorsClass.py", line 258, in pollMotors
self.control()
File "/home/pi/RWPi/motorsClass.py", line 430, in control
elif (self.motorsMode == Motors.TRAVEL): self.controlTravel()
File "/home/pi/RWPi/motorsClass.py", line 342, in controlTravel
self.setMotorsPwr(lPwr,rPwr) # pwrs=(lPwr,rPwr)
File "/home/pi/RWPi/motorsClass.py", line 585, in setMotorsPwr
PDALib.digitalWrite(Motors.M2DirB,0) #set to off/coast
File "/home/pi/RWPi/PDALib.py", line 188, in digitalWrite
return clearDioBit(DIO_OLAT,pin-8)
File "/home/pi/RWPi/PDALib.py", line 353, in clearDioBit
writeDio(reg,t)
File "/home/pi/RWPi/PDALib.py", line 324, in writeDio
r = spi.xfer([0x40,reg,val&255,val>>8])
IOError: [Errno 9] Bad file descriptor
^C
** Control-C Detected
robot.cancel() called
bumpers.cancel() called
Motors.cancel() called
Waiting for Motors.control Thread to quit
UltrasonicDistance.cancel() called
Waiting for UtrasonicDistance.readThread to quit
do_run went false. Stopping pollBumpers thread
do_run went false. Stopping pollUltrasonicDistance thread
myPDALib.PiExit(): PDALib.pi.stop() called
whimpy.py says: Bye Bye
pi@raspberrypi:~/RWPi $
import threading
dio_lock=threading.Lock()
with dio_lock: # critical section #ALAN
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 $
Users browsing this forum: No registered users and 5 guests