def initIMU(self): self.get_logger().info('Initializing IMU ') self.imu_msg = TwistStamped() self.x_accel_offset = 0 self.y_accel_offset = 0 self.z_accel_offset = 0 self.x_gyro_offset = 0 self.y_gyro_offset = 0 self.z_gyro_offset = 0 i2c = busio.I2C(board.SCL, board.SDA) self.mpu = adafruit_mpu6050.MPU6050(i2c) self.bool_caliberate_imu = self.parameters["offsets"]["caliberate_imu"] if self.bool_caliberate_imu: self.get_logger().info('Caliberating IMU') self.mpu6050_caliberation() self.get_logger().info('IMU Caliberation Complete') else: self.x_accel_offset = self.parameters["offsets"]["x_accel_offset"] self.y_accel_offset = self.parameters["offsets"]["y_accel_offset"] self.z_accel_offset = self.parameters["offsets"]["z_accel_offset"] self.x_gyro_offset = self.parameters["offsets"]["x_gyro_offset"] self.y_gyro_offset = self.parameters["offsets"]["y_gyro_offset"] self.z_gyro_offset = self.parameters["offsets"]["z_gyro_offset"] self.enable_debug_output = self.parameters["offsets"][ "enable_debug_output"] self.get_logger().info('Loaded Caliberationfrom Parameter Server') self.get_logger().info('Initialized IMU ')
def __init__(self): i2c = busio.I2C(board.SCL, board.SDA) self.mpu = adafruit_mpu6050.MPU6050(i2c) self.angle_init = False self.pitch = 0 self.roll = 0 self.pitch_out = 0 self.roll_out = 0
def __init__(self): """Sets up connection to IMU sensor""" try: self.i2c = busio.I2C(board.SCL, board.SDA) self.mpu = adafruit_mpu6050.MPU6050(self.i2c) except: print("No IMU connection")
def __init__(self, normalise_rates=False, gyro_normalisation_values=[1, 1, 1], acceleration_normalisation_values=[1, 1, 1]): """ Constructor for a Gyro object Parameters: gyro_normalisation_values: array The raw value at which the normalised rate = 1. Default=[1,1,1] implies raw values are returned acceleration_normalisation_values: array The raw value at which the normalised rate = 1. Default=[1,1,1] implies raw values are returned """ i2c = busio.I2C(board.SCL, board.SDA) self.mpu = adafruit_mpu6050.MPU6050(i2c) self.normalise_rates = normalise_rates self.gyro_normalisation_values = gyro_normalisation_values self.acceleration_normalisation_values = acceleration_normalisation_values # Initialise calibration stuff self._calibrating = False self.calibration_gyro = (0, 0, 0) self.calibration_acc = (0, 0, 0)
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import busio import adafruit_mpu6050 i2c = busio.I2C(board.SCL, board.SDA) mpu = adafruit_mpu6050.MPU6050(i2c) mpu.accelerometer_range = adafruit_mpu6050.Range.RANGE_2_G mpu.gyro_range = adafruit_mpu6050.GyroRange.RANGE_250_DPS while True: # this prints out all the values like a tuple which Mu's plotter prefer print("(%.2f, %.2f, %.2f " % (mpu.acceleration), end=", ") print("%.2f, %.2f, %.2f)" % (mpu.gyro)) time.sleep(0.010)
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT # Display inclination data five times per second # See this page to learn the math and physics principals behind this example: # https://learn.adafruit.com/how-tall-is-it/gravity-and-acceleration import time from math import atan2, degrees import board import busio import adafruit_mpu6050 i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_mpu6050.MPU6050(i2c) # Given a point (x, y) return the angle of that point relative to x axis. # Returns: angle in degrees def vector_2_degrees(x, y): angle = degrees(atan2(y, x)) if angle < 0: angle += 360 return angle # Given an accelerometer sensor object return the inclination angles of X/Z and Y/Z # Returns: tuple containing the two angles in degrees
import time import board import busio import adafruit_mpu6050 i2c = busio.I2C(board.SCL, board.SDA) ADD = busio.I2C.scan(i2c) mpu1 = adafruit_mpu6050.MPU6050(i2c,ADD[0]) mpu2 = adafruit_mpu6050.MPU6050(i2c,ADD[1]) while True: print("Acceleration: X1:%.2f, Y1: %.2f, Z1: %.2f m/s^2" % (mpu1.acceleration) + " " + "Acceleration: X2:%.2f, Y2: %.2f, Z2: %.2f m/s^2" % (mpu1.acceleration)) # print("Acceleration: X2:%.2f, Y2: %.2f, Z2: %.2f m/s^2" % (mpu1.acceleration)) # print("Gyro X:%.2f, Y: %.2f, Z: %.2f degrees/s" % (mpu.gyro)) # print("Temperature: %.2f C" % mpu.temperature) print("") time.sleep(0.001)
import time import board import busio import adafruit_mpu6050 i2c = busio.I2C(board.SCL, board.SDA) dev = adafruit_mpu6050.MPU6050(i2c, address=0x68) while True: x, y, z = dev.acceleration print(f"\rAcceleration: X:{x:.2f}, Y: {y:.2f}, Z: {z:.2f} m/s^2", end="\r") time.sleep(0.1)
from adafruit_extended_bus import ExtendedI2C as I2C # sudo pip3 install adafruit-extended-bus import adafruit_mpu6050 # sudo pip3 install adafruit-circuitpython-mpu6050 import board, digitalio, math, time i2c = I2C(11) accelerometer = adafruit_mpu6050.MPU6050(i2c) print( "This is the step counter training program, for aspiring AI-powered step counters." ) print("Calibration time!") calibration = [] def calibrate(): global calibration total = [0, 0, 0] for i in range(100): acceleration = accelerometer.acceleration total[0] += acceleration[0] total[1] += acceleration[1] total[2] += acceleration[2] total[0] /= 100.0 total[1] /= 100.0 total[2] /= 100.0 calibration = total calibrate()
def __init__(self, min_speed=10, max_speed=26): # Create I2C interfaces. self.i2c_display = busio.I2C(board.SCL, board.SDA) self.i2c_accel = I2C( 11) # GPIO23 = SDA, GPIO24 = SCL -- enable the i2c-gpio overlay! # Update minimum and maximum speeds (in pixels / second) self.min_speed = min_speed self.max_speed = max_speed self.current_speed = min_speed # to start # Create list of obstacles (empty to start) self.obstacles = [] # Create a variable to hold the score self.score = 0 # Create a dino 🦖 self.dino = Dino(self) # The PiOLED is 128 pixels x 32 pixels, and communicates over I2C, so we need to give it the I2C interface created above. self.display = adafruit_ssd1306.SSD1306_I2C(128, 32, self.i2c_display) # Initialize the accelerometer. self.accelerometer = adafruit_mpu6050.MPU6050(self.i2c_accel) # Initialize the UI button. self.button = digitalio.DigitalInOut(board.D26) self.button.direction = digitalio.Direction.INPUT self.button.pull = digitalio.Pull.UP # Variable to hold acceleration (updated often from the MPU6050) self.acceleration = [0, 0, 0] # Variable to hold gyro data (updated often from the MPU6050) self.gyro = [0, 0, 0] # Time that we last added an obstacle self.last_added_obstacle = time.time_ns() / 1000000000 # Get display width & height. self.width = self.display.width self.height = self.display.height # Create blank image for drawing. # Make sure to create image with mode '1' for 1-bit color. # 1-bit color means the only options are full brightness (fill=255) or off (fill=0) -- this display is monochrome. self.image = Image.new('1', (self.width, self.height)) # Get drawing object to draw on image. self.draw = ImageDraw.Draw(self.image) # Draw a black filled box to clear the image. self.draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0) # Draw a line at the bottom of the screen (height = 31 & width = 127 because they start at 0, so we are drawing from (0, 31) to (128, 31)). # This is the ground for the dino game. The line also is at full brightness (fill=255), 3 pixels high (width=3), and has curved edges (joint="curve"). # self.draw.line([(0, self.height), (self.width, self.height)], fill=255, width=3, joint="curve") # Draw the ground for the dino game (see where we make the bitmap on line 18). Fill = white (255). TODO get final line # self.draw.bitmap((0, 27), bitmap(bitmaps["ground"]), fill=255) # Update display. (not the obstacles because we haven't started yet) self.update_display() if not self.dino.calibration_exists: print( "Calibrating gyro in 3 seconds. Please put the MPU6050 down on a flat surface with the Z axis (chip) pointing up." ) time.sleep(3) self.dino.calibrate() # Time that the game started (for screen recording) self.start = time.time_ns() / 1000000 # milliseconds # Start the game! Call the update() function every 0.01 seconds. # This returns a function that will stop the loop. self.stop1 = call_repeatedly(0.01, self.update) self.stop2 = call_repeatedly(0.0001, self.get_acceleration)
def __init__(self, i2c_bus, address=0x69): self.motion_sensor = adafruit_mpu6050.MPU6050(i2c_bus, address=address)
import time from math import atan2, degrees import board import busio import adafruit_mpu6050 from adafruit_is31fl3731.charlie_bonnet import CharlieBonnet as Display gyroi2c = board.I2C() # uses board.SCL and board.SDA sensor = adafruit_mpu6050.MPU6050(gyroi2c) ledi2c = busio.I2C(board.SCL, board.SDA) display = Display(ledi2c) display.fill(0) # Given a point (x, y) return the angle of that point relative to x axis. # Returns: angle in degrees def vector_2_degrees(x, y): angle = degrees(atan2(y, x)) if angle < 0: angle += 360 return angle # Given an accelerometer sensor object return the inclination angles of X/Z and Y/Z # Returns: tuple containing the two angles in degrees