Custom Arduino firmware LWCloneU2

chunksin

Active member
vacBacker
Feedback
19 (100%)
Credits
687CR
I've been putting together a solution for playing mame driving games in their original cabs using a PC or raspberry pi and as part of testing I've stumbled across an interesting solution and thought I'd document it here in case it's useful for anyone else who likes a tinker!

Getting a driving game running in mame is relatively straightforward, you need some kind of analog input to handle steering, gas and brake pedals and digital input for things like buttons. If you want to be really fancy you can add outputs to the list and use something like a LEDWiz or PACDrive to produce signals that via MameHooker can turn lamps on and off and even run the force feedback motor via a relay.

Analog and digital inputs can be done quite easily using an Ultimarc APac, based on the same hardware as an IPac you can wire up potentiometers found in driving controls to the board and it simulates analog game controllers in your emulator, works brilliantly well and a great plug and play solution.

Now you also need to amplify your video signal because PC or a Pi can't output enough voltage to properly display an image on an arcade monitor. So for that you can use either the Ultimarc video amplifier or add a JPAC to do the same job plus it gives you some added protection from sending bad signals to your monitor.

Add on an output device and things are starting to get expensive! I remember reading about a microcontroller called a Pinscape board when I was building a virtual pinball cab a few years ago and that seemed to handle inputs and outputs really well. I was already using an IPac and a LEDWiz at the time so never bothered investigating any further but I thought it might be worth a look for use on this driving cab solution.

The codebase seems to go back to a project called 'LUFAMamePanel' or 'AVR Atmega V-USB Mame Arcade Panel Controller' which was started back in 2012/3 or so by a German developer and went through several iterations and forked projects, all doing variations on a theme but they were limited to digital inputs only. After a bit of digging I found the source code for a project called LWCloneU2 on github (https://github.com/cithraidt/lwcloneu2), this is a similar all in one solution but includes analog support so is ideal for what I need. The developer was good enough to provide compatible code for various models of Arduino, the Uno, Leonardo, Mega2560, ProMicro and a custom board he made himself.

I had a quick look at prices and found a decent looking Leonardo clone board on Amazon for £6.99 so bought it and set about trying to compile the code and make adjustments to the configuration. I went for the Leonardo just because it has enough IO for the project and I read it's a good board for beginners. I spent an afternoon trawling through the source code to get an idea of how it hangs together and bodged up a working config with 2 analog joysticks, 10 digital keyboard inputs and 6 outputs. Initial testing went badly - until I realised that I needed to hook up all of the configured inputs, using a single pot gave very odd results, both axis moving on both joysticks. Once I got a second pot added it worked fine although running a jstest or advj on the Pi results in constant input rather than delta changes which is how the APac seems to work so still some work to do there.

I was keen to test out the output from the board, it seemed to be recognised in Windows and I could use LEDBlinky to dim and brighten the test LED I had configured so it all looked good but MameHooker didn't want to know. Reading up it seems it doesn't play too well with 64 bit Windows so thought that must be the issue, reinstalled 32 bit Windows 7 from scratch but it still wouldn't work. MameHooker would start up and then either instantly crash and vanish or give me an error message when the board was plugged in. I noticed when using LEDBlinky that it was being assigned an ID number when plugged in which was LEDWiz-4. Using the official LEDWiz OCX file it expects a LEDWiz on ID1 for it to work - back to the German arcade forums and a guy called CycleMatt who had compiled a tool called lwcconfig, run that from a command line and you can change the ID number. Once I'd set that correctly it worked flawlessly, I managed to get a flashing light showing outputs for the Start Lamp and wheel motor in Power Drift
smiley1.gif


Next job is to try to get the inputs behaving better on the Pi, you can use them and they work but they are constantly being reported by the Arduino, not sure if I can tackle that from the Pi side by changing the evdev settings (fuzz?) or if there is some recoding required. Once I have everything working well I'll fully document what you need to do to get this running and how to change the layout if you need to. I've also ordered a breakout board for the Leonardo to add screw terminals to the board to make it more PAC-board like: https://www.ebay.co.uk/itm/Arduino-UNO-Expanded-Wing-Screw-Genuine-Keyes-Shield-Sensor-DUE-Flux-Workshop/121957150603?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649

Chunksin2019-03-12 13:49:06
 

chunksin

Active member
vacBacker
Feedback
19 (100%)
Credits
687CR
Cheers guys, had a very successful evenings testing, I've now got a custom firmware pretty much finalised for the Pi driving cab project, it creates a single controller with X,Y and Z axis so enough for analog controls plus digital keyboard inputs for everything else. I managed to tweak the fuzz to reduce the noise and remove the flatness deadspot using evdev so it works perfectly
smiley20.gif
Next job on is to mess with RGBCommander and see if I can get it to flash some LEDs!
 

Zipper

User
Credits
76CR
I love stuff like this. I need to try the analog inputs.

I've got mamehooker running ok on win 7 64-bit feeding an arduino. I use it to operate MVS-like lamps and credit counters. This was me testing it before I popped it into a Dino King:


The how-to is all here if interested:

https://forum.arcadeotaku.com/viewtopic.php?f=6&t=35743

You don't actually need mamehooker to pick up on MAME outputs - which means you don't need Windows at all. Here's the same thing scaled right down... on a Pi with an little arduino nano:


Pi is attached to (behind) the screen.

Zipper2019-03-13 09:00:22
 

Brettster

Arcade, and the Chuck Norris effect
vacBacker
Feedback
17 (100%)
Credits
990CR
Nice project

I have a driving cab waiting for a mame solution too :)

I got mame hooker working fine on win10 64bit for the Afterbuner cab i had
 

chunksin

Active member
vacBacker
Feedback
19 (100%)
Credits
687CR
Zipper said:
You don't actually need mamehooker to pick up on MAME outputs - which means you don't need Windows at all. Here's the same thing scaled right down... on a Pi with an little arduino nano:


Pi is attached to (behind) the screen.

Great little project that, what version of Mame were you using? For performance reasons I'm using advmame on the Pi, I know they have a scripting system used to handle outputs, just wondered what you were using? The new versions of Mame output to a network port so that would be relatively easy to code a monitor, I had a play with RGBCommander last night and got it working to a point, I can light up LEDs on the Arduino based on the rom but there doesn't seem to be any dynamic lighting triggers in advmame unless I've configured it all wrong!

I managed to get Mamehooker working on Windows 10 but not Windows 7 64bit - I am using embedded version though so it may be missing some prereqs

Chunksin2019-03-13 12:48:23
 

Zipper

User
Credits
76CR
It's the latest mame (or it was when i tried) - maybe .182 or something. I got it from https://choccyhobnob.com/
It ran the neo games in the video above fine. I didn't notice any slowdown.

This script shows how to get the outputs over serial: https://github.com/arfink/NGMM-4-MAME/blob/master/mame.py

Just change the location to point to the arduino.

In the other video I posted, I handled the output in the script and controlled the GPIO instead (no need for the arduino).

Zipper2019-03-13 13:17:53
 

chunksin

Active member
vacBacker
Feedback
19 (100%)
Credits
687CR
I think RGBCommander is really only designed for lighting up LED buttons on control panels, there are rom specific settings but it only seems to be for the number of sticks and buttons the original games had! I'm investigating whether the scripts in advmame might be able to do it, it supports LED events that are detected in the game and output to the keyboard or parallel port. There is a system function that allows you to run a bash command so a simple python script to make the GPIO pins high or low might work. I just hope that the shaker motor signal is detected as an LED event or it might just be limited to flashing the start button on credit entry.
Chunksin2019-03-15 13:49:19
 

chunksin

