Thursday, November 9, 2017

White Wii Nunchuck shaped Bluetooth Joystick - £1.85/£1.99 on eBay

When I attended PiWars (a great event for people who like to build robots using the Raspberry Pi) earlier this year I saw a whole range of controllers being used for controlling Raspberry Pi based robots. With many using USB dongle ones and others using Bluetooth or even apps on phones.  Expecting to see some BlueDot next year a great Android App for controlling things over Bluetooth.

Since my phone is iPhone (too long to explain) BlueDot isn't an option for me and with my continuing goal of finding cheap solutions to a problem I found a £1.85 Bluetooth controller from eBay - 3D Bluetooth Virtual Reality Glasses VR BOX Game Remote Control Gamepad Handles

What I liked about it is that it uses AAA batteries so if at an event the batteries died I could swap them out. No need to wait for recharging.

It's billed as being for mobile gaming and virtual reality and no mention of working with a regular computer or even Raspberry Pi, so I took a punt for £1.85, well actually £3.70, as I bought two. (walking on the wild side). 

eBay order confirmation

NOTE: The listing I bought from has ended but the same seller has a new listing at £1.99

I've seen some similar listing with switches on the side.  This is not the model I have bought. Mine has no switch.


What it looks like on eBay listing

They arrived and actually look good.  Plastic is definitely cheap, but they don't feel flimsy.
Below are a couple of pictures of the real thing.  A little different to the render but I'm happy..

Top. Joystick, power button, mode change button and 4 facing buttons.

Front has 2 additional buttons for when held straight rather than sideways
Time to start testing.

Setting up a new Raspbian image and using Raspberry Pi ZeroW I added the two AAA batteries and it came on. Blue light flashes to say ready to pair.

Using the Bluetooth Add Device in Raspbian the device VR BOX was found and paired successfully.  The unusal name actuallymakes sense as the listing on eBay is for "3D Bluetooth Virtual Reality Glasses VR BOX Game Remote Control Gamepad Handles"


Bluetooth on Raspbian paired with VR BOX

From the eBay listing and the single sheet of instructions the Gamepad/Nunchuck has 4 modes that you select with a combination of the @ and the relevant button.

A : Function Select (Music Video Mode.)
B : Function Select (game mode)
C : Function Selection (VR video self-timer mode.)
D : Function Select (mouse self-timer mode.)

It starts up in Music mode, so to use as a gamepad you have to change this after it pairs.

As I wanted to test in game mode I did [@]+[B]

I previously created a small python/pygame program to help with the testing.  It's available on GitHub.

