top of page
Search
Writer's pictureKevin

Tales from the Whatsapp - A Question of Conflicts

I'm just going to start by dropping the Whatsapp in here and then explain what's going on.

So here's what's going on... Tomorrow's the next big meet up with the Stev3 Crew, and I promised them I'd pull in everyone's efforts and make some improvements to the current controls so that we can hit the ground running.


We've been using two main APIs to move our bot around:

  • BrickPi3 - Dexter Industries (the API that came with the Brick, for obvious reasons)

  • pyPS4controller - hooks up to the Bluetooth PS4 controller.

In our last Tales from the Whatsapp, you saw a discussion about the control conflict between the Controller and the Brick.


In a nutshell, the controller code is event driven. This means that it listens until an event is triggered (button is pressed, button is released, stick is moved). When an event happens, it fires off whatever code you asked it to do and STOPS LISTENING. When the code is done, it goes back to listening for events.


This means that (a) any event that occurs while the code is running is ignored, and (b) you can't ask for the status of any of the controller outputs. In other words, you can listen for a press or a release, but you can't ask it "is the button still pressed?" at any given time.


pyPS4Controller is a great bit of code, and has done us proud for everything we needed in the past. But, we're getting smarter, and are looking for some smarter solutions.


I thought I had it. There's another API called pygame, which allows you to query the controller for raw input data. This seemed like the bridge we needed.


I spent all day hacking pygame, and I now have a beautiful I/O system going that gets constant raw data from the controller in a while True block rather than a listener. Every frame update it gets back an array like this:

[ 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0.7376, 0.3443, 0.0, 0.0 ]

...where each item corresponds with a button or a stick position:

[ dpad_up, dpad_down .... square, circle .... RStickX, RstickY ]

That's nice, but then I made it even better.


If nothing is happening, it prints nothing to the trace log.

If something new has happened (press, released, moved), it prints to the trace log.

If something is still happening (button is held, stick is not zero), it prints this as well.


So now it looks like this:

NEW: Up pressed
-----
Up is pressed
-----
Up is pressed
-----
NEW: Up released
-----
-----
NEW: L Stick moved (0.234, 0.456)
-----
NEW: L Stick moved (0.327, 0.778)
-----
NEW: L Stick moved (0.0, 1.0)
-----
L Stick active (0.0, 1.0)
-----
L Stick active (0.0, 1.0)
-----

...and it can do multiple actions at once.

-----
Up is pressed
Left is pressed
Circle is pressed
L Stick active (0.446, 0.873)
NEW: RT pressed
-----

THIS IS AMAZING!


Or at least it should be.


When I tried to import both my adapted pygame code and brickPi3 at the same time...

BOOOOOO! THIS IS NOT AMAZING!


I have not solved this problem. But I did waste a couple of hours trying to, and then with heavy heart, rolled everything back to pyPS4controller.


You thought this was going to be a story of triumph, but it is in fact a Greek Tragedy.











11 views0 comments

Recent Posts

See All

Comments


bottom of page