Active member
vacBacker
Feedback
19 (100%)
Credits
687CR
If anyone wants to have a crack at compiling the firmware for the Arduino, you need to download Atmel Studio (https://www.microchip.com/mplab/avr-support/atmel-studio-7) and install it then add a system path in Windows to the avr runtimes: C:program Files (x86)AtmelStudio7.0toolchainavr8avr8-gnu-toolchainbin.

Once you've done that you need to install git and download the LUFA firmware source code: git clone https://github.com/cithraidt/lwcloneu2 then in the same git window change to the lwcloneu2 folder and run git submodule update --init to grab the LUFA source.

You're now ready to compile the source, from the Start Menu choose Atmel 7.0 Studio Command Prompt and a DOS style window will open. Change to the folder containing the code you just downloaded and change to the firmware folder. To compile the various versions use these commands:

make --directory=<folder_name> clean

make --directory=<folder_name> all

Where <folder_name> is one of these depending on the model of Arduino:

arduino_leonardo

arduino_mega2560

arduino_promicro

arduino_uno

The Leonardo and ProMicro are compatible boards with the same pinout so are interchangeable. There are 2 files you need to modify to change the hardware options and pin layout, they are devconfig.h and pinout.h

The devconfig.h file can be modified to enable or disable the options available (0=disabled, 1=enabled, not present defaults to 0)

#define ENABLE_LED_DEVICE - enables the LEDWiz functionality

#define ENABLE_ANALOG_INPUT - enables analog joystick inputs

#define ENABLE_PANEL_DEVICE - enables joystick input

#define NUM_JOYSTICKS # - defines the number of joysticks configured

#define USE_MOUSE 0 - enable mouse support via analog input

#define USE_CONSUMER 0 - not really sure, seems to be a volume controller?

#define USE_KEYBOARD 0 - enables keyboard input

#define USE_ACCELGYRO 0 - enables sixaxis controller emulation - set NUM_JOYSTICKS to zero if enabled

You can then assign the hardware pins, check out the arduino_promicro and arduino_mega2560 folders for examples. I'll post my custom config here later on, if you have any questions or run into any issues please let me know and I'll try and help out.

Finally to write the firmware to the Arduino you need the avrdude tool, you can grab a copy here: https://drive.google.com/open?id=13WvdPzxMewn3duD7WckFqwsO-huQ9ZJR

Copy the .hex file into the same folder as avrdude.exe, press the reset button twice quickly on the board and the status light will glow. You now need to find the COM port that has been assigned by Windows, go to Device Manager and after you press the button you should see a new entry appear for the Arduino under COM/LPT Ports, make a note and then run this command to write the file, replacing COMX with eg. COM2 if the device is detected on COM2 and replace <firmware_filename> with the name of the hex file:

For Leonardo use:

avrdude -c avr109 -p atmega32u4 -P COMX -U flash:w:<firmware_filename>.hex:i

For Mega2560 use:

avrdude -c wiring -p atmega2560 -P COMX -b 115200 -v -U flash:w:<firmware_filename>.hex:i

For the Uno use:

avrdude -c arduino -p m328p -P COMX -v -U flash:w:<firmware_filename>.hex:i




If you're using the LEDWiz functionality you need to change the ID of the device so it will work with MameHooker, download the tool from here: https://drive.google.com/open?id=1rCOcQJRg_zvFxWAHsjvg5yoATxQ8yplZ

Then with the Arduino plugged in, from a command line run lwcconfig.exe -p 1

Chunksin2019-03-22 10:00:03
 

chunksin

Active member
vacBacker
Feedback
19 (100%)
Credits
687CR
Just a bit of an update on this, after a lot of testing with the Pi it seems I just cant get outputs working with decent enough performance in the games I need to emulate so it has to go onto a PC. Advance Mame has a very limited event system and Mame172 which is the best performing on a Pi in my experience won't run without skipping frames. Not the end of the world but it would have been great to have a small plug and play package, perhaps one to revisit when the Pi 4 comes out next year
smiley1.gif


I found the issue with my version of 64 bit Windows 7, I was using my read only image which is based on a Thin Client profile, very cut down and doesn't include the speech modules needed by programs like LEDBlinky and MameHooker. I rebuilt from scratch using the Application Compatibility template and it worked first time so I'll be releasing that as a new image too.

In the meantime I can share my compiled .hex files if anyone would like to give this a try, the config I'm using emulates a 3 axis controller, keyboard and LEDWiz. It works really well and with any luck will be going into a cab next weekend so I'll post back with another update and some pics.
 

ColinD

Active member
Feedback
6 (86%)
Credits
1,363CR
I noticed issues with the pi last year on advance mame. I was using a small pro micro.

I forget the details now but it seemed that if you sent too much data over the usb things started to slow down (like a buffer was too small or not getting read/flushed fast enough).

It was basically just an led flashing once per so many transmit loops, and when plugged into the pc, it flashed at a speed I was expecting but when it was plugged into the pi it slowed down.
 
Top