Pages: [1] 2   Go Down
Author Topic: Using values from 9DOF Razor in Arduino Mega  (Read 3800 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Torino, Italy
Offline Offline
Sr. Member
****
Karma: 2
Posts: 312
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Torino, Italy
Offline Offline
Sr. Member
****
Karma: 2
Posts: 312
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
ANG! 20.00, 30.00, 10.00

Then I would do something like:
Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Torino, Italy
Offline Offline
Sr. Member
****
Karma: 2
Posts: 312
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
val = val * 10 + (Serial.read() - '0');
Logged

Torino, Italy
Offline Offline
Sr. Member
****
Karma: 2
Posts: 312
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
Serial,read(); Serial.read();

to eliminate the comma and space that seperates each value?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Only if you are certain that there are two characters in the serial buffer when you call Read".
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 08, 2011, 08:03:04 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

"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.

Pages: [1] 2   Go Up
Jump to: