Raspberry pi + python ultra sonic sensor


#1

I’m trying to control the Ultimate 2.0 robot via Raspberry Pi and Python. At first I tested the ports 1-4 with the encoder motor everything worked fine.

megapi import *

def onRead(v):
print(“distance:”+str(v)+" cm");

if name == ‘main’:
bot = MegaPi()
bot.start()
while 1:
sleep(0.1);
bot.ultrasonicSensorRead(8,onRead);

Now after running the example code for the ultrasonic sensor. I can hear the sensor working for some time. Then it stops and the following message appears:
“device reports readiness to read but returned no data (device disconnected or multiple access on port?)”


#2

Hi, may be you can take a look at this link: https://www.raspberrypi.org/forums/viewtopic.php?t=228315.
By the way, this is kind of DIY program which belong to advanced personal applicatin and beyong our support ability.


#3

This code works for me with ultimate 2 and RPI

from megapi import *
distance = 123

def onRead(v):
global distance
print(“distance:”+str(v)+" cm")
distance = v

bot = MegaPi()

bot.start(’/dev/ttyUSB0’)

sleep(2)

print (“start test”)

while 1:
sleep(2)

bot.ultrasonicSensorRead(6,onRead)
print (distance)
idist = int(distance)
if idist < 12:
    print("time to turn")

UltrasonicRead function will not allow return or global variable
#4

Thank you, for your replies. I had to manually change the settings for UART in /boot/cmdline.txt.


#5

you mean to remove the console entry (like console=serial0,115200).

Enjoy your new capabilities.


#6

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