Dodgy Jet: Creating a Rail Shooter Game with MPU6050 Motion Controls and Arduino in Processing
So I've been learning how to use MPU6050 just because of the versatility of its uses. I've been trying to learn it from scratch and extract angle data from it, and when I finally succeeded in getting it to work, my ambitions grew and just to make my learning complete, I started to develop it into a more complete setup. This is the whole project explained sequentially to take you on the same journey I went through. However I will provide all the code and 3D model files etc in the end, if you're looking for a tutorial, this is not the best place, but I'll try to provide links to all the necessary material here. This blog is meant more as a means of project display. So here we go...
MPU6050: Accelerometer & Gyroscope Sensor
This module consists of an accelerometer and a gyroscope sensor. The gyroscope gives us the reading of current angular acceleration, which can be integrated over time to get the value of the current angle. This provides very accurate reading for a short term measurement but the issue with this approach to get angles is that: any error, however small in the readings from the sensor will get accumulated over time and this will drift the value of the calculated angle away and away from the actual value, this phenomenon is known as gyro drift. No worries, we have other ways to calculate angles: there is another sensor on the module... drum rolls.... the accelerometer. The accelerometer provides us with the value of of linear accelerations in the three axes. Given that the gravitational acceleration always acts fully in the negative z direction, we can measure how much of the component of this acceleration acts in the current orientation and hence get an estimate of the angle using trigonometry. But alas, However the result from this approach stays pretty stable in the long term, but there are two issues here: first, the results are pretty noisy as the sensor is très sensitive to jitters and vibrations. second, we can not calculate yaw in this manner as rotation about the z axis itself does not affect the gravitational vector’s direction relative to the accelerometer axes. To resolve these issues, we fuse their reading to cure each one's imperfections by using sensor fusion algorithms like Kalman filter(which is what I have used) or Extended Kalman Filter(EKF). I will not go into the mathematics of these as this is supposed to be just a project display, but if you are interested in going a bit deep, you can go through this playlist on youtube by Phil's Lab. I personally liked his material.
Now we’re familiar with the setup which we are gonna use for calculating the angles, If you'd like to read more about this module or its specifications, here is the datasheet. MPU6050 uses the I2C communication protocol, so we're gonna access its registers and program it using the Wire.h library. You will find all the code files for the calculation of angles here.
Processing Development Environment: the graphics
Once we have the angles, we look for a good ahh 3D model for a plane. preferably a fighter jet, which I found on the internet, however, I had to download its texture's bitmap file separately and then set the texture. The Software I used for coding out the graphics part is Processing. You will find all the 3D files etc. and the complete code for the game here. What it does is, basically, reading out the reading being relayed to the Serial port by the arduino that has our previous code uploaded and making the changes accordingly.
I just needed a plane initially, 'cuz all I wanted to do was just see and object's movement in the software dictated by the movement of my MPU... But I'm glad that I later chose a fighter model so it suited what I ended up creating eventually. I also added the star field simulation. This was inspired by a this video by Daniel Shiffman on his channel: The coding train. Do check out his wonderful channel as he has a complete processing course: the best one out there!
Now Onto the Demonstration
The concept basically is: There is a plane that we are controlling via our sensor, this appears as a static image as the star field all around moves towards us so the plane appears to move sort of into the screen. An enemy plane appears randomly in small intervals and you need to shoot it down before it comes too close, otherwise the games gets over and a message is displayed. The missile-shooting system too will be operated via an external button.
Here's a video of when I first got it to work on processing whose code you'll find here :
Here’s a video showcasing the complete game and its electronics setup, find the codes and files here:
PS: Please adjust the Serial port number according to those being printed in the lower console part.
Also, You'll notice that the jet freezes and jumps back to center before returning to function, that is because I have added the part to refresh the code in Arduino after fixed intervals, to correct the gyro drift in Yaw which couldn't be treated using the Kalman filter.


.png)
👍
ReplyDelete