Hello, my son has built the robot, loaded code for the explorer hat, as well as the code that Sandy Macdonald posted on GitHub (he has flash). Neither of us has any idea what to do next - how to go from having the code there to instructing the robot to move. Are there any online instructions that would help a complete novice (me) and a child to get the thing going. We are about to buy a wifi dongle and a battery pack but we could make it move initially while it’s wired up to the monitor and power supply I think. None of the online resources I’ve found actually tell us what to do next. Please, please help.
I feel your pain! this site gives great start to finish instructions ,I have used it myself so I cant offer anything expect the link ,good luck [Sts pi robot , https://www.hackster.io/sherna-liew/sts-pi-roving-robot-f46637
You have to build your code based on what you want it to do. And what you want to use to control it.
I built a couple of rovers based on the Explorer phat. I used a USB wireless keyboard for control. This is the code I used.
#!/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
To get keyboard software I used
sudo apt-get install libusb-dev
git clone https://github.com/walac/pyusb
cd pyusb
sudo python3 setup.py install
To have it run automatically on boot I did the following.
run crontab -e
add
@reboot sudo python3 /home/pi/RoverKB.py &
https://learn.pimoroni.com/tutorial/robots/controlling-your-robot-wireless-keyboard
Thank you so much! Taking a look now
Thank you. That’s really helpful
Hi there. I created a basic guide for a school challenge project. Not sure if it is what you are looking for but hope it helps.
Thank you - this is hugely helpful!
It may be a bit of work, and some trial and error with the code. Very rewarding when you get it working though.