Author Topic: computer program (psodo code) for sensor  (Read 1446 times)

0 Members and 1 Guest are viewing this topic.

Offline coronabotTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
computer program (psodo code) for sensor
« on: April 12, 2011, 01:14:17 AM »
How I can do integral for sensor acceleration measurement and recieve the velocity
« Last Edit: April 13, 2011, 08:25:22 AM by coronabot »

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 10
Re: computer program (psodo code) for sensor
« Reply #1 on: April 12, 2011, 08:59:29 AM »
I assume that your accelerometer is being read from an ADC, and that it outputs a constant voltage when there is no acceleration.  So I'll call this voltage VN.  Thus, if your ADC is, say, 8-bit, then VN would correspond to 127 on the adc (in general, it should be about 1/2 of the total ADC output).

I'll also assume you know what the time step is because you have a timer set up.  So:

int oldT = 0;
int newT = 0;
startTimer(); //however your uC  does this
int acData = VN; //start by assuming it's zero...this is not really necessary

double dt = 0.0;
double vel = 0.0;
double acDataDouble = 0.0;
acData = readAccelerometer - VN;
acDataDouble = turnADCIntoAcceleration(acData);
newT = getTimeFromTimer();
dt = newT - oldT;
dt = convertDtToSeconds();

vel = vel + acData*dt;
oldT = newT;
end loop

How this works: this uses a very basic approximation to the integral: it tuns everything into a rectangular box with height of acData and width of dt.  

The function convertDtToSeconds() is there because your timer is probably 8 bit (or whatever bit) and outputs a number of total clock ticks.   This, coupled with the frequency of your timer, will tell you what the time step is in seconds.  Of course, you COULD just consider time to be in clock ticks, but it would make conceptualizing the information a bit weirder :-P

turnADCIntoAcceleration(acData); takes acData, which is in the ADC output range (say 0 to 255, in an 8-bit adc) and turns it into the actual acceleration.  your accelerometer has a datasheet that tells you how to perform this calculation, it usually involves turning the ADC output into volts, then turning the volts into m/s/s.

Without more infon about your accelerometer and uC, that's about all I can do, but it should get you started!

**edit: forgot to reset oldT in the loop.

Current project: tactile sensing systems for multifingered robot hands


Get Your Ad Here