I downloaded it to the PiZeroW (one of these days I'll do the proper git clone thing) and ran the program.
I run from the command line as the terminal window prints the details in text while the pygame window gives a graphic representation of the gamepad so I can see if multiple button presses are possible and which button numbers are pressed.  The code includes an analogue joystick so it will show the full range of movement for the gamepad joystick.


python2/pygame gamepad tester

Pressing the buttons the appropriate red square appeared and the terminal window stated which button was pressed or released.  Success! It's being detected as a gamepad and the buttons are working.

I then used the joystick which looks like an analogue stick you see on many projects. Only it's not. The joystick is digital.  Up, Down,Left,Right.  No range in values.  Except for a strange thing.
The positive values are 0.999969482422 and the negative values are -1, so you can't test for 1 for down and right when in gamepad (sideways) mode as 0.999969482422 is not 1. A little gotcha for coding.

The different modes

As listed above there are 4 different modes for the controller.

[@]+[A] for music
Nothing happened on the Pi. I haven't figured out what Bluetooth Music Mode is yet.
I tried this mode with my phone and it does provide volume up/down, mute an play so does what is expected with the target device.

[@]+[B] game mode
This is gamepad in horizontal mode, like a traditional controller with the joystick on the left and the buttons on the right.
For example, if you push up the Up joystick is detected

[@]+[C] VR video self-timer mode
This is a gamepad in Nunchuck mode. So vertical.
For example, if you have the controller held vertically and press up then Up Joystick is detected.

All the buttons are the same as for game mode.

[@]+[D] mouse self-timer mode
Guess what the joystick operates like a mouse held like a Nunchuch (vertical) with the lower front button (trigger) being left click and the higher front button (trigger) being right click.
In mouse mode buttons A and B still get recognised as gamepad buttons so that could be useful.Not sure how or when but extra buttons are always useful. 

Button combinations

In game mode some of the buttons could be pressed at the same time while others could not.

@ and Power are reserved so have no button function.

Bottom trigger and top trigger could not be pressed at the same time. Lower trigger always took priority even if upper was pressed first.

Bottom trigger could be pressed with any of the face bottoms (A,B,C,D). Same for Top Trigger.

A could be pressed with C, but not B or D. A stays active and the others do not show.
If you press B or D first and then press A, A will be activated and the other will be released.

B and C can be pressed at the same time

D is over ridden by all face buttons, so if you have D pressed and then press any of the other their function is activated and D is released.

Also, either trigger and two button face combinations that were noted to work above will also work.
So, if you need 2 buttons at the same time and one is the master and the second one needs to be one of 4 I'd recommend using a trigger as the master and the face buttons as the secondary as this works for all face buttons.
If you need 3 buttons then there are a small number of combinations that will work.
Top button, A and C worked for me.

Future Note: I have a feeling this section may need a chart of some nature. Need to figure out how to represent it.

For Python/Pygame and I expect other system the buttons are numbered:

Gamepad Mode


Face button arrangement
  A 
C   D
  B


Top Trigger: button 7
Bottom Trigger: button 6
A: button 4
B: button 0
C: button 3
D: button 1

VR mode (Nunchuck)

Face button arrangement
  C
B   A
  D

Top Trigger: button 7
Bottom Trigger: button 6
A: button 1
B: button 3
C: button 0
D: button 4

When I saw it changed I thought the button values would have turned clockwise 90 degrees so the buttons were in the same position (top, left, right, bottom) would have the same values but they don't. Top and bottom shift correctly, but left and right are mirrored.

Where did 2 and 5 go.  I wonder if they're on the board and not broken out. Might be a tear down thing


Summary

For £1.85, or £1.99, last time I checked if your need is digital controls then I think this is a hit.  It paired easily. The instructions though really sparse are enough to change modes.
With a phone it does the media things promised from mode A and on the Raspberry Pi and expect and device that supports a Bluetooth Gamepad and Mouse it does exactly what it says on the tin.

For controlling robots it's ideal (except for no analog).
Joystick for direction control and then buttons for other functions. Maybe take a picture, fire a missile, change mode from piloted to auto mode for different challenges at an event like PiWars.
With the advantage of using regular AAA batteries there is no fear that at an even the controller will run out of power and need to be charged.


A little but extra

As each controller having a unique Bluetooth Device ID it's possible to set up a cronjob on Linux to  pair with a specific gamepad when the Raspberry Pi (ZeroW) is booted/rebooted so  at events if you had 5 robots, 5 controllers each controller would be set up to work with a specific controller
It's not too complicated to do.

Get the device ID for the VR Box using the command bluetoothctl
Mine was FF:FF:70:00:76:8B

Run crontab -e
Select your editor if running for the first time. I use Nano

Go to the end and add the line
@reboot echo "connect FF:FF:70:00:76:8B" | bluetoothctl

If using Nano do a [ctrl]-[x] to exist and 'y' to save.
Reboot an you're done.

This will try to auto pair when the Pi boots.

You can also do a cronjob to continuously try to reconnect and not just at boot.
This is useful if the Pi has completed booting before the gampad has been turned on or if the controller hasn't been used for 10 minutes or in pairing mode fails to pair after 3 minutes and so needs to repair when the controller is turned back on


















Wednesday, May 10, 2017

Raspberry Pi 3 with Google AIY VoiceHat

I was lucky enough to get a copy of Issue 57 of The MagPi with the VoiceHat from Google on the cover.

This is a great bit of kit with Raspberry Pi Hat, speaker, microphone, and a cardboard box to put it in.
For a bit of fun I fitted it into a cardboard R2D2 that I had from before Christmas.

This is not the droid you're looking for
After doing the usual of asking it to tell a joke and give me the weather and details of what's nearby I was wondering what I could use the VoiceHat for.

We have an Amazon Dot and it's main purpose in the house is playing radio station.  I tried this out and nothing played.  Seems streaming radio support isn't built in.

Then keeping up with what others were doing Mike Redrobe posted on the Raspberry Pi forum that he had it playing YouTube audio. https://www.raspberrypi.org/forums/viewtopic.php?f=114&t=182665

With a bit of assistance I got this working.  If you're careful with the name of the video you can get it to play fairly much any song.  My kids loved this.

But I still wanted to play streaming radio.  You know, one command and it's off rather than needing to keep telling it new songs to play.

In a later post Mike mentions using VLC as the player and armed with his example I began looking to see if I could get streaming radio to work.

I did. Yeah! Then I went on the trail to add playing podcasts.  Again a success.
You need to install VLC

sudo apt update
sudo apt upgrade
sudo apt install vlc

Radio Stations supported are:
Absolute Radio
Absolute 80s
Absolute 90s
Absolute 00s
Eagle Radio
BBC Radio 1
BBC Radio 2
BBC Radio 3
BBC Radio 4
Capital FM


Podcasts Supported are:
Good Job Brain
No Such Thing As a Fish
Freakonomics


Here is a little video of it working.  I recorded it in Portrait as the cardboard R2D2 it is in is portrait shared so if viewing on a phone will fill the screen, while of viewing on a computer it will have the side bars which is probably better than seeing the messy table.




The action.py file that's needed with the commands built in is available on GitHub
https://github.com/winkleink/RPi_AIY


Other useful bits and pieces

Knowing what Google is actually hearing

While setting up the radio stations Absolute 90s was interpreted by Google as Absolute 90s, but Absolute 00s was interpreted as absolute noughties
It took me a while to figure this out and command that helped me was:

sudo journalctl -u voice-recognizer -n 20 -f

This shows what's happening when you issue a command,
Here's a small snippet showing what the voice command is converted to.
Absolute 00s or is that Absolute Noughties

Make sure to stop and start the service after each change to action.py

sudo systemctl stop voice-recognizer && sudo systemctl start voice-recognizer

Want to add more radio stations
This website has a list of streaming radio stations and their urls.  This is the UK list.

Stopping the playback

Since I'm using the button to stop the playback based on Mike Redrobe's code I decided to do the GPIO setup at the top so it is shared by all the commands rather than having it in each command.

The code is a while loop that keeps going until the button is pressed and then a kill statement to stop the process.  To be truthful I'm not sue how the kill bit works, I just know it does, so thank you to Mike for sharing.

while gpio.input(23):
    time.sleep(1)


pkill = subprocess.Popen(["/usr/bin/pkill","vlc"],stdin=subprocess.PIPE)
p.kill()


Doing the podcasts
Getting the most recent podcast to play is a bit more hacky than the radio stream as the radio stream is always the same while each podcast episode has a different url.  To achieve this I first had to find the relevant rss feed and then parse the file to find the start and end of the url to the mp3.  audioboom.com podcasts aren't too bad, but the Freakonomics one based off the feedburner rss was a little more complex as the url to the mp3 is actually a redirect url and they don't work.

https://www.podtrac.com/pts/redirect.mp3/audio.wnyc.org/freakonomics_podcast/freakonomics_podcast050317.mp3

You can see in the middle there is redirect.mp3 and then what looks like a path to a different website audio.wnyc.org
This meant the parser had to first find "/redirect.mp3/" and then from there find "mp3" to get the main bit for the audio.wnyc.org link.
Finally it needed to add back in "http://"
You can see how this is done in the code below.

url = 'http://feeds.feedburner.com/freakonomicsradio?format=xml' response = urllib.request.urlopen(url) data = response.read() # a `bytes` object text = data.decode('utf-8') startmp3 = text.find('/redirect.mp3/')+14 endmp3 = text.find('.mp3',startmp3+16)+4 if startmp3 > 0: command = "http://"+text[startmp3:endmp3]


Have a play and see what you can get the Raspberry Pi AIY to do.
Just be sure to backup any files you modify before changing them.
I didn't backup my action.py when I started playing but luckily google has made the original files available on GitHub https://github.com/google/aiyprojects-raspbian

I'm really looking forward to seeing how this grows and expands.

Just saw that https://twitter.com/k_tinkerer has been doing some great things with the AIY as well.  Worth checking out if you want to do more.

http://ktinkerer.co.uk/

I'll definitely be adding the shutdown and reboot commands from here.


Thursday, February 16, 2017

Green Screen photo booth using Raspberry Pi

We've seen a number of photo booths herehere and even the All Seeing Pi being done with Raspberry Pi. All of these photo booths are based on taking a picture and maybe putting an overlay to add a banner or something like a funny hat or mustache.

My thoughts were could the Raspberry Pi 3 do Green Screen.  You know that special effect from the movies where the background is removed and a different image put in it's place.

As a kid I think the first time I remember seeing this was Superman.


How cool would that be to make your own version of the special effects used in big budget movies.

So, I started the search for tools under Linux that would permit the green screen to be done.
As a short summary green screen/chroma key is where a a single colour is removed from an image.
Most often it is green as modern cameras are more sensitive to green and a bright green works best as it's less likely to be a colour in a natural scene.  I remember when I was younger hearing it being done with blue screen as well.  


After a bit of looking I found Imagemagick a jack of all trades image processing tool.  It has a function to remove a single colour from an image and as importantly for my use includes a 'fuzzy' search for the colour which gave a bit of tolerance to the lighting.  

In the code the most important line is:
os.system('/usr/bin/convert -limit thread 4 ' + folder +'imagecam.png -fuzz '+fuzzpercent+' -transparent "#'+rgbnum +' " ' + folder + 'imagecamt.png')
I know there is a lot going on in there.  She short version is it takes imagescam.png, makes transparent the colour rgbnum with a tolerance of fuzzpercent and saves it as imagecamt.png

This forum thread was really useful in understanding how to use it.

Once I had my head around this it was then a matter of doing the rest of the code.
Take the picture
Remove the green
Layer a background and the image with the green removed. 
For testing I used some of my kids PlayMobil and a piece of A0 green card.  


All looks like it's working well even with the ability to change the background using the arrow keys

Now for the next level.
Scale it Up to Life Size !!!!

This of course means I needed a green screen background with standard that I bought from eBay.  No idea when I'll use the black or  white backgrounds that came with this kit, but I have them now.

And then Tweet the pictures.
For Tweeting I used tweepy and followed the excellent guide by Alex Eames @RasPiTV 

Finally, wouldn't it be great to have a little remote control and not have to rely on a keyboard.  Since the Raspberry Pi 3 has Bluetooth I thought this might be the ideal solution.  No wires and no messing about.  Since Pygame was already being used for the displaying of the images and I knew Pygame had joystick support built in this looked like the obvious choice.

Again, on eBay I found this small little Bluetooth gamepad and thought it would be perfect.  Super small which means you can have it in your hand but not interfere with your final picture.

Iddy biddy, teeny weeny, black gamepad 

My usual style is before bring a new feature into a project I like to test it standalone to make sure it works.  For the gamepad I created a small Python/Pygame program to test the gamepad 

It paired with the Raspberry Pi first time and worked perfectly with the gamepad test program.  So, 100% sure it will function with the chromaCam setup.

Now all the parts are in place.  PiCamera to take the picture.  Imagemagick to remove the background colour.  Pygame to merge background, foreground picture with green removed and finally an overlay with the background images changed with the keyboard or the super small gamepad.  (Oh yeah, the circular thing at the bottom is an analogue joystick)

The chromaCam set up got it's outing at the last Wimbledon Raspberry Jam.  Here are some pictures.
On the day I used a camera tripod and a lot of tie wraps to hold the Pi Display and the Pi Camera onto the tripod. I would definitely recommend a more secure mounting method 

Getting set up https://twitter.com/MrTomsWorld

Who doesn't love a bit of Harry Potter - https://twitter.com/rdhayler


Those Lions are dangerous

That doesn't look like a sensible thing to do - https://twitter.com/HackHorsham

Final notes.
If you did click the link above that explains Chroma Key/Green Screen then you'll have read that lighting is really important.  The goal is to one specific colour. If your lighting is uneven then due to shadows or folds in the green screen the green will be different giving varying results.
Taking out too much - https://twitter.com/Codepope

I'm sure the top of the Raspberry Pi logo isn't transparent


A few bits of rogue green - https://twitter.com/gowolade

If you want to make your own Green Screen Photo Booth then the code is available on GitHub


Final Thoughts:
Yes, this project worked and was great fun to build and see people using it.
For the project I limited the image size to 640x480 as even at that resolution it took about 2 seconds for the picture to be updated.  This meant it took a little bit of patients to get the picture you wanted before pressing the button to Tweet the image.

I took the set up to Hack Horsham as well but I didn't bring my own lighting and the lighting in the room was perfect for a Jam but didn't give enough contrast for chromaCam to work.  So, it didn't make the final cut and I stuck to Button Flash.

You can see the setup in the mannequin challenge video tweet. 



Again, this was a great learning experience for me and once the computing power for the Raspberry Pi enables it or access to the GPU is supported this will be amazing when the green screen transparency can be done in real time.  
I expect as new devices come out I'll be revisiting this project to see if the performance is improved. 



Making Games with Scratch and Sense Hat on Raspberry Pi


I'm a big fan of the Raspberry Pi Sense Hat and Scratch for doing interesting things and learning to code.
Previously I made a Sense Hat Cave Game using Python where you tilt the Sense Hat to move around as well as a Crane controller programmed in Scratch.
This time the goal was to create game that uses the Sense Hat as the display while programmed in Scratch.  The best of both world.  The goal for me was to lean to control the pixels a bit better in Scratch.

The result is Collect the Dots a simple game where you're a blue dot collecting red dots.  It's kind of like snake without the tail.



You control your dot using either the small joystick on the Sense Hat or use the keyboard on the computer.  Press Down to start the game.  You'll see in the video it tells you this on the Sense Hat, but if no Sense hat is attached and you're running on screen there is no prompt.


As can be seen in the video the on screen display mirrors on the Sense Hat dots, so either can be used.
Strangely even though the screen display is bigger I found it easier to play if I only looked at the Sense Hat display.
The game can also be played without the Sense Hat. Uploaded to Scratch website https://scratch.mit.edu/projects/144708660/

Embedded from the Scratch Website



You can download the code from GitHub

TIP: As the wonderful Cat Lamin points out in her blog post about using doing an animation on the Sense Hat using Scratch you have to run the code once to enable the sensors and capability of the Sense Hat.  So, the first time you run the program on a Raspberry Pi with a Sense Hat it may not work correctly.  Just stop it and start again and all will be perfect

A thought for an enhancement would be to control your dot by tilting the Sense Hat rather than using the joystick or keys.

Monday, January 30, 2017

Controlling screen brightness on pi-topCEED on Raspbian Jessie with guizero app and keyboard shortcut

I backed the pi-topCEED during the crowd funding campaign and have used it a lot since.

pi-topCEED

My kids also use it for homework and general Minecraft playing.
As I like to make things I have a number of microSD which means I regularly use it with Raspbian Jessie.

There are a couple of drawback to using Raspbian on the pi-topCEED.  The pi-topHUB doesn't shutdown the power completely leaving the backlight on and the Raspberry Pi in kind of a suspecd mode. The red LED is still on.  Also, there is no way to control the screen brightness.

Doing the usual Google to see if there was a way around this I came across this GitHub repository that looked like it would do the job.  https://github.com/rricharz/pi-top-install
It includes code to shutdown the pi-topHUB which also completely shuts down power to the display and to the Pi as well as provide a program called 'brightness' to control the screen brightness.

After following the instructions in the README.md I had the pi-topHUB shutting down correctly and if from the terminal I type brightness increase the screen brightness would go up and vice versa brightness decrease did what you'd expect.

All very good, but not very user friendly if you're running the GUI which is where I spend most of my time. 

With that in mind and having recently heard good thing about guizero (https://github.com/lawsie/guizero) which is based on tkinter and is said to make building GUI apps  in Python easier.

It looked to me like a simple app with 2 buttons [Darker] [Brighter] is all I needed so I read up a little on guizero.  Using the App using grid layout example (https://lawsie.github.io/guizero/app/) I soon has a small app that did exactly what I wanted.
Code available on Git Hub (https://github.com/winkleink/pi-top-brightness)

The program itself is quite short. Code below.  This makes 2 buttons.  Button1 and Button2.  When you press them they run the relevant system comment to change the brightness. 


from guizero import *
from subprocess import call

def brighter():
    call(["brightness", "increase"])

    
def darker():
    call(["brightness", "decrease"])
    
    
app = App(title="pi-top", height=50, width=145, layout="grid", bgcolor=None)

button1 = PushButton(app, brighter, text="Brighter", grid=[0,1])
button2 = PushButton(app, darker, text="Darker", grid=[0,0])
app.display()


Here is a video showing it working.


After a quick chmod +x  pi-top-brightness-gui.py the program was executable and so I could run it directly.

Then I added the program to the main menu under the System Tools section.
In the video you can see I have a pi-top icon.  This I took from their Twitter account (https://twitter.com/GetPiTop)  I expect they'll be OK with that as I'm adding even more branding to my pi-topCEED.
I explained in a recent post on backing up Minecraft Worlds how to add a program to the menu (http://www.winkleink.com/2016/07/minecraft-pi-backup-worlds-on-exit.html)

It's great to have a little gui app to change the brightness of the screen and even better to learn a new skill with guizero.

One thing I didn't mention is that when I rebooted the Raspberry Pi the first time after installing the programs the screen brightness was far lower than I wanted it be by default.

To fix this I added a single line cronjob.  Crontab with cronjobs is a way to schedule when programs run automatically with one of the options being to run at boot up. It's a simple way to get something running each time you login. It's the system used by people who lets say want to check their network connection every hour.  Just setup a cronjob to run every hour and it will take care of it for you.   I used this previously to enable the Bluetooth keyboard and trackpad or the NexDock. (http://www.winkleink.com/2016/10/nexdock-thoughts.html)

So, I used crontab to set the brightness of the screen to 8 on startup.

In the terminal type:

crontab -e

If this is your first time it asks which editor you want to use.  I do [2] for nano.
Then the crontab file is opened where you put the jobs you want to run.
There are some comments in the file to help you get started, but if you want to do serious scheduled jobs then it's worth 

Go to the bottom of the file and type

@reboot brightness 8

Then the usual nano [CTRL]-[x] to save and close.  Answering [y] to save the file with the same name

Now when I start up the pi-topCEED the default brightness is '8' which suits me.


Finally, finally, the rricharz GitHub explains how to set the keyboard shortcuts for the pi-top laptop so the brightness keys work.  All good if I had a pi-top laptop, but I have a pi-topCEED where it's bring your own keyboard and the one I'm using doesn't have any fancy keys.
So, I looked up how to add keyboard shortcuts.  Again, it ends out to be not too hard.  Just added a couple of entries into a config file.

First thing I had to do is decide what key combination I wanted to use. I settled on:
[Shift]-[Windows Key]-[Up Arrow] for brighter
[Shift]-[Windows Key]-[Down Arrow] for darker

To do this you need to edit the lxde-pi-rc.xml file.
The file is a in a hidden folder in your home folder. If using the default user pi then the following command will open it 

nano /home/pi/.config/openbox/lxde-pi-rc.xml

As the file is in the home folder it doesn't need sudo to edit it

Then scroll down to the keyboard section and add the following to the end.
Easiest way is in nano find the </keyboard> text using the command [Ctrl]-[w] to do a Where is

Make sure you use spaces for the indent and get the <keybind> to line up with the other <keybind> entries

    <keybind key="S-W-Up">
      <action name="Execute">
        <command>brightness increase</command>
      </action>
    </keybind>
    <keybind key="S-W-Down">
      <action name="Execute">
        <command>brightness decrease</command>
      </action>
    </keybind>

To finish like above use [Ctr]-[x] to save and close.

To test select [shutdown] in the drop down menu and then [logout].
No need to reboot to enable the keyboard shortcuts.
Once you log back in the lxde-pi-rc.xml setting will be re-enabled and the 2 new key bindings will be active giving you the ability to change the brightness of your pi-top/pi-topCEED using [Shift]-[Windows Key]-[Up Arrow] and [Shift]-[Windows key]-[Down Arrow]

This has been a fun little project to improve the use of the pi-topCEED.
I like the work rricharz has done getting the pi-topHUB to turn off completely and to control the brightness as well as Laura Sach has done to make guizero so easy to use.

With the help of other peoples effort I now have the pi-topCEED completely shutdown as well as the Raspberry Pi and the ability to change the screen brightness using either a little gui app or with the keyboard.  Not a bad result for a couple of hours.

If you have any questions let me know in the comments.