Explorer Hat Pro and Raspberry PI - Motor control NOK

Hi,
We’re new to Raspberry Pi and Explorer Hat Pro. We looked pretty much everywhere on the net and couldn’t find any information on how to fix it.
The problem : using the explorer hat library, we are not able to control the motors (forwards or backwards).
If power-supplied by the GND and +5V, each motor is OK, that means wires and motors are OK.
We are able to light on and off the LEDs.
We do not have any new idea on what to check to investigate.
Is there anybody to suggest some test to perform so we can make some progress in our investigation ?
Thanks
NiVeSaCo

I have several Explorer pHats here. I can control my motors via python once I run the one line installer.
So the obvious questions are as follows:
Did you run, curl https://get.pimoroni.com/explorerhat | bash
And how do you have the motors hooked up? A picture of the wiring may help.
What motors are you using, and what power supply are you using?
And could you post the python code your using.

Yes we ran curl…
For the motors we use STS PI robot with its two motors.
For the moment the robot is plugged to a plug. But then we will use a powerbank.
We use python code of sandyjmacdonald. But as the web interface did not work we tried just by using python and typing :
import explorerhat as eh
eh.motor.oneforwards(100) (for example)
Here is a picture of the wiring : I tried to change it without success

Ok, everything looks to be wired OK. I used a wireless keyboard to control mine.
Not sure if you made a typo just here in your post but it should be
eh.motor.one.forwards(100)

I did not made a typo because at first the program was with a dot but it did not work and without it does not said error
Do you have any idea why it does not accept it with the dot ?
Thank you
NiVeSaCo

Try

import explorerhat

explorerhat.motor.one.forwards(100)

here is the answer of python

import explorerhat
Explorer HAT Basic detected…
explorerhat.motor.one.forwards(100)
Traceback (most recent call last):
File “<pyshell#1>”, line 1, in
explorerhat.motor.one.forwards(100)
AttributeError: ‘function’ object has no attribute ‘forwards’

Ok, I’m also confused? I don’t know why your getting that message.
Here is my code. I haven’t run it in a while but it worked OK the last time I did run it.

#!/usr/bin/env python
import os
import usb.core
import usb.util
import explorerhat
import time
import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)  
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)  

x=(2) #shutdown variable
S=(100) #speed default value.
M=(0) # 0 = Not moving, 1 = Moving Forward, 2 = Moving Backward,



USB_VENDOR  = 0x1997 # Rii
USB_PRODUCT = 0x2433 # Mini Wireless Keyboard

USB_IF      = 0 # Interface
USB_TIMEOUT = 5 # Timeout in MS

BTN_LEFT  = 80 # <
BTN_RIGHT = 79 # >
BTN_DOWN  = 81 # V
BTN_UP    = 82 # ^
BTN_STOP  = 44 # Space
BTN_EXIT  = 41 # ESC
BTN_OK    = 40 # OK
BTN_ONE   = 30 # 1
BTN_TWO   = 31 # 2
BTN_THREE = 32 # 3
BTN_FOUR  = 33 # 4
BTN_FIVE  = 34 # 5
BTN_SIX   = 35 # 6
BTN_SEVEN = 36 # 7
BTN_EIGHT = 37 # 8
BTN_NINE  = 38 # 9
BTN_ZERO  = 39 # 0

dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
endpoint = dev[0][(0,0)][0]

if dev.is_kernel_driver_active(USB_IF) is True:
  dev.detach_kernel_driver(USB_IF)

usb.util.claim_interface(dev, USB_IF)

def Shutdown(channel):  
    global x
    x = (0)


