Author Topic: How to Model an accelerometer in MATLAB?  (Read 4557 times)

0 Members and 1 Guest are viewing this topic.

Offline georgen1Topic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
How to Model an accelerometer in MATLAB?
« on: February 22, 2011, 09:44:49 AM »
Hello all,

I'm in the process of adding an eccelerometer to a self balancing robot and I need to model the accelerometer in MATLAB to find out the freequency, however, I am unsure of how to do so. Could anyone give me some advice how to go about doing it as I'm lost. Any help would be greatly appreciated.

Thanks
George

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 360
  • Helpful? 10
Re: How to Model an accelerometer in MATLAB?
« Reply #1 on: February 22, 2011, 10:41:30 AM »
Can you be a bit more specific?  Do you want the frequency of the robot (as it would be a pendulum sort of deal), or the frequency of a particular accelerometer?

If you know the mass (m) and stiffness (k) of the accelerometer (that is, if you're just assuming the accelerometer is a mass-spring-damper system), then the frequency is just

w = sqrt(k/m)

(technically you'd need to add damping as well, but for a first approximation this isn't bad).

Is that what you're looking for?

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline georgen1Topic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: How to Model an accelerometer in MATLAB?
« Reply #2 on: February 22, 2011, 05:06:51 PM »
Thanks for the reply,

I have an ADXL213 and my task is to find it's frequency. Ive fired up simulink and there's a 3-axis accelerometer there, but im using a 2-axis accelerometer, so i'm guessing i can tweak that block in simulink to try find the desired accelerometer model? But yeh, it's to test it in a pendulum form.

George

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 360
  • Helpful? 10
Re: How to Model an accelerometer in MATLAB?
« Reply #3 on: February 22, 2011, 06:11:18 PM »
Check out the datasheet for your accelerometer:  www.analog.com/static/imported-files/data_sheets/ADXL213.pdf

It has the resonant frequency in it at 5.5KHz.  Damping is going to be a bit of a pain -- it's not on the sheet, and to be honest it's really hard to figure out an accurate value analytically.  My best advice there is to just use the 3-axis accelerometer block's default of 0.707. Then change your damping and see if your robot simulation falters.

My guess is that nothing bad will happen in your sim if you get the damping wrong.  Ultimately, 5.5KHz is a REALLY high response for a pendulum robot, where you'll be working more in the "less than 10Hz" range of operation (unless you have an insanely high frequency pendulum bot? :-P )  That basically means that the bot is going to be moving so much slower than the accelerometer can respond, so the accelerometer will essentially act like an instantaneous measurement of the true acceleration (plus noise, drift...).  If your sampling rate is low enough it won't matter anyway :-)

And yeah, use the 3-axis block and just set acceleration in z = 0, and rotations about x and y to be zero as well, and you'll have a two axis block :-D

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline georgen1Topic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: How to Model an accelerometer in MATLAB?
« Reply #4 on: February 22, 2011, 06:37:02 PM »
Thanks a lot, il give it a shot tomorrow!

Offline georgen1Topic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: How to Model an accelerometer in MATLAB?
« Reply #5 on: February 24, 2011, 07:10:40 AM »
Hey, i couldnt get my head round to do it on MATLAB. However, im trying to find the bandwidth of the accelerometer. Ive connected the accelerometer to the pc and moved the board, and then saved the data using hyperterminal. Ive got an excel graph, but im not sure how to work out what bandwidth it is running at, is there any way of me working it out with the graph? Again, any help would be greatly appreciated.

Thanks
George

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 360
  • Helpful? 10
Re: How to Model an accelerometer in MATLAB?
« Reply #6 on: February 24, 2011, 08:43:21 AM »
The bandwidth depends on how you set up the circuit to read from the accelerometer.  Essentially, you'll be putting a low pass filter in with the circuit.  Whatever you choose as your capacitors is what will determine the bandwidth.  See page 9 of the data sheet.

Do you know how to do frequency-domain stuff like the Fourier Transform?  If not, finding the bandwidth (or even describing what the bandwidth is) will be a challenge.  If you know these things, then:

It's basically impossible to tell the bandwidth from data unless you know exactly the frequencies you are exciting the accelerometer at.  This is typically done with a vibration table that sweeps through a set of frequencies and a set of amplitudes.  You school MIGHT have one, but in all honestly a fair guess is all you'd ever need.  To do it on a vibration table, you'll excite the accelerometer at a known frequency and amplitude.  You'll then record the accelerometer data and put it into the frequency domain.  Then all you have to do is check the data to see where the amplitudes start dropping off.  The 3dB bandwidth (where the power is 3dB, or 1/2, the maximum) is what you'll most likely be looking for.

