Using SPIKE Prime for GBCs
Posted by Huw,I've been building Great Ball Contraptions (GBCs) for around five years now and enjoy displaying them at public events. It has been frustrating, therefore, that I have not been able to do so over the last 18 months or so.
It was particularly frustrating because 45678 SPIKE Prime, the new LEGO Education robotics platform, was released last January and I had planned to build and display a number of modules incorporating it during 2020.
Thankfully, public displays have now recommenced in the UK, and I exhibited at three of them during August, which finally gave me a chance to test out my SPIKE-powered modules over a long period of time.
I posted a video of one of them in the Worldwide GBC Facebook group last week which gathered requests for more photos and information, so I thought I'd write about here because it might also be of interest to a wider audience.
GBC Ball Sorter
GBCs use 14.2mm diameter balls that are nowadays found in sets as minifig footballs, bowling balls and so on. Normally they are white and orange, so I wanted to build a module that differentiated between them and sent them in separate directions. The SPIKE light sensor should enable the colour of balls to be determined and some action taken depending on whether it's white or orange.
(Note: I'm not going to cover other GBC standards in this article; you can read them at GreatBallContraption.com)
The machine, therefore, needs to accept a batch of balls, feed them one at a time to a light sensor, then send them in one direction or the other depending on their colour.
To make it obvious to the observer what is happening I wanted to queue them up after sorting, then release them as a batch once ten of a particular colour had passed through the sensor. For good measure I will also count the balls as they pass and display the total on the hub's display.
Here, then, is the result of many hours of trail and error that now works reliably:
In addition to the large SPIKE Prime hub, it utilises three medium motors and one light sensor. One motor drives the belt, another sorts the balls, the third releases them.
Lifting the balls
Using a conveyer belt to lift balls and get them in a single file is the most efficient, but also the most boring, way to do so. However, I was not concerned with that as this section of the machine is not its main focus.
The yellow liftarms on the side of the machine act as an agitator: as the belt rotates the 3l arm pushes the 7l one upwards, which moves the hinged base of the 'in basket' upwards which helps prevent balls getting stuck.
The new large Technic frames make building assemblies such as this much easier.
As motor speed can be set in the code there's no need for complex gearboxes to slow them down, therefore it can be directly connected to the belt.
I also used the large Technic frames to simplify the machine's chassis and reduce piece count.
Colour detection
The clever part of the machine is this assembly which contains the colour sensor, and the ball moving and ball release mechanisms.
Balls roll off the conveyer and come to rest on the red section below. When the colour sensor mounted above it detects there's something there, by measuring reflected light, it takes a colour reading. If it's white, the black 'X' is rotated 180 degrees clockwise, if it's anything else (the sensor can't detect orange) it moves in the other direction.
Balls are then queued until ten of the same colour have passed under the sensor at which point they are released to the next GBC in the line. This is accomplished with a gate mechanism.
Here, the gates are both closed.
When it's rotated in one direction balls in the front ramp can roll across it and pass to the next machine.
When it's rotated 90 degrees in the other direction, the balls in the rear ramp are released.
The code
Nowadays, SPIKE Prime can be programmed in one of three ways, including Python. However, When I started on this module, Scratch code blocks was the only method available. To be honest, I quite like them, so I've stuck with them.
The program's main loop is below, top left. When the program starts, the initialise routine is executed before looping round forever looking for balls.
I'm not going to explain everything because, thanks to simplicity of Scratch, and the modularity of the program, you should be able to work most of it out.
The key thing, however, is detecting when a ball is under the sensor. This is done by measuring reflected light which varies depending on the proximity of the object under the sensor and how much light it reflects. By trail and error, I found that it reads above 80 when a ball is under it.
So, when a ball bas been detected, its colour is measured. If it's white the moveWhiteBall routines is executed, when it's anything else moveOrangeBall is run.
Initialise sets the speed of the motors, moves and releases balls by calling the appropriate routines, in case there are any left in the machine from the last time the program was run, sets up counters and starts up the conveyer belt.
The top four routines below are concerned with moving and releasing balls, while those at the bottom are triggered when an orange or white ball counter reaches 10, at which point the counter is reset and the balls released. While that's happening, the belt is paused.
The pair in the middle are triggered when buttons on the hub are pressed. They display the number of orange or white balls that have passed, and also release the balls, which is useful in the event of a malfunction.
The total number of balls that have passed through is also counted and that is displayed every time 10 are released.
The code can be downloaded here. Rename the file sorter.llsp then put in your SPIKE directory, which on Windows is C:\Users\{username}\OneDrive\Documents\LEGO Education SPIKE. Start a new project, then File|Open...
Trial and error
Developing GBCs requires a lot of trial and error. In addition to the usual mechanical aspects, the software requires a lot of adjustment during testing, too. Here are some of the problems I had to overcome:
- The angle of the ramp determines how fast the balls drop to the 'X' below the sensor. If they hit it too quickly, they bounce and if they are still moving when the 'X' moves, the mechanism jams.
- The speed at which the 'X' ball mover turns is critical: too fast and the ball is 'thrown' around the corner which can result in it getting jammed between it and the sides of the ramp.
- The speed of the belt, and thus the throughput of balls, has to match the speed at which balls can be detected and moved. As it is, the machine can't quite handle the GBC standard of 1 ball per second but in practice GBC circuits are never usually run at that speed for any length of time anyway.
- Occasionally white balls would get stuck in the release mechanism resulting in not all of them passing through it. Since the video was taken, I have redesigned the gate to help prevent that, and it now releases twice in succession which seems to have cured the problem.
Although I used 45678 SPIKE Prime Set as the basis of this module, you could also use 51515 Robot Inventor, which contains enough motors and a colour sensor.
Anyway, I hope you found this interesting and perhaps encourages you to start building your own GBCs. If so, join the Facebook group and let us know how you get on.
Further reading: I wrote about a predecessor to this machine and another one in an article we published last year when SPIKE was first released.
75 likes
14 comments on this article
I like the idea of programmable GBCs, they add another dimension to the design and build process which I appreciate since I'm a coder anyway. I've just never had any LEGO robotics kit in my life, because they're way too expensive. That's not yet taking into account that SPIKE Prime is an educational set, which means an even higher price (though not by very much; the price jump from 17101 to 51515 is actually bigger than the price jump from 51515 to 45678).
I guess I'll stick to Powered Up for now. If I build a GBC, I want it to be relatively compact, and nothing like the sprawling Rube Goldberg machines you see at events.
GBCs, one of the things on my to-do list. So, yes, this was very interesting!
I know very little about GBC or Spike so this article was a great overview of the challenges and choices involved. Thanks!
What shows do you go to? Looks fantastic the colour reader adds so much!
^ As many as I can down south.
These educational sets would be great if the pricing wasn't as prohibitive.
Nice machine Huw, interesting this color seperator GBC. I've seen red and blue balls travel through big GBCs as well. I wonder for how long the balls will stay separated in the chain, I guess it depends on what kind of GBCs follow after yours, but it surely would be one of those mesmerizing components that make me hang around a large GBC chain for quite a while and keep it interesting. Good job!
And I just realized I could make a (simpler) color separator GBC with my LEGO BOOST set.. I think.
Is there a way to continuously power the SPIKE hub?
I find watching GBCs incredibly relaxing
Haha, I just found your BOOST color seperator article from two years ago, Huw! https://brickset.com/article/45287/using-boost-for-gbcs
^ Yes, I've been fiddling about with this module for a while! SPIKE is far more suitable than Boost for the purpose.
Though I have no desire whatsoever to build GBC's, I find them really facinating and love articles like this.
@Huw: does the "If reflection > 80%" line need tweaking depending on the ambient light, or does the sensor/hub dynamically adjust when turned on or every couple of minutes? Because I'm guesssing most conventions have much more light than most lego-rooms.
I guess the answer is I've not tried it in enough different environments to know. It's not been a problem so far though. The ball is illuminated by the bright light in the sensor so ambient light probably doesn't make that much difference to the readings.
For those wanting to start with GBC using Spike the following ball counter is a good introduction
https://legostudiovives.be/spikeprimesmallballcontraption/
It also features a spiral lift using the included flex hoses for raising the balls instead of a conveyor belt, but some other pieces still need to be sourced.
For those who enjoy watching GBC artistry at work the following in just unbelievable
https://akiyuki.jp/en/works/1983
I love these GBC articles! I've never been to a show, but have watch videos of these machines, and I find them fascinating! I like the color choices of this module too.