Stepper Motor Control with Me Shiel for Raspberry Pi


#1

Hello everyone,

I am a french beginner who is actually working on a robotic project.
I am using a MakeBlock MeOrion connected to my Raspberry Pi 4 with a MakeBlock MeShield for RaspberryPi and two stepper motors MakeBlock 42BYG. I have tested my motors with an Arduino program and it works perfectly but when I try to run the Python code with the megapi library the motors don’t move.

I used this tutorial at the beginning.
Then with raspi-config, I disabled login shell over serial and enabled the serial port hardware but I still have the same problem, my code is running but my motors don’t move. Here is my test code (found it on github [here](https://github.com/Makeblock-official/P … otorRun.py)) :

from megapi import *

if __name__ == '__main__':
    bot = MegaPi()
    bot.start()
    bot.stepperMotorRun(1,0);
    sleep(1);
    while 1:
	    bot.stepperMotorRun(1,-3000);
	    sleep(10);
	    bot.stepperMotorRun(1,0);
	    sleep(4);

Thanks for your help,

Thomas


#2

Bonjour Thomas:

You may find this link of mine helpful:
https://forum.makeblock.com/t/me-shield-for-raspberry-pi-makeblock/13588

Ensure that only the firmware is loaded on the Orion.

I eventually found that the Me Shield had more negatives than positives. I could not add other HATS to the Raspberry Pi. I removed the HAT and used a USB cable between the Makeblock and the Raspberry PI. This has worked with my Ultimate II, starter kit, and Mbots. I shall explain the steps in the next email.


#3

DISABLE ONBOARD BLUETOOTH ON RASPBERRY PI DEVICE.
(TO BE ABLE TO INTERFACE WITH ARDUINO/MAKEBLOCK
Disable Bluetooth on boot
Add following lines to /boot/config.txt
dtoverlay=pi3-disable-bt
enable_uart=1
Disable systemd service that initializez Bluetooth Modems connected by UART.
$ sudo systemctl disable hciuart
removed /etc/systemd/system/multi-user.target.wants/hciuart.service
Disable use of console UART
sudo raspi-config
select option5, interfacing options
option P6, serial, and select NO
OR
edit /boot/cmdline.txt
remove the console entry (like console=serial0,115200)
ONLY remove that portion of the line
Reboot Raspberry Pi device.
$ sudo rebooot
TEST COMMS BETWEEN PI AND MAKEBLOCK
plug USB cable into makebock device

on Raspberry, open terminal window
ls /dev/tty*

plug other end of USB cable into Raspberry
on Raspberry, open terminal window
ls /dev/tty*

The new entry, probably ttyUSB0, is the port you want to use

from megapi import *
bot = MegaPi()
bot.start(’/dev/ttyUSB0’)


#4

Is that the same for megapi pro?


#5

That is an interesting question. I do not believe the megapi pro is available outside of Asia so I cannot test it in North America. Since both are based on the mega2560, then it should be possible.

It would make for a powerful machine. The megapi pro controlling the motors and analog sensors and the Raspberry Pi being the brain, with python, and opencv for computer vision on the camera.


#6

This is of great interest to me. The pro is physically different, it doesn’t sit upon the pi, and all the pictures I can find (and that’s about all I can find) have it connected to the pi through usb, no shield.


#7

The usb cable is the technique that I use to connect my Raspberry Pis (zero Wh, 3B and 3a+) to my mbot, orion/starter and ultimate 2.0 robots


#8

Hey dr-roboto, thank you so much for your answers !

I had already seen your topic on the me shield but your solution didn’t work for me that’s why I posted my own topic :wink:
I already disabled bluetooth and did every step of your second comentary but the problem still persists.
How can I ensure that only the firmware is loaded on the Orion ?

Concerning the USB cable option, as I tested my motors with an arduino program with my computer linked to the Orion with USB cable, I have some noise in this cable and when I use it to link my Raspberry Pi to the Orion one loop of the arduino program is running at the launch and at the end of my python program. Maybe this problem is linked to having only the firmware loaded on the Orion ?

Thank you again for your help !


#9

I still use mblock 3, so for the firmware
choose the Upgrade Firmware by going toConnect->Upgrade Firmware,then Close when it shows Upload Finish


#10

I have done it but my firmware was already up to date and my motors still don’t run !
The problem can’t come from the motors as they’ve been tested with an arduino program and it’s not from the software as the program I used is the official test program from PythonForMegapi.
Maybe it’s because motors are underpowered ?


#11

I power my makeblock and RPI separately.
You can test the makeblock <-> RPI connection with the ultrasonic sensor
from megapi import *
distance = 123

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

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

#bot.motorRun(M1,0)
sleep(2)

print (“start test”)

while 1:
sleep(2)

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

#12

I just tried your code with my raspberry linked to the Orion with a USB cable and nothing happens, it returns 123 as the distance…


#13

I do not understand. This should be easy. First check that the changes on the RPI have been done.
I listed the changed files. for the cmdline.txt I listed what it should look like, what is removed and also list the old file cmdline.txt.old

pi@raspberry1:~ $ tail /boot/config.txt
max_framebuffers=2
[all]
#dtoverlay=vc4-fkms-v3d
start_x=1
gpu_mem=128
dtoverlay=pi3-disable-bt
enable_uart=1
pi@raspberry1:~ $

pi@raspberry1:~ $ cat /boot/cmdline.txt
console=tty1 root=PARTUUID=5e3da3da-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
pi@raspberry1:~ $
pi@raspberry1:~ $ cat /boot/cmdline.txt.old
console=serial0,115200 console=tty1 root=PARTUUID=5e3da3da-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
pi@raspberry1:~ $
sudo systemctl disable hciuart
removes /etc/systemd/system/multi-user.target.wants/hciuart.service

/etc/systemd/system/multi-user.target.wants $ ls
avahi-daemon.service remote-fs.target
console-setup.service rpi-eeprom-update.service
cron.service rsync.service
dhcpcd.service rsyslog.service
dphys-swapfile.service ssh.service
gldriver-test.service sshswitch.service
networking.service triggerhappy.service
nfs-client.target vncserver-x11-serviced.service
raspberrypi-net-mods.service wpa_supplicant.service


#14

I found my makeblock raspberry shield. I connected it to port 5 on the orion and had the ultrasonic on port 3

the following code worked:

from megapi import *
distance = 123

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

bot = MegaPi()
bot.start()

sleep(2)

print (“start test”)

while 1:
sleep(2)
bot.ultrasonicSensorRead(3,onRead)
print (distance)
idist = int(distance)
if idist < 12:
print(“time to turn”)

here is the output

is

Version:3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0]
init MegaPi
<megapi_python3.mSerial object at 0x75ec1f10>
start test
123
123
distance:135.4310302734375 cm
135.4310302734375
distance:134.72413635253906 cm
distance:11.051724433898926 cm
11.051724433898926
time to turn
11.051724433898926distance:19.05172348022461 cm

distance:40.034481048583984 cm
40.034481048583984


#15

Changes have been done for me :

pi@raspberrypi:~ $ tail /boot/config.txt
start_x=1
gpu_mem=128
dtoverlay=pi3-disable-bt
enable_uart=1

pi@raspberrypi:~ $ cat /boot/cmdline.txt
console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

and I had already written those lines of course :
sudo systemctl disable hciuart
removes /etc/systemd/system/multi-user.target.wants/hciuart.service

pi@raspberrypi:/etc/systemd/system/multi-user.target.wants $ ls
avahi-daemon.service remote-fs.target
console-setup.service rpi-eeprom-update.service
cron.service rsync.service
dhcpcd.service rsyslog.service
dphys-swapfile.service ssh.service
gldriver-test.service sshswitch.service
networking.service triggerhappy.service
nfs-client.target vncserver-x11-serviced.service
raspberrypi-net-mods.service wpa_supplicant.service


#16

Now your code for the ultrasonic sensor test is working on my raspberry pi with both me shield and usb cable so no problem with the link between the rpi and the orion. However, the problem is still persisting with the motors and I can’t understand why…


#17

That is good news on something working. You must have separate power to the orion and the RPI. When testing, I plug the orion and the RPI into their AC adapters and put the robot on a box so that the wheels can spin freely.
I looked at your initial email and that is python 2 code. Remove all the ‘;’ semi-colons.for python 3.

I do not have a stepper motor but the orion/starter kit does come with DC motors which attach to the orange plugs. Try those with the following code:

from megapi import *

bot = MegaPi()
bot.start(’/dev/ttyUSB0’)
bot.motorRun(M1,0)
sleep(1)
bot.motorRun(M1,50)
sleep(2)
bot.motorRun(M1,0)
sleep(2)
bot.motorRun(M1,-50)
sleep(2)
bot.motorRun(M1,0)


#18

If it isn’t too much trouble, I have a question for you. Is the steeper command only for the stepper motor and is the encoder command only for the encoder motor?

Motor%20Command


#19

Yes because they are different types of motors.
https://learn.adafruit.com/all-about-stepper-motors/what-is-a-stepper-motor

https://en.wikipedia.org/wiki/Rotary_encoder


#20

Thank you, I’m very grateful for the response.