It just needs the serial port. Change the start call to;
bot.start(’/dev/ttyS0’)
Does MegaPi can connect Raspberry Pi with USB cable?
Change the start call to;
bot.start(’/dev/ttyS0’)
Still not work…
init MegaPi
<megapi.mSerial instance at 0x76792bc0>
Traceback (most recent call last):
File “encoderMotorMove.py”, line 14, in
bot.start(’/dev/ttyS0’)
File “/usr/local/lib/python2.7/dist-packages/megapi.py”, line 83, in start
self.device.start(port)
File “/usr/local/lib/python2.7/dist-packages/megapi.py”, line 15, in start
self.ser = serial.Serial(port,115200,timeout=10)
File “/usr/lib/python2.7/dist-packages/serial/serialutil.py”, line 261, in init
self.open()
File “/usr/lib/python2.7/dist-packages/serial/serialposix.py”, line 278, in open
raise SerialException(“could not open port %s: %s” % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyS0: [Errno 2] No such file or directory: ‘/dev/ttyS0’
from the command line;
"sudo nano /boot/config.txt"
add: "enable_uart=1"
or more likely change the existing: “enable_uart=0” to a 1
Ctrl+X / Yes to Save.
“reboot”
After reboot the serial port should be enabled on that port.
Thank you! I use your method, not error anymore.
But the encoderMotorRun api du not work. just not move when the api be called.
My Python code is:
from megapi import *
bot = MegaPi()
bot.start(’/dev/ttyS0’)
while True:
bot.encoderMotorRun(1,100)
the battery is ok. But the encoderMotor did not run.
-
Did you update the firmware on the MegaPi? I just got mine but that’s the first thing step before connecting it up to the raspberryPi. It might already be up-to-date but chances are it’s not, and then you can at least rule having old software on your MegaPi ruled out. If you need help, I can walk you through that… it’s not very difficult when you know where everything is.
-
I see you have the bluetooth module connected. Does the MakeBlock android/iOS app work? Even though you don’t have a “completed” project just pick the Robot Tank Arm and push forward or back to see if everything is connected correctly. (Port 1 maps to the right tank track in their example, so the motor will turn when you push forward/back)
-
Do you see the logs from the MegaPi Python module? Minimally you should see “init MegaPi” and no other lines indicating an error.
Finally, I just wanted to let you know I’ve noticed a bug in the firmware / latest release of MegaPi for Python. It will not effect the call you are trying to make now but you will run into the same issue soon. (like if you try to use the encoder motor functions that have callbacks). My strong suit is developing software, I just started to do this for fun and just got my MegaPi this weekend, I’m debugging the problem now and I can let you know when I fix it. Callbacks aren’t called and most of the time it’ll poop out after 1 serial call to the arduino chip.
I’ve got the MegaPi python module locally now & I’ve identified where the python framework is sending a byte to identify the motor for the callback (which is just the port combined with the #61, this maps to the encoder board in the firmware). Anyhow I send a byte to the functions that require an “extID” and the resulting callback only has 4 bits of the byte. In other words I send MegaPi a 61 through the serial port and it sends back a 13 when it should match what I send. (13 is the 4 right most bits of 61)
I’m hoping it’s something to do with a Raspberry Pi update, since MakeBlock also didn’t account for the change of Serial Port. FYI, you may eventually want to disable Bluetooth on the RaspberryPi so the original port that Makeblock expects is open for it to use (after I fix the bug, if it’s necessary I’ll let you know- the addresses have different baud-rates).
If I can’t fix it in MegaPi for Python then I’ll just look at updating their firmware to account for this. I should have time to look at it today. I’ll let you know when I figure it out, I know how frustrating it can be getting started.
I bet you are still having this issue, use the below firmware for now until MakeBlock fix there official version. Or I finish a better version. I fixed that call for you, it was reading a unsigned int and was off by 1 byte. Looking at their Python/Node libraries that byte is used to determine between the 3 encoder motor functions which aren’t implemented. You can at least use “encoderMotorRun” now with the version below. I wouldn’t even attempt to use the other encoder calls right now from Python, you’ll have a bad time.
Firmware_for_MegaPi_EncoderFix.ino.zip (11.6 KB)
Encoder motors keep running on MegaPi port 1 and port 2
Thank you so much! Everything is ok now~ I look up for the answer from the net, then I found that raspberry pi3B has a bug, But I did not know what the excatly issue is, then I fix it by someone’s experience, The motor start run~
OK, I will upload your firmware to megapi~
I found that the offical userguide has something wrong. For example:http://learn.makeblock.com/en/python-for-megapi/
It told us to upload this firmware which in the picture to megapi:
http://learn.makeblock.com/en/wp-content/uploads/2016/04/firmware.jpg
But this firmware can not build,there’s some compile error, Just like method does not exist.
Then I try another firmware(Firmware_for_MegaPi) to upload. Everything is ok.
I’ve got all the encoder motor functions working with callbacks to python.
The firmware you want in my repo is; “Firmware_For_MegaPi_New”. I still want to figure out what a few mysterious pieces of code do in the encoders before I propose a pull-request with the MakeBlock code.
Cool! I try your Firmware_For_MegaPi_New~ Everything is good now~ the speed param is ok.
* **encoderMotorMove**( port, speed, distance, **def** onFinish )
* **encoderMotorMoveTo**( port, speed, position, **def** onFinish )
These two api have some problem. When the api had been invoked, it not stopped. the callback function onFinish did not work. Please help. Thank you~
I user your new code. Firmware_For_MegaPi_New, from https://github.com/TheCodingYeezus/Makeblock-Libraries
I’ve got a few dead-lines at work today that are super urgent, once I get these apps launched today then later if I have time I’ll update the GITHUB with the directions on how to connect and which calls I’ve verified to have working…
That said; those calls work fine for me. Could you post your python code? I’ll throw you an example back later when I have some time.
my code is very easy.
from megapi import *
def onFinish(v):
print ‘onFinish’
bot = MegaPi()
bot.start(’/dev/ttyS0’)
bot.encoderMotorMove(1,100,100,onFinish)
===================
When I run this py, the encoderMotor run a long time. Did not stop. I expect the encoder motor will stop automatically, beacuse I pass the distance parameter already. And the callback fucntion onFinish did not been called.
Thank you.
I don’t remember changing the MegaPi.py file, just adding a bunch of debugging but I’ll make sure I didn’t. The only other difference is I’m running with BT disabled on the Pi right now (so using /dev/ttyAMA0). I’ll also switch it back to the other port and see if that makes a difference.
Also it’s not necessary per-say, but it’s a smart idea to add at least a small sleep(.1) or so, to give the system a chance to initialize after “start” and before calling any other bot functions.
- Encoder Motor
- encoderMotorMove( port, speed, distance, def onFinish )
-
encoderMotorMoveTo( port, speed, position, def onFinish )
what’s the difference between these two python api?
I try the two method they are the same effect…
Thank you~
Megapi programming - encoder motor wont stop
Hello I do not get the communication between Raspberry pi and megapi.
I have set as described the config.
On the robot I have the current firmware firmware_for_MegaPi_EncoderFix.ino.zip transferred. PC is used to communicate.
If I start the Python script motorRun.py I get the following error message
Hi @roboterpi, were you able to solve your problem. I am not able to use the Ultra Sonic Sensors. The call back function in the examples is not executing. Any help is much appreciated @tec_support
Hello yes the problem solved.
First you have to transfer the Firmware_for_MegaPi.ino to the MegaPi.
Then make the settings as described above on the Raspberry pi.
The next Python command should drive the robot
bot.encoderMotorRun (1.255)
bot.encoderMotorRun (2.255)
@roboterpi - Yes, I was able to run the bot.encoderMotorRun(). However, the functions which have call backs. Like the bot.ultrasonicSensorRead(). I am not able to use it. The callbacks are not working.
I really want to use the Ultrasonic Sensor. I’m using a Raspberry Pi 3 and Python to run the codes. How were you able to read the Ultrasonic Sensor?
@tec_support - If you can guide too.
Hi mizzi,
Actually, MegaPi only provides a interface for Raspberry Pi to make more possibility for geeks, due to the complexity of Raspberry Pi and Python program, I am afraid that we don’t have enough ability to support further except the existed instructions we provided on our Github page. Hope you can figure it out!