On Fri, 18 Dec 2015 21:25:39 -0800 (PST),
brandoningram21@gmail.com declaimed the
following:
I'm trying to build a wifi controlled rover with a BBB using python.
My basic plan is to use a joystick on the client, store multiple joystick
inputs to variables, then relay the variables via socket to the BBB.
As mentioned, this is programming 101 -- comp.lang.python might help
(though you might be down at the level of the Python tutors list rather
than the higher concerns of c.l.p).
My concern is that "store multiple joystick inputs..." bit. You don't
plan real-time control?
I initially thought this could easily be done by using socket until I
realized that I can't find a way to differentiate the variables from one
another. I've thought of sending a specific string before sending the
Welcome to IP communications. UDP/IP is fire&forget but guarantees that
the entire sent packet will be received as is, or not received at all (if
you need confirmed delivery you have to build that into your application).
TCP/IP guarantees delivery, but is a stream protocol where what you send
may arrive in multiple smaller pieces, or small sends may arrive as one
long piece -- you have to define how things are delimited in the stream.
actual joystick value so that I could just add something simple like "if
recv=axis0 then recv=joystick variable", but this proved extremely slow and
unreliable during joystick movement, especially if it was more than one
axis being moved.
I read that json could be used but I didn't quite understand it.
If you are already finding timing problems, JSON might be more overhead
than you want to incorporate -- since it converts everything to text
strings for sending, and has to parse the text on the other end.
One possibility is to use the struct module (to keep things in binary
form) and package all control inputs into a single message.
packet = struct.pack("hh??", joystick_x, joystick_y,
button_1, button_2)
(I'm assuming joystick is +/- and fits in a short... buttons are on/off
boolean)
The reading side would have to read 6-bytes; if the read is short,
another read has to be issued and the parts combined.
The processing then becomes something like
(jx, jy, b1, b2) = struct.unpack("hh??", packet)
and now just issue the control commands for each part; no IF ladder
needed.
If you really can't create a packet system that sends every control
value each time, you might want to read up on the implementation of MIDI
(it's a stream system that handles on/off and variable value data...). I
suggest it just as an idea of how an asynchronous stream could be
implements.
Heck, even a simple asynchronous stream could be:
packet = struct.pack("hh", controlID, controlValue)
(sending 4-bytes per control) where the receiver is
(cID, cV) = struct.unpack("hh", packet)
controls[cID](cV)
where controls is a list
def joystickX(value):
#do something with the value
controls = [ joystickX,
` joystickY,
button1,
... ]