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/toit-hc-sr04@2.0.0

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
A. Saunders
  • 815
  • 1
  • 6
  • 19

1 Answers1

1

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:

  • either switch to the open-source version (https://github.com/toitlang/jaguar) which already has the RMT, or
  • implement the hc-sr04 driver yourself without using the RMT.

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.

Florian Loitsch
  • 7,698
  • 25
  • 30