Cant use NXT Ultrasonic

Hi!

I tried the “Stay on table”-Bot and it works.
Now I wanna use the NXT Ultrasonic Sensor instead of the LegoInfraRed.

I found an Example how to use it in /home/pi/Dexter/BrickPi3/Software/Python/Examples,
but no Class in /home/pi/Ae/Roboter/Interface for it.

I tried it myself by modifiing the EV3-File, here’s my result:

#!/usr/bin/python

class lego infrared sensor

file: LegoInfraRedSensor.py

Author: Th. Kaffka, Cologne, Germany

Date: 21.10.2020

modifiziert von Andreas Ehrhard

11.07.2023

import brickpi3

BP = brickpi3.BrickPi3()

class NXTUltrasonicSensor:

def init(self, port):
self.port = port
BP.set_sensor_type(port, BP.SENSOR_TYPE.NXT_ULTRASONIC)

def del(self):
pass

def getPort(self):
return self.port

def getDistance(self):
return BP.get_sensor(self.port)

My Problem:
When I started the modified Script (appended at the End) of the “Stay on table”-Bot with the NXT-Sensor instead of the EV3-Sensor, I get the following Error:

%Run StayOnTable_Ehr.py
Start
Traceback (most recent call last):
File “/home/pi/Ae/Roboter/Modelle/13_StayOnTable/StayOnTable_Ehr.py”, line 75, in
dist = us.getDistance()
File “/home/pi/Ae/Roboter/Interface/NXTUltrasonicSensor.py”, line 74, in getDistance
return BP.get_sensor(self.port)
File “/usr/local/lib/python3.5/dist-packages/brickpi3-0.0.0-py3.5.egg/brickpi3.py”, line 707, in get_sensor
raise SensorError(“get_sensor error: Invalid sensor data”)
brickpi3.SensorError: get_sensor error: Invalid sensor data

What is wrong?
Where can I find the other Classes for the older Sensors?

2 Likes

??? Why are some Words much greater than others???
(This is not important, but …)

1 Like

The words change their character based on the “characters” surrounding them.

Things like asterisks, (*), hash marks, (#), and others can be “magic” and cause the formatting to change.

If you want to create a “block of unformatted text”, sometimes called a “code block”, you start it with three back-ticks, (```), on a line by themselves, and then add three more after the last line in your code block.

Let me snip your text and see what I can do with it:

I put three back-ticks before the “#!/usr/bin/python” part, and three more back-ticks on the line directly after the “brickpi3.SensorError: get_sensor error: Invalid sensor data” line.

Another formatting tip:
If you want to use a “magical” character, (things like “#”, “&”, “*”, “`”, (or others I don’t remember), as actual printable characters you must “escape” them with the “backslash” (\).  In order to show the backslash, I have to type it twice (\\) so that it “escapes” itself and is shown.

So, now let’s look at that code. . .

#!/usr/bin/python

class lego infrared sensor
file: LegoInfraRedSensor.py
Author: Th. Kaffka, Cologne, Germany
Date: 21.10.2020
modifiziert von Andreas Ehrhard
11.07.2023
import brickpi3

BP = brickpi3.BrickPi3()

class NXTUltrasonicSensor:

def init(self, port):
self.port = port
BP.set_sensor_type(port, BP.SENSOR_TYPE.NXT_ULTRASONIC)

def del(self):
pass

def getPort(self):
return self.port

def getDistance(self):
return BP.get_sensor(self.port)

Which seems to result in:

%Run StayOnTable_Ehr.py
Start

Traceback (most recent call last):
File “/home/pi/Ae/Roboter/Modelle/13_StayOnTable/StayOnTable_Ehr.py”, line 75, in
dist = us.getDistance()
File “/home/pi/Ae/Roboter/Interface/NXTUltrasonicSensor.py”, line 74, in getDistance
return BP.get_sensor(self.port)
File “/usr/local/lib/python3.5/dist-packages/brickpi3-0.0.0-py3.5.egg/brickpi3.py”, line 707, in get_sensor
raise SensorError(“get_sensor error: Invalid sensor data”)
brickpi3.SensorError: get_sensor error: Invalid sensor data

There!

Now it’s readable. . . .

Jump to the next message. . . .

2 Likes

First things first regarding solving your problem:

You need to tell us:

  1. What you want to do.
    (You already did that.)

  2. Why doing it that way is important.
    (Maybe a different sensor will work better?)

  3. What system, hardware, and operating system (and version!), you are using.

  4. Are there any particular restrictions or constraints that force you to do it THIS way instead of a different way?

    • Is it a school project?
    • Are there any specific requirements?
    • Is it limited to “parts on hand”?
    • Is there anything else we need to know to help you with this?

Once we have a thorough understanding of what you want and what you have, we can begin to try to help.

:+1:

2 Likes

Thank you for the answer!
I try to be more exactly:

  1. It would be wonderful to use all the NXT-sensors and motors with the BrickPi3.

  2. It is important, because I have 15 old NXT-Sets at my school. Most of the Controller are brokenand I wanna use it sustainable with python instead of throwing them away.

  3. I use BrickPi3 with an Raspberry Pi 3 Model B ARM-Cortex-A 53 4x 1,2 GHz, 1 GB RAM.
    The Image is the one from your homepage:
    https://dexteros.s3-us-west-1.amazonaws.com/raspbian4robots/2019.04.05_Raspbian_For_Robots_by_Dexter_Industries_Stretch.zip
    I use the VN-Connestion from an Win 10-PC.

  4. It is a school-Project. I’m an Informatik-Teacher and want to show how to use the old NXT-Hardware instead of throwing it away.
    All I need is a Class for each Sensortype of the NXT-Education-Set.
    On your homepage (see How To Program the LEGO Robot BrickPi3 in Python Programming Language) is written, that the NXT-Sensor are supported. That was one of the reasons to buy som BrickPi (3 at the moment) to test it.

2 Likes

Thank you!
I thought it was a good idea to copy and paste the exact code …

2 Likes

Interesting!

An excellent project idea! :+1:  Teaching students how to re-use things is a good thing to teach.

If I might ask, where are you from?

Unfortunately, I am not an expert in the Brick Pi, as I don’t have one and and my experience with the NXT framework is minimal.

I am going to ask @cleoqc to step in and see if she has any ideas.

Since the Brick Pi is relatively old, (as in decades, maybe?), you might become the reigning expert on this!

I will help as much as I can.

3 Likes