toit:无法导入`hc_sr04`

发布于 2025-02-03 06:20:09 字数 1889 浏览 2 评论 0原文

我无法在我的TOIT代码中导入HC_SR04。我收到错误:for前缀'rmt'找不到的软件包。如何修复rmt导入问题?

我已经安装了HC_SR04软件包。

$ toit pkg install github.com/lask/[email protected]

我尝试在我的代码中导入它:main.toit

import hc_sr04


main:
  print "Start"

在运行它时,我会收到错误:for prefix'rmt'找不到

$ toit run --device <UUID> main.toit
<pkg:toit-hc-sr04>/driver.toit:1:8: error: Package for prefix 'rmt' not found
import rmt
       ^~~
<pkg:toit-hc-sr04>/driver.toit:41:14: error: Unresolved type: 'Channel'
  echo_ /rmt.Channel
             ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:42:17: error: Unresolved type: 'Channel'
  trigger_ /rmt.Channel
                ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:45:21: error: Unresolved type: 'Signals'
  rmt_signals_ /rmt.Signals
                    ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:56:20: error: Unresolved identifier: 'Channel'
    trigger_ = rmt.Channel trigger --output --idle_level=0
                   ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:57:17: error: Unresolved identifier: 'Channel'
    echo_ = rmt.Channel echo --input
                ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:62:24: error: Unresolved identifier: 'Signals'
    rmt_signals_ = rmt.Signals 1
                       ^~~~~~~
Compilation failed.

$ toit version
+---------+------------+
| VERSION |    DATE    |
+---------+------------+
| v1.20.1 | 2022-05-03 |
+---------+------------+
  • package.yaml < /代码>
dependencies:
  hc_sr04:
    url: github.com/lask/toit-hc-sr04
    version: ^2.0.0

I cannot import hc_sr04 in my Toit code. I get the error: Package for prefix 'rmt' not found. How can I fix the rmt import issue?

I have installed the hc_sr04 package.

$ toit pkg install github.com/lask/[email protected]

I try importing it in my code: main.toit

import hc_sr04


main:
  print "Start"

When I run it, I get the error: Package for prefix 'rmt' not found

$ toit run --device <UUID> main.toit
<pkg:toit-hc-sr04>/driver.toit:1:8: error: Package for prefix 'rmt' not found
import rmt
       ^~~
<pkg:toit-hc-sr04>/driver.toit:41:14: error: Unresolved type: 'Channel'
  echo_ /rmt.Channel
             ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:42:17: error: Unresolved type: 'Channel'
  trigger_ /rmt.Channel
                ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:45:21: error: Unresolved type: 'Signals'
  rmt_signals_ /rmt.Signals
                    ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:56:20: error: Unresolved identifier: 'Channel'
    trigger_ = rmt.Channel trigger --output --idle_level=0
                   ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:57:17: error: Unresolved identifier: 'Channel'
    echo_ = rmt.Channel echo --input
                ^~~~~~~
<pkg:toit-hc-sr04>/driver.toit:62:24: error: Unresolved identifier: 'Signals'
    rmt_signals_ = rmt.Signals 1
                       ^~~~~~~
Compilation failed.

$ toit version
+---------+------------+
| VERSION |    DATE    |
+---------+------------+
| v1.20.1 | 2022-05-03 |
+---------+------------+
  • package.yaml
dependencies:
  hc_sr04:
    url: github.com/lask/toit-hc-sr04
    version: ^2.0.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光 2025-02-10 06:20:09

这似乎是软件包管理器中的错误。

HC-SR04软件包取决于SDK环境^2.0.0-Alpha.1。但是,您正在使用v1.20.1运行。

从理论上讲,软件包管理器不应给您该包,而是一个没有这种环境要求的较旧版本。也就是说,看起来较旧的HC-SR04软件包也需要RMT(ESP32的遥控外围)。

如何修复:

我最近这样做是一种练习:

// Distributed under BSD0. (see my profile).
import gpio

TRIGGER ::= 21  // Change to your trigger pin.
ECHO ::= 22  // Change to your echo pin.

measure_distance trigger echo:
  trigger_start := Time.monotonic_us
  trigger.set 1
  while Time.monotonic_us < trigger_start + 10:
    // Do nothing while waiting for the 10us.
  trigger.set 0

  while echo.get != 1: null
  echo_start := Time.monotonic_us
  while echo.get == 1: null
  echo_end := Time.monotonic_us
  diff := echo_end - echo_start
  return diff / 58

main:
  trigger := gpio.Pin TRIGGER --output
  echo := gpio.Pin ECHO --input
  while true:
    print "measured $(measure_distance trigger echo)cm"
    sleep --ms=50

此代码不如基于RMT的代码那么精确,但这也不错。

This seems to be a bug in the package manager.

The hc-sr04 package depends on an sdk environment ^2.0.0-alpha.1. However, you are running with v1.20.1.

In theory, the package manager should not have given you that package, but an older version that doesn't have this environment requirement. That said, it looks like older hc-sr04 packages also need the RMT (ESP32's remote-control peripheral).

How to fix:

I recently did this as an exercise:

// Distributed under BSD0. (see my profile).
import gpio

TRIGGER ::= 21  // Change to your trigger pin.
ECHO ::= 22  // Change to your echo pin.

measure_distance trigger echo:
  trigger_start := Time.monotonic_us
  trigger.set 1
  while Time.monotonic_us < trigger_start + 10:
    // Do nothing while waiting for the 10us.
  trigger.set 0

  while echo.get != 1: null
  echo_start := Time.monotonic_us
  while echo.get == 1: null
  echo_end := Time.monotonic_us
  diff := echo_end - echo_start
  return diff / 58

main:
  trigger := gpio.Pin TRIGGER --output
  echo := gpio.Pin ECHO --input
  while true:
    print "measured $(measure_distance trigger echo)cm"
    sleep --ms=50

This code is not as precise as the RMT based code, but it's not bad either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文