Python + Gamepad = CLI grabber + drawille = CLI stick direction visualiser

So I am creating a robot gr4dalek with a Mecanum drive. I thought omni-drive — I can drive in all directions. When on manual I need to give angle direction and speed. So gamepad. Also, keep it simple, I didn’t want to use pygame, tell me more about bloat processing.

Best suited are the joypad sticks e.g. on playstation-like gamepad. I didn’t have any. The last one I got for 800CZK (30E), had only arrows and 6 buttons. My sister forgot it at some art exhibition anyways.

So I bought this beauty from ebay for just only 300CZK (11E). 

Two sticks, arrows, and 10+ buttons should be enough together with the 2.4GHz wireless USB dongle, is exactly what I want for my robot.

How to control it? As both stm mcu and raspberry pi are running micropython and python respectively, I need to get some info about the stick position with some module. After googling, pygame seemed obvious, but I didn’t want to have it loaded, also I wanted to go the hard way with absolute control :D.

Then I walk past this example where they use sys pipe to access raw data from the gamepad. On every user input action, the mcu in gamepad sends an array of whole numbers. In python terms list of ints. A little fiddling with the controller and the gibberish random numbers started to give sense. So I build a class around it. Got it working with buttons, arrow buttons (behave differently) and in the end, I also decoded the 2D position of sticks.

In the stick position refinement process, I needed to find if I understand the data right. So I needed to visualize it. I could have just created some TKinter or kivy GUI frontend and show the direction visually. But as the controlling Raspberry Pi 2 does not run X windows server, and I didn’t want to install it — why should I — I needed a terminal friendly textual based 2D plot. The first thing that came to my mind was ascii art where character represents graphical data.

But I wanted higher resolution and finally, I came across drawille from asciimo. This program ported to an awful lot of languages and is quite simple. It uses Braille UTF-8 characters (not every font has them). Braille is a set of character made for the visually impaired. The thing we want to know is that every character consists of 8 point or circles if you wish, which are full or blank. If you choose the right font you get only the full ones and the blank ones are omitted, which gives you 8 pixels per char. Thereby „8 times“  more resolution then whole character based ascii art.

However, this also mean you must setup the font of your console. I am currently using font Terminus on Windows 7 via Putty SSH connection which supports Braille very well. You must configure the putty this way:
Translation – UTF8 type font
Appearance – [x] check proportional fonts, Terminus font

After that, you can get this beautiful, oldschool, fast enough, though not double buffered so flickery insight of in what positions are your gamepad sticks.

I was catching the direction data wrongly, but with the help of visual guidance, I have arrived to a conclusion. First, each stick generates two separate HW_button line events. One for horizontal axis potentiometer position and one for the vertical. Also, it sends for each axis data of two bytes, which are supposed to be connected in one 2 byte number.

It just struggled me that this together with a gamepad can be used as a tool for maintenance of sexual arousal over terminal in long-distance relationships. Only if you admit, for a split of a second, that the joypad sticks look like areolas. Or perhaps not.

Anyways.. github will be the place where the program will be when polished. Just kidding. It’s there already with all its dirt and fluffiness. Here you hordes of zombies. Eat my code! The implementation is split into halves, just to let you possibly use only the gamepad control, without the otherwise splendid and mostly useful braille cli DirectionVisualiser class.

Forward in 10 years and I will create CLI graphics of the whole gamepad with the buttons and stuff, all clingy and responsive to real user input. Or not. No, not really. But you can, maybe. If you like terminals.

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *