Tuesday, March 5, 2019

Pi Fighter - Heavy Bag Workouts with your Favorite Characters

Overview

One of the many challenges of working with Raspberry Pis is deciding what to build.  In addition to being able to be used as a  general purpose Linux computer, Raspberry Pis open a world of physical computing.  I started playing with Pis a few years ago and have since built a few control projects, which were great fun and a good start on developing my skills as a Pi hobbyist. After a few of these simple projects and honing my Python skills, I was looking for a bigger challenge.  I certainly found it in Pi FIghter.  It has provided hours of challenge, fun, conversation and exercise. 

I studied karate for years and I always loved doing some bag work, i.e. hitting a heavy bag as if you are sparring with a partner.  I decided to combine the two - a bit of physical computing with a heavy bag.  A lot of Pi projects end up being a bit more complicated than you think.  This was no exception.

Concept  

The concept is essentially a real world old school fighting video game.  The player chooses an opponent and challenges them to a sparring match.  Each player has a certain number of health points that decrement each time the other player lands an attack.  Instead of clicking a joystick or mouse button, the player hits a heavy bag.  The strength of the hit is measured by an accelerometer.  The Pi translates the acceleration of the heavy bag (measured in g) into the number of health points to decrement from the opponent.  One of the players wins once the their opponent runs out of health points.  

As this is intended to be an aid to getting and remaining fit, a few simple features have been added to encourage players to extend their sparring session and to return for more sessions.  A win in a sparring match results in the player regenerating some of the health points lost in the match.  This lets the player take on another opponent and still have a fighting chance of winning.  The player also gets a percentage (2%) of the opponent's health points added to their overall health points.  So if the player starts the session with 100 health points and they beat two opponents with 100 health points, their base health points increase to 104 health points.  This allows them to take on tougher opponents next time they spar.


In action with Richard Kirby

The User Experience

The user is presented with a simple User Interface (see below) that allows the player to choose who to fight.  I've loaded Pi Fighter with old school Star Wars™ characters.  Once the user selects an opponent, Pi Fighter displays a message on the interface and the LED matrix indicating that the opponent is ready.  The sparring match begins when the player first hits the heavy bag.  This just ensures the player is actually ready - they often need some time to put on heavy bag gloves or otherwise prepare themselves. The server reads through files that contain the opponents attacks, which have strength of attack and the timing.  The files were created by modifying logged attacks to reflect the particular character.  Darth Vader hits much harder than an C3-PO and is a lot harder to defeat. 


In action below - in the back garden as it does take up quite a bit of space. 


Son Joh has a go



Once the fight is underway, the Pi displays the fight status (see below), including the strength of the player's attacks and the imaginary opponent's attacks as well as their respective health points.  The player can clearly see the state of the sparring match via the Pimoroni Unicorn Hat HD™.  When the fight completes, the player is informed of the end result via the LED matrix.  A graph of the fight is displayed on the interface.  This graph displays each attack and how the health points decreased throughout the fight.   
















The overall effect is a fun, engaging, high intensity workout.  It isn't quite at the point where it is addictive for the casual user, but I am sure some would enjoy using it regularly and improving their strength and sparring skills as they progress. 

A video of it in use is available here.

Design

The high level architecture is shown below.  The Raspberry Pi's starring role is to connect to the accelerometer (MPU-6050), a LED matrix display, and a server, which runs the sparring match.  The server can be run on another Pi or Windows™ laptop, but works fine running on the same Pi (a 3B+ in this case).  I connect to the Pi via VNC Connect as you certainly don't want to connect a screen to interface to the PI - it vibrates violently as the match progresses.    

Pi FIghter Architecture
The software consists of two medium complexity Python programs.  The diagram below shows how it all fits together.  I rewrote it a while ago as an object oriented program with reasonable results (for me).  I won't bother you a description of the code, other than to say it is multi-threaded and makes use of tkinter, queues, and matplotlib.  As the server code is Python, it could easily run on a laptop.  The code is intended to run a web server, but currently only runs locally. 

As Pi Fighter is subjected to significant vibration, the physical build makes use of Loctite™, lock washers, and lock nuts.  I used a Pimoroni Unicorn HD™ LED matrix as it is very bright and capable - a very simple display is needed as it allows the player to understand the state of the sparring match even if it is vibrating wildly.  The whole build has worked perfectly, despite the numerous shocks and high levels of vibration.

Build

The build is quite simple and a bit prototypy.  No custom PCB or anything at the moment.  The build just consists of the Pi on the bottom, a bit of stripboard in the middle, and the Unicorn Hat HD on the top.   I use USB boot for this build - easy to damage a SD card mechanically.




I decided to use stripboard as a first try as it is so quick and easy to work with.  The stripboard also provides opportunities for adding all sorts of LEDs, buttons, etc. should I want to do so.  At the moment, it only pulls out the I2C pins (SDA and SCL), 3.3V, and GND for the MPU-6050 accelerometer.  I used an extra tall header to connect Pi, stripboard, and LED Matrix together.



To keep it all together despite vibration and shock, I used decent quality M2.5 bolts with Locktite and lock washers.

The base is acrylic and is attached to the belt with zip ties.  The USB power cord is attached to the stripboard to prevent the usb power getting ripped from the Pi - a very real possibility and a late addition. 

Future Work and Expansion

There is loads more things to do, just to deal with the bugs and making the code more robust.  At some point, I would also like to improve the physical build - it needs to be more compact.   I should really design a custom board for it.  

In terms of extending the project, there are a number of things that could be done:
  • Function to allow players to offer up their fights so you can fight against them.  All the major pieces for this are already in place - just need a way of offering the already recorded fights to others. 
  • Function to track users over long periods of times (weeks, months, or even years) so the players can see themselves improve. 
  • Additional game features such as levels, unlocking special challenges, specialist attacks, etc.  
  • Haptic feedback - I have already built a prototype of this with a Raspberry Pi Zero W.  The W vibrates some mobile phone vibrators to indicate you have been hit. 
  • Other exercises - again I have prototyped this via a W - counts number of skips for you. 
  • Real time fighting with other players anywhere in the world.  This would be a server function that arranges and manages sparring sessions between players.  There is an issue of calibrating between types of heavy bags, so that would have to be dealt with some how.  Otherwise people will have just deal with the fact a fight could be unfair.

Conclusion

As a Raspberry Pi project, this has been great fun.  Fun to build, fun to use, and fun to show people.   I recently took it the Milton Keynes Raspberry Jam and it was quite a popular project and presentation.  As there are a number of characters, everyone can choose an appropriate character to spar with.  I certainly need to add more characters as many of the old school characters are not familiar to the latest generation.   

As noted in the Future Work section, this sort of project can provide an ongoing challenge for a Raspberry Pi maker.  It can be extended significantly and is complicated enough to continue to learn as you build.  I am sure I will return to work on it at some point, but on to other things at the moment.

In addition to the Milton Keynes presentation, I presented at RaspberryPint (Twitter and Facebook @raspberrypint).  The presentation was at Skills Matter (@skillsmatter).  Video is available here.

Appendix


A few more photos here, including a screenshot if you use VNC Connect via your phone.  






Version 1 of Pi Fighter (left) Version 2.0 (right) 
Control via Phone (Android VNC Connect)


No comments:

Post a Comment