Need help with multiple variables over python socket.

756 views
Skip to first unread message

brandon...@gmail.com

unread,
Dec 19, 2015, 9:21:36 AM12/19/15
to BeagleBoard
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.

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 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.

What I need is a simple and efficient way to send multiple variables over an internet connection through python. It does not matter if all variables are sent at once as long as they can be differentiated.

Any help is really appreciated.

William Hermans

unread,
Dec 19, 2015, 10:53:04 AM12/19/15
to beagl...@googlegroups.com
This is a programming 101 question that has nothing to do with the Beagle hardware. You should create an account on stackoverflow.com, and pose your question there. Assuming, someone has not already asked a similar question, as they do not realy care for duplicate questions . . . Basically, that means search their site before asking.

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jerry Davis

unread,
Dec 19, 2015, 11:12:06 AM12/19/15
to beagleboard
there are several ways to accomplish this, limited to your imagination.
lets assume that the number of variables you want to send does not vary.

1. if they are binaries, you could pack it all into a string and send the string, then unpack it on the receiving side (see struct module)

2. you could just format the variables into a string, with some delimiter (like , or ;) in between the values.

3. you could use json (see the json module in python doc's, it really is straightforward). json.dumps creates the json string, jso.loads does the other side of the equation. just create the string, send it, and the other side does a loads, and you now have the variables back.

4. others could be done, but this is all I can readily think of on a Saturday morning.





--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer

The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. 
- Anonymous

If writing good code requires very little comments, then writing really excellent code requires no comments at all!
- Ken Thompson

Dennis Lee Bieber

unread,
Dec 19, 2015, 1:07:52 PM12/19/15
to beagl...@googlegroups.com
On Fri, 18 Dec 2015 21:25:39 -0800 (PST),
brandon...@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,
... ]
--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com HTTP://wlfraed.home.netcom.com/

Reply all
Reply to author
Forward
0 new messages