As is shown in the image above the output is a text string containing angles for roll pitch and yaw.
I am reading these into my UNO but I need a method to extract the angles and convert them to numbers.
I have previously done this using the substring and toInt commands.
However, here the length of the string changes dynamically depending on the value of the angle so substring wont work for me. If the angle goes from 10 to 100 for example then incorrect values will be read in through substring.
I'm hoping someone here can show me another method whereby I can convert each of the roll pitch and yaw value to numbers and store them.
Thanks
Easy enough No need to buffer anything (or worse; buffering into String ) Assuming you know how to handle the serial port, in your loop do:
() If initialise flag is true, zero your 3 number variables Y, P and R. Note that we need to do "Y". Rest all countrs /flags.
() Read the next byte until you get past the #YPR (basically anything that is a letter or '=' is discarded)
() When you get a valid digit, put it into the appropiate Y P or R variable, first multiplying previous content by 10. Basically this converts ASCII to int, one char at a time, as they arrive. .
() If you see a decimal point start counting in a seperate variable. Everytime you add a digit in above step, count one if we started counting. If you see a "-" just note this in another boolean variable
() When you see a "," or "\n" the number is done. Move on the next in the Y P R pointer. When done all 3, you're done!
Draw backs - the code will not fail if the number is badly formatted (like two decimal poins) but return some (unpredictable) result. I have used above algorithm in my "nnnC" function published as a post (Search for "nnnC"), yours just needs to do 3 numbers.
guix:
Sure, just show me how to compile and run code with the code tags, because I like writing working examples with immediate output
I open a terminal (ctrl-alt-t on my machine), use vi to edit my perennial "t.c" file that I use as a scratchpad, then compile it with "cc -o t t.c" and test it with "./t"
Or, if I happen to have the Arduino IDE open, then I just throw my ideas into that and press "verify" if I don't need to worry about the output of it and just want to confirm the syntax before looking a fool by posting (as I have before) badly formatted code.
Thanks for the replies everyone, lots of good ideas
However, unfortunately I have been unable to try them because I am having trouble reading in and storing the serial data from the 9DOF.
I have used the sketch below, which works for reading in commands I send over Xbee (via HW serial) but not in this case
It seems the error is occurring due to the format in which data is sent out by the board,i.e. a mix of Strings and the actual angles which are floats
Any ideas on how I could read this in correctly to my board, or just the floats as that's really what I\m after, I'm hitting a wall