OneWheeled

Notes on accel and gyro scaling using a combined Sparkfun accel/gyro board

 

Further notes on software processing and scaling of the accel and gyro outputs when using a Sparkfun combined accel and gyro board:        
I used IMU Combo Board - 2 Degrees of Freedom - ADXL203/ADXRS300, bought from "Active-robots."
 

ADXRS300 gyro gives a voltage of 5mV per degree per second rotation speed and a value of about 2.5V when not rotating at all (when supplied with a stable 5V power supply).

Link for more info (download the datasheets):

 

 

   

Code to process and scale the accel and gyro readings from this particular board:

 

ACCELEROMETER signal processing

 

Sample  accel reading  (0-5V on an analog input pin gives a value of 0-1023 in the software) 10 times and add them together to give the value of “accsum”

 

  x_accdeg =(float) ((accsum/10) - 512)/3.45;

 

No tilt should give voltage of 2.5V (have to orientate your accelerometer correctly in frame of skateboard) so we get accsum and divide it by 10 to get a mean value. This is done to reduce electrical noise etc in the signal. A level non-tilted skateboard should mean you now have a value of about 512 (half the range 0-1023 representing 2.5V). x-accdeg is tilt angle in degrees. However we want to have a –ve value if tilted to left from vertical plane, and a +ve value if tilted  from vertical plane to the right) so you now have to subtract 512 (so a no-tilt perfectly balanced situation now gives you a tilt angle of zero) and divide that by 3.45 to then correct the value (on the 0 - 1023 scale) to angle in degrees (tilt your accelerometer a known angle and measure the voltage change, then work this out in the scale of 0-1023 then work out your value to correct that into degrees  – you will find the value you need is about 3.45

   

 

  if (x_accdeg<-72) x_accdeg=-72; //cap accel angle degrees values to a range of -72 to +72 (72 degree tilt each way, more tilt than we should ever actually reach)

  if (x_accdeg>72) x_accdeg=72;

 

Code above just stops a way out ridiculous angle being entered into the rest of the program

 

 

 Accelerometer angle change is about 3.45 units (on the scale of 0 – 1023) per degree (no change from one wheeler) tilt in range 0-30 degrees(sin theta)

    Convert tilt to degrees of tilt from accelerometer sensor. Sin angle roughly = angle for small angles so

    no need to do trigonometry.   x_accdeg is now (more or less for small angles less than 30 degrees)  in DEGREES of tilt from vertical

    NOTE: This is how I originally did the accelerometer calculations and it worked OK. I have sinced changed things slightly in terms of the way the signal from the accel is sampled using a different type of filter than simply adding 10 readings together then dividing them by 10. See the notes page on the "Savitsky Golay" filter.

 

         

    GYRO signal processing

  

Additional notes on scaling of gyro output to suit whatever gyro you have:

The analog input on the microcontroller accepts an input voltage of 0 - 5V from whatever sensor you attach to it. By some sort of convention most sensors give an output of 0-5V

For my micro, an input voltage of 0V gives a value of 0 in the code, while an input voltage of 5V gives a value of 1023 in the code (0 - 1023 = 1024 individual points)

This is because the micro has to convert the continuous analog voltage to a digital value it then uses in the rest of the code. So 5V has to be converted to the highest value on some sort of scale, with 2.5V giving the mid point on this scale. For some reason related to computer maths binary, the scale the micros use is 0 - 1023.
Some very simple micros I think use a scale of 0 - about 256 from memory.

So your voltage of 0 – 5V coming into the analog input can be coverted to a value on scale of 0 – 1023 i.e. a decent number of steps so it can detect small voltage (angle) changes.

The more points on the scale the smaller the voltage change it can detect. If there were only a few points, the skateboard would be jumpy as it could only detect big changes in voltage inputs from the sensors.

The scale of 0 - 1023 is somehow in the setup of the analog inputs in first part of program (adapted more or less unchanged from Trevor Blackwell's code if you look at it). I don't fully understand how this setup works but it affects the resolution or number of steps in the scale so I have not messed about with it.

An input of 2.5V gives a value in the code of about 512.



How to adapt this software for your particular gyro voltage output per degree per second of rotation (tilting):

The gyro gives a value of 5V (1023 in the micro after converting this voltage to a number on this scale of 0-1023) when rotating (tipping) at maximum rate clockwise, and a value of 0V (0 in the micro on the 0 – 1023 scale) when rotating at maximum rate anticlockwise and 2.5V (about 512 in the micro on the 0-1023 scale) when not rotating at all – i.e. when balanced.

Therefore there are 1024 "steps" in our scale of 0-1023 representing an input of 0 - 5Volts.

Therefore there are 1024 divided by 5 "steps" on this scale per volt i.e. 204.8 steps per volt.

Question: How many "steps" on this scale of 0-1023 represent a rotation of ONE degree per second ? (this is what we actually want to know for the rest of the calculations).

Answer: There are 204.8 steps per one volt change. We know my gyro output changes by 20mV (i.e. 0.02 volts) per degree per second, because it says so in the datasheet that come with it.
Therefore there will be 204.8 x 0.02 steps on the digital scale of 0 - 1023 per degree per second of gyro rotation (with MY gyro) i.e. = 4.096 steps per degree per second (i.e. for convenience about 4).

So for your gyro,

If your micro "reads" a 5V input on an analog input pin as a value (to be used in rest of the calculations) of 1023, an input of 0V as a value of 0 and an input of 2.5V as a value of about 512, THEN the calculation is as follows:

204.8 "steps" on this scale per one volt of input from the gyro. If, for example, your gyro changes by 25mV (0.025V) per degree per second rotation speed, then there will be 204.8 x 0.025 steps on the digital scale of 0 - 1023 per degree per second of rotation with YOUR gyro i.e. = 5.12
You could probably just use a value of 5 for simplicity.

I suspect the outputs of hobby gyros are made like this to keep the maths simple - for mine the value is about 4, for others (25mV per degree per second) the value is about 5 "steps" on the 0-1023 scale per degree per second of gyro rotation.

 

 

 Incidentally, the Wii Remote contains an ADXL330 accelerometer and may actually be cheaper than ordering one from an electronics supplier. The new version will have a gyro too. I am sure there will be many hacks appearing on the web for these devices.