Controling Megapi via raspberry pi and python


#1

Hi all,
i really want to know how can i communicate Megapi with my raspi i found this( https://github.com/Makeblock-official/PythonForMegaPi ) and try to upload the firmware for Megapi but i saw just error about serial communication.
what should i choose as a Board in arduino ide?
should i connect it only with wiring or can i use also usb cable?
i will be really happy, if someone helps me about this.
Thank you


#2

look at my info here:
All the programming will be done on the raspberry pi and just default software on makeblock. The connection is thru USB

I have done this two different ways.
The first is with the HAT on the PI and RJ25 cable to port 5 (grey) on the Makeblock

The second is with a usb cable between the two boards:

In both cases I could use python on the PI with the following package
https://github.com/Makeblock-official/PythonForMegaPi 4


#3

thank you for your response.
i have another issue during the wiring. if i connect raspberry pi and megapi with special wiring area, which is on Megapi board, than the raspberry cannot power the MegaPi up. I dont know why, but i will connect it vie usb for know. Do you have any idea why this problem occurs?


#4

The raspberry pi (RPI) uses 5V. The megapi requires 6-12v. I use a separate 5v cylinder battery for the RPI.
note: If you use an usb cable between the RPI and megapi, ensure that it is 5 pin so that it works with both power and data. The correct cable will produce a /dev/ttyUSB0 entry on the RPI.


#5

thank you very much. i did everything and its working. One last question. i want to control dc motor and i am using MegaPi Encoder/DC Driver V1 for that and i tried;

from megapi import *

if name == ‘main’:
bot = MegaPi()
bot.start()
bot.motorRun(1,0);
sleep(1);
while 1:
sleep(1);
bot.motorRun(1,50);
sleep(1);
bot.motorRun(1,0);
sleep(1);
bot.motorRun(1,-50);
sleep(1);
bot.motorRun(1,0);

this example code from github and this code;

from megapi import *

if name == ‘main’:
bot = MegaPi()
bot.start()
bot.encoderMotorRun(1,0);
sleep(1);
while 1:
bot.encoderMotorRun(1,-200);
sleep(5);
bot.encoderMotorRun(1,0);
sleep(5);

but i cannot run the dc motor although i have attached external power cable and i tried the connection with digital write example to see the blinking leds. if i summary, the connection is seems to be right with digitalWrite code but if it comes to runing motor, it didnt work at all with my python code. Can you please help me to solve the problem.
Best regards


#6

If you are using a USB cable then
change bot.start()
bot.start(’/dev/ttyUSB0’)

If it is an encoder motor then use the following code:


#7

This worked for me:

from megapi import *
import time

bot = MegaPi()
bot.start(’/dev/ttyUSB0’)

port = 1
speed = 50
while port < 4:
bot.encoderMotorRun( port, speed )
time.sleep(2)
bot.encoderMotorRun( port, 0 )
time.sleep(1)
bot.encoderMotorRun( port, -speed )
time.sleep(2)
bot.encoderMotorRun( port, 0 )
time.sleep(1)
port = port + 1


#8

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