Pi-Hut Christmas Tree

IMG_20171226_201348727_HDR[1]My son was fortunate enough to receive a Pi-Hut Christmas tree for an early Christmas present. He’s only eight, so I wanted to supervise his soldering, but I needn’t have worried – the only problem encountered was when I fitted a missing LED, fitted it to the wrong side, removed it and broke the leg. Fortunately, I had a spare lying around in the shed so it was an easy fix.

The Christmas tree is a lovely kit. Gold plating means that the soldering is effortless and the edges have also been guilded for a bit of luxury. The whole board sits rather neatly on top of a Pi Zero W. I’ve been logging in remotely with SSH, but a development I’d like to try is to use BlueDot to remote control it from a phone.

There is a set of instructions for installing the gpiozero software, although I’m unfamiliar with this – I’ve always used rpi.GPIO in the past with good success. One thing I found missing was a map of all of the LEDs. They’re all numbered, but that doesn’t relate to anything on the pin connections. Mapping the outputs (BCM or Board number) was a bit of a mission but I think I’ve succeeded.

LED

Output (BCM)

Output (Pin)

Star 2 3
1 4 7
2 15 10
3 13 33
4 21 40
5 25 22
6 8 24
7 5 29
8 10 15
9 16 36
10 17 11
11 27 13
12 26 37
13 24 18
14 9 21
15 12 32
16 6 31
17 20 38
18 19 35
19 14 8
20 18 12
21 11 23
22 7 26
23 23 16

24

22

15

The next step I’d like to try is to run this from ScratchGPIO, possibly using SID as a starter so that I can run Scratch on a laptop and transfer it over later. For the meantime, I’ve just got a couple of alternative python programs to play with.

All-in-all, this has been a lovely kit that uses loads of outputs on the PiZero. Soldering is effortless and programming with the standard program supplied was easy. One day I’ll read the docs for GPIOzero, but for now, I’ll be teaching with Scratch.

Merry Christmas and a Happy 2018 to everyone!

 

 

 

 

Advertisement

Game programming in Scratch

xwingpicNot much to write about this time round (seems time is getting even more sparse these days…) but I wanted to post a little game created with my son in Scratch.

Usually, Scratch programming gets put on the back burner – I’ve often got other projects in mind and so I tend to neglect Scratch. This half-term, my son asked for an introduction to programming and so game writing was on the cards. We worked through some simple thoughts and it ended up being inspired by the Star Wars films since he’s quite a fan.

I particularly wanted to show how a simple game can be built up incrementally, starting with simple movement of a space ship and then adding asteroids that have to be dodged. These were given a simple animation of top-to-bottom to give an impression of forward movement.

Once this was working, the detection of hits came in. This was relatively easy – I’m always so impressed at how Scratch offers this as standard, particularly as I’ve tried writing this type of game as a late teenager in Forth on my Jupiter Ace. With that, the nearest one could get was redefining the character set to look like the objects being controlled and then deciding if you’re near enough.

The next stage was to add some kind of missile or torpedo. I know there are a number of ways of solving this but my preferred method was to make the spaceship’s x-position into a global variable (Available to all sprites). This is an excellent introduction to variable types, particularly as we forgot to tick the correct box initially.

A simple game like this is an excellent introduction to programming – it ticks all of the boxes of sequential instructions, conditional branching and iteration, with a generous smattering of local and global variables.

Our little game doesn’t have any custom sound effects, or a timer. These are no real challenge to add.

The final file is available to download for anyone who wants to try it.

Construction Brick Scratch Interface Device

SID-Lego

Lego compatible Scratch Interface Device

I’ve been thinking about this for a while. It’s all very well having a battery pack connected to some motors and LEDs with Lego, but what about making it all controllable remotely from Scratch running on a laptop or desktop?

In previous articles, I’ve looked at using the Raspberry Pi and the Lego firmly next to each other, with a wireless keyboard and mouse and that oh-so-bulky monitor cable.

Then I figured out what the implications of “Scratch Interface Device” meant when I spotted it on CympleCy’s website.

Having bundled the Raspberry Pi into the case and added a set of LEDs and switches, it was a simple job to install SID and get working. The last four digits of the Raspberry Pi are used to identify it, and these must be entered on to the PC (Or Mac, if you have one). Once this is done, starting Scratch and enabling Remote Sensor Connections allows the laptop to control the Raspberry Pi without having the bulky connections. Now it’s possible to seriously integrate Lego and intelligent control.

ChristmasLights (4)

ScratchCPIO program to run the lighting

The whole setup now runs faster as the Scratch project runs on a PC, while the Raspberry Pi handles the GPIO control remotely. I haven’t yet tried all of the functions, but all those that I’ve tried have been supported. My PiBlox case has space for the Raspberry Pi camera, so running “Broadcast Photo” causes a photograph to be taken and stored on the Pi’s SD card. At the moment, it’s directed to /home/root/photos but maybe that’s something I’ve done wrong – ScratchGPIO running directly sends the files to /home/pi/photos which is a little more convenient (more later on why…).

Now… I could have stopped here, but inspiration has now kicked in. At the time of these early experiments, I was using an external battery pack and then…

…the Raspberry Pi Zero was released!

And the reason I’m now thinking of experimenting further?

Proposed Lego compatible Scratch Interface Device:

  • PiBlox case
  • Raspberry Pi Zero… when I can get hold of one.
  • WiFi dongle – perhaps a hacked and built-in version?
  • Li-Ion battery and 5v converter/charger
  • Connector setup to suit 0.1 connectors easily at the side of the case. Digital (and analogue?) Inputs, Motors and Servos.
  • Motor and GPIO controller (Explorer pHAT or maybe PiRoCon v2 depending on the size)
  • Mini amplifier PCB and small speaker.

For now, here’s a video of some of the work in action.

Lego and Raspberry Pi controlled missile launcher

Scratch program

Scratch program for the Missile Launcher

My son has had his eye on these things called “spring shooters” for a while… and so have I. I picked up a few from BrickLink and managed to incorporate them into a little experiment I wanted to try.

This little build uses four spring shooters and two servos controlled by the Raspberry Pi. The program, written in Scratch and interfaced using CympleCy’s ScratchGPIO7 allows rapid firing of the spring shooters by dislodging each in turn. The servo has been set to nudge the end of the dart and then return to the centre position so that the dart isn’t gripped by the actuator. This seems to work well, although I had a few problems with not initially allowing enough time for the servo to travel to its full extent.
The program allows control of the panning with the arrow keys, as well as individual control of the darts by pressing 1,2,3 or 4. Alternatively, press [f] – “fire!” for a rapid salvo directed at your target. I’ve also added a couple of warning LEDs to the base. Green is safe… and Red Flashing warns people to duck. The next step is to add a PIR sensor and let Scratch handle our security needs…

I’m sure that those with experience of OpenCV will use it to track targets and deal with them automatically.

Raspberry Pi controlled Lego model

Lego Space base

Lego Space base using two servos and LEDs.

Here’s a simple creation to illustrate the kind of things possible using the Lego interface I’ve built along with servos and LEDs.

I’ve put some kind of radar antenna on top of a large servo. Scratch can be easily programmed to make the servo sweep backwards and forwards, scanning for distant incoming spacecraft. I’ve put a green LED into the top of the radar which flashes constantly. The light projects down the transparent green antenna, making for quite an attractive show.

Getting into your spaceship is quite tricky given that the crew compartment is at the top. There’s a miniature servo connected to a lift arm. Pressing the Spacebar triggers a Scratch routine to lift the arm, wait enough time to transfer the crew and then lower to the ground.

I suppose the next step would be to add a flashing red LED to simulate thrusters along with sound effects played from Scratch.

Lego – Raspberry Pi interface

Lego interface

Lego interface – a L298 H-bridge motor controller with a servo buffer board.

Here’s the current version of my Lego interface for the Raspberry Pi. It uses an L298 H-bridge motor controller (covered in a previous post) combined with another of my 74HC541 buffers to protect the Raspberry Pi when connecting LEDs and servos.

The whole thing runs from a 5v plug-top power supply which is adequate for driving the 4.5v Lego motors along with a bunch of servos.

There’s a bit of scope here for making the whole thing more compact but that’s another step once I’m happy with the design. I need to add input connections so that the Lego creations can be fully interactive.

It would be also interesting to see if I could make this into a Lego case, perhaps with a bunch of plates combined with 1 x 4 x 3 Panels.

Lego and Raspberry Pi working together

In a small way, I consider the Raspberry Pi almost like a Lego brick. On its own, it’s an interesting device, but it becomes more useful when combined with other components. What would be more natural then, than combining it with other Lego devices along with a few of my own devising.

In previous posts, I’ve shown how servos can be used to create some pretty whacky Lego devices, but equally I feel that individual bricks are open to hacking. Note… this is pretty gruesome stuff – drilling, cutting and filing Lego bricks. Even something rather similar to “The Kragle” ends up being used!

Rather than steal my son’s Lego, I’ve been using Bricklink as a place to buy specific parts. I’ll try to list their code numbers here so that they’re easy to find on the Bricklink website. Bricklink serves as a market place for people to sell their bricks.

In a follow-up post, I’ll show the current version of my Lego interface.


Lego LEDs

Lego LED

Lego LED in 2×2 brick

I’ve debated at length in my own head how to wire these. Essentially all the device needs is a resistor and LED to be mounted into some kind of transparent brick, or for the LED to be poking out of the top.

A standard 5mm LED can be fitted inside a 2×2 brick if it’s allowed to shine through the top. This does mean that other bricks can’t be stacked with it, but it is a neat solution. These 2×2 bricks are also available in clear (I bought Trans-clear, Trans-Dark blue, Trans-Green, Trans-Red).

To make this, you will need:

  • 2×2 Brick (Part No:3003)
  • 2×2 Plate (Part No: )
  • 0.1″ Female connector cables
  • LED
  • 100 Ω resistor (1/8 Watt is easier to fit in)

Lego switches

Lego tactile switch

Lego tactile switch

These are easily made with a 6mm tactile switch with a long actuator. A blob of hot glue can be used to keep the whole switch central while the rest of the brick and plate is assembled around it.

The switch is connected to a short cable with 0.1″ Female connectors on the end.

To make this, you will need:

  • 2×2 Brick (Part No:3003)
  • 2×2 Plate (Part No: )
  • 0.1″ Female connector cables
  • 6mm tactile switch
4-way tactile switch

4-way tactile switch on a PCB in a Lego frame

I’ve also had success with using 4 tactile switches on a PCB. The spacing between studs is 8mm, therefore it’s a fairly simple job to create a PCB which matches. I’ve linked one side of each switch to a common line to reduce the amount of cables required.


Servos

Lego large servo

A Large servo in a Lego framework

Connecting servos into a Lego system remains the biggest challenge. I tried a number of ideas before I settled with two possible solutions.

The micro servos seem to work best glued to a tile. It needs to be something that’s compatible with the plastic of both the servo and plate. Hot glue seems to work well here, although I have had success with some solvent-based cements. Polystyrene cement worked well for a bit, but surprisingly I did have on piece fall apart.

Larger servos require a different approach. I build a framework out of bricks and plates and that seemed to work well, but it does mean that right-angle parts are needed to mount it if the axis of rotation needs to be vertical. The egg-drawing robot used a large servo resting on a tile, with some small axles to centre the spindle within the Lego grid system.

To make a framework that supports a servo on its side, I used:

  • 2 bricks – 1 x 6 (Part no: 3009)
  • 1 plate – 1 x 6 (Part no: 3666)
  • 2 plates – 4 x 6 (Part no: 3032)
  • Optional – Bracket – 2 x 2 with 2 holes (Part no: )

All of the pieces need to be stuck together with solvent and then the servo can be linked into this with hot glue. It’s fairly neat, robust and fits into the Lego system well.

The servo actuator needs a little bit of treatment to make it useful. Small servos can use a Technic axle and pin connector (Part no: 3651) and larger servos will need modifying with a pulley (Part no: 4185) along with some small screws. I used PCB pillars to get the spacing just right.

Lego framework with brackets

Large servo in Lego frame

22mm Pulley for actuator

Servo actuator using a pulley

Lego framework

Largeservo and the Lego framework

Small servo

Small servo on Lego tile with actuator

Programming the Egg drawing robot

Scratch program for drawing squares on the egg.

The program to draw a grid of squares on the egg.

In order to program the egg drawing robot, a simple pair of loops are required. One draws the horizontal lines and the other is responsible for the vertical ones.

In this case, I’ve simply laid out a loop that:

  • Move to the left
  • Drop the pen
  • Move to the right
  • Lift the pen
  • Rotate the egg upwards slightly
  • Repeat ten times

Followed by:

  • Rotate the egg upwards to the start position
  • Move to the left
  • Drop the pen
  • Rotate the egg downwards
  • Lift the pen
  • Move the pen to the right slightly
  • Repeat ten times

In order to make the servos move over their required range, it was necessary to find out what the extremities of movement were – for example, how far can the pen be moved from left to right. This then gave a range of values. I subtracted the smaller value from the larger value. Then I needed to find the offset – the starting value. I then used a scale value to make my servos now respond to a range of 0 to 100. A bit difficult to describe, but a quick visit from the formula for a straight line: y = mx+c

Overall, the grid is fairly simple but it’s ideal for demonstrating control over movements, the resolution of the servos and the pen and just showing what can be done with some Lego and some servos.

Next up… hopefully bitmap conversion!

Egg drawing robot

I quickly knocked together this Egg drawing robot after seeing the “EggBot” somewhere online. I’ve been experimenting with building a Lego motor/switch/servo Raspberry Pi controller and it seemed like something that might demonstrate some capabilities of the various parts needed.

There are three servos: Pen Lift, Rotate egg, Side-to-side for the pen.

The servos are controlled using ScratchGPIO and to make the range of the servo match my coordinates I needed to dust off my O-level maths for the straight-line formula: y = mx + c

Ultimately, the project was a minor success. I’ve learnt that the egg needs to have warmed to room temperature otherwise the condensation makes it too slippery to grip. I also needed a sharper pen – I seem to have picked a “bluntie”.

Construction of the ScratchGPIO Dalek – Invasion part 2

Here’s the second installment of the ScratchGPIO Dalek – the first part can be found here. My apologies for the poor quality of the photos. Our camera was in use elsewhere so I only had my mobile. I’ve discovered that it can’t cope with low light and close up shots. Perhaps I should have used the Raspberry Pi camera?

MostOfTheBitsHere’s what I’m starting with. I’ve got a servo, some LEDs (red and blue), Chromed LED bezels and a few bits of plastic cut on a CAM router. The Dalek is a plastic container originally for bubble bath,but having removed the bottle it seems that there’s plenty of space for a circuit board, a servo and some odd bits of wiring.

EyeballThe eyeball LED is made from a piece of chromed brass tubing removed from an old telescopic aerial. The LED is soldered to the end of stranded wire and carefully sealed with hot glue. A clip-on LED bezel is used to complete the effect and makes for a realistic eyeball. I would have liked to add on some disks half-way along the stalk, but I didn’t really have time and perhaps it can be easily overlooked?

AssemblingTheHead
The head lights are made from blue LEDs soldered to the end of stranded wire and then pushed into some chrome LED bezels that were in my parts stock. At first, the LEDs were difficult to see as they are so directional and the bezel tended to shield the light coming out sideways. The solution? Fill the bezel with hot glue (in fact, overfill it) and then let it cool without disturbance. This created a slightly domed translucent light which can be seen from all angles. For such a simple solution, I’ve been really impressed with the result. And I didn’t burn my fingers. The domed lights are then pushed into the head. The slightly rubbery nature of the head means that I’m unlikely to need to put the nuts on the other side of the bezels. I don’t think I’d succeed either!

BufferboardThe bufferboard is one of my own designs (more information here) that I produced a while ago. I use it as I managed to kill a couple of Raspberry Pi computers while experimenting with servos. I’m not sure whether it’s a faulty servo that did it, but ever since I’ve been quite nervous to work without this board in the way. It also level shifts the outputs to 5v instead of 3.3v, making the LEDs (particularly the blue LEDs) able to run at their full brightness. Having said all that, I’ve used a 330Ω resistor in series.

ServoWheelThis part is a simple three-spoke wheel that fits on top of the servo horn. The outside diameter has been chosen to fit the inside hole under the dalek head. The three spokes gives plenty of space for the wires to pass upwards from the base. I had pondered how to get the servo horn to stay attached as I’ve found hot-glue to be a bit unreliable on these and there wasn’t really room for the M2.5 screws that I usually prefer. Cable ties to the rescue! They hold well, especially as I filed a few little slots in the servo horn. Simple and effective.

DalekbaseFitting the servo up inside the base was a bit more tricky. There are quite a few obstructions up inside the base which have to be taken care of. In the end, I cut out the top section carefully and then used added a foamed pvc plate which carries the servo. After nearly exhausting my supply of M3 screws, the servo was mounted and the horn fitted.

TwoPartsOfDalekHere are the final two parts ready for fitting together. The cables will pass down through the wheel into the base where they are then connected to the buffer board with 330Ω resistors. The servo is also connected. I ran a little test routine from Scratch to ensure that the servo can reach both extremes and also find the centre. Once this was done, a few blobs of hot glue ensures that the head and the wheel are joined.

Coming up… programing the Dalek with ScratchGPIO (and Python eventually).