Import ultrasonic2 causes traceback error


#1

I have the following imports at the top of a MicroPython program:

import cyberpi
import mbot2
import ultrasonic2
import sys

When I upload the program to my mBot2 it immediately displays a traceback error on line 3, the ultrasonic2 import line. When I comment out the line and calls to the sensor, the program runs fine. I need to access the sensor. Is there a bug in the ultrasonic2 library or am I missing something?

Thanks for reading.


#2

The error is ImportError: no module named ‘ultrasonic2’. The full error message is

PYB: fast reboot
MicroPython 6bb454b6-dirty on 2021-03-10; ESP32 module with ESP32
Type "help()" for more information.
>>> free the memory of this function
event: 0 error occured:
Unhandled exception in thread started by <bound_method>
Traceback (most recent call last):
File "events/event_manager.py", line 156, in __event_cb_task
File "events/event_manager.py", line 156, in __event_cb_task
File "events/event_manager.py", line 121, in __event_cb_task
File "run_app.py", line 130, in default_task
File "<string>", line 1, in <module>
File "main7.py", line 3, in <module>
ImportError: no module named 'ultrasonic2'

Not sure why there would be no module named ultrasonic2 as it is present in the documentation and spelled in that manner. Is there a library add step for this module?


#3

What coding application are you using?
Also, include don’t just magically work, so make sure that the include file is downloaded and works with your coding application.
I recommend the mBlock Python editor (include in mBlock 5.4):
image

Thanks for your time,
Best_codes


#4

Hi, thanks for your response. I am using mBlock v5.4.0. You say to make sure the include file is downloaded and works with your application. How do you do that? I did not have to do anything special with the cyberpi, mbot2, or sys modules and they work.


#5

Is the ultrasonic sensor extension added?
Are you using the Python editor?
image


#6

Ultrasonic sensor extension added (in the Blocks editor–although it doesn’t persist across mBlock sessions; I have to re-add it when I open mBlock).

Yes, I am using the mBlock Python editor.


#7

@Best_codes, you originally said “include [imports] don’t just magically work.” Well then, as I asked before, what do you have to do to get them to work? As I originally stated, only the ultrasonic2 import fails. The others work. I’ve tried with the most recent versions of mBlock and mLink. I add the Ultrasonic 2 extension in the Blocks editor every time. Still fails. What is the correct module to access the Ultrasonic 2 sensor’s API?


#8

Click the “Libraries” button:
image
Install the top three libraries:


You can also click the “Help docs for cyberpi” text for more help:
image
This should appear:

Or, you can click the link below:
https://www.yuque.com/makeblock-help-center-en/mcode/cyberpi-api
(It is slow, though).

Thanks for your time,
Best_codes


#9

Oh, just curious, what spellchecker do you use? Is it LanguageTool?


#10

The makeblock and cyberpi libraries are installed by default. mkcloud is irrelevant.


#11

I just know how to spell.


#12

For anyone who runs in to this problem, I received the solution from Makeblock support. The Ultrasonic 2 API is declared in the mbuild module, so that’s what you need to import:

import mbuild
...
dist = mbuild.ultrasonic2.get()

This wasn’t clear in the documentation and Worst_codes troll is useless.


#13

Glad you found out how to do it!
And please don’t bully!
Have a nice day!
@Crackel @CommandeR


#14

Please don’t deliberately waste people’s time when you clearly don’t know the answer.


#15

OK, sorry if I wasted your time. Now please stop.


#16

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.