If you *really* want to figure out the bandwidth experimentally (it's a fun exercise!) then you need to put the accelerometer on something that moves through a known trajectory.  Then you'll just compare the frequency components of the output of the accelerometer with those of the trajectory, and you'll have a vague guess.  But again...you really don't need to, and that will only lead to a wild guess.  Without a vibration table...it's a bit of a challenge!

What is your sampling frequency?  If it's higher than 500Hz then you'll be fine -- the data sheet for your accelerometer says you can't get a bandwidth above 250Hz, so by the sampling theorem, you need double that as a sampling rate in order to resolve frequencies at 250Hz (http://en.wikipedia.org/wiki/Sampling_theorem). 

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline Aberg098

  • Full Member
  • ***
  • Posts: 66
  • Helpful? 0
Re: How to Model an accelerometer in MATLAB?
« Reply #7 on: February 24, 2011, 09:28:39 AM »
Just a thought, If you don't have access to a vibration table, it's possible to rig up something by putting the accelerometer on a vertical wheel. You should attach your accelerometer at a distance from the centre and measure the offset. If you can rotate the assembly at a known velocity (it's pretty easy to measure rpm these days...) you are able to subject your accelerometer to a "known" frequency.

Offline georgen1Topic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: How to Model an accelerometer in MATLAB?
« Reply #8 on: February 24, 2011, 04:03:37 PM »
Thanks for the replies guys, you're helping me a lot.

Ive done Fourier before, but im really unsure as to go about applying it to the graph ive got in excel from the outputted data. It's a sinusoidal wave, and the minimum value ive gotten is 459 and the maximum peak i got was 558. Im unsure as to what these mean, im assuming ADC units? So a difference of 100 ADC units? Again, any help would be greatly appreciated.

Also i dont have access to a vibration table, just need to analyse the graph.

thanks
george

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 360
  • Helpful? 10
Re: How to Model an accelerometer in MATLAB?
« Reply #9 on: February 24, 2011, 05:43:15 PM »
Yeah, your values are in ADC values.  I'm not sure what the voltage is in your analog-to-digital conversion or how many bits you're using (but my guess is you're using a 10 bit ADC?)  I'll assume it's a 10 bit, operating at 5V.  Then what you really have, in volts, is:

459/(2^10) * 5 V
and
558/(2^10)*5 V

with 2^10 = 1024, those values sound about right.  Your accelerometer should have a mV/g conversion, so that will then give you your acceleration.

I don't think excel can do the fourier transform (FT), so take your excel file and open it in matlab (use the xlsread function, or if it is comma-separated value, use csvread, both in matlab).

Here is the matlab code to generate the FFT with the correct frequencies (I wrote this a long time ago, and it's been very useful):

Code: [Select]
%fftmaker returns!
function fftMag = fftmaker(data,Ts)
%takes in an array of data and a sampling time Ts, and plots the FFT of the
%data

Fs = 1/Ts; %sampling frequency
L = length(data);
NFFT = 2^nextpow2(L);
fftMag = fft(data,NFFT)/L;
freqs = Fs/2*linspace(0,1,NFFT/2);

%now plot't
plot(freqs,2*abs(fftMag(1:NFFT/2)));
title(['FFT of Data, Sampling Time = ', num2str(Ts)]);

The peaks are where your main frequencies lie.  Now my guess is that you won't really learn much from the FFT other than you excited the accelerometer with a sine wave, but it'd be neat to try.

MIKE

**EDIT: fixed the code block
« Last Edit: February 24, 2011, 05:44:26 PM by mstacho »
Current project: tactile sensing systems for multifingered robot hands

Offline georgen1Topic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: How to Model an accelerometer in MATLAB?
« Reply #10 on: February 24, 2011, 05:51:35 PM »
thanks a lot, you're being such a big help

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 360
  • Helpful? 10
Re: How to Model an accelerometer in MATLAB?
« Reply #11 on: February 24, 2011, 05:57:33 PM »
No worries.  This forum has helped me a lot too.

One oops: my formula was wrong.  Let's say your voltage on the ADC is 5V, and let's say the conversion (of V/g) of your accelerometer is M, some value.  Then the acceleration is actually:

((ADCReading/(2^10)*5) - 2.5) * M

This is because, without any acceleration, your accelerometer is actually still giving out 1/2 of the maximum voltage.  This way it can measure both positive AND negative accelerations in each axis.  So just subtract 2.5V (or 1/2 of the supply voltage if it isn't 5V) to get the voltage you need to convert!

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline georgen1Topic starter

  • Jr. Member
  • **
  • Posts: 7
  • Helpful? 0
Re: How to Model an accelerometer in MATLAB?
« Reply #12 on: February 24, 2011, 06:13:33 PM »
i dont mean to be a nuisance, but im now having difficulty loading the data into your code, ive managed to get the data read in from the xls file no problem. and in your matlab code which ive put in an m-file, ive set Ts as 500, and im having problem with the L = length(data); line, is it a case of storing the values from the xls file into an array and replacing the array name into the (data) part?

thanks again
george

EDIT: Ts=500
« Last Edit: February 24, 2011, 06:21:09 PM by georgen1 »

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 360
  • Helpful? 10
Re: How to Model an accelerometer in MATLAB?
« Reply #13 on: February 24, 2011, 06:31:41 PM »
My code should run like a matlab function.  so put it in the directory you're working in, and call it as though it were just a regular old a function.  You do need to turn the xls data into an array, but that's what xlsread does.  So let's say that array is called myData.  then run it like this:

fftmaker(myData,500);

and it will plot.  Make sure that semicolon is there, or else it'll output a LOT of crap that means very little :-P

You need to copy my code into an m-file, the save it as fftmaker.m, or else it won't run (or if you know the other way to make matlab functions, but I never bother with them except as separate m-files :-P)

MIKE

Current project: tactile sensing systems for multifingered robot hands

 


Get Your Ad Here

data_list