14.2.4 Motion sensor

In this section, we use the MPU6050 driver to complete an application that gets the current pose data and we open the shake detection function. When the device shakes, an interrupt triggered and we count this shake and display it to the screen.

from time import sleep
import RPi.GPIO as GPIO
from JMRPiSpark.Drives.Attitude.MPU6050 import MPU6050
from JMRPiSpark.Drives.Attitude.MPU6050 import DEF_MPU6050_ADDRESS


class demo:
    _count = 0
    _shakeCount = 0
    _myAttitude = None

    def __init__(self):
        self._myAttitude = MPU6050( DEF_MPU6050_ADDRESS )

    def _shakeDeviceCallback(self, channel):
        self._shakeCount += 1
        print("\n--- SHAKED DEVICE: {} ---\n".format(self._shakeCount))

    def run(self):

        # Open attitude with all sensor ( accel, gyro, temp )
        self._myAttitude.setAccelRange( MPU6050.ACCEL_RANGE_2G )
        self._myAttitude.setGyroRange( MPU6050.GYRO_RANGE_250DEG )
        # self._myAttitude.openWith( accel = True, gyro = True, temp = True, cycle = False )

        # Init shake check INT -- GPIO 25 ( BCM )
        GPIO.setup( MPU_INT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP )
        GPIO.add_event_detect( MPU_INT_PIN, GPIO.RISING, callback=self._shakeDeviceCallback, bouncetime=20 )
        # enable motion check int

        # Demo will run 2 min, you can shake device at during
        while True:
            if self._count > 120: break
            print( self._myAttitude.getAllData ( ) )
            self._count += 1

        # Clear up gpio resource
        GPIO.remove_event_detect( MPU_INT_PIN )
        self._myAttitude.disableInt() # Disable MPU6050 motion interrupt
        self._myAttitude.sleep()    # close MPU6050

if __name__ == "__main__":
    print("MPU6050 demo is end.")

Save as 14_2_4_mpu6050.py and execute the following command in the terminal:

$> python 14_2_4_mpu6050.py


Was this article helpful?

YES     |     NO


Enjoying the project? Spotted a mistake? Any opinions on the website? Let us know!