Shop OBEX P1 Docs P2 Docs Learn Events
Accelerometer Memsic 2125 Displacement calculate in Arduino — Parallax Forums

Accelerometer Memsic 2125 Displacement calculate in Arduino

Hello,

I was working in a project and I was in a conflict about how to get the displacement with Memsic Dual-Axis accelerometer 2125 in arduino board?

Someone could help me with the code?

Comments

  • Bollywood wrote: »
    how to get the displacement with Memsic Dual-Axis accelerometer 2125

    I've used a Memsic accelerometer and an Arduino but I haven't yet used the two items together but I'll attempt to offer some suggestions.

    I'd start out with making sure you can acquire data from the accelerometer. Can you read the output from the sensor and display it to the terminal?

    IIRC, the Memsic accelerometer output a pulse length proportional to the acceleration. I think there are a couple different ways to receive these signals. How are you measuring these pulses?

    What sort of "displacement" are you trying to measure? Can you provide more information about what you're trying to accomplish?

  • Duane Degn wrote: »
    Bollywood wrote: »
    how to get the displacement with Memsic Dual-Axis accelerometer 2125

    I've used a Memsic accelerometer and an Arduino but I haven't yet used the two items together but I'll attempt to offer some suggestions.

    I'd start out with making sure you can acquire data from the accelerometer. Can you read the output from the sensor and display it to the terminal?

    IIRC, the Memsic accelerometer output a pulse length proportional to the acceleration. I think there are a couple different ways to receive these signals. How are you measuring these pulses?

    What sort of "displacement" are you trying to measure? Can you provide more information about what you're trying to accomplish?

    I was follow this project that use the memsic and arduino together.
    https://www.arduino.cc/en/tutorial/memsic2125

    I had a LCD that I can use to show the values. I just don't know how to convert the acceleration in dislocation space.I saw this page that explain something about it.
    http://perso-etis.ensea.fr/~pierandr/cours/M1_SIC/AN3397.pdf

    But I dont know how to do integral in arduino.


    I want with the accelerometer calculate how much the hand move in a movement. the accelerometerI will put in my hand and I want show in my lcd how much I dislocate my hand ahead and back.

  • ercoerco Posts: 20,257
    Interesting project to determine displacement using accelerometers. You're rolling your own IMU. It's a great academic exercise, although I'm not sure what the overall accuracy would be after sensor errors and integrating twice. It made me wonder if a computer mouse could be done using only accelerometers, and maybe gyros. Overall accuracy isn't critical, but consistency is.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2017-05-08 05:46
    As erco suggests, you're going to end up with some pretty severe errors.

    Here's a bit of pseudo code (I suppose it didn't end up being pseudo code) on how I understand the integration required.

    I'm not averaging the sensor readings or doing any of the other filtering suggesting in the pdf. This is just to provide an idea of how to integrate the values.
    /* I have no idea if these intervals are practical or not. */
    const unsigned long LOOP_INTERVAL = 10000; // in microseconds, 100Hz loop frequency 
    const unsigned long DEBUG_INTERVAL = 500000; // in microseconds, 2Hz debug frequency
    
    const int xPin = 2;     // X output of the accelerometer
    
    unsigned long lastDebugTime;
    unsigned long lastLoopTime;
    unsigned long now; // so time comparisons are done against the same instance in time
    long accel;
    long speed = 0;
    long displacement = 0;
    
    void setup() {
      Serial.begin(115200);
      now = micros();
      lastDebugTime = now;
      lastLoopTime = now;
      pinMode(xPin, INPUT);
    }
    void loop() {
    // This code may be wasting a lot of time. 
    // You'd want to spend as much time as possible reading the sensor
    // and averaging the values.
    
      now = micros();
      if (now - lastLoopTime > LOOP_INTERVAL) {
        lastLoopTime += LOOP_INTERVAL;
        accel = readAccelerometer();
        speed += accel; // First integration. You may want to scale these values.
        displacement += speed; // Second integration.
        if (now - lastDebugTime > DEBUG_INTERVAL) {
          lastDebugTime += DEBUG_INTERVAL;
          displayData();
        }
      }
    }
    
    void displayData() {
      Serial.print(F("accel = ")); // The "F" moves the text out of programming space. Programs don't fill up as fast using this technique.
      Serial.print(accel, DEC);
      Serial.print(F(", speed = ")); 
      Serial.print(speed, DEC);
      Serial.print(F(", displacement = ")); 
      Serial.println(displacement, DEC);
    }
    
    long readAccelerometer() {
    // You'd really want to be averaging this value instead of spending time doing nothing.
      
      long pulseX = pulseIn(xPin, HIGH);
      long accelData = pulseX - 5000;
      // accelData should probably be scaled by some value but don't scale it up since it wouldn't increase the
      // precision and it would make overflow problems more pronounced.
      // P.S. The Arduino example code did not handle the data well.
      return accelData;
    }
    

    Here's the actual integration part again.
        accel = readAccelerometer();
        speed += accel; // First integration. You may want to scale these values.
        displacement += speed; // Second integration.
    

    I'm pretty sure you can integrate the values this way as long as these calculations are performed at regular intervals.

    Displaying debug information each loop would take up too much time. In the above example, I have the program display the debug information twice a second.

    I haven't tested the above code. I don't know if it even compiles. I just typed it into this text box.

    I only used a single axis of the accelerometer. To use both axes, you'd just make a "x" and "y" version of each variable.

    My guess is the above code will give awful results. I think the results could be greatly improved by implementing the filters mentioned in the pdf article.
Sign In or Register to comment.