def __new__(cls, *args): if RobotInstance.getRobot() == None: r = MyRobot(*args) RobotInstance.setRobot(r) return r else: return RobotInstance.getRobot()
def __new__(cls, ipAddress="", buttonEvent=None): global _isBtnHit if RobotInstance.getRobot() == None: r = MyRobot(ipAddress, buttonEvent) r.isEscapeHit() # Dummy to clear button hit flag RobotInstance.setRobot(r) for sensor in RobotInstance._sensorsToRegister: r.registerSensor(sensor) return r else: r = RobotInstance.getRobot() r.isEscapeHit() # Dummy to clear button hit flag return RobotInstance.getRobot()
def __init__(self, id, **kwargs): ''' Creates a light sensor instance with given id. IDs: 0: front left, 1: front right, 2: rear left, 3: rear right The following global constants are defined: LS_FRONT_LEFT = 0, LS_FRONT_RIGHT = 1, LS_REAR_LEFT = 2, LS_REAR_RIGHT = 3. @param id: the LightSensor identifier ''' self.id = id self.sensorState = "DARK" self.sensorType = "LightSensor" self.triggerLevel = 500 self.brightCallback = None self.darkCallback = None self.isRegistered = False for key in kwargs: if key == "bright": self.brightCallback = kwargs[key] elif key == "dark": self.darkCallback = kwargs[key] robot = RobotInstance.getRobot() if robot == None: # deferred registering, because Robot not yet created RobotInstance._sensorsToRegister.append(self) else: if self.brightCallback != None or self.darkCallback != None: robot.registerSensor(self) Tools.debug("LightSensor instance with ID " + str(id) + " created")
def __init__(self, id, **kwargs): ''' Creates an infrared sensor at given port. For the Pi2Go the following infrared sensors are used: id = 0: front center; id = 1: front left; id = 2: front right; id = 3: line left; id = 4: line right. The following global constants are defined: IR_CENTER = 0, IR_LEFT = 1, IR_RIGHT = 2, IR_LINE_LEFT = 3, IR_LINE_RIGHT = 4 @param id: sensor identifier ''' self.id = id self.sensorState = "PASSIVATED" self.sensorType = "InfraredSensor" self.activateCallback = None self.passivateCallback = None for key in kwargs: if key == "activated": self.activateCallback = kwargs[key] elif key == "passivated": self.passivateCallback = kwargs[key] robot = RobotInstance.getRobot() if robot == None: # deferred registering, because Robot not yet created RobotInstance._sensorsToRegister.append(self) else: if self.activateCallback != None or self.passivateCallback != None: robot.registerSensor(self) Tools.debug("InfraredSensor instance with ID " + str(id) + " created")
def __init__(self, pin=40): self._checkRobot() self.robot = RobotInstance.getRobot() self._pin = pin self._beeperThread = None Tools.debug("Beeper instance created with beeper at pin: " + str(pin)) GPIO.setup(pin, GPIO.OUT) self.turnOff()
def _readADC(self, channel): robot = RobotInstance.getRobot() try: robot._bus.write_byte(SharedConstants.ADC_I2C_ADDRESS, channel) robot._bus.read_byte(SharedConstants.ADC_I2C_ADDRESS) # ignore first byte data = robot._bus.read_byte(SharedConstants.ADC_I2C_ADDRESS) except: raise Exception("Exception while accessing PCF8591P I2C Expander. channel: " + str(channel)) data = 0 return data
def __init__(self, id): ''' Creates a Led instance with given ID. IDs of the double LEDs: 0: front, 1: left side , 2: rear, 3: right side. The following global constants are defined: LED_FRONT = 0, LED_LEFT = 1, LED_REAR = 2, RED_RIGHT = 3. @param id: the LED identifier ''' self.id = id self.robot = RobotInstance.getRobot() self._blinkerThread = None Tools.debug("Led instance with ID " + str(id) + " created")
def _readADC(self, channel): robot = RobotInstance.getRobot() try: robot._bus.write_byte(SharedConstants.ADC_I2C_ADDRESS, channel) robot._bus.read_byte( SharedConstants.ADC_I2C_ADDRESS) # ignore first byte data = robot._bus.read_byte(SharedConstants.ADC_I2C_ADDRESS) except: raise Exception( "Exception while accessing PCF8591P I2C Expander. channel: " + str(channel)) data = 0 return data
def getValue(self): ''' Returns the current intensity value (0..255). @return: the measured light intensity @rtype: int ''' self._checkRobot() nb = self.id if nb == 0: nb = 1 elif nb == 1: nb = 0 robot = RobotInstance.getRobot() return robot.analogExtender.readADC(nb)
def __init__(self, id, home, inc): ''' Creates a servo motor instance and sets it at home position. For most servos: home = 300, inc = 2 @param id: the id of the motor (0..3) @param home: the PWM duty cycle for the home position (0..4095) @param inc: the increment factor (inc_duty/inc_position) ''' self.id = id self.home = home self.inc = inc self._checkRobot() self.robot = RobotInstance.getRobot() self.robot.pwm.setDuty(SharedConstants.SERVO_0 + self.id, home) Tools.debug("ServoMotor instance created")
def getValue(self): ''' Returns the current intensity value (0..1000). @return: the measured light intensity @rtype: int ''' self._checkRobot() Tools.delay(1) nb = self.id if nb == 0: nb = 1 elif nb == 1: nb = 0 robot = RobotInstance.getRobot() return int(self._readADC(nb) / 255.0 * 1000 + 0.5)
def __init__(self, **kwargs): ''' Creates a sensor instance. ''' self.sensorState = "FAR" self.sensorType = "UltrasonicSensor" self.triggerLevel = 20 self.nearCallback = None self.farCallback = None for key in kwargs: if key == "near": self.nearCallback = kwargs[key] elif key == "far": self.farCallback = kwargs[key] robot = RobotInstance.getRobot() if robot == None: # deferred registering, because Robot not yet created RobotInstance._sensorsToRegister.append(self) else: if self.nearCallback != None or self.farCallback != None: robot.registerSensor(self) Tools.debug("UltrasonicSensor instance created")
def __init__(self): ''' Creates a display instance either from class Display4tronix or DisplayDidel. Because the 4tronix display is multiplexed (one digit shown after the other, a display thread is used to display all 4 digits in a rapid succession. ''' self._checkRobot() robot = RobotInstance.getRobot() self.text = "" self.pos = 0 self.dp = [0, 0, 0, 0] if robot.displayType == "4tronix": Display._myInstance = Disp4tronix() self.available = True elif robot.displayType == "didel": Display._myInstance = DgTell() self.available = True elif robot.displayType == "didel1": Display._myInstance = DgTell1() self.available = True else: self.available = False
def setColor(self, *args): ''' Sets the RGB color value of the two LEDs with current ID. @param args list of [red, green, blue] RGB color components 0..255 or three color integers 0..255 ''' self._checkRobot() if len(args) == 1 and type(args[0]) == list: red = int(args[0][0] / 255.0 * 4095) green = int(args[0][1] / 255.0 * 4095) blue = int(args[0][2] / 255.0 * 4095) elif len(args) == 3: red = int(args[0] / 255.0 * 4095) green = int(args[1] / 255.0 * 4095) blue = int(args[2] / 255.0 * 4095) else: raise ValueError("Illegal param in setColor()") id = (self.id + 3) % 4 robot = RobotInstance.getRobot() robot.ledPWM.setPWM(3 * id, 0, blue) robot.ledPWM.setPWM(3 * id + 1, 0, green) robot.ledPWM.setPWM(3 * id + 2, 0, red)
def _checkRobot(self): if RobotInstance.getRobot() == None: raise Exception("Create Robot instance first")
def isRightHit(): robot = RobotInstance.getRobot() if robot != None: return robot.isRightHit() return False
def isEscapeHit(): robot = RobotInstance.getRobot() if robot != None: return robot.isEscapeHit() return False
def isButtonHit(): robot = RobotInstance.getRobot() if robot != None: return robot.isButtonHit() return False