def test_touch_sensor(self): self.deviceClientSocketMock.send_command.return_value = 'ev3-ports:in1' self.clientSocketMock.send_command.return_value = True sensor = TouchSensor(INPUT_1) val = sensor.value() self.assertEqual(val, 1) self.assertTrue(isinstance(val, int)) self.assertEqual(len(self.clientSocketMock.mock_calls), 1) fn_name, args, kwargs = self.clientSocketMock.mock_calls[0] self.assertEqual(fn_name, 'send_command') self.assertDictEqual(args[0].serialize(), {'type': 'DataRequest', 'address': 'ev3-ports:in1'}) val2 = sensor.value() self.assertEqual(len(self.clientSocketMock.mock_calls), 1) self.assertEqual(val, val2)
from ev3dev2.sensor.lego import TouchSensor from ev3dev2.sensor.lego import Sensor from ev3dev2.sensor import INPUT_1, INPUT_2 from ev3dev2.display import Display lcd = Display() # Connect Pixy camera pixy = Sensor() # Connect TouchSensor ts = TouchSensor(address=INPUT_1) # Set mode pixy.mode = 'ALL' while not ts.value(): lcd.clear() if pixy.value(0) != 0: # Object with SIG1 detected x = pixy.value(1) y = pixy.value(2) w = pixy.value(3) h = pixy.value(4) dx = int(w / 2) # Half of the width of the rectangle dy = int(h / 2) # Half of the height of the rectangle xb = x + int(w / 2) # X-coordinate of bottom-right corner yb = y - int(h / 2) # Y-coordinate of the bottom-right corner lcd.draw.rectangle((dx, dy, xb, yb), fill='black') lcd.update() for i in range(0, 4): print(pixy.value(i), file=stderr)
potencia_andar_Frente = -40 potencia_andar_navalhada = -90 potencia_andar_Tras = 40 potencia_garra_apanhar = 50 potencia_garra_largar = -50 potencia_esquerda_rodar_1 = -30 potencia_esquerda_rodar_2 = -12 potencia_direita_rodar_1 = 30 potencia_direita_rodar_2 = 12 distancia_detetar_zombie = 24 import time ######################### # Testes # ######################### while True: sensorLuz.mode = 'COL-COLOR' if sensorToque.value() == 1: garra.on_for_rotations(SpeedPercent(potencia_garra_apanhar), rotacoes_dar_navalhada) time.sleep(3) garra.on_for_rotations(SpeedPercent(potencia_garra_largar), rotacoes_dar_navalhada) #print(sensorLuz.color) sensorSom.mode = sensorSom.modes[0] print(sensorSom.distance_centimeters) #print(sensorSom.distance_centimeters)
if i == 2: SPOT = GOAL i = i - 1 hasFoundSpot = False straightLine = 0 goRight=True bookTurns=5 while not hasFoundSpot: currentColor = cl.value() if currentColor != C_BLACK and currentColor != C_NO_COLOR and currentColor != C_WHITE and currentColor == SPOT: hasFoundSpot = checkForSpot(SPOT) straightLine = 0 elif hasBumpedBook() or isBookNearBy() or straightLine > 3000 or ts.value(): if bookTurns <= 5: bookTurns = bookTurns + 1 turnRight() RMC.on(-100) LMC.on(-100) sleep(1) turnLeft() else: if backUpThenTurn(SPOT): hasFoundSpot = True straightLine = 0 elif isOnBlackLineOrTable(currentColor): # Turn & go
#!/usr/bin/env python3 from ev3dev2.sensor.lego import ColorSensor, TouchSensor from ev3dev2.led import Leds from sys import stderr from time import sleep cl = ColorSensor() ts = TouchSensor() # Put the color sensor into COL-COLOR mode. cl.mode = 'COL-COLOR' colors = ('unknown', 'black', 'blue', 'green', 'yellow', 'red', 'white', 'brown') while not ts.value(): # Stop program by pressing touch sensor button print(colors[cl.value()], file=stderr) #Sound.speak(colors[cl.value()]).wait() sleep(1) Sound.beep()
#!/usr/bin/env python3 import time import sys from ev3dev2.motor import LargeMotor, OUTPUT_A from ev3dev2.sensor.lego import TouchSensor from ev3dev2.sensor import INPUT_1 m = LargeMotor(OUTPUT_A) t = TouchSensor(INPUT_1) for i in range(10): print(t.value(), file=sys.stderr) print(t.value()) time.sleep(1) m.on_to_position(10, 90) time.sleep(2) m.on_to_position(10, 180) time.sleep(2) m.on_to_position(10, 90) time.sleep(2) print('vse konchilos', file=sys.stderr)