Python - bot.start not working


#1

I have been trying to get the Raspberry Pi to work with a MegaPi. My code is as follows (followed this tutorial https://github.com/Makeblock-official/PythonForMegaPi):

from megapi import *

bot = MegaPi()
bot.start()

The program returns a version and also says init MegaPi then prompts me to exit. Any ideas?


#2

If that is your total code, then you have not asked it to do anyting.other than initiate.

Look at the examples directory on that github site.
You will see examples such as the following:

bot.motorRun(M1,0);
sleep(1);
while 1:
sleep(1);
bot.motorRun(M1,50);
sleep(1);
bot.motorRun(M1,0);
sleep(1);
bot.motorRun(M1,-50);
sleep(1);
bot.motorRun(M1,0);

My ALLIE works wonderfully.


#3

Sorry, I should of included this in the question. I have at other stages tried to run other things after it (as basic as print statements), however nothing runs after I call bot.start(). This leads me to believe that there is some error I can’t see occurring on that line.


#4

I noticed that you are importing gigglebot. That is for the micro:bit processor.

You should be importing megapi

What raspberry Pi and makeblock product are you using?

Also check out my posting here:
https://forum.makeblock.com/t/me-shield-for-raspberry-pi-makeblock/13588

As it tells you the changes you have to make to the PI code.

The only code that you should have on the makeblock is the default firmware.


#5

Sorry, I wrote that import accidentally. I have amended the question to fix that issue. I am using a Makeblock Ultimate 2.0 and a Raspberry Pi 3B+. In terms of your post my understanding is I don’t need the Me Shield - I have some header connecters connecting to the MegaPi from the Raspberry Pi.


#6

I ran the following code in Thonny:

test program to talk to makeblock

from megapi import *
bot = MegaPi()
bot.start()
print(“thats all”)
bot.motorRun(M1,0)
sleep(2)
print(“thats all”)

======= Here is the output in the shell window =======
Version:3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516]
init MegaPi
<megapi_python3.mSerial object at 0xb6373690>
thats all
thats all

The megapi code for bot.start is:
def start(self, port=’/dev/ttyAMA0’):
self.ser = serial.Serial(port,115200,timeout=10)

If you are not using /dev/ttyAMA0 then you have to supply start with the dev that you are using.

In a terminal window type dmesg | grep tty
That you give you some more info.


#7

I’m pretty sure that I am using /dev/ttyAMA0, as that is what the tutorial specified and I (think) that I disabled Bluetooth. As per this image on the tutorial I have my serial disabled - is this correct? When I get home tonight I will type that command in the terminal window.


#8

#to disable Bluetooth:
#add following lines to /boot/config.txt
dtoverlay=pi3-disable-bt
enable_uart=1

#disable systemd service that initializes Bluetooth connected to UART by runing
sudo systemctl disable hciuart

#that will remove file /etc/systemd/system/multi-user.target.wants/hciuart.service

#disable use of console UART by
sudo raspi-config
select option5, interfacing options
select P6,serial and select NO

OR
#edit file /boot/cmdline.txt
remove portion of line that contains “console=serial0,11500”
ONLY remove that portion of line

reboot

I found the following link very helpful
https://www.raspberrypi.org/documentation/configuration/uart.md


#9

I did most of that via the tutorial. Definitely did this part:


#10

okay lets try another approach (USB)
Standard version of Raspian on the PI with NO changes to files.
Connected ultrasonic sensor to port 5 on my ultimate 2. (megapi)
plug in USB A cable on makeblock.

power up PI (mine is a PI 3B)
ls /dev/tty*
plug in USB cable from makeblock to PI (I used top left USB slot)
ls /dev/tty*

record any changes between two ls commands.
In my case /dev/ttyUSB0 is added. that is the port to use with bot.start

here is my code to check for the ultrasonic (the posting loses indentation)

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(5,onRead)
print (distance)
idist = int(distance)
if idist < 12:
    print("time to turn")


#11

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