Showing posts with label gamepad. Show all posts
Showing posts with label gamepad. Show all posts

Tuesday, March 29, 2016

Testing Gamepad on Raspberry Pi using Pygame

I bought a really small Bluetooth gamepad/media remote on eBay and needed a small program to test the gamepad buttons on the Raspberry Pi 3 once I set up Bluetooth.

So I created a short pyton program using Pygame.
It's available to donwload from GitHub.
https://github.com/winkleink/pygame_test_gamepad




Tuesday, August 25, 2015

9th Egham Jam – Show and Tell Competition – Prizes donated by Innovation in Education


The good news keeps in coming

Yesterday another pack of prizes arrived in the post for the next Egham Raspberry Jam Show and Tell Competition.  http://eghamjam9.eventbrite.com to register to attend and if you wish to have a chance of winning the prizes to bring along your Project to Show off.

Two (2) PiTrol kits from Innovation in Education were waiting fro me.
These are great kits as they allow you to build your own game controller with the instructions giving step by step details on how to solder it and then a snake game is also provided that works with the controller directly.

Kit of parts
Finished PiTrol


Nevil Hunt is the lead person at Innovation in Education and I have had the pleasure of meeting him at a few Egham Jams where he has come along with the PiTrol, 7 Segments of Pi and PiDapter (link from PiTrol site)

7 Segments of Pi is another simple idea that really captures the imagination. As the name states it is a large 7 segment display and button that you solder from a kit and then code is provided to play a game  

The PiDaptor allows you to connect 2 PiTrols to the one Raspberry Pi.

Each of these kits is great for learning to solder and with the provided code gives the owner the ability to understand how to read inputs from the GPIO pins and use it to play games.  Who doesn't want to play games on their Raspberry Pi.

In the case of The PiTrol prize once it has been soldered up I'd expect it would be possible to then get the controller to work with the many game emulators out there and possibly with the excellent RetroPi emulation system.

The Prize Pool is getting bigger and I'm still hoping to add a few more before the event.


Monday, January 26, 2015

Egham Raspberry Jam - 25th of January

What a day. More than double the attendees. More than double the stands, more teachers and kids attending.  More fun and learning than ever before.

When the Egham Jam was announced back in December I thought we would do better than the last Jam (46 registrations) but was blown away by the response.
Maybe a lot of new Raspberry Pis were sold at Christmas.
We were not only 'SOLD OUT' but also had more people who wanted to attend as well as people who register who advised me they couldn't make it so their tickets were released.

In the end 90 attended, more than double the October event and a record for the Egham Jam.

I was also really delighted the number of educators who attended looking for ways to integrate the Raspberry Pi into their schools Computing work.

There were at least 12 show and tell standard, some of them had multiple displays making for a very interesting day.  Also, very delighted that on a number of stands children were leading the show and tells. Giving great demonstrations on what they have build.

I tried to meet everybody who attended as possible and got the chance to connect teachers and people with specific needs with others who I know from previous Jams demoed a similar project.

Below are some pictures from the event.  Lots of fun.  Roll on the next Egham Raspberry Jam.

Surrey & Hampshire Hackspace were out in force

Scrolling messages


Pirates and MiWars winner

Raspberry Pi Development solution in a box


Robots are go.  Controlled over Wifi

Kodi set up being demoed by a young engineer


PiTrol - build it yourself controller

Remote arm controlled in Python


RISCOS on an HDMIPi



Seven Segment of Pi display



Getting the Wifi network set up to demo Airplay

Minecraft and RetroPie


Scratch 




Robot arm controlled from Scratch


Seven Segments of Pi. It's a big one.



Saturday, September 20, 2014

Setting up RetroPie on Raspberry Pi with USB gamepad

Every since it started I've watch the development with RetroPie a Raspberry Pi image that includes a number of console/computer emulators optimised for the Raspberry Pi.

I'm not a major gamer but sometimes a bit of nostalgia is worthwhile so I decided to install RetroPie

A few things to note. RetroPie is built on Raspbian, so the foundation is solid.
You can even use raspi-config to expand the SD partitions and set overclocking and GPU memory split.

The easiest way to use RetroPie is to download the pre-made image from the website http://blog.petrockblock.com/retropie/

Use your tool of choice to burn the SD card.  I did it on Windows 7 so I used Win32DiskImager

Once install in the Pi and all cables attached I added power.
Up comes the lovel RetroPie splash screen and it boots straight into Emulation Station which is a great front end giving access to the emulators where roms are available.
Splash Screen

On first boot Emulation Station prompts you to configure your controller/gamepad to operate the menus in Emulation Station. This confused me a little as I thought it was asking to configure the gamepad for the games.

But once I got a few roms on it became clear the this setup was only for Emulation Station and for that it worked perfectly.


I quit Emulation Station and ran raspi-config to expand the partition, split the memory 256MB/256MB as I read the PSX emulator may need it and also to do some minor over clocking (it can't hurt to have a bit more speed)

Then to get RetroPie setup correctly.

From the home directory do:

cd RetroPie-Setup
sudo ./retropie_setup.sh

This brings up the RetroPie configuration menu.
I selected Option 1 to do the Binaries-based INSTALLATION.
RetroPie comes with the bare minimum to get going in the image so you have to do this to get the other emulators.
As expected this can take a bit of time. 
Main Menu for retropie_setup

Once this finished I then selected option 3. SETUP (only if you already have run one of the installations above)

This is where you get the extra options to customize RetroPie.
If you are building a cabinet or want that genuine old school feeling of using original controllers there are a number of tools in the SETUP menu to make it a lot easier.  I was not interested so the only thing I enabled here is 310 SAMBA ROM Shares.
This enables SAMBA shares so you can drag and drop files from your computer to the Raspberry Pi making it much easier to get ROMs into RetroPie

SETUP menu 
At this stage RetroPie has all the emulators installed and the menus works but there are no ROMs and the games (once ROMs are copied across) will only work with the keyboard.

I mentioned I enabled SAMBA above so I could copy ROMs across. In the end I didn't use this. I used WinSCP which allows you to login to the Pi and drag and drop files from your computer onto the Pi in a nice GUI tool 

You have to copy the ROMs to the relevant folders for the different emulators.
Really simple. Reminded me of the olf Windows 3.11 FileManager with 2 panes to drag and drop between.

Last item was to get the gamepad working with the games and not just the Emulation Station menu.
This took a bit of hunting online as I have a feeling the method may have changed in the last update of RetroPie to v2.3.

In the end the method I found that work for me with my gamepad was from a reply on the following webpage.  http://blog.petrockblock.com/forums/topic/how-to-configure-a-controller-on-retropie/ from user flipbug

sudo chown pi /opt/retropie/configs/all/retroarch.cfg

cd /opt/retropie/emulators/RetroArch/installdir/bin

sudo ./retroarch-joyconfig -j 0 >> /opt/retropie/configs/all/retroarch.cfg

The first command gives the user pi the right to change the retroarch.cfg file.
The second goes to the directory where the retroarch-joyconfig command is located
The final command rund retroarch-config and outputs its results to retroarch.cfg 

I found the tool to be a bit sensitive when dealing with the analog sticks so ended up having to do it a few times.

In case my configuration matches some elses here is the configuration I use.

input_player1_joypad_index = "0"
input_player1_b_btn = "2"
input_player1_y_btn = "0"
input_player1_select_btn = "8"
input_player1_start_btn = "9"
input_player1_up_btn = "h0up"
input_player1_down_btn = "h0down"
input_player1_left_btn = "h0left"
input_player1_right_btn = "h0right"
input_player1_a_btn = "3"
input_player1_x_btn = "1"
input_player1_l_btn = "4"
input_player1_r_btn = "6"
input_player1_l2_btn = "5"
input_player1_r2_btn = "7"
input_player1_l3_btn = "10"
input_player1_r3_btn = "11"
#input_player1_r3_axis = "+3"
input_player1_l_x_plus_axis = "+0"
input_player1_l_x_minus_axis = "-0"
input_player1_l_y_plus_axis = "+1"
input_player1_l_y_minus_axis = "-1"
input_player1_r_x_plus_axis = "+3"
input_player1_r_x_minus_axis = "-3"
input_player1_r_y_plus_axis = "+2"
input_player1_r_y_minus_axis = "-2"

It was a bit of a trial and error session getting RetroPie working but in the end I have a fantastic emulator system set up on a model B+ that looks well and performs brilliantly.  I am really impressed at how well it runs PSX games as from a raw CPU power I always though the PSX and the Raspberry Pi would be quite similar.

Now all I have to do is actually be better at the games to fully enjoy them.


Thursday, April 18, 2013

Raspberry Pi, Unipolar Stepper motors, ULN2003 Darlington Pairs, USB Gamepad, Python

For the Raspberry Jam on Sunday I want to bring something that moved and was also interested in getting stepper motors to work with the Raspberry Pi so I decided to build a vehicle using stepper motors to drive the wheels (very slowly) and control it with a USB Gamepad/Joystick

The first thing I had to do was to sort out the sequence of pulses for the Stepper Motors. I did this originally with an Arduino Uno as I wanted to remove as many opportunities for human error as possible and since this would be the first time I would user stepper motors with the Raspberry I thought it best to begin with a platform where I was use to doing I/O.

To operate a stepper motor you send signals to the 4 lines in a set sequence.
For the motors I purchased are 28BYJ48 DC 5V and their sequence is. Yours may be different.

        Line1 Line2 Line3 Line4
Step 1    0     0     1     1  
Step 2    1     0     0     1  
Step 3    1     1     0     0  
Step 4    0     1     1     0  

To get the motor to go in reverse you just run this sequence in reverse order.
Also, strangely if you are doing this using an Arduino the stepper library worked first time for me even though  the sequence is different. But when I tried a second, third or more times it failed unless I changed the sequence.  See here for the details on the sequence in the Arduino Stepper Motor library: http://www.tigoe.net/pcomp/code/circuits/motors/stepper-motors/
This post also has a lot of useful information on stepper motors generally

The first thing that I discovered was that the stepper motors and driver board that was a ULN2003 and some indicator LEDS with the right connector for the motors I bought (Amazon UK / Amazon US - these are available cheaper on eBay) had a slightly different pulse sequence than the examples I found so once I figured that out the motors turned clockwise and anti-clockwise as I expected.

All good, motors and driver board working as expected.

Next to get it wired up to the Raspberry Pi.





Here is an layout using a breadboard and a couple of UNL2003 Darlington Pair ICs. These are the same ICs as on the board so the wiring is the same. The board just makes it a lot easier.

NOTE: I used Fritzing to make the layout and it shows the motor as having 6 wires. There are 6 terminals on  unipolar stepper motor, but the model I bought had the two power lines tied together so only came out to a single wire.  Depending on the unipolar motor you have it may have 5 or 6 wires.



With all the wiring done next it was onto the code.

As I said above the goal was to get the motors controlled by a USB gamepad. The gamepad I used is a Saitek P380 (Amazon UK / Amazon US). I bought mine in PC World for about £10.00 For this I decided to use Python as learning to code properly in Python is one of my 2013 resolutions. Also, Python has a nice library in Raspbian for the GPIO pins and I knew that Pygame which works with Python 2.6x had the ability to read USB gamepads.

After figuring out all the mad stuff to do with getting the motors to work on the Arduino I was delighted that using Python and the GPIO library I got the motor to spin with no real problems.

After a bit of hunting on the Internet I got the code to read the USB gamepad and depending on how you push/pull the analog sticks the motors turned.  Effectively allowing you to drive the vehicle like a tank with independent control for both wheels.

Here is the code I used. It is very simplistic as my hope is that it will be easily understandable by others so it can form the basis of something more interesting. I would expect with a bit of effort even I could reduce the code to about a 3rd of its current size and someone who can code properly could get it even shorter. But for this exercise I wanted to literally show every step in the sequence so it is easy to read, easy to understand and east to adapt.


#!/usr/bin/env python

import os, sys, pygame 
from pygame import locals
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.cleanup()

# set the delay between steps
stepDelay = 0.002

# set up motor 1
GPIO.setup(8, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)

GPIO.output(8, GPIO.LOW)
GPIO.output(16, GPIO.LOW)
GPIO.output(18, GPIO.LOW)
GPIO.output(22, GPIO.LOW)

# set up motor 2
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)

GPIO.output(11, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(15, GPIO.HIGH)
GPIO.output(21, GPIO.HIGH)


os.environ["SDL_VIDEODRIVER"] = "dummy"
pygame.init()

pygame.joystick.init() # main joystick device system

deadZone = 0.6 # make a wide deadzone
m1 = 0 # motor 1 (1 = forward / 2 = backwards)
m2 = 0 # motor 2 (1 = forward / 2 = backwards)
try:
   j = pygame.joystick.Joystick(0) # create a joystick instance
   j.init() # init instance
   print 'Enabled joystick: ' + j.get_name()
except pygame.error:
   print 'no joystick found.'


while 1:
   for e in pygame.event.get(): # iterate over event stack
      if e.type == pygame.locals.JOYAXISMOTION: # Read Analog Joystick Axis
         x1 , y1 = j.get_axis(0), j.get_axis(1) # Left Stick
         y2 , x2 = j.get_axis(2), j.get_axis(3) # Right Stick

         print x1
         print y1
         print x2
         print y2

         if x1 < -1 * deadZone:
             print 'Left Joystick 1'

         if x1 > deadZone:
             print 'Right Joystick 1'

         if y1 <= deadZone and y1 >= -1 * deadZone:
    m1 = 0 # Dont go forward or backwards

         if y1 < -1 * deadZone:
             print 'Up Joystick 1'
             m1 = 1 # go forward
             print m1
             
         if y1 > deadZone:
             print 'Down Joystick 1'
             m1 = 2 # go forward
             print m1

         if y2 <= deadZone and y2 >= -1 * deadZone:
    m2 = 0 # Dont go forward or backwards
              
         if y2 < -1 * deadZone:
             print 'Up Joystick 2'
             m2 = 1

         if y2 > deadZone:
             print 'Down Joystick 2'
             m2 = 2

         if x2 < -1 * deadZone:
            print 'Left Joystick 2'

         if x2 > deadZone:
            print 'Right Joystick 2'

         
   if m1 == 1: # motor 1 go forward
# step 1 motor 1
      GPIO.output(8,GPIO.LOW)
      GPIO.output(16,GPIO.LOW)
      GPIO.output(18,GPIO.HIGH)
      GPIO.output(22,GPIO.HIGH)

   if m2 == 1: # motor 2 go forward
# step 1 motor 2
      GPIO.output(11,GPIO.LOW)
      GPIO.output(13,GPIO.LOW)
      GPIO.output(15,GPIO.HIGH)
      GPIO.output(21,GPIO.HIGH)

   time.sleep(stepDelay)



   if m1 == 1: # motor 1 go forward
# step 2 motor 1
      GPIO.output(8,GPIO.HIGH)
      GPIO.output(16,GPIO.LOW)
      GPIO.output(18,GPIO.LOW)
      GPIO.output(22,GPIO.HIGH)

   if m2 == 1: # motor 2 go forward
# step 2 motor 2
      GPIO.output(11,GPIO.HIGH)
      GPIO.output(13,GPIO.LOW)
      GPIO.output(15,GPIO.LOW)
      GPIO.output(21,GPIO.HIGH)

   time.sleep(stepDelay)

   if m1 == 1: # motor 1 go forward
# step 3 motor 1
      GPIO.output(8,GPIO.HIGH)
      GPIO.output(16,GPIO.HIGH)
      GPIO.output(18,GPIO.LOW)
      GPIO.output(22,GPIO.LOW)

   if m2 == 1: # motor 2 go forward
# step 3 motor 2
      GPIO.output(11,GPIO.HIGH)
      GPIO.output(13,GPIO.HIGH)
      GPIO.output(15,GPIO.LOW)
      GPIO.output(21,GPIO.LOW)

   time.sleep(stepDelay)

   if m1 == 1: # motor 1 go forward
# step 4 motor 1
      GPIO.output(8,GPIO.LOW)
      GPIO.output(16,GPIO.HIGH)
      GPIO.output(18,GPIO.HIGH)
      GPIO.output(22,GPIO.LOW)

   if m2 == 1: # motor 2 go forward
# step 4 motor 2
      GPIO.output(11,GPIO.LOW)
      GPIO.output(13,GPIO.HIGH)
      GPIO.output(15,GPIO.HIGH)
      GPIO.output(21,GPIO.LOW)

   time.sleep(stepDelay)

   if m1 == 2: # motor 1 go reverse
# step 4 motor 1
      GPIO.output(8,GPIO.LOW)
      GPIO.output(16,GPIO.HIGH)
      GPIO.output(18,GPIO.HIGH)
      GPIO.output(22,GPIO.LOW)

   if m2 == 2: # motor 2 go reverse
# step 4 motor 2
      GPIO.output(11,GPIO.LOW)
      GPIO.output(13,GPIO.HIGH)
      GPIO.output(15,GPIO.HIGH)
      GPIO.output(21,GPIO.LOW)

   time.sleep(stepDelay)

   if m1 == 2: # motor 1 go reverse
# step 3 motor 1
      GPIO.output(8,GPIO.HIGH)
      GPIO.output(16,GPIO.HIGH)
      GPIO.output(18,GPIO.LOW)
      GPIO.output(22,GPIO.LOW)

   if m2 == 2: # motor 2 go reverse
# step 3 motor 2
      GPIO.output(11,GPIO.HIGH)
      GPIO.output(13,GPIO.HIGH)
      GPIO.output(15,GPIO.LOW)
      GPIO.output(21,GPIO.LOW)

   time.sleep(stepDelay)

   if m1 == 2: # motor 1 go reverse
# step 2 motor 1
      GPIO.output(8,GPIO.HIGH)
      GPIO.output(16,GPIO.LOW)
      GPIO.output(18,GPIO.LOW)
      GPIO.output(22,GPIO.HIGH)

   if m2 == 2: # motor 2 go reverse
# step 2 motor 2
      GPIO.output(11,GPIO.HIGH)
      GPIO.output(13,GPIO.LOW)
      GPIO.output(15,GPIO.LOW)
      GPIO.output(21,GPIO.HIGH)

   time.sleep(stepDelay)

   if m1 == 2: # motor 1 go reverse
# step 1 motor 1
      GPIO.output(8,GPIO.LOW)
      GPIO.output(16,GPIO.LOW)
      GPIO.output(18,GPIO.HIGH)
      GPIO.output(22,GPIO.HIGH)

   if m2 == 2: # motor 2 go reverse
# step 1 motor 2
      GPIO.output(11,GPIO.LOW)
      GPIO.output(13,GPIO.LOW)
      GPIO.output(15,GPIO.HIGH)
      GPIO.output(21,GPIO.HIGH)

   time.sleep(stepDelay)


Once I put together the physical vehicle using cardboard, Nutella jar lids and some glue I tried it out.
It worked. The main thing that would need to be improved is the wheels.
As the Nutella jar lids are light plastic they wobbled a lot causing them to grind on the cardboard chassis. I used some toothpicks to stop the wheels turning in too much and this for the most part stopped the problem.
Here is a short video of it working.


As you can see I definitely won't be racing this bad boy, but it was great to work with stepper motors, python and pygame as well as upgrade some of my cardboard cutting and shaping skills.