go away spammer

Author Topic: need helping using data transfered 1 byte at a time  (Read 1634 times)

0 Members and 1 Guest are viewing this topic.

Offline popcornhoboTopic starter

  • Jr. Member
  • **
  • Posts: 11
  • Helpful? 0
need helping using data transfered 1 byte at a time
« on: February 21, 2010, 11:35:04 AM »
We are attempting to read in value from a tilt sensor for an auto stabilizing hover platform, but ran into a bit of a problem when the sensor only sends us 1 byte of the signal at a time in this format:
Byte1: X
Byte2:+/-
Byte3: X-axis tens digit of angle value
Byte4: X-axis ones digit of angle value
Byte5:"." (decimal point)
Byte6: one digit after decimal of X-axis angle value
Byte7: 20 (appears as a blank space)
Byte8: 20 (appears as a blank space)
Byte9: Y
Byte10:+/-
Byte11: Y-axis tens digit of angle value
Byte12: Y-axis ones digit of angle value
Byte13: "." (decimal point)
Byte14: one digit after decimal of Y-axis angle value
Byte15: 0d
Byte16: 0a (tilt sensor schematic says these are the last two bits but never saw                                   them come through... the schematic also labeled the data output pin wrong so it is          not to be trusted)

We can't figure out an efficient way to take these single bits and condense them into something consistent that we can use like X -05.3 if the angle is -5.3 degrees.
We tried making code that would detect the X bit and read in the next 15 or so bytes but it was pretty inefficient by the time it worked correctly, and it still didn't string together the read in bits so we could actually use them. Could really use some help, and appreciate any we can get.
Thanks for taking the time to read this post :)

Offline madsci1016

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: need helping using data transfered 1 byte at a time
« Reply #1 on: February 21, 2010, 12:01:19 PM »
I may have misread, but this seems like it's sending a plain ASCII sentence with all the values of the sensor. This is a common thing to see, as GPS and other devices send data like this. What you are trying to do is 'parse' the ASCII string into usable data.

The trick to handle a data string like this, is to break up the different parts into the different data fields you want. So you would store The X value, Byte(s) 2 through 6 into one string; so you would have, for example, "+10.6".

Then you call a built in function called atof();  (ASCII to float). This will return the actual value of the number, 'described' by the string.

For example, (And my syntax could be and probably is wrong, but this should give you an idea)

char Xstring[5] = '+10.6';
float Xval = 0;

Xval = atof(&Xstring);

Now Xval is set to 10.6, and can be used just like any other value.

So then the trick is to just 'chop up' the sentence first into different strings, and you can do that by looking for the 'X' and 'Y' characters to come through, and grabbing the 5 bytes after each.

Offline popcornhoboTopic starter

  • Jr. Member
  • **
  • Posts: 11
  • Helpful? 0
Re: need helping using data transfered 1 byte at a time
« Reply #2 on: February 21, 2010, 11:32:32 PM »
thanks for the help, that format conversion came in handy :)
we ended up just taking in an array of 16bytes and then searching through that array till we found the X value, and made that the first value in another array and adjusted the rest of the data in the array according to where the X was located using iterations.
« Last Edit: February 22, 2010, 11:33:16 AM by popcornhobo »

 


Get Your Ad Here