Introduction: How to Make an Mbed USB Slingshot ?
With some friends, we had the idea to use a real slingshot to play Angry Birds instead of using a standard mouse! The objective is to plug the slingshot into a computer over USB and to kill pigs with it!
The problem is that we had to find a microcontroller or a little board with a USB controller and a USB library already developed. Quite recently, Mbed has published a USB device library which is able to emulate a bunch of peripherals.
Combining the two previous ideas, we decided to design a real slingshot using an Mbed connected to a computer over USB to play Angry Birds!
Take a look at the video!
With a real slingshot, you tilt the slingshot and stretch sling. The idea to measure these was using:
- An accelerometer - this can measure the tilt by tracking the gravity vector (which way is down!)
- A rubber stretch sensor - this can be used as the sling, and measure how much it is stretched
The slingshot was crafted by Chris Jarratt, from a branch found in Epping Forest, London! This is the structure in to which I embedded all the electronics.
Step 1: What Do You Need to Realize Your Own Mbed Slingshot ?
- The main element is an Mbed. We are using the new LPC11U24 mbed but you can use the LPC1768 mbed instead
- A piece of wood in order to build the slingshot. I advice you to know a good carpenter ;) Take a look at Chris website
- Then you need some sensors to detect angle and strength:
- 3-axis accelerometer for the angle: ADXL345. You can easily read accelerometer data as an mbed library is available to use it
- A stretch sensor for the strength. We use this flexible cord which the resistance changes when stretched. To use this sensor, you also need a 10k resistor in order to read a voltage in a voltage divider circuit
- Two pin header sockets to plug the mbed
- A USB type B connector
- A USB cable A to B to connect the mbed to a computer
- A solder iron
- A breadboard
- A veroboard
Step 2: First Prototype
In order to test if the project was doable or not, we decided to make our first prototype on a breadboard.
The wiring is very simple:
- The accelerometer is connected over SPI to the mbed
- The stretch sensor which is a resistor is part of a voltage divider circuit. We will read the tension on p15 (an analogue pin of the mbed)
- The USB Type B connector is connected to the mbed over the specific D+/D- pins and provides power supply
To test that all different parts are working properly, we have implemented a very simple program to test all different parts:
- print accelerometer data
- print strength value
- move the mouse on the screen
Then we did some experiments to see that all was working:
- move the breadboard
- pull the stretch sensor
- move and pull the stretch sensor at the same time
- observe that the mouse is moving on the screen
This step took us 10 minutes to setup:
- the wiring is very simple
- accelerometer library already written
- USB device library available
- library available to read an analogue value (strength value)
- the most tricky part is the connection of the stretch sensor:
- In my opinion the best solution is to take two wires, strip them and twist the piece of metal in order to squeeze the stretch sensor.
The program is accessible on the mbed website: angry birds test
Now that we know that the project is doable, let's embed all the electronics in to a real slingshot ;)
Step 3: Put It in a Real Slingshot
After having checked that the project was doable, you can now embed it in to a real slingshot.
Wires will also be embedded inside the slingshot:
- two holes has to be drilled to connect the stretch sensor to the mbed
- one another hole is needed to connect the USB cable to the mbed
Now that you have the slingshot, you can solder on a veroboard:
- The accelerometer
- The resistor
- Two pin headers for the mbed
- All wires embedded in the slingshot (two for the stretch sensor and 4 for the USB connector).
You can then check that all is working by using the program used in the previous step to check that everything is working.
Step 4: Develop the Software for Your Slingshot!
The first question concerns the USBMouse. The options are an absolute or relative mouse. The answer is quite simple because the mbed doesn't know the absolute position of the bird on the screen, so the natural solution is a relative mouse (like a normal mouse) - position the cursor over the mouse, then the slingshot takes over and moves relative to the starting point based on interpreting the manipulations of the slingshot.
The angle of the slingshot is the main thing: we need to calculate the direction of the vector to apply to the mouse position.
We simply use the fact that we know gravity is causing a 1G force on the accelerometer, and use that to calculate the angle of the slingshot with some simple trigonometry.
The mouse position is then calculated based on the vector offset calculated using the angle of the slingshot, and the stretch sensor reading.
Because we send relative movements, we actually calculate the desired position, then work out the difference from where we know we are and send that.
The general idea for how it works for a complete firing comes in a few steps:
- We start by WAITING, with the cursor over the bird - regardless of how we tilt the slingshot, nothing happens
- When we see a strong enough stretch, we consider that the start of AIMING, and click and hold the left mouse button
- We then continuously calculate a vector based on the angle of the slingshot, and the stretch of the sling
- This is translated in to relative mouse movements with some more trigonometry, and the mouse is moved as appropriate
- As we are positioning based on a vector but sending relative mouse positions, we keep a note of the accumulated movements so we can send the difference each time
- We enter FIRING when we see a fast reduction in the sling stretch
- At this point we release the mouse button, then return the mouse back to the starting position, ready for the next throw!
You can find the previous algorithm implemented in this program. But you have to be aware that it is dependant on several things:
- the accelerometer position
- the connection and the length of the stretch sensor
The first time that I tried my slingshot, the cursor on the screen was not coming back exactly at the same initial position after a shoot. To solve this issue, I modified this on Windows:
- go into the Control Panel
- Hardware and Sound
- Mouse in the devices and printers section
- in the pointer options tab, deselect Enhance pointer precision
- You can also reduce the cursor speed
Some fun with the mbed USB slingshot:
Turns out it works! Here it is in action!
- mbed website
- slingshot article on the mbed website
- mbed USB library