def setUp(self): super().setUp() base.SetInitialGPIOState() with GPIO.write_context(): GPIO.output(REED_LOWER, REED_CLOSED) # Kontakt oben offen GPIO.output(REED_UPPER, REED_OPENED) # Kontakt unten geschlossen self.controller = controlserver.Controller(start_jobs=False)
def runningDark(count, period): i = 0 j = 0 while j < count: for i in range(8): GPIO.output(i, 0) time.sleep(period) GPIO.output(i, 1) time.sleep(period) j = j + 1
def test_CloseDoorByContact(self): ftr = self.CloseDoorTest() with GPIO.write_context(): GPIO.output(REED_LOWER, REED_CLOSED) # Kontakt oben offen GPIO.output(REED_UPPER, REED_OPENED) # Kontakt unten geschlossen self.assertTrue(ftr.WaitForResult(), "Door is closed.") self.assertLess( ftr.GetRuntime(), DOOR_MOVE_UP_TIME, "Door close duration has not been reached due to contact.")
def StopMotor(self, end_state:int = DOOR_NOT_MOVING): """ Schaltet die Releais zur Motorsteuerung so, dass der Motor ausgeht und die Drehrichtung zurückgesetzt wird. :param int end_state: der damit erreichte Status der Tür, in der Regel also :data:`config.DOOR_OPEN` oder :data:`config.DOOR_CLOSED`. """ self.info("Stopping motor.") GPIO.output(MOTOR_ON, RELAIS_OFF) GPIO.output(MOVE_DIR, MOVE_UP) self._SetDoorState(end_state)
def SwitchIndoorLight(self, swon:bool): """ Schaltet die Innenbeleuchtung ein, wenn ``swon`` True ist. (sonst aus). .. seealso:: :meth:`SwitchOutdoorLight` :meth:`IsIndoorLightOn` """ self.light_state_indoor = swon GPIO.output(LIGHT_INDOOR, RELAIS_ON if swon else RELAIS_OFF) self.info("Switched indoor light %s", "on" if swon else "off") self._CallStateChangeHandler()
def brake(self, current_delay, direction): self._stop = False accel_index = min(range(len(self._bra_curve)), key=lambda i: abs(self._bra_curve[i]-current_delay)) logging.debug("braking down within %d steps", accel_index) for step in xrange(accel_index): step_delay = self._bra_curve[accel_index-step] GPIO.output(self.PUL, True) GPIO.output(self.PUL, False) self._steps += 2*(direction-.5)*self._positive if self._steps_per_rev > 0: self._angle += (direction-.5)*(self._positive*720./self._steps_per_rev) self._angle %= 360 time.sleep(step_delay) self.step(accel_index, not direction) self._stop = True
def test_OpenDoorByContact(self): #ctrl.board.door_state = DOOR_CLOSED #with GPIO.write_context(): # GPIO.output(REED_LOWER, REED_CLOSED) ftr = self.OpenDoorTest() with GPIO.write_context(): GPIO.output(REED_LOWER, REED_OPENED) # Kontakt unten offen GPIO.output(REED_UPPER, REED_CLOSED) # Kontakt oben geschlossen self.assertTrue(ftr.WaitForResult(), "Door is open.") self.assertLess( ftr.GetRuntime(), DOOR_MOVE_UP_TIME, "Door open duration has not been reached due to contact.")
def StartMotor(self, direction:int): """ Schaltet die Motorsteuerungsrelais so, dass sich der Motor in die entsprechende Richtung dreht. :param direction: Drehrichung. Der Einfachheit halber wird hier :data:`config.MOVE_UP` bzw. :data:`config.MOVE_DOWN` für die Richtung erwartet und entspricht damit der Bewegungsrichtung der Tür. .. seealso:: :meth:`SyncMoveDoor` :meth:`StopMotor` :meth:`_SetDoorState` """ self.info("Starting motor (%s).", "up" if direction == MOVE_UP else "down") GPIO.output(MOVE_DIR, direction) GPIO.output(MOTOR_ON, RELAIS_ON) self._SetDoorState(DOOR_MOVING_UP if direction == MOVE_UP else DOOR_MOVING_DOWN)
def __init__(self, name, pins, min_angle=-5, max_angle=365, positive=1, vend=4500, vstart=20, skewness=.75, accel_steps=4000, skewnessbra=.9, bra_steps=500): def _accel_velocity(x): """ calculate the acceleration/deceleration velocity in the interval [0,1] """ return (.5-.5*cos(x*pi))*(vend-vstart)+vstart def _accel_skewing(x): """ skew the velocity cosine by a parabolic function """ return pow(x, skewness)/pow(accel_steps, skewness) def _bra_skewing(x): """ skew the velocity cosine by a parabolic function """ return pow(x, skewnessbra)/pow(bra_steps, skewnessbra) self.name = name self.PUL, self.DIR, self.ENBL = pins self._steps_per_rev = 0 self._enabled = True self._angle = 0 self._min_angle = min_angle self._max_angle = max_angle self._steps = 0 self._stop = True self._delay = 1./vend self._positive = positive self._brake_steps = accel_steps for p in pins: GPIO.setup(p, GPIO.OUT) GPIO.output(p, False) self._accel_curve = [ 1./_accel_velocity(_accel_skewing(x)) \ for x in xrange(accel_steps) ] self._bra_curve = [ 1./_accel_velocity(_bra_skewing(x)) \ for x in xrange(bra_steps) ]
def get_distance(): # print("Distance Measurement In Progress") if isinstance(GPIO, MockGPIO): time.sleep(2) return 15 GPIO.setup(TRIG, GPIO.OUT) GPIO.setup(ECHO, GPIO.IN) GPIO.output(TRIG, False) # print("Waiting For Sensor To Settle") time.sleep(1) GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False) while GPIO.input(ECHO) == 0: pulse_start = time.time() while GPIO.input(ECHO) == 1: pulse_end = time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17150 distance = round(distance, 2) print("Distance:", distance, "cm") return distance
def step(self, steps, direction): if steps: delay = 0 logging.debug("INPUT -- %s: actual_step/steps/direction: %d / %d / %d", self.name, self._steps, steps, direction) self._stop = False GPIO.output(self.DIR, direction) for step in xrange(steps): try: step_delay = self._accel_curve[min(step, abs(step-(steps-1)))] except: step_delay = self._delay if self._stop or \ (self._angle <= self._minimum and not direction) or \ (self._angle >= self._maximum and direction): logging.debug("BREAK -- %s: actual_step/steps/direction: %d / %d / %d", self.name, self._steps, steps, direction) self.brake(step_delay, direction) break GPIO.output(self.PUL, True) GPIO.output(self.PUL, False) self._steps += 2*(direction-.5)*self._positive if self._steps_per_rev > 0: self._angle += (direction-.5)*(self._positive*720./self._steps_per_rev) self._angle %= 360 time.sleep(step_delay) logging.debug("END -- %s: actual_step/steps/direction: %d / %d / %d", self.name, self._steps, steps, direction)
def test_DoorOpen(self): ftr = base.Future(self.board.OpenDoor) self.assertTrue(ftr.WaitForExectionStart(1.0), "Future starts within time.") self.assertFalse( self.board.OpenDoor(), "Calling OpenDoor() while door is moving should not be possible!" ) self.assertFalse(ftr.HasResult(), "OpenDoor() is running.") with GPIO.write_context(): GPIO.output(REED_LOWER, REED_OPENED) # unten kein Signal mehr self.assertFalse( self.board.IsDoorClosed(), "Door should not be closed when lower reed is HIGH" ) with GPIO.write_context(): GPIO.output(REED_UPPER, REED_CLOSED) # oben auf LOW ftr.WaitForResult(0.5 + UPPER_REED_OFFSET) self.assertTrue(ftr.HasResult(), "OpenDoor() is finished.") self.assertTrue(_GetSaveState(), "Board state has been saved.") self.assertTrue(self.board.IsDoorOpen(), "Door should be opened when upper reed is LOW") self.assertFalse( self.board.IsDoorClosed(), "Door should not be closed when lower reed is HIGH" ) self.assertTrue(ftr.WaitForResult(), "OpenDoor() succeeded.") self.assertFalse( self.board.OpenDoor(), "Calling OpenDoor() while door is opened shouldn't be possible" ) self.assertFalse(_GetSaveState(), "Board state not saved when operation fails.")
def CloseDoorTest(self): ctrl = self.controller ctrl.board.door_state = DOOR_OPEN with GPIO.write_context(): GPIO.output(REED_UPPER, REED_CLOSED) # Kontakt oben geschlossen GPIO.output(REED_LOWER, REED_OPENED) # Kontakt unten offen ftr = base.Future(ctrl.CloseDoor) self.assertTrue(ftr.WaitForExectionStart(0.5), "Door close command is running.") with self.assertRaises(TimeoutError): ftr.WaitForResult(2) with GPIO.write_context(): motor_on = GPIO.input(MOTOR_ON) move_dir = GPIO.input(MOVE_DIR) self.assertEqual(ctrl.automatic, DOOR_AUTO_OFF, "Door automatic is temporary disabled.") self.assertEqual(motor_on, RELAIS_ON, "Motor is running.") self.assertEqual(move_dir, MOVE_DOWN, "Door is moving down.") self.assertTrue(ctrl.board.IsDoorMoving(), "Door state is 'moving'.") return ftr
def test_DoorClose(self): # Tür auf "Offen" setzen: self.board.door_state = DOOR_OPEN with GPIO.write_context(): GPIO.output(REED_UPPER, REED_CLOSED) GPIO.output(REED_LOWER, REED_OPENED) ftr = base.Future(self.board.CloseDoor) self.assertTrue(ftr.WaitForExectionStart(1.0), "Future starts within time.") self.assertFalse( ftr.HasResult(), "Calling CloseDoor() when door is open should be possible" ) self.assertFalse( self.board.CloseDoor(), "Calling CloseDoor() while door is moving should not be possible" ) with GPIO.write_context(): GPIO.output(REED_UPPER, REED_OPENED) # oben kein Signal mehr self.assertFalse( self.board.IsDoorOpen(), "Door should not be opened when upper reed is HIGH" ) with GPIO.write_context(): GPIO.output(REED_LOWER, REED_CLOSED) # unten auf LOW ftr.WaitForResult(0.5 + LOWER_REED_OFFSET) # anderen Thread ranlassen self.assertTrue(ftr.HasResult(), "CloseDoor() is finished.") self.assertTrue(_GetSaveState(), "Board state has been saved.") self.assertTrue(self.board.IsDoorClosed(), "Door should be closed when lower reed is LOW") self.assertFalse(self.board.IsDoorOpen(), "Door should not be open when upper reed is HIGH") self.assertTrue(ftr.WaitForResult(), "CloseDoor() succeeded.") self.assertFalse( self.board.CloseDoor(), "Calling CloseDoor() while door is open shouldn't be possible." ) self.assertFalse(_GetSaveState(), "Board state not saved when operation fails.")
def SetInitialGPIOState(): """ Setzt den initialen GPIO-Status für die Tests. """ if not hasattr(GPIO, 'allow_write'): raise RuntimeError("Need GPIO dummy for setting initial board state!") with GPIO.write_context(): GPIO.setmode(GPIO.BOARD) GPIO.output(REED_UPPER, REED_OPENED) GPIO.output(REED_LOWER, REED_CLOSED) # Tür geschlossen GPIO.output(SHUTDOWN_BUTTON, 1)
def lightUp(number, period): GPIO.output(number, 1) time.sleep(period) GPIO.output(number, 0)
def enable(self, enabled=True): self._enabled = enabled GPIO.output(self.ENBL, enabled)
def runningLight(count, period): i = 0 j = 0 while j < count: for i in range(8): lightUp(i, period) time.sleep(period) j = j + 1 runningLight(4, 0.5) print("-------------------------------------------------------") i = 0 while i < 8: GPIO.output(i, 1) i = i + 1 def runningDark(count, period): i = 0 j = 0 while j < count: for i in range(8): GPIO.output(i, 0) time.sleep(period) GPIO.output(i, 1) time.sleep(period) j = j + 1 runningDark(3, 0.5)