Go Down

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

#### lau06275

##### Feb 28, 2011, 11:58 pm
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

#### Fabio Varesano

#1
##### Mar 01, 2011, 10:30 pm
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

#2
##### Mar 02, 2011, 12:49 am

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

#### Fabio Varesano

#3
##### Mar 03, 2011, 12:05 pm
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

#4
##### Mar 03, 2011, 03:11 pm
Thats excellent, thanks. I'll go forward with that and see how I get on.

#### Fabio Varesano

#5
##### Mar 04, 2011, 06:47 pm
Good, keep us posted. Note that the above is not copy and paste ready code.. you should adapt it.

#### lau06275

#6
##### Mar 07, 2011, 03:28 pm
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');`

#### Fabio Varesano

#7
##### Mar 08, 2011, 01:04 am
"15" -> ('1' - '0') * 10 + '5' - '0'
"125" -> ('1' - '0') * 100 + ('2' - '0') * 10 + '5' - '0'

#### lau06275

#8
##### Mar 08, 2011, 01:28 pm
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

#9
##### Mar 08, 2011, 01:31 pm
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

#10
##### Mar 08, 2011, 01:48 pm
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

#11
##### Mar 08, 2011, 01:51 pm
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?

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 pmLast 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

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

#13
##### Mar 08, 2011, 02:06 pm
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

#14
##### Mar 08, 2011, 02:08 pm
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

Please enter a valid email to subscribe