 |
Author |
Topic: sending efficient serial data (Read 750 times) |
|
abe
|
sending efficient serial data
« on: Aug 2nd, 2004, 10:50pm » |
|
this is more of a general coding question I think. At the moment I'm trying to use Processing to control 4 DC motors via a PIC. The PIC is reacting each time it gets a byte from Processing. I want to be able to control all four motors with each byte sent. My math skills, well... Basically I can't figure out how to encode that byte sent in such a way that it sends info for each of the 4 motors (each with 3 possible states) discreetly. If I could just figure out what this operation is called I'm sure google would provide answers...
|
|
|
|
narain
|
Re: sending efficient serial data
« Reply #1 on: Aug 3rd, 2004, 12:52pm » |
|
3 (possible states) ^ 4 (motors) equals 81 (possibilities) which is less than 2 ^ 7 (minimum bits required) ...so it's possible, according to information theory. But that's all theory will tell you. Easiest way would be to encode the 3 possible states into 2 bits -- state 1 = 00 (= 0 in binary) state 2 = 01 (= 1 in binary) state 3 = 10 (= 2 in binary) then form your byte by packing them together one after the other, so to speak, to form 8 bits = 1 byte. If you have these values as bytes or ints (say v1, v2, v3, v4 for the 4 motors) then you can pack them together as v = v1<<6 | v2<<4 | v3<<2 | v1. This is guaranteed to work because we know the values in v1,..,v4 take up only 2 bits, so they won't overlap. To get the values out again: v1 = v>>6 & 3 v2 = v>>4 & 3 v3 = v>>2 & 3 v4 = v & 3 To find out more, see http://en.wikipedia.org/wiki/Bitwise_operation ... '|' in the above expression means bitwise OR, '<<' and '>>' mean bit shift.
|
« Last Edit: Aug 3rd, 2004, 12:55pm by narain » |
|
|
|
|
abe
|
Re: sending efficient serial data
« Reply #2 on: Aug 3rd, 2004, 11:56pm » |
|
bling! all coming together now. stared at those bitshifts many a time without a trace of understanding. starting to make sense now... many thanks, off to the lab.
|
|
|
|
narain
|
Re: sending efficient serial data
« Reply #3 on: Aug 4th, 2004, 2:35am » |
|
I should have been clearer, but I was in a bit of a hurry at the time... sorry.
|
|
|
|
abe
|
Re: sending efficient serial data
« Reply #4 on: Aug 5th, 2004, 2:18am » |
|
oh no, you where totally clear and on point, the staring at the bitshifts I was referring to took place various times over the past few years.
|
|
|
|
|