Tuesday, May 7, 2019

Some tips on using the Smart 2 Wheel Arduino Robot chassis

I'm a big fan of these basic 2 wheel chassis.  They're cheap to buy.  I've seen them on eBay for less than £6.00 without the batter holder.  There is enough space on them an Arduino or PiZero with a motor controller alongside the battery pack.
2 wheel smart robot chassis
They're usually listed as "2 Wheel Smart Robot" with the word Arduino added in there somewhere even thought they are not Arduino specific.  I think it's there to improve SEO for all the people making their first robot using an Arduinio.  Also, they are not smart, there is no intelligence provided.  They usually come with  tachometer encoders for the wheels, but you need extra electronics to read them.

I've used these chassis a few times and these are some gotchas that I'd to figure out.

1. The perspex is clear
If the perspex is opaque or brown in colour then this is a layer that can be pealed off.  It's there to protect the perspex.  You can leave it on but it's supposed to come off.

2.  The caster goes to the back.
Even though most of the pictures you see show the caster wheel at the front this is really a drag wheel and should be at the back of the bot.
The fact the caster spins means if it is put at the front then you're pushing against the caster when turning.  It's like having a wobbly shopping trolley.  Easier to pull behind you than push in front. Also, you can see from the image the (correct) front has more mounting options for sensors which makes sense.

3.  The battery box can be screwed on the underside
All pictures I've seen always have the AA battery box on the top.  Makes sense as gravity will keep the batteries in.  But, if needed the battery box can be screwed to the underside of the deck giving more real estate on the top for other components.

4.  The connections on the motors are very fragile
Where you solder the wires to the motors is very fragile and prone to snapping off if put under any strain.  So, it's best to put in place some strain relief.
The easiest and without needing any additional hardware is to knot the two wires around the plastic fastener holding the motor in place.  In this way if someone pulls on a wire or it gets snagged it doesn't put any pressure on the actual connection on the motor.

Strain relief on the motor wiring

Some final thoughts

I've used these motors with L298N as well as L9110s motor controllers.  Both work great.  The L298N is bigger and really over specced for these little motors so the L9110s is more than capable of driving them.
The cheap L9110s board only has one GND connection and assuming using a different power supply for the motors and the controller (Pi/Arduino) you'll need to tie the GND from the different power supplies, the controller and the L9110s together.  I usually use one of the additional GND pins on the controller.  Arduino Uno has three GND pins, the Arduino Nano has 2 and the Raspberry Pi has 8.

L9110s - only one GND connection


I've heard reports that as the gears are plastic they can be shredded.  A lot of fast reversing with a decent power supply can put the cogs under a lot of strain.  This is one of the main compromises for the price.  So, either limit the speed at which the motors can change direction or be ready to replace the motors.

These little yellow motors look like they're all the same, but the gearing inside them can be different.  If you do need a replacement motor you may not get the same gearing ratio which would affect the ability of your robot to go in a straight line or just move consistently.

Even with all this I think these kits are a great start.  They're easy to put together.  Reliable (enough), powerful (enough) and cheap so a great place to start.
Add an Arduino Nano and an L9110s with a small breadboard to connect everything together and you have a capable platform to develop from for less than £10.00.
You can then add ultra-sonic sensors, line following sensors, infra-red remote control, Bluetooth interface, or even the tachometers to measure speed and lots more...
Giving an easy and cost effective introduction to robots.
This may not be the chassis you finish with but it's definitely a good place to start.
And if you're already thinking of upgrades there is a 4 wheeled version as well for not much more.

4 Wheel Smart Chassis

Happy roboting.








Thursday, February 28, 2019

Arduino - avrdude: stk500_getsync() not in sync - How to fix


This is  reminder for me and hopefully useful for others either new to Arduino or using different boards.

Sometimes on uploading you can get an error message

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00




Of course the attempt number changes as it tries 10 times.

There are a few relatively obvious things that can help stop this error message.

Select the right board.  If you're using an Uno, select Uno. If you're using a Leonardo, select Leonardo.

Select the correct processor: ATMega328 is the Uno one and the default

Make sure your USB port is selected.

Even after doing both of these and it all looking right you might still be getting the error message.

This is where we get into a discussion about Arduino compatible boards.  The Arduino is an Open Source design and so it is perfectly legal and acceptable for anyone to copy the design and make their own boards and sell them (but they cannot call it an Arduino, topic for another day)

Some of these compatible boards use a different USB chip (CH340) and the ATMega328 could have a different bootloader to the actual Arduino.

if you are using a compatible board that has a CH340 make sure to install the driver for the chip otherwise you may not even be able to see the board in the IDE.

Then for the bootloader there is an alternative option under the Processor called "ATMega328P (Old Bootloader)"  Not where I expect to see an alternative bootloader option but there it is.


ATMega328p (Old Bootloader)


This is the one I have to use for compatible boards.

So, once the correct board, processor, and port are selected you should be able to program your Arduino with ease.

Finally, finally. If you're running Linux and not Windows there is one more thing to take into account and that's permissions.  By default a regular user account does not have permission to read/write to USB serial ports so you have to add your user to the group with access. 

The command you need is as follows where [USERNAME] is replaced with your username.

sudo usermod -a -G dialout [USERNAME]


WARNING: This command is run using sudo and so has root access.  It's always the right thing when finding commands online that use sudo to do your own research before blindly using the command.
So, here's where I got the command from https://www.arduino.cc/en/Guide/Linux/


Hope you find this useful as I expect I'll be back here again when I set up my next computer or buy some new compatible boards.


Arduino and NRF24L01 - test code



A while back I made a game using some Arduino Nano and NRF24l01 called ButtonFlash (https://www.winkleink.com/2016/08/buttonflash-game-made-with-raspberry-pi.html)

Mnadatory video showing it being played.



It was good fun to make and people seemed to enjoy it but it wasn't in a state for someone else to make easily and wasn't robust so running repairs were needed.

But I really liked it and wanted to do a version that others could make easily.
At the same time EasyEDA was starting to be advertised as a simple web based PCB design tool with integration to a PCB fabrication facility so getting the boards manufactured would be super easy.  So, I took the plunge and designed a PCB and had 10 of them manufactured.

Soldered up

I soldered a pair and started running  code to test them and it kept failing.  I tried different libraries and different setups and every time I couldn't get it to work reliably.  This is part of the pleasure of using code and electronic components created by others.

Today I figured out one of the NRF24l01 that I used can transmit but not receive.  Maybe I damaged it as had power caps in the wrong way around when testing early on.

I soldered up a 3rd but without the switch and LED and ignoring the bad one the pair I used were reliable today.



Now as I tried lots of sample code and had different results with them I have prepared my own tester code.
This code uses the Arduino, NRF24l01, LED attached to Pin2 (through current limiting resistor) and switch on Pin3 using internal pull up.

When uploaded the Arduinos start in transmit mode.
Press the switch on one and it transmits and switches to receive mode.
Now you have one in transmit and one in receive mode.

Pressing the switch on the transmit one and it's LED will flash 5 times and change to receive mode and the receive one will turn it's LED on full and change to transmit mode.

It's amazing how much fun it is to press the switch. See the change. Then press the switch on the other one and see them swap roles.

So, if you are messing with Arduino and NRF24l01 then maybe this code will be useful to help you.

Code is available on GitHub 


Now to actually make ButtonFlash 2.0.



Thursday, November 8, 2018

Electronics Maker Christmas stocking fillers list


It's that time of year when my family ask me for ideas of presents for Christmas and my Birthday.  (both December)

With electronics bits being a bit specific/esoteric I stopped trusting that if I said what I wanted they would get me the right thing after I gave my wife a link without a description for a new CPU.
By the time she clicked it the link redirected to a few screws.  Luckily she checked before buying and I was able to get her the alternative link.

So, these day I provide a wish list email with more items than I expect to get so I still have the surprise when I open the packaging as to what they give me.

This year I've decided to share the list.  This list may be added to as I think of more things.
Before you ask things like Arduinos, Raspberry Pis, Circuit Playground boards are not on this list as these are things I have usually already got a stash of.  This list is for the other bits that make those things useful.  The kind of things it would be useful to have before starting a project but may not have already or may have run out of.

I try to keep the items to less than £20 so for many these are good stocking fillers.


USB Current Tester - £4.50
I buy random items off the Internet and for batteries and USB PSUs I want a way to see if they meet their stated ratings.






Heat Resist Soldering Mat£9.90 - £15.00 - There are bigger and smaller ones out there as well.
I have a regular A3 cutting mat but like this because of the different compartments to keep bits as I regularly drop screws and tools and hope this will help me be more organised when working on something.




I sometimes use buttons with spade connectors and at the moment I solder the wires on which is not ideal.  Want to start using the correct connectors for the correct job and not just bodge something that later causes a headache that I have to fix.




Anker 4-Port USB 3.0 Hub  - £10.99  
Sometimes for the Raspberry Pi I need to burn multiple SD cards.  The great Etcher will burn multiple images at the same time so with this I can add 3 more to be burned in parallel (it's not 4 as I lose a port to connect it)




Starting to try and be more organised so like the idea of this set of wires.  I have loose spools of wire around the place so the plan is to replenish the spools when they run out.



Saying that I still do breadboard stuff so it's always good to replenish the stock of Dupont jumper wires.



Having recently committed a project to an actual prototyping board rather than using a breadboard I want to do more of that for my Raspberry Pi projects so need a bunch of 2x20 female connectors to hand.  Last one I used I 'borrowed' form a Kickstarter I backed, so that's now an incomplete kit.


I'm always looking for microSD cards, so every year I add them to the stocking filler list.
They're small and I'm clumsy so replenishing stock is an ongoing activity. 




Friday, August 24, 2018

Workbench in the garage

I've been meaning for a while to set up a permanent workspace for doing electronics and finally cleared a corner of the garage.

Once cleared I of course needed a workbench and whenever I searched on line this steel bench kept coming up. Listed on the UK eBay for about £50 which to me seemed like an OK price to pay for a bench with main surface size of 120cm x 59cm and with a drawer the full length of the bench.



I asked on Twitter what it was like and a few people replied that the metal is quite light but it's sturdy once built.

I got one and for £50 I think it's definitely worth the price.

The metal frame is not the strongest or thickest metal I've seen and without the wood tops being in place it does flex. I expected to have to add braces at the side and back but in the end they are not needed as once the work surface and other wooden pieces are added it gets nice and rigid.
One thing to be aware of is the wood is MDF and so has limited strength in itself.
The lower shelf on mine is flexing already due to the boxes I have on it.  I note this in the video below and also my plan to add some wooden battens to support the lower shelf.
The lower shelf and the work surface are not screwed down and are held in by the frame so it will be easy enough to lift out the self add the battens and put the the shelf back on.

The draw was also pointed out to be quit flimsy and I agree. It's not robust but it works and for tool and storage for light parts it will do the job.  Again the base on the drawer is MDF.

The back pegboard on mine is slightly bowing out.  Not sure if I've done something wrong or if it's the kit.  The top shelf is OK.  Again I'm not expecting it to take much weight but as a place for odd small bits it will work well. It's also high enough above the main work surface that it should be ideal for adding additional lighting in the future.  Maybe some RGB LED strips 

Finally, the instructions are really poor.  The kit I received wasn't missing any parts but let's say with my flat pack skills it took me a fair while to get it all together. 
The one tip I'd give is watch the number of holes in the metal parts to figure out which way is up.

So, overall I'm very happy with the bench. I've used it a fair bit since getting it and I like the size.  Much smaller and it would be a challenge to have equipment out and have a decent sized work surface.  (There is a smaller sized version that's 81cm x 41cm and from searching eBay there are also versions without the back and top shelf.

Finally, here's a video of the bench so you can see what it's really like.



Onward and upwards with the making. 

Tuesday, August 21, 2018

Shakey Shakey - a game using title switches

For the most recent Egham Raspberry Jam @EghamJam we did two workshops.
One was NoPi Blinky, making LEDs blink without any programming. More to come on this as during the workshop we found a few typos.

The other was Shakey Shakey.

For the workshops my goal is to do something slightly different that not only teaches the attendees something new from either a programming or electronics standpoint but also shows them creative options.

Shakey Shakey was based around really simple tilt switches.  They're basically a small tube with a contact inside that moves as you tilt the tube.  Really simple and really cheap. On eBay UK they're about £1.50 for 20. So, ideal when you need a bunch for the Jam
Tilt Switch

The only other things needed then were some female-female jumper wires and some 2 core wire.
I cut the female-female wires in 2 giving me a pair of wires that had the female connectors on one end and bare wire the other.  Cut the 2 core wire into about 150cm lengths and used them to connect the switch to the other wires giving a nice long wire for the kids to shake without (hopefully) unplugging the wires or pulling on the Raspberry Pi.

With the hardware sorted I them did the worksheet which is available to download from GitHub along with other Jam worksheets.

Shakey Shakey worksheet
The worksheet goes through:

  • Connecting a tilt switch
  • Python code to read the switch and display how many times it connects
  • Adding a pause to deal with debouncing
  • Adding a second tilt switch
  • Updating the code so the switches have to be activated alternatively
  • Final update of the code to add a timeline making it into a game

We had a full workshop and the kids who took parts seemed to really enjoy the coding and then playing the new game they created.

At the end I challenged the participants to come back to the next Egham Raspberry Jam (21st October 2018) with their own program that uses the same tilt switch controllers.  The worksheet at the end gives some suggestions.  Here's looking forward to the 21st of October to see what fantastic creative ideas the kids come up with.





Tuesday, June 19, 2018

Cat or Dino- Scratch 2.0 game



I saw a tweet from the @Raspberry_Pi Foundation about a game called Knifey Spoony by @Jonic one of their senior web developers did using pico-8.





The idea of the game is to select if the image is a spoon or a knife as quickly as you can with a timer running getting faster and faster the higher your score, so the pressure is on.

I thought this could be done with Scratch, so I fired up Scratch 2.0 and had a go.

Here is the result.  It uses only built in graphics making it easy for anyone to replicate and extend.
It is done with Scratch 2.0 but there is nothing special so it should work with Scratch 1.4 on the Raspberry Pi as well.

https://scratch.mit.edu/projects/229582133




Wednesday, February 21, 2018

PiGPIOPin - when you want to use pin numbers and not BCM on the Raspberry Pi in Python

A while back I was doing some bare Arduino compatible boards using the ATMega328 chip. From this I saw that Arduino did not map the pins on the IC to the same numbers on the Arduino.  They used a simple order on the Arduino board and the Arduino ID so it was easy to find the right pin for wiring and also for your code.

Only problem is when you use the chip with the bootloader the mappings are different to the pin number so you need a lookup tool to know when you want to use Arduino Pin 13 that it's ATMega Pin 19.
After a while this became a bit annoying so I put together an .h file with a bunch of constants that got around this.

http://www.winkleink.com/2014/01/arduinopinsh-converts-atmega328-pins.html

On the Raspberry Pi there is a similar situation.  The default numbering mechanism used is the BCM numbering which is great if you're operating at chip level, but at a board level it means things like:

BCM GPIO 5 is board pin 29

I still have to count pins to get to board pin 29, so having to also know it's then GPIO 5 in my code can sometimes be a bit annoying.

The original RPi.GPIO library allows you to choose BCM or Board numbers

 import RPi.GPIO as GPIO  

GPIO.setmode("GPIO.BCM")
or
GPIO.mode("GPIO.BOARD")

Giving the user the choice.

With the fantastic GPIOZero BCM numbering is enforced and there is no option to use board numbering.
GPIOZero is a great library and makes controlling motors, working with MCP3008 (analog input) and distance sensors a lot easier.

For those who want to use board pin numbers and want to take advantage of GPIOZero the use of the BCM numbering can be annoying.

To make it easier I decided to do a similar lookup table like I did for the Arduino.
Mapping Pin number variables to BCM numbers.
The code and an example is on GitHub.

It's straight forward to use.

Have PiGPIOPin.py in the same folder as your code or in the python path.

Put the following line at the top

from PiGPIOPin import *

Then instead of using something like

red = LED(17) # 17 is the BCM number that is at board pin 11

use

red = LED(PIN11) 

Each BCM number is mapped a PIN* variable.

The rest of the code then works as normal.

Using this gives the option back to use board pin numbers and not BCM numbers.





Thursday, February 8, 2018

More cheap stuff from eBay - Gaming Player Handheld Video Tetris Game Console Kids Children Boy Portable Toy



I'm drawn to the underside of eBay. The placed where strange bits of electronics are sold.  This interest comes from making things and needs odd bits of electronics to realise my (awful) creations.

On one of my regular trawls of ebay I found this.

Gaming Player Handheld Video Tetris Game Console Kids Children Boy Portable Toy


Looked interesting.  OK the display is obviously mono-colour LED and not back lit but for £1.39 I thought it was worth a punt.

From the Listing the dimension are: Product size: 14*8*2cm
Bigger than the Sony PSP Go 12.8*6.9*1.64cm
If Sony can squeeze a 4.3" widescreen display in the PSP Go then this could be a very useful case to work from. A nice sized case with 10 buttons.

For reference a Raspberry Pi Zero is 6.5cm x 3 cm.  An easy fit and with dimensions greater than the PSP Go one of the many 3.2" LCD displays I see running from a Pi should fit nicely.

£1.39 for a case with 10 buttons that should easily take a Raspberry Pi Zero and 3.2 " screen with space for other bits is a bargain.

I see a plan coming together so I ordered one two weeks ago and it arrived today.

The packaging is the same as the listing which is always a good start.



Opened the box and inside was the actual console. OK a bit rough and the sticker isn't straight.  Not a problem as I plan to remove all that anyway.




But there was something else not right.  I checked the listing again and confirmed the dimension Product size: 14*8*2cm

The dimensions on the actual one I received seem a bit off.  Annoyingly I don't have a ruler with me at the moment so I'm using AAA (not AA) as the standard measure.

An AAA battery according to Wikipedia is 4.45cm long.
From the picture below it looks like this may not be 8cm wide or 14 cm in length.



It is more like 9cm in length so a Raspberry Pi Zero may still fit inside and 5cm in width.
There goes the plans for a 3.2" LCD display. Maybe one of the  0.96" OLED displays might fit.


I almost forgot.  What about the 999 games.
It does have Tetris, a variety of Breakout games, a Tank combat game and a Driving game.

The driving game is especially bad as there are only 2 lanes and you just move from left to right as the cars come down the screen.




In summary how does it compare to the listing title.

Gaming Player Handheld Video Tetris Game Console Kids Children Boy Portable Toy
It is a game playing handheld video Tetris Game Console.
No kids, children or boys included, but it definitely is portable and a toy.

As an added bonus you also get the amazing driving game, breakout and tank combat games, so in a way it over delivers on the gaming promise.  I didn't count whether there are 9999 but I suspect we all know the answer to that.

But, if your reason for getting it is to have a 14cm x 8cm x 2cm case with an opening to put a 3.2" display and a Raspberry Pi Zero you will be very disappointed as it's most definitely a lot smaller than the listing.

It is held together with screws so maybe a teardown is needed to see what's inside and if there is anything I can salvage from it.



UPDATE:
I think I may have found an actual correct listing for the one I purchased
http://www.ebay.co.uk/itm/272730431909
Listing states the dimensions are: 10 x 4.5 x 2.4cm

Then there's also this listing for £1.79
https://www.ebay.co.uk/itm/222452404318
With the dimension listed as: 15.5*6.5*2.5cm

If the larger one really is those dimensions then a project may be on to use it as a shell for something more fun.





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.