GPIO.add_event_detect(5, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

while True:
    control = None
    try:
        control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT)
        print(control)
    except:
        pass

    if control != None:
        if BTN_ONE in control:
          S = (10)

        if BTN_TWO in control:
          S = (20)

        if BTN_THREE in control:
          S = (30)

        if BTN_FOUR in control:
          S = (40)

        if BTN_FIVE in control:
          S = (50)
            
        if BTN_SIX in control:
          S = (60)

        if BTN_SEVEN in control:
          S = (70)

        if BTN_EIGHT in control:
          S = (10)

        if BTN_NINE in control:
          S = (90) 

        if BTN_ZERO in control:
          S = (100)

        if BTN_DOWN in control and M != 1: # Go into reverse if not moving forward, and stop turning if turning.
          explorerhat.motor.backwards(100)
          M = 2
            
        if BTN_UP in control and M != 2: # Go into forward if not moving in reverse, and stop turning if turning.
          explorerhat.motor.forwards(100)
          M = 1
            
        if BTN_LEFT in control and M == 0: # Turn left while stoped
          explorerhat.motor.two.forwards(50)
          explorerhat.motor.one.backwards(50)
            
        if BTN_RIGHT in control and M == 0: # Turn right while stoped
          explorerhat.motor.two.backwards(50)
          explorerhat.motor.one.forwards(50)
            
        if BTN_LEFT in control and M == 1: # Turn left while moving forwards
          explorerhat.motor.two.forwards(100)
          explorerhat.motor.one.forwards(50)
            
        if BTN_RIGHT in control and M == 1: # Turn right while moving forwards
          explorerhat.motor.two.forwards(50)
          explorerhat.motor.one.forwards(100)
            
        if BTN_LEFT in control and M == 2: # Turn left while moving backwards
          explorerhat.motor.two.backwards(100)
          explorerhat.motor.one.backwards(50)
            
        if BTN_RIGHT in control and M == 2: # Turn right while moving backwards
          explorerhat.motor.two.backwards(50)
          explorerhat.motor.one.backwards(100)
            
        if BTN_OK in control: # Stop moving and stop turning 
          explorerhat.motor.stop()
          M = 0
            
        if BTN_EXIT in control:          
          raise SystemExit

        if BTN_STOP in control:
          os.system("sudo shutdown now -P")

    time.sleep(0.02)

    if x == 0:
        os.system("sudo shutdown now -P")
    elif x == 1:
        raise SystemExit

# Last edited on Jun 11th 2018
# run sudo crontab -e
# add
# @reboot sudo python3 /home/pi/RoverKB.py &

i tried with your program by cpy it directly in python but python found an error :

================ RESTART: programstspi1.py ================
Traceback (most recent call last):
File “/home/pi/Documents/programstspi1.py”, line 5, in
import usb.core
ImportError: No module named ‘usb’

It must work if you used it, doesn’t it ? I may have done something wrong…
Thanks for your help
NiVeSaCo

I should have clarified, my code post was just to show that particular command worked for me.
My full code won’t work without installing some other software first, plus it will only work with my particular keyboard, the one I coded it for. Sorry about that.
The USB error is because I did the following before running my python file.
sudo apt-get install libusb-dev
git clone https://github.com/walac/pyusb
cd pyusb
sudo python3 setup.py install

This is what I originally did to get my keyboard setup for use with my Rover.
https://learn.pimoroni.com/tutorial/robots/controlling-your-robot-wireless-keyboard

oh ok ! So you think I have to do it ? As I am new to raspberry pi and do not code in python so well, I do not know what to do. I followed this tutorial : https://www.youtube.com/watch?v=FYo3VUzdWm0 and at the very end theweb interface has an error like in the video that is why I changed app.py used and deleted the dots. The web interface is not in error anymore but it does not work. here is the code I wanted to use before trying with python itself.
import explorerhat as eh
from flask import Flask, render_template

app = Flask(name)

@app.route("/")
@app.route("/")
def update_robot(state=None):
if state == ‘forward’:
eh.motor.onebackwards(100)
eh.motor.twoforwards(100)
if state == ‘back’:
eh.motor.oneforwards(100)
eh.motor.twobackwards(100)
if state == ‘left’:
eh.motor.twostop()
eh.motor.onebackwards(100)
if state == ‘right’:
eh.motor.onestop()
eh.motor.twoforwards(100)
if state == ‘stop’:
eh.motor.onestop()
eh.motor.twostop()
if state == ‘anti-clockwise’:
eh.motor.onebackwards(100)
eh.motor.twobackwards(100)
if state == ‘clockwise’:
eh.motor.oneforwards(100)
eh.motor.twoforwards(100)
template_data = {
‘title’ : state,
}
return render_template(‘main.html’, **template_data)

if name == “main”:
app.run(host=‘192.168.1.20’, port=80)

I personally think the dots need to be there, and then the error you get when they are sorted out.
I’d run the explorer hat installer again, maybe it glitched or something?

Might be time to get Phil @gadgetoid or maybe Sandy @sandyjmacdonald involved.

Are you running the latest Raspbian?

pi@raspberrypi:~ $ curl https://get.pimoroni.com/explorerhat | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 38364 100 38364 0 0 101k 0 --:–:-- --:–:-- --:–:-- 101k

This script will install everything needed to use
Explorer HAT/pHAT

Always be careful when running scripts and commands copied
from the internet. Ensure they are from a trusted source.

If you want to see what this script does before running it,
you should run: ‘curl https://get.pimoroni.com/explorerhat

Note: Explorer HAT/pHAT requires I2C communication

Do you wish to continue? [y/N] y

Checking environment…
Updating apt indexes…

Checking hardware requirements…

Checking for packages required for GPIO control…
raspi-gpio is already installed
RPi.GPIO installed and up-to-date

I2C already enabled

Checking packages required by I2C interface…
Python 2 smbus installed and up-to-date
Python 3 smbus installed and up-to-date

Explorer HAT/pHAT comes with examples and documentation that you may wish to install.
Performing a full install will ensure those resources are installed,
along with all required dependencies. It may however take a while!

Do you wish to perform a full install? [y/N] y

Checking for dependencies…
python-cap1xxx is already installed
python3-cap1xxx is already installed

Installing python-explorerhat…
install ok installed
Installing python3-explorerhat…
install ok installed

Checking for additional software…
python-pygame is already installed
python3-pygame is already installed

Downloading examples and documentation…
Resources for your Explorer HAT/pHAT were copied to
/home/pi/Pimoroni/explorerhat

All done. Enjoy your Explorer HAT/pHAT!

I ran it again : is that normal ?

For the Raspbian I am not sure but I think I have raspberry pi 3 which goes with the sts pi

Raspbian is the operating system. Some software has issues running on older versions.
For example Pimoroni state “Our software does not support Raspbian Wheezy”.
Open Terminal and type:

cat /etc/os-release

Ok, stretch is the latest so it not that. You could try updating it anyway, from terminal run
sudo apt-get update
and then
sudo apt-get upgrade

I updated it but it still does not work. Do you think it come from my code ?
I also tried with Python 2 but it is the same.

I’m not sure what’s going wrong to be honest. My python skills are all self taught. I’m not a noob but I’m no expert either.
If you open your code in idle, there is a check code option in the top menu under Run.
That will flage a lot of the common errors like incorrect indents etc. It might also give you a better error massage if you run the code from idle. I can’t copy and run your posted code because all the formatting is lost. If you put your code between thee ` and three more at the end the formatting is kept.

code

Sorry for not replying. I was unable to post yesterday. Here is my code. I checked as you said and discovered many error syntaxe : I do not understand why python consider as an error the spaces after flask import for example. Is there over main errors ?

import explorerhat as eh
from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
@app.route("/<state>")
def update_robot(state=None):
    if state == 'forward':
        eh.motor.onebackwards(100)
        eh.motor.twoforwards(100)
    if state == 'back':
        eh.motor.oneforwards(100)
        eh.motor.twobackwards(100)
    if state == 'left':
        eh.motor.twostop()
        eh.motor.onebackwards(100)
    if state == 'right':
        eh.motor.onestop()
        eh.motor.twoforwards(100)
    if state == 'stop':
        eh.motor.onestop()
        eh.motor.twostop()
    if state == 'anti-clockwise':
        eh.motor.onebackwards(100)
        eh.motor.twobackwards(100)
    if state == 'clockwise':
        eh.motor.oneforwards(100)
        eh.motor.twoforwards(100)
    template_data = {
        'title' : state,
    }
    return render_template('main.html', **template_data)

if __name__ == "__main__":
    app.run(host='192.168.1.20', port=80)

Thank you for your help !