def calibrate_sensors(bus): global calibration calibration = {"pitch": 0, "sample_count": 0} print "Calibrating..." MPU6050.loop(bus, duration=3.0, callback=calibrate) print "Done." return calibration["pitch"] / calibration["sample_count"] """
def __init__(self, debug=False): super().__init__() self.accBiases=[-86,9974,327] #(-261.5295, -40.716, -17.288) self.debug=debug self.freq=100 self.bus = smbus.SMBus(1) self.address = 0x68 self.firstAngles=True self.bus.write_byte_data(self.address, 0x6b, 0) self.bus.write_byte_data(self.address, 0x00, 0) self.bus.write_byte_data(self.address, 0x1C, 0b00001000) self.bus.write_byte_data(self.address, 0x10, 0) self.bus.write_byte_data(self.address, 0x1B, 0) self.bus.write_byte_data(self.address, 0x08, 0) self.gyroOffsets=self.calibrateGyro() self.mpu = MPU6050.MPU6050() self.mpu.setGResolution(2) self.bus.write_byte_data(self.address, 0x1C, 0b00001000) self.bus.write_byte_data(self.address, 0x10, 0) self.mpu.setSampleRate(self.freq) time.sleep(0.01)
def init_gyroscope( samplerate, gresolution): # Wake the MPU-6050 up as it starts in sleep mode ready = False while ready == False: try: gyro.mpu6050 = MPU6050.MPU6050( ) # var init. KEEP HERE, MIGHT FAIL OTHERWISE gyro.mpu6050.setup() gyro.mpu6050.setSampleRate(samplerate) # 100 probably ok gyro.mpu6050.setGResolution( gresolution) # 2g for lessening vibration ready = True # Let us know if setup successful except: pass # If setup fails, try again (DO NOTHING) time.sleep(0.01)
def __init__(self, CODE): #list of angles to do pushing if (not CODE or (CODE != 6050 and CODE != 9250)): raise ValueError self.__gyro = None self.maxAngle = -10000; self.minAngle = 10000; # init the Gyro Scope if CODE == 6050 : # TODO: DO INIT FOR 6050 import MPU6050 __gyro = MPU6050.MPU6050() return None
def __init__(self): "Set up the Sensors stuff here" # QUSTIONABLE self.address = 0x68 # This is the address value read via the i2cdetect command self.bus = smbus.SMBus(1) self.gyro = MPU6050(bus, address, "MPU6050") self.library = Data() self.ultra_front_data = "put stuff here" self.ultra_top_data = "put stuff here" self.ultra_bottom_data = "put stuff here" self.gyro_data = [] # put stuff here (list) self.ULTRA_FRONT_PIN = "number" self.ULTRA_TOP_PIN = "number" self.ULTRA_BOTTOM_PIN = "number" self.GYRO_PIN = "number" joystick.init() self.controller = joystick.Joystic(0) self.controller.init() self.axes = self.controller.get_numaxes()
#!/usr/bin/python import MPU6050 import math mpu6050 = MPU6050.MPU6050() mpu6050.setup() mpu6050.setSampleRate(100) mpu6050.setGResolution(16) #sometimes I need to reset twice I = mpu6050.readData() Threshold = 0.2 ShakeFlag = False while True: #wait until new data available while (mpu6050.readStatus() & 1) == 0: pass #read initial reading and put in I class I = mpu6050.readData() PeakForce = 0 for loop in range(20): #wait until new data available while (mpu6050.readStatus() & 1) == 0:
def __init__(self): self.ag = MPU6050.MPU6050(0x68) self.ag.set_gyro_range(MPU6050.MPU6050.GYRO_RANGE_250DEG) self.gyro_abs = {'x': 0.0, 'y': 0.0, 'z': 0.0} self.t = time.time()
# create data array data = [float()] * 9 while check.value(): print("Waiting") utime.sleep(1) led.toggle() print("Initialising Sensors") # initialise barometer baro_i2c = I2C(1, scl=Pin(19), sda=Pin(18)) baro = mpl(baro_i2c, mode=mpl.PRESSURE) # initialise MPU6050 mpu = MPU6050.MPU6050(bus=0, scl=Pin(17), sda=Pin(16)) mpu.set_accel_range(16) mpu.set_gyro_range(500) start = utime.ticks_ms() def get(): global data global start try: pressure = baro.pressure() temperature = baro.temperature() motion = mpu.read() dt = utime.ticks_diff(utime.ticks_ms(), start)
#!/usr/bin/python import MPU6050 import math import numpy import time mpu6050OUT = MPU6050.MPU6050() mpu6050IN = MPU6050.MPU6050() mpu6050OUT.setAddress(0x68) mpu6050IN.setAddress(0x69) mpu6050OUT.setup() mpu6050OUT.setSampleRate(1000) mpu6050OUT.setGResolution(16) #sometimes I need to reset twice I = mpu6050OUT.readData() mpu6050IN.setup() mpu6050IN.setSampleRate(1000) mpu6050IN.setGResolution(16) #sometimes I need to reset twice I = mpu6050IN.readData() Threshold = 0.2 ShakeFlag = False count = 0 countIn = 0
#!/usr/bin/env python ######################################################################## # Filename : MPU6050RAW.py # Description : Read the Raw data of MPU6050. # Author : freenove # modification: 2016/07/18 ######################################################################## import MPU6050 import time mpu = MPU6050.MPU6050() #instantiate a MPU6050 class object accel = [0] * 3 #store accelerometer data gyro = [0] * 3 #store gyroscope data def setup(): mpu.dmp_initialize() #initialize MPU6050 def loop(): while (True): accel = mpu.get_acceleration() #get accelerometer data gyro = mpu.get_rotation() #get gyroscope data print "a/g:%d\t%d\t%d\t%d\t%d\t%d " % (accel[0], accel[1], accel[2], gyro[0], gyro[1], gyro[2]) print "a/g:%.2f g\t%.2f g\t%.2f g\t%.2f d/s\t%.2f d/s\t%.2f d/s" % ( accel[0] / 16384.0, accel[1] / 16384.0, accel[2] / 16384.0, gyro[0] / 131.0, gyro[1] / 131.0, gyro[2] / 131.0) time.sleep(0.1)
if not (ShakeFlag): ShakeFlag = True print("{0} vibration detected: {1} G".format(name, PeakForce)) else: ShakeFlag = False def vibration(): while True: detectVibration(mpu6050, "IN") detectVibration(mpu6050OUT, "OUT") mpu6050 = MPU6050.MPU6050() address = 0x69 mpu6050.setAddress(address) mpu6050.setup() mpu6050.setSampleRate(1000) mpu6050.setGResolution(16) mpu6050OUT = MPU6050.MPU6050() mpu6050OUT.setAddress(0x68) mpu6050OUT.setup() mpu6050OUT.setSampleRate(1000) mpu6050OUT.setGResolution(16) #sometimes I need to reset twice vibIn = vibThread(mpu6050, "IN") vibOut = vibThread(mpu6050OUT, "OUT")
def get_acc_y(bus): (gyro_scaled_x, gyro_scaled_y, gyro_scaled_z, accel_scaled_x, accel_scaled_y, accel_scaled_z) = MPU6050.read_all( bus ) return (accel_scaled_x - zero_values["acc_y"]) * 90 + 90 acc_x_val = accel_scaled_x - zero_values["acc_x"] acc_y_val = accel_scaled_y - zero_values["acc_y"] # acc_y_val = acc_y_val - 1 acc_z_val = accel_scaled_z - zero_values["acc_z"] # print "\t%s,%s,%s" % (acc_x_val,acc_y_val,acc_z_val) R = math.sqrt(math.pow(acc_x_val, 2) + math.pow(acc_y_val, 2) + math.pow(acc_z_val, 2)) angle_y = math.acos(acc_y_val / R) * RAD_TO_DEG return angle_y
def get_gyro_y_rate(bus): (gyro_scaled_x, gyro_scaled_y, gyro_scaled_z, accel_scaled_x, accel_scaled_y, accel_scaled_z) = MPU6050.read_all( bus ) gyro_rate = -1 * (gyro_scaled_y - zero_values["gyro_y"]) return gyro_rate
def orient(bus): MPU6050.loop(bus, duration=999999.0, callback=dump_data)
def balance(bus): MPU6050.loop(bus, duration=999999.0, callback=adjust_wheels)
import MPU6050 import time mpu = MPU6050.MPU6050() #class untuk MPU6050 accel = [0]*3 #untuk store accelerometer data gyro = [0]*3 #untuk store gyroscope data def setup(): mpu.dmp_initialize() #inisialisasi MPU6050 def loop(): while (True): accel = mpu.get_acceleration() #get accelerometer data gyro = mpu.get_rotation() #get gyroscope data print("a/g:%d\t%d\t%d\t%d\t%d\t%d\t"%(accel[0],accel[1]accel[2],gyro[0],gyro[1],gyro[2])) print("a/g:%.2f g\t%.2f g\t%.2f g\t%.2f d/s\t%.2f d/s\t%.2f d/s"%(accel[0]/16384.0,accel[1]/16384.0,accel[2]/16384.0,gyro[0]/131.0,gyro[1]/131.0,gyro[2]/131.0)) time.sleep(0,1) if __name__ == '__main__': #mulai program disini print("Mulai untuk alat . . . ") setup() try: loop() except KeyboardInterrupt: #untuk exit saat menekan Ctrl+C pass
def close_threads(): hilo_recepcion.stop_thread() time.sleep(0.05) hilo_envio.stop_thread() time.sleep(0.05) hilo_grafico.stop_thread() time.sleep(0.05) if (sys.argv[3] != "SIM_MODE"): SIM_MODE = 0 else: SIM_MODE = 1 com = Comunicacion.Comunicacion(sys.argv[1], BAUD_RATE, SIM_MODE) acelerometro = MPU6050.mpu6050(com) ultrasonido = Ultrasonic_Sensor.Ultrasonic_Sensor(com) grafico = graphic.grafico(acelerometro, ultrasonido) hilo_recepcion = ThreadHandler.ThreadHandler(com.receive, "Hilo de recepcion") hilo_envio = ThreadHandler.ThreadHandler(com.send, "Hilo de envio") hilo_grafico = ThreadHandler.ThreadHandler(grafico.run, "Hilo Grafico") open_threads() acelerometro.obtener_datos() #acelerometro.calibrar() acelerometro.print_datos() pl = Planing.Planing(com, sys.argv[2]) pl.run() #time.sleep(5) close_threads()
import classThreads import MPU6050 import time import db #init Accelerometer Sensor inSensor = MPU6050.MPU6050() inSensor.setAddress(0x69) inSensor.setSampleRate(1000) inSensor.setGResolution(16) inSensor.setup() testID = 62 acc = classThreads.ManyAccThread(inSensor, testID, 2000) acc.start() acc.join()
time.sleep(0.05) def close_threads(): hilo_recepcion.stop_thread() time.sleep(0.05) hilo_envio.stop_thread() time.sleep(0.05) hilo_grafico.stop_thread() time.sleep(0.05) if (sys.argv[3] != "SIM_MODE"): SIM_MODE = 0 else: SIM_MODE = 1 com = Comunicacion.Comunicacion(sys.argv[1],BAUD_RATE,SIM_MODE) acelerometro = MPU6050.mpu6050(com) ultrasonido = Ultrasonic_Sensor.Ultrasonic_Sensor(com) grafico = graphic.grafico(acelerometro,ultrasonido) hilo_recepcion = ThreadHandler.ThreadHandler(com.receive, "Hilo de recepcion") hilo_envio = ThreadHandler.ThreadHandler(com.send, "Hilo de envio") hilo_grafico = ThreadHandler.ThreadHandler(grafico.run, "Hilo Grafico") open_threads() acelerometro.obtener_datos() #acelerometro.calibrar() acelerometro.print_datos() pl = Planing.Planing(com,sys.argv[2]) pl.run() #time.sleep(5) close_threads()
import MPU6050 import time mpu = MPU6050.MPU6050() accel = [0] * 3 gyro = [0] * 3 def setup(): mpu.dmp.initialize() def loop(): while (1): accel = mpu.get_acceleration() gyro = mpu.get_rotation() print("a/g:%d \t %d \t %d \t %d \t %d \t %d" % (accel[0], accel[1], accel[2], gyro[0], gyro[1], gyro[2])) print("HI") time.sleep() if __name__ == "__main__": setup() try: loop() except KeyboardInterrupt: pass