Module UltrasonicSensor
[hide private]
[frames] | no frames]

Source Code for Module UltrasonicSensor

  1  # UltrasonicSensor.java 
  2   
  3  ''' 
  4  Class that represents an ultrasonic sensor. 
  5   
  6   This software is part of the raspibrick module. 
  7   It is Open Source Free Software, so you may 
  8   - run the code for any purpose 
  9   - study how the code works and adapt it to your needs 
 10   - integrate all or parts of the code in your own programs 
 11   - redistribute copies of the code777 
 12   - improve the code and release your improvements to the public 
 13   However the use of the code is entirely your responsibility. 
 14   ''' 
 15   
 16  import SharedConstants 
 17  from RobotInstance import RobotInstance 
 18  from Tools import Tools 
 19  import RPi.GPIO as GPIO 
 20  import time 
 21   
22 -class UltrasonicSensor():
23 ''' 24 Class that represents an ultrasonic sensor. 25 '''
26 - def __init__(self, **kwargs):
27 ''' 28 Creates a sensor instance. 29 ''' 30 self.sensorState = "FAR" 31 self.sensorType = "UltrasonicSensor" 32 self.triggerLevel = 20 33 self.nearCallback = None 34 self.farCallback = None 35 for key in kwargs: 36 if key == "near": 37 self.nearCallback = kwargs[key] 38 elif key == "far": 39 self.farCallback = kwargs[key] 40 robot = RobotInstance.getRobot() 41 if robot == None: # deferred registering, because Robot not yet created 42 RobotInstance._sensorsToRegister.append(self) 43 else: 44 if self.nearCallback != None or self.farCallback != None: 45 robot.registerSensor(self) 46 Tools.debug("UltrasonicSensor instance created")
47
48 - def getValue(self):
49 ''' 50 Returns the distance. 51 @return: Distance from target in cm, or -1 if no object or error 52 @rtype: float 53 ''' 54 self._checkRobot() 55 # Set pins as output and input 56 GPIO.setup(SharedConstants.P_TRIG_ECHO, GPIO.OUT) 57 GPIO.output(SharedConstants.P_TRIG_ECHO, GPIO.LOW) 58 59 # Allow module to settle 60 time.sleep(0.1) 61 62 # Send max 10 us trigger pulse 63 GPIO.output(SharedConstants.P_TRIG_ECHO, GPIO.HIGH) 64 time.sleep(0.00001) 65 GPIO.output(SharedConstants.P_TRIG_ECHO, GPIO.LOW) 66 67 # Prepare for echo 68 GPIO.setup(SharedConstants.P_TRIG_ECHO, GPIO.IN) 69 70 # Determine echo pulse length 71 start = time.time() 72 count = start 73 74 # Wait max 1 s for HIGH signal 75 while GPIO.input(SharedConstants.P_TRIG_ECHO) == GPIO.LOW and count - start < 1: 76 count = time.time() 77 if count - start >= 1: 78 Tools.debug("Timeout while waiting for echo going HIGH") 79 return -1 # error 80 81 # Wait for LOW signal 82 elapsed = 0 83 while GPIO.input(SharedConstants.P_TRIG_ECHO) == GPIO.HIGH: 84 elapsed = time.time() - start 85 if elapsed > 0.015: # echo considered as failed (distance too big) 86 break 87 88 # Distance = speed_of_sound * elapsed / 2 89 distance = 34300 * elapsed / 2.0 90 # round to 2 decimals 91 distance = int(distance * 100 + 0.5) / 100.0 92 return distance
93
94 - def getDistance(self):
95 ''' 96 Returns the distance. 97 @return: Distance from target in cm formatted to two decimals, or -1 if no object or error 98 @rtype: str 99 ''' 100 return format(self.getValue(), ".2f")
101
102 - def getTriggerLevel(self):
103 return self.triggerLevel
104
105 - def setTriggerLevel(self, level):
106 self.triggerLevel = level
107
108 - def getSensorState(self):
109 return self.sensorState
110
111 - def setSensorState(self, state):
112 self.sensorState = state
113
114 - def getSensorType(self):
115 return self.sensorType
116
117 - def onNear(self, v):
118 if self.nearCallback != None: 119 self.nearCallback(v)
120
121 - def onFar(self, v):
122 if self.farCallback != None: 123 self.farCallback(v)
124
125 - def _checkRobot(self):
126 if RobotInstance.getRobot() == None: 127 raise Exception("Create Robot instance first")
128