NXT-Ultrasonic_Sensor.py returning Invalid sensor data errors


#1

Hi there, having no luck getting my NXT (2) ultrasonic to work with a Raspi3 & BrickPi3. Have made sure it works with original Lego Mindstorms NxT microcomputer - so I know the cable & sensor are working. Also have tested the BrickPi3 with the NXT motors, touch & color sensors - they work fine. Have tried the NXT Ultrasonic sensor in all ports (while updating relevant code) using your Python & C examples - but no data being returned. I’ve tried adding a time delay (20secs) between setting the sensor type & getting the sensor data, but to no avail! - any ideas would be appreciated.


#2

Ultrasonic Sensors can be troublesome!

So we know that the Ultrasonic​ Sensor and cable works, and the ports work. Which leaves most likely an issue with your code or firmware. My bet is your code and the example code. @Matt has told me to try waiting a few seconds after the initializing the ports with the command: BP.set_sensor_type(BP.PORT_1, BP.SENSOR_TYPE.NXT_ULTRASONIC) sleep for maybe 10 seconds and then get sensor readings. The completed code looks like this:

import brickpi3
import time
BP.set_sensor_type(BP.PORT_1, BP.SENSOR_TYPE.NXT_ULTRASONIC)
time.sleep(10)

try:
    while True:
        try:
            print(BP.get_sensor(BP.PORT_1))
        except: 
            print('error')
        time.sleep(0.1) # slow down a little

except KeyboardInterrupt:
    BP.reset_all()

That should work. Of course 10 seconds is overkill, I just wanted to rule out any possibility that the sensor wasn’t ready. If that works you could experiment with less time, like maybe a few seconds.

If that doesn’t work I’d update your firmware and software. There should be a icon on the desktop that opens Di_update. Select your robot, update the firmware, and update software. If it does work, it wouldn’t hurt to do it anyways, because Dexter Ind has been adding features and big fixes so.


#3

Hi, updated the firm & software, and retried with delays - all motors, touch & colour sensor still working fine, but the ultrasonic refuses to return data - thinking of attaching a SunFounder ultrasonic instead (as I already have one & cant really justify £25 for a new EV3 ultrasonic to replace the nxt ultra as not sure it would function :()
BrickPi3 Troubleshooting Script log

Checking for hardware, and checking hardware and firmware version.

Manufacturer : Dexter Industries
Board : BrickPi3
Serial Number : A137D784514D32384E202020FF0F2A2F
Hardware version: 3.2.1
Firmware version: 1.4.3
Battery voltage : 10.085
9v voltage : 9.009
5v voltage : 5.316
3.3v voltage : 3.272
V 7.2

This version of Raspbian was modified by Dexter Industries on the Jessie Raspbian Build.
This version was updated on 2017.06.12
#############
Start: Fri 21 Jul 14:11:52 BST 2017
End: Fri 21 Jul 14:35:39 BST 2017


#4

Did you try running this example program? It looks like @graykevinb’s code should work (I made a couple minor changes). If the sensor isn’t configured yet, get_sensor will raise an exception that needs to be dealt with (which is why you need try: except: ). If you don’t want to use try: except: and you still want the program to run, you will indeed need a delay between set_sensor_type and get_sensor. The EV3 sensors can take several seconds to configure, but any other sensor should be configured within maybe 100ms.


#5

Hi Matt, yep, that’s the program I’ve been testing with. TBH this was supposed to be an intermediate test (as I already had the NXT ultra), before moving onto hard wiring extra sensors either via the BrickPi3 grove connector, or more likely directly wiring to the GPIO connector.


#6

When you run that example, what gets printed in the terminal? Can you paste the text or post a screen-shot?

Can you also try using the sensor in a different port?


#7

That looks like the standard example… here’s the output, rows & rows of:
get_sensor error: Invalid sensor data
get_sensor error: Invalid sensor data
get_sensor error: Invalid sensor data
get_sensor error: Invalid sensor data
^C[1, 28, 15, 0, 0, 0]
I’ve tried all ports, & all permutations of power supplies…


#8

You changed the port number on lines 28 and 37? Did you try another cable? Do you have another NXT ultrasonic sensor to try? Can you re-try the ultrasonic sensor with the NXT?

I just ran the example, and it’s working great for me.


#9

Yep, changed both lines, different cables, dont have another NXT ultrasonic sensor, still works on the NXT microcomputer. Also tried the NXT light sensor python scripts - works flawlessly across different ports & different cables.
Couple of extra points, I can hear the NXT ultra ‘buzzing’ (albeit only when held on my ear!) when I run on the BrickPi as well as the NXT - so the BrickPi’s obviously doing something!
Another point is the NXT is only picking up distances from a minimum of 4cms - dunno if that’s an issue? Maybe the Brick’s expecting so extra initialisation…
Could be time to get my soldering iron out & attach my SunFounder ultrasonic to the Bricks GPIO…


#10

Another point is the NXT is only picking up distances from a minimum of 4cms - dunno if that’s an issue?

AFAIK , the range is 6cm…255cm, specifity +/- 3cm. That matches both to my experience and also to the specs in Wikipedia (German):

Ultraschallsensor (Bild) Er kann den Abstand zwischen sich und einem Objekt messen. Der Messbereich liegt zwischen 6 cm und 255 cm.
Ref.: https://de.wikipedia.org/wiki/Lego_Mindstorms_NXT


#11

Some success :), but in a weird way - I can retrieve sensor data, but only by moving to another app (eg fileman or web browser) & putting some pressure on the cpu! So when I run the following code:

# Hardware: Connect an NXT ultrasonic sensor to BrickPi3 Port 1.
# 
# Results:  When you run this program, you should see the distance in CM.

from __future__ import print_function # use python 3 syntax but make it compatible with python 2
from __future__ import division       #                           ''

import time     # import the time library for the sleep function
import brickpi3 # import the BrickPi3 drivers

BP = brickpi3.BrickPi3() # Create an instance of the BrickPi3 class. BP will be the BrickPi3 object.

BP.reset_all()
time.sleep(2)
port2use = BP.PORT_1
BP.set_sensor_type(port2use, BP.SENSOR_TYPE.NXT_ULTRASONIC)
time.sleep(2)

def milli():
	return time.time()

millis = milli()
errcount = 0
delay = 0.02
try:
    while True:
        try:
            value = BP.get_sensor(port2use)
            print( value, 'cm ', delay, 'ms delay ', errcount, ' errors since ',  round(milli() - millis, 2), 'sec ')                       # print the distance in CM
            millis =  milli()
            errcount = 0
        except brickpi3.SensorError as error:
			#delay += 0.005
			errcount += 1
			error = str(error)
			if errcount == 1 or error != "get_sensor error: Invalid sensor data":
				print( error )
        
        time.sleep(delay)  # delay sometime(!) to reduce the Raspberry Pi CPU load.

except KeyboardInterrupt: # except the program gets interrupted by Ctrl+C on the keyboard.
    BP.reset_all()        # Unconfigure the sensors, disable the motors, and restore the LED to the control of the BrickPi3 firmware.

I receive:
[1, 28, 15, 0, 0, 0]
get_sensor error: Invalid sensor data
17 cm 0.02 ms delay 306 errors since 6.23 sec
get_sensor error: Invalid sensor data
11 cm 0.02 ms delay 88 errors since 1.81 sec
11 cm 0.02 ms delay 0 errors since 0.02 sec

… by moving onto fileman, going to a large folder (eg python_games) & pressing f5 a few times! or going onto Dexter’s blog & scrolling down a few pages! Changing the delay has little effect on this, & I find it hard to fathom why it would give an initial sensor error in spite of the initial 2x2 seconds delay…