Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

best method to transmit PPM (servo control) in serial to wireless transmitter? (how to combine binary + integers into one serial byte)

Solved!
Go to solution

I am pulling my hair out here...I am hoping that someone can help guide me in the right direction.  I am just learning binary and hexadecimal, so please forgive me (and correct me!) if I state something incorrect.  I must give credit where credit is due, as I am using info from a post on RCgroups.com to drive this development.

 

 

I am attempting to control a small indoor RC helicopter using LabVIEW and a USB joystick.  I will be communicating with a wireless transmitter via rs232 (converted to TTL), the protocol is 125000, 8n1. Each frame is 14 bytes with 2 header bytes.  I wish to transmit PPM data (pulse position modulation) which is basically just a 10 bit range (1024 possible steps) which dictates servo position, for each channel. 

 

Byte 3 & 4 are channel 1, Byte 5 & 6 are channel 2, 7 & 8 are CH 3, ........and so forth ending at bytes 13 & 14 which is CH 6.  

 

Each pair of bytes begins with "00" (binary).

 

Byte 3 & 4 should look like "00 00 00 xx xx xx xx xx", where the first "00" is the header, then "00 00" is the servo identifier, and "xx xx xx xx xx" represents the servo position. The identifier is actually integrated into the servo position, since the bits serve a dual purpose.

 

Therefore it will all look like this:

CH 1: 00 00 00 xx xx xx xx xx ( position has a valid range of 0 - 1023)

CH 2: 00 00 01 xx xx xx xx xx (range 1024 - 2047)

Ch 3: 00 00 10 xx xx xx xx xx (range 2048 - 3071)

etc....  if you convert the range to binary you can see how the second half of the servo identifier bits are provided by the servo range.

 

 

I plan on using VISA write to send each byte to make up the entire frame, and then I will pause ~10ms between frames. My question is, however, how the heck should I code this??!!  I think that I need to write, take CH 1 for example, bytes 3 and 4 together into a string and then split them back apart to be sent as two separate bytes.  however, I do not know how to mix my header and the first two bits of my servo identifier, which is binary "00 xx", with my servo position (which I know that I can write as a decimal, as VISA write will convert this to binary). Any thoughts on the best way to do this, in consideration of all of the above?

 

Does an empty string character equate to a binary "0" ? 

 

Did I totally confuse everyone?  I really hope not, but I'm pretty tired so let me know if I need to simplify my question.  I'm hoping that someone can enlighten me as to the best way to combine binary constants w changing decimals (or hex).  And if anyone has thoughts on a good way to organize my entire vi, I would appreciate that too!  Right now I'm just going to use sequence structures.... i would post my code but right now it isn't much to look at

 

thank you very much!!

 

 

 

 

 

 

0 Kudos
Message 1 of 5
(6,211 Views)
Solution
Accepted by topic author Jefff

No, you didn't confuse everyone, but I think you might have confused yourself. Smiley Wink

 

What you basically need to do is to create the array of bytes that will be sent and then use the Byte Array to String so you can send it over the serial port. How the array of bytes is created can be done many ways. You are dealing with 6 16-bit numbers, of which the lower 10 bits are the values of the servo positions. I don't know what you want the front panel to look like, but if you have 6 separate controls (one for each servo) with each set to have a range of 0 to 1023 (for 10 bits), then you just need to OR each value based on the servo number. Put this all into an array, add the header bytes, and use the Byte Array to String to get a string you can send over the serial port. You can also do this in a loop. Attached is a VI to show both concepts. You did not indicate what version of LabVIEW you're using, so it's in 8.2.

Message 2 of 5
(6,192 Views)

Thank you!  I am actually using 8.0, any chance you can save that so i can open it with my older version?  I appreciate your help, as I work on this as my midnight hobby, so i tend to confuse myself when its dark and I've been up for tool ong and my eyes are blurring etc 🙂

 

 The servo position range actually changes for each channel:

 

CH 1: 00 00 00 xx xx xx xx xx ( position has a valid range of 0 - 1023)

CH 2: 00 00 01 xx xx xx xx xx (range 1024 - 2047)

Ch 3: 00 00 10 xx xx xx xx xx (range 2048 - 3071)

etc

 

which is how the servo identifier is incorperated into the position.  For CH 2 and CH3 for instance, the "01" and "10" come from the conversion from hex/decimal to binary.  ie: CH2 "1024" in binary is "010000000000" so all I need to do is pad the left side w/ zeros to make my 16 bit string, and then follow your instructions.  i think 😉   ....  I will work on this tonight, again thank you very much for your help.  working in binary is very new to me, and I haven't written serial drivers in over a year, so i appreciate the guidance. 

 

if  you could convert that VI into a usable file for 8.0 i would be very appreciative, but either way I will see what I can do tonight and will post my code for all to see (and correct).

 

regards,

 

Jeff

 

 

0 Kudos
Message 3 of 5
(6,180 Views)

i was able to jump on a newer version of LV and i saved it for 8.0, so i'm all set with that. 

 

J

0 Kudos
Message 4 of 5
(6,170 Views)

I spoke to soon!

 

this is exactly what I needed, thank you! :smileyhappy:

0 Kudos
Message 5 of 5
(6,156 Views)