Can't control Megapi via raspberry pi and python


#1

Hi Dr Roboto,

I want to use raspberry pi 4B to control mblock Orion. But I can’t control either DC motor and get reading from ultrasonics sensor.

I have followed these materials: https://github.com/Makeblock-official/PythonForMegaPi

Here is what I have done:

  1. download Arduino and makeblock library

  2. move makeblock library into Arduino and upload the firmware to the board
    (i tested with makeblock app on my iphone and confirm it is good)

  3. in /boot/cmdline.txt, i removed “console = serial0, 115200”
    but I kept serial port: enable in raspberry pi configuration
    (if I do both the top two actions, I will not have /dev/ttyAMA0)

  4. according to /boot/overlays/README, I added line “dtoverlay=disable-bt” to disable bluetooth

  5. i reboot raspberry pi and sudo systemctl disable hciuart

  6. I installed megapi

  7. sudo chmod a+rw /dev/ttyAMA0
    sudo chmod a+rw /dev/ttyUSB0
    (to let megapi be able to access it in step 8)

  8. In python:

    from megapi import *
    Version:3.7.3 (default, Jul 25 2020, 13:03:44)
    [GCC 8.3.0]

    bot=MegaPi()
    init MegaPi

    bot.start(’/dev/ttyUSB0’)
    <megapi_python3.mSerial object at 0xb6670030>
    (I have the beep sound from makeblock Orion board)

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

    sleep(2)
    bot.ultrasonicSensorRead(3, onRead)
    [Errno 9] Bad file descriptor
    (I plugged my ultrasonics sensor to port 3)

  9. I found some similar issue here: Megapi python raspberry pi 3B Problem,Please Help~

    but I don’t know whether it applies to raspberry 4B or not.

Could you help me to resolve this issue please?

Thank you.


#2

Let me start by saying I have no experience at all with a RaspberryPi and I just own my mBot (mCore) for like a week. But maybe I help you with your problem in another way.
What is your end goal here? Do you wirelessly wanna communicate/control your bot? Because there are other options as well which may be more suitable.
For example this weekend I succesfully (after completely Oscilloscoping the Sh** out of all the hardware components) connected a NodeMCU v3 ESP8266 to the mCore and I can now read/write serial data over WiFi to my mBot.

But back to your problem. As I said I have no knowledge about RaspberryPi but am I correct when I say you can run python scripts from it? And are you able to install packages and such things? In other words does it support full/normal python? Have you tried running your python code from a Pc (which OS are you using?)?

By some Googling I also found this “easy” tutorial: Connect RaspberryPi to Arduino Uno. The mOrion board is just like the mCore based on an Arduino Uno so maybe you have better luck searching with those terms.

Personally I suggest to abandon the entire Makeblock firmware if you are doing a project like this. If you are a beginner and don’t understand all the code and can’t get it to work it’s better to start with some easy code written by yourself. The code you use is great and has many applications but if you don’t know what you are doing just start simple.

Buttttt please tell me something more about your project and your end goal. Because right now I’m just blabbering about and making up stories which might not be applicable to your situation


#3

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