简体   繁体   English

我正在使用 VS Code 用 python 对 EV3 进行编程,但我的红外传感器无法正常工作

[英]I'm using VS Code to program an EV3 with python but I'm having trouble getting my infrared sensor to work

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?有人知道怎么修这个东西吗?


I think you should assign an Input port for infrared sensor like this:我认为您应该为红外传感器分配一个输入端口,如下所示:

from ev3dev2.sensor import INPUT 1从 ev3dev2.sensor 导入输入 1
ir = InfraredSensor(INPUT 1) ir = 红外传感器(输入 1)

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确保您导入了 shebang 行和正确的模块

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.确保您的 EV3DEV 代码与新的 EV3DEV 2 代码保持同步,并且您已将正确的操作系统映像闪存到 microSD 卡上。

  • Otherwise, the code won't run smoothly否则,代码将无法顺利运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM