Using a Bicycle in a trainer as a Game Controller in The Crew ( previously in TDU2)

Disclaimer - I'm writing this because I've been asked to but I'm having way too much fun using this to spend a lot of time writing about it.

If you want to see more of my stuff then follow me @makerofadventures on Instagram. (https://www.instagram.com/makerofadventures)

Stripes in screenshots and video are an artefact of phone camera frame rate - they aren't there when playing.


TLDR: How I built my Virtual Bicycle




The Backstory

 March 2020:  lockdown one in the UK hits and we at Orion's lodge are told we cannot travel more than five miles for non-essential purposes. March is where the weather in Scotland starts to improve again and I'd normally jump in my Jeep Renegade Trailhawk and go exploring - driving round exploring ever smaller back roads and sometimes even further is very much a hobby of mine. 

But Orion's Lodge is all about bringing technology into a small area to try to create or recreate unique outdoor experiences, from our Bug Safari rides to an indoor rifle range we  are working on providing ourselves and our guests with incredible fun. So how to solve the driving challenge, including some off-road options.

From some of the early experiments with the safari ride I have three WXGA ultra short throw projectors, and we also have space in the games room with big white walls to set them up. So I decided to invest in an open world driving game. For me it came down to two Test Drive 2 Unlimited or Forza Horizons 4, both of which had huge scope for just exploring. The choice was made for me by my owning a Playstation 3 for TDU and not the PC or XBox for FH4.

So I bought a second hand wheel and pedals ( and later a better one and later still an even better one), a wheel stand stand , set it all up. I then spent  many, many hours exploring Ibiza on and off road.

July 2020:  Something changed again - great weather (which is not a given in Scotland) and an idle bet on Facebook as to how far I, a non cyclist,  could cycle the following day. Orion's Lodge is 600 feet (200m) above sea level so I reckoned if I pointed it generally downhill I could go a long way. 39.5 miles later and I had had a fantastic day out and ridden double my previous longest ride. This rapidly became my new to-do thing, I rode 330 miles for Diabetes UK in September and . I swapped the first gaming wheel for an old bike which I then learned enough bike maintenance to renovate. I fell off hard twice but got hooked on the cycling and mostly abandoned the driving game, even though I had just moved to exploring the second island - O'ahu in Hawaii.

I'm neither a fast nor a competitive cyclist, like like to cruise bolt upright and admire Scotland's scenery for hours on end.


October 2020:  The weather turned bad, as it does in the west of Scotland, constant wind and rain so cycling was no longer fun. Clocks changed, it's now dark from 5pm to 7:30am so   after work cycling is out but my fitness had improved greatly and I still wanted to cycle - so what to do next, well this is Orion's lodge and we use the technology and make the experience - so I set out to build a controller to let me explore TDU2 on my bike.


Version One - Early tests

The newly built hybrid cruiser bike was ready to go so I bought the cheapest magnetic bike turbo trainer I could find ( £49 ) and fitted my bike in it. As I was playing on a playstation I had to build all the smarts into something that emulated a controller - there was no other way to interact with the game so I did some reading an bought an Arduino Leonardo - I had other Arduinos but the earlier generations could not emulate usb devices.


I got the Arduino acting as an HID controller and proved I could send meaningful data to the game, it took some time but I read the HID information from the wired playstation controller clone I had and presented as that.

I then decided I needed to pass through actual controller inputs so I could control the maps and other controls in games and just override acceleration, brake and steering so I looked at how to get the Arduino to read the controller, I dug a USB host shield out of the parts box only to discover it didn't work with the Leonardo - only with an older Uno so added an Uno with the host shield connected by serial (I later found I could connect it just not by plugging the shield in as normal but a cocktail of wiring). I added an SPI based LED and button Display for debugging and tuning and connected first a light sensor and then a magnetic compass on an i2c bus. 


My front wheel was mounted on a turntable designed to get patients in and out of hospital beds (£15) with a wheel riser velcroed to it. The i2C compass module was taped onto this.

All this was messy, unreliable (subsequently tracked down to i2c problems, more later...) and ultimately a crossed wire let out the magic smoke and it died. But before that I did get one glorious first ride.

You can see that first ride here (https://www.facebook.com/johnlpage/videos/10157974216585847), from just outside the airport in O'ahu in Test Drive 2 Unlimited on the Playstation - the vehicle is the Volkswagen Beetle.


Version Two point Oh - Replatforming

Once the Arduino Leonardo died - I was facing a four day wait for a replacement so looked at what else I could use. I remembered that raspberry pi zeros could act as USB devices and found an excellent library to enable that from Milad in Canada (https://github.com/milador/RaspberryPi-Joystick) to whom I am very grateful.

I also at this point used two BBC micro:bits to give wireless connectivity from the sensors, one mounted on the back wheel measuring rotation via it's accelerometer and one at the front wheel acting as a digital compass.  

Pi zeros have one USB port and if I wanted it to be a USB device (controller) I could not plug anything else into it. So I had on Pi Zero W acting as a virtual controller and another with the physical controller and a Micro:Bit on a USB bud reading input data and sending over wifi to the one acting as a game controller. The two micro:bits on the bike communicated with the micro:bit plugged into the Pi using their in-built 2.4Ghz networking.

Sadly this wasn't great either - complex, hindered by the woeful wifi on the pi's and subject to enough lag to make the whole ting worse than the Arduino version - I tried connecting the micro:bits by serial, and eventually went back to the i2c light sensor ( light shining through rear wheel, take on spokes to break beam) and compass.

Version Two point One - Making it work

At this point I had some key breakthroughs.

Importantly I remembered the Raspberry Pi 4 can also act as a USB gadget, and it has the ability to be a gadget and a USB host at the same time . So I replatformed on that , a single Pi 4 (I've had one for a while and never found a use for it beyond what my Pi 3s can do)

I also realised the reason my i2 sensors were so unreliable is I'd wired them up using some twisted pair ethernet cable and I had power over one pair and data (SDA/SCL) over the other - i2c is designed for communications on circuit boards, we have these devices as an offshoot fo the mobile phone industry and long wire runs cause capacitance between them and signals that don't provide clean on and off - even with the required pull-down resistors (https://hackaday.com/2017/02/08/taking-the-leap-off-board-an-introduction-to-i2c-over-long-wires/)

I fixed that using shielded cabling and putting SDA and +ve on one cable and SCL and -ve on the other. At this point I got to what has been basically the stable hardware platform I've using ever since.


Hardware Setup

  • Raspberry Pi 4 (2GB) + microsd card + case 
  • Sanyo PDG-DWL2500 UST Projector (£80 on Ebay)
  • PS 3 ( and Test Drive Unlimited 2 )
  • Aidapt Patient Transfer Turntable
  • USB Powered hub (Pi4 is powered by USB port used when it's a gadget)
  • qmc5883l magnetic compass (Handlebar Direction)
  • BH1750 digital light sensor (Wheel Speed)
  • Audio Phono cabling
  • Generic PS3 style USB controller
  • Wireless clicker (https://www.amazon.co.uk/gp/product/B077SRP4Q9)
  • Phono socket to screw in adapter
  • Dupont M to F wires
  • PlanetX 365X Mag Trainer
  • Bicycle of your choice
  • Rechargeable or AA small flashlight.


    Wiring is very simple, plug in the USB devices to the pi, the pi into the hub, the hub into the PS3. use the phono cables to connect the i2c devices to the following pins on the Pi: 


    pin 1 - 3.3v

    pin 6 - Ground

    pin 2 - SDA

    pin 3 - SCL 


Hardware wise, attach the light sensor to one leg of the trainer and velcro a flashlight to the other side.

Tape the magnetic compass to the turntable.








I should add I have Xoss sensors for speed and cadence which I use to feed into Wahoo->Strava to record my efforts. But that's not integrated with the rest.

 

Software 


The software has also been through various iterations - It's been in Python since moving to the rPi and is available here (https://github.com/orionslodge/ps3bike), I've not checked in each version.


Version 1:

    Emulated a handheld controller - Acceleration was with a button (could not get it emulate analogue trigger on PS controller) and used a modified PWM (clicking on and off) to try and match speed to pedalling speed.

    Added the clicker to give fingertip controls for (Change to reverse gear for 2 seconds, then 1st gear), Get back on road / Ignore phone-call, Brake!!, restart software.

    Had to move sensor reading into own Process (thread) as i2c reads were slow and impacting controller feedback.

Version 2:

   Changed emulation to emulate Driving Force GT  steering wheel and pedals, this gave a much more analogue throttle response which was a lot easier - on hills I didn't need to pedal like crazy to get up them.


Version 3:

    Added a GUI -  Point a browser at the PI to get a GUI showing speed, Steering and let you modify the amount of throttle per pedal speed.


December 2020: Spent my time cycling more than 200 miles round O'ahu and Ibiza, didn't do much development at all - did try to find other games but very few PS3 games supported a wheel and a first person view. Midnight club Los Angeles worked but cycling round LA wasn't much fun.

Christmas 2020: My wonderful teenage daughters dug in their spare parts bin, scoured EBay for a cheap desktop and made me a Gaming PC for Christmas - for those that care (i5 quad core 4th gen, 8GB RAM, 500GB disk, Geforce 970). This meant I could get a much larger range of games. 


Initially Assetto Corsa as it is hugely configurable and I could make something with bike-like physics, it also had large open maps of Glencoe and other parts of Scotland - you can see this here (https://www.facebook.com/johnlpage/videos/10158092558585847)


I then discovered "The Crew" a ubisoft game with 6000km (4500 miles) or roads round the USA and unlike Assetto Corsa lots of variation and interactive scenery. I adapted to code to work with that (mostly by mapping controls in the game) and am currently cycing round a virtual USA looking for wildlife.

https://www.facebook.com/710605846/videos/pcb.10158120980605847/10158120977085847/


Software Part 2 - Telemetry

One thing about PC games is a number of them offer telemetry about what's happening in the game to allow you to attach simulator hardware. The crew does this and can sent UDP packets with your location, speed and acceleration  data in 3 dimensions as well as roll, yaw, pitch etc. I've modified the code to read this and use it to calibrate pedalling speed to game speed by adapting the throttle input to match.

https://www.facebook.com/photo?fbid=10158127977825847&set=a.131767365846

Mini Bike

Along side all this - I also decided to hook the bike controller up to some of the Bug Safari (robot vehicle, macro camera , drives round garden) technology. So I built a lego car with streaming macro video and got it to read the speed and steering info from the controllers web interface (I've not published that code) - it uses continuous rotation servos to drive the wheels.


https://www.facebook.com/johnlpage/posts/10158125625175847



Whats next?

Well apart from having thousands of miles of cycling to do - I'm working on attaching a motor (generator) to the trainer  to act as a variable resistance source, I can combine that with the telemetry information so I can hopefully coast down hills and have better resistance going up them. Currently it's like pedalling on the flat or a slight uphill all the time. Details to follow if I succeed.

I have the generator up now and a load that creates plenty of resistance. Needed two universal joints and an 8mm shaft to make it steady.

https://www.facebook.com/710605846/posts/10158146515580847/


pwm on pi pin 18, to PNP transistor, that and 5v line to a solid rate relay.

Now I have up to about 150W of resistance through 2 universal joints, a DC motor and a car windscreen clearing heater. Hooked to the pitch telemetry. No resistance on downhills, 100% at 9 degrees of slope.


It's loud but very functional.


Moved to Belt Driven, way smoother.





Last Ride - 16 miles New Orleans to The bottom of the Texas gulf coast. 

 


Rubber bushes to mount the motor are much quieter.

Comments

Popular posts from this blog