Go Down

Topic: Using values from 9DOF Razor in Arduino Mega (Read 4981 times) previous topic - next topic

lau06275

I hope someone can help, I am a bit of a novice with this stuff.

We are currently in the middle of a project and are using the Sparkfun Razor 9DOF IMU to calculate angles of rotation. The plan is for these angles in the X, Y, Z directions to be used to fire motors, sort of an attitude correction system.

We have used the code availible from the link on the sparkfun website that from what I see is commonly used, this is programmed on the Razor, at this stage we are now reading this angle data through the serial monitor on the arduino in the form given by that code of:

ANG! 20.00, 30.00, 10.00   (where 20, 30, 10 etc are example angles)


My question, which may make me sound very simplistic I am aware, is how we get the arduino board to recognise these angles so that we can apply conditions that will start or stop the motors, e.g if the angle in the x axis (in this example 20.00) is greater than 10 degrees turn on pin 1 and 2 etc.
Is there a way for the arduino to recognise these values independently, knowing that one will be for pitch, one for roll and one for yaw.

Any help would be greatly appreciated.

Chris

I think you could connect the Mega and the Razor through their serial connections (RX<->TX, TX<->RX) and then on the Arduino you have to read the data coming from the Razor, parsing them into yaw, pitch, roll.

lau06275

Thanks for the reply.

Yeh I have got that far, I created just a simple read program that is displaying the serial data in the form shown previously. My problem is that I am a complete novice to this, how do I go about getting the arduino to recognize the data as three seperate things that I can then use.

Chris

You should code on the Arduino using Serial.read() (check documentation http://www.arduino.cc/en/Serial/Read and the example there).

Supposing you are receiving a line like:
Code: [Select]
ANG! 20.00, 30.00, 10.00

Then I would do something like:
Code: [Select]

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// skip "ANG! "
Serial.read(); Serial.read(); Serial.read(); Serial.read(); Serial.read();
                // read the first decimal number
                int val = Serial.read() - '0';
                val = val * 10 + (Serial.read() - '0');
                // in val you have now the read value for the integer part

                // skip the dot
                Serial.read();
               
                int val2 = Serial.read() - '0';
                val2 = val2 * 10 + (Serial.read() - '0');

                float finalval = (float) val + ((float) val2 / 100.0);

                // say what you got:
Serial.print("I received: ");
Serial.println(finalval);
               
                // TODO: repeat the above for the remaining 2 angles.
}
}


Not tested but should give you an idea.

lau06275

Thats excellent, thanks. I'll go forward with that and see how I get on.


Good, keep us posted. Note that the above is not copy and paste ready code.. you should adapt it.

lau06275

In the first serial.read() for the first integer. Why has the value been multiplied by 10

Code: [Select]
void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// skip "ANG! "
Serial.read(); Serial.read(); Serial.read(); Serial.read(); Serial.read();
                // read the first decimal number
                int val = Serial.read() - '0';
                val = val * 10 + (Serial.read() - '0');
                // in val you have now the read value for the integer part

Code: [Select]
val = val * 10 + (Serial.read() - '0');

"15" -> ('1' - '0') * 10 + '5' - '0'
"125" -> ('1' - '0') * 100 + ('2' - '0') * 10 + '5' - '0'

lau06275

Right, makes sense, you have to work in tens hundreds etc within arduino. I guess this is why you have divided by 100 when adding the float values

lau06275

When doing this for the other angles also, do I just include

Code: [Select]
Serial,read(); Serial.read();

to eliminate the comma and space that seperates each value?

AWOL

Only if you are certain that there are two characters in the serial buffer when you call Read".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

lau06275

Yes there are it is exactly in the form I showed you at the start. When using Serial. read() does this read each digit in the line of data consecutively?

Another question I had was about your use of - '0' after the number.

Is this only the case in the example I have shown where the numbers are whole values e.g 20.00 instead of 18.54, or is this just used as a 0 always?

AWOL

#12
Mar 08, 2011, 01:58 pm Last Edit: Mar 08, 2011, 02:03 pm by AWOL Reason: 1
Quote
Yes there are it is exactly in the form I showed you at the start

That's not what I asked you.

At the point when you call "Serial.read" twice, are you sure that there are two characters already received that you want to ditch?

If there is only one, the first read will return the character, and the second will return -1.

You should always make sure (using the "available" method) that there are sufficient characters in the buffer before you call "read".

Quote
Another question I had was about your use of - '0' after the number

Are you confusing me with someone else?
Normally "- '0'" is used to convert an ASCII numeric character like '2' to its decimal value 2.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

lau06275

Is it not a similar case to earlier in the code when there has been 5 Serial.read function being used to omit !ANG

AWOL

Code: [Select]
if (Serial.available() > 0) {
// skip "ANG! "
Serial.read(); Serial.read(); Serial.read(); Serial.read(); Serial.read();


So, you call "Serial.available" and say it returns 1.
What happens then?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up