## Code conversion

12 posts / 0 new
Author
Message

I am trying to understand the calculation being performed in some existing code written to read accelerations from an ADXL345 accelerometer board so that I can write some AVR code using a different accelerometer.

The calculation is in this code:

rxVal[CHANNEL_ROLL] = input pulse width in micro seconds from transmitter.
CHANNEL_ROLL = 1                                                                                             // Index to channel
ROLL_RX_MULT = ROLL_A_MAX/400 =  20/400                                                      //ROLL_A_MAX =  20,   max roll angle set point in degrees
ROLL_RX_SUB =1500 * ROLL_RX_MULT

if(updateFlags & 1<<CHANNEL_ROLL){  // New roll
rxRoll  = (float)rxVal[CHANNEL_ROLL]*ROLL_RX_MULT - ROLL_RX_SUB;              //THIS IS THE LINE THAT I NEED SOME HELP WITH, APPARENTLY CALCULATING DEGREES
rxRoll  = constrain(rxRoll, -ROLL_A_MAX, ROLL_A_MAX);                                    //insure that it is within limits
}

I understand the code itself BUT can anyone help in explaining the calculation being performed here?  The input is the pulse width of the transmitter and I am guessing it is some sort of a conversion but what is going on here?  What do the '400' and '1500' represent?

Many thanks to anyone who understands this calculation when done with the ADXL345.

This topic has a solution.

David Abineri

Last Edited: Wed. Jul 11, 2018 - 11:17 PM

So where did this code come from?

Have you checked the ADXL product page to see if AD have provided any useful app notes, etc, which cover this?

Any hints in the datasheet?

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution.

Thanks, Awniel.

I have looked at the ADXL345 data sheet and I find no hints that might help.  Code came from a web published project.

David Abineri

The 400 may be from the I2C speed of 400KHz.  I just noticed this but no idea where the 1500 comes from.

David Abineri

Go on - how about a link to said "web published project" ?

Did you chcek AD app notes?

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

The line

`rxRoll  = (float)rxVal[CHANNEL_ROLL]*ROLL_RX_MULT - ROLL_RX_SUB;  `

is dead easy. rxVal[] is an array. so rxVal[CHANNEL_ROLL] is likely the value associated with the roll axis. But, this is a plain-jane 3-axis accelerometer, so why they call one axis "roll" does not make much sense. What ever.

ROLL_RX_MULT must then be the "gain factor" that converts sensor counts into science units (probably "g").

Then ROLL_RX_SUB is the offset to correct for the zero value not being exactly zero.

That is:  Y = X*m + a, m -> ROLL_RX_MULT and a -> -ROLL_RX_SUB

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

I think I need someone who knows the ADXL345.

David Abineri

ka7ehk wrote:
why they call one axis "roll" does not make much sense.

Wherein JAMES DRYDEN & RYAN BARBACCIA wrote:
For a quadcopter application, a 6-axis inertial measurement unit (IMU) is desired, consisting of a gyroscope and accelerometer on the same board. We selected a Sparkfun 9DOF Sensor Stick, seen in Figure 8. This board includes an ADXL345 accelerometer, an ITG-3200 gyroscope, and an HMC3885L magnetometer.

So maybe the "roll" is coming from the gyro - not the accelerometer ... ?

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

Jim, The ROLL_RX_Mult is 20/400 as explained.

How is this main calculation calculating in degrees?  I do not follow the conversion from micro seconds of the pulse to degrees of roll?

Many thanks.

David Abineri

Coming from the transmitter is a pulse representing a roll command on one channel and a pulse representing pitch on another channel etc.

The roll pulse if being converted to degress of roll and I am trying to see how this conversion is done.

Thanks,

David Abineri

Last Edited: Wed. Jul 11, 2018 - 11:20 PM

It MAY be in arbitrary units rather than degrees. I would have to study both the accelerometer data sheet AND the board to give you a definitive answer, and I don't have time to do that now.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

dabineri wrote:
a pulse representing a roll command

dabineri also wrote:
a pulse representing pitch

So what does that have to do with the ADXL, then ?

The ADXL gives its output as I2C or SPI - not pulses.

So it sounds like something else is (re)encoding the data into some sort of pulse code - nothing to do with the ADXL ?

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Jul 12, 2018 - 07:18 AM