3

The code I'm using is:

#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, MoveSteering, OUTPUT_B, OUTPUT_C, SpeedDPS
from ev3dev2.sensor.lego import InfraredSensor
from sys import stderr
from time import sleep

motorB = LargeMotor(OUTPUT_B)
steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
ir = InfraredSensor()

wf = 1
motorB.position = 0
steer_pair.on(steering=0, speed=SpeedDPS(265))
while motorB.position*0.612 < 640:
    if ir.proximity * 0.7 < 23:
        motorB.position = 0
    print(motorB.position*0.612, ir.proximity * 0.7, file = stderr)
    sleep(0.1)
     
steer = 28
rots = -1
steer_pair.on_for_rotations(steering=0, speed=25, rotations=0.5*wf)
steer_pair.on_for_rotations(steering=steer, speed=15, rotations=rots*wf)
steer_pair.on_for_rotations(steering=-steer, speed=15, rotations=rots*wf)
steer_pair.on_for_rotations(steering=0, speed=25, rotations=0.7*wf)

When I run the code I get the following error:

Traceback (most recent call last):
  File "/home/robot/part3/self_park_ir.py", line 9, in <module>
    ir = InfraredSensor()
  File "/usr/lib/python3/dist-packages/ev3dev2/sensor/lego.py", line 838, in __init__
    super(InfraredSensor, self).__init__(address, name_pattern, name_exact, driver_name='lego-ev3-ir', **kwargs)
  File "/usr/lib/python3/dist-packages/ev3dev2/sensor/__init__.py", line 78, in __init__
    super(Sensor, self).__init__(self.SYSTEM_CLASS_NAME, name_pattern, name_exact, **kwargs)
  File "/usr/lib/python3/dist-packages/ev3dev2/__init__.py", line 223, in __init__
    chain_exception(DeviceNotFound("%s is not connected." % self), None)
  File "/usr/lib/python3/dist-packages/ev3dev2/__init__.py", line 54, in chain_exception
    raise exception from cause
ev3dev2.DeviceNotFound: InfraredSensor is not connected.

I have tried to use different ports and wires, as this was the fix for another problem I had, but that did not work this time. Anyone know how to fix this?

2 Answers2

0


I think you should assign an Input port for infrared sensor like this:

from ev3dev2.sensor import INPUT 1
ir = InfraredSensor(INPUT 1)

0

Also, some hardware tips for accuracy.

Don't 'Hot Plug' any sensors

  • This is where you plug it in once the device is already on.

Ensure you import the right modules

  • Make sure you have the shebang line and the right modules imported

Make sure your EV3DEV code is up to date with the new EV3DEV 2 code and you have the correct OS image flashed onto the microSD card.

  • Otherwise, the code won't run smoothly
SamTheProgrammer
  • 1,051
  • 1
  • 10
  • 28