예제 #1
0
    def __init__(self,
                 server_ip,
                 acllimitval,
                 shakeslimitcount,
                 color,
                 port=2260):
        self.acl = None
        while 1:
            try:
                self.acl = mpu6050.accel()
                print("accel init")
                break
            except:
                print("accelinit iicerror")

        self.np = neopixel.NeoPixel(machine.Pin(14), 1)
        self.np[0] = (10, 0, 0)
        self.np.write()
        self.ip = server_ip
        self.port = port
        self.acllimitval = acllimitval
        self.shakeslimitcount = shakeslimitcount
        self.shakescount = 0
        self.val = None
        self.color = color

        self.gravity = 16900
        self.last_magnitude = 0

        self.vibrator = machine.Pin(12, machine.Pin.OUT)
        self.vibrator.value(0)

        self.np[0] = (0, 0, 255)
        self.np.write()
        time.sleep(0.5)
        self.server_socket = socket.socket()
        starttime = time.time()

        self.started = 99999999

        while 1:
            self.np.write()
            try:
                self.server_socket.connect((self.ip, self.port))
                self.np[0] = (0, 255, 0)
                print("connected")
                self.np.write()
                self.server_socket.setblocking(0)
                break
            except:
                self.np[0] = (255, 0, 0)
                time.sleep(2)
            if starttime + 5 < time.time():
                self.started = time.time()
                break

        self.np.write()
 def __init__(self, scl_pin, sda_pin, samples=10):
     """
     Initialise the MPU6050 sensor to read accelerometer and gyroscope data.
     :param scl_pin: A Pin object connected to SCL on the sensor.
     :param sda_pin: A Pin object connected to SDA on the sensor.
     :param samples: An integer representing number of readings to take the average of.
     """
     i2c = I2C(scl=scl_pin, sda=sda_pin)
     self.accelerometer = accel(i2c)
     self.calibrated_values = []
     self.samples = samples
예제 #3
0
 def __init__(self, i2c, wheels=None):
     self.accel = mpu6050.accel(i2c)
     if wheels: self.wheels = wheels
     self.reader = {
         # 'x':self.accel.gyro_x,
         # 'y':self.accel.gyro_y,
         # 'z':self.accel.gyro_z,
         'x': self.accel.accel_x,
         'y': self.accel.accel_y,
         'z': self.accel.accel_z,
     }
     self.init_calib()
예제 #4
0
    def __init__(self, sclPin, sdaPin, accadd, oledadd):
        self.sclPin = sclPin
        self.sdaPin = sdaPin
        self.oledadd = oledadd
        self.accadd = accadd

        #print('{}-{}-{}-{}' .format(self.sclPin, self.sdaPin, self.oledadd, self.accadd))

        self.i2c = I2C(scl=Pin(self.sclPin), sda=Pin(self.sdaPin))
        self.oled = ssd1306.SSD1306_I2C(128, 64, self.i2c, self.oledadd)

        self.accelerometer = mpu6050.accel(self.i2c, self.accadd)

        self.accelerometer.set_frs_ac(3)

        self.scale = self.accelerometer.read_scale(0x1C)
        print(self.scale)
예제 #5
0
    def __init__(self, acllimitval, shakeslimitcount, color):
        self.acl = None
        while 1:
            try:
                self.acl = mpu6050.accel()
                print("accel init")
                break
            except:
                print("accelinit iicerror")

        self.np = neopixel.NeoPixel(machine.Pin(14), 1)
        self.np[0] = (10, 0, 0)
        self.np.write()
        self.acllimitval = acllimitval
        self.shakeslimitcount = shakeslimitcount
        self.shakescount = 0
        self.val = None
        self.color = color

        self.vibrator = machine.Pin(12, machine.Pin.OUT)
        self.vibrator.value(0)
예제 #6
0
def midisend():
    wlan = network.WLAN(network.STA_IF)
    sleep(0.5)
    wlan.active(True)
    while not wlan.isconnected():
        print("not connected yet")

    # set up socket
    udpip = "192.168.0.106"
    port = 5045
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    i2c = I2C(scl=Pin(5), sda=Pin(4))
    mpu = mpu6050.accel(i2c)

    # infinite loop of sending data
    while True:
        sleep(0.05)
        dat = mpu.get_values()
        barr = bytes(str(dat), 'utf-8')
        sock.sendto(barr, (udpip, port))
예제 #7
0
# 在这里写上你的代码 :-)
from machine import I2C, Pin
import mpu6050, time
i2c = I2C(scl=Pin(17), sda=Pin(16))
accelerometer = mpu6050.accel(i2c)

while True:
    print(accelerometer.get_values())
    time.sleep_ms(300)
from machine import I2C
import q4
import time, math
import mpu
import mpu6050

i2c = I2C(I2C.I2C0, freq=100000, scl=30, sda=31)
acc = mpu6050.accel(i2c)
acc.error_gy()
#ay=acc.get_values()
#mpu.one_filter(ay["AcX"],ay["AcY"],ay["AcZ"],ay["GyX"],ay["GyY"],ay["GyZ"])
i = 0
gyx = 0
gyy = 0
r = []
q = []
while 1:
    ay = acc.get_values()
    i = i + 1
    if i == 256:
        #print(r)
        print(q)
        #print (q[0],q[1],q[2])
        i = 0
        #print(ay["AcX"],ay["AcY"],ay["AcZ"],ay["GyX"],ay["GyY"],ay["GyZ"])
    #r=mpu.one_filter(ay["AcX"],ay["AcY"],ay["AcZ"],ay["GyX"],ay["GyY"],ay["GyZ"])
    q = q4.IMUupdate(ay["GyX"] / 65.5 * 0.0174533,
                     ay["GyY"] / 65.5 * 0.0174533,
                     ay["GyZ"] / 65.5 * 0.0174533, ay["AcX"] / 8192,
                     ay["AcY"] / 8192, ay["AcZ"] / 8192)
    time.sleep(0.0035)
예제 #9
0
파일: single.py 프로젝트: bllato7/esp32
def value():
    i2c = I2C(scl=Pin(22), sda=Pin(21))
    acc = mpu6050.accel(i2c)
    return acc.get_values()
예제 #10
0
# Scan I2C Register from an MPU6050
from machine import I2C, Pin
from time import sleep_ms
import mpu6050

i2c = I2C(scl=Pin(4), sda=Pin(5))

accelerometer = mpu6050.accel(i2c, 0x68)

count = 13

while (count <= 117):
    scale = accelerometer.read_scale(count)
    print('{}-{}'.format(hex(count), scale))
    count += 1
    sleep_ms(500)
예제 #11
0
oled = SSD1306_I2C(128, 64, i2c1,
                   addr=0x3c)  #OLED显示屏初始化:128*64分辨率,OLED的I2C地址是0x3c
oled.text("OLED OK", 0, 0)  #写入第1行内容
oled.show()  #OLED执行显示
#电机控制模块初始化I2C,pca9685芯片
i2c2 = I2C(sda=Pin(16, Pin.OUT, Pin.PULL_UP),
           scl=Pin(17, Pin.OUT, Pin.PULL_UP),
           freq=40000)
pca = PCA9685(i2c2)  #初始化PCA对象
pca.reset()  #复位pca
pca.freq(freq=1000)  # set freq = 1000Hz
oled.text("PWM OK", 0, 20)  #写入第2行内容
oled.show()  #OLED执行显示
#陀螺仪初始化
i2c3 = I2C(scl=Pin(15), sda=Pin(27))  #陀螺仪 scl->21   sda->33
accelerometer = mpu6050.accel(i2c3)  #初始化陀螺仪对象
#电子罗盘初始化
qmc = QMC5883L()  #初始化电子罗盘对象

##############################################
#  服务函数
##############################################


def key(KEY):
    global key_node
    time.sleep_ms(10)  #消除抖动
    if KEY.value() == 0:  #确认按键被按下
        key_node = 1

예제 #12
0
        fre = round(((2093 - 262) * p / 100) + 262)
    elif t == 3:
        fre = round(((247 - 65) * p / 100) + 65)
    elif t == 4:
        fre = round(((2093 - 65) * p / 100) + 65)
    return (fre)


neo_pixel = NeoPixel(Pin(12, Pin.OUT), 24)
musica_1 = PWM(Pin(25))
Naranja1 = TouchPad(Pin(4))
Naranja2 = TouchPad(Pin(2))
Naranja1.config(1000)
Naranja2.config(1000)
i2c = I2C(scl=Pin(22), sda=Pin(21))
MPU = mpu6050.accel(i2c)
U_Sonico = HCSR04(trigger_pin=32, echo_pin=35)
while True:
    if 300 > (Naranja1.read()):
        for i in range(3):
            neo_pixel[i - 1] = hex_to_rgb('#ff0000')
        neo_pixel.write()
        musica_1.freq(784)
        musica_1.duty(512)
        time.sleep_us(1968750)
        musica_1.freq(0)
        time.sleep_us(31250)
    if 300 > (Naranja2.read()):
        for i in range(3, 6):
            neo_pixel[i - 1] = hex_to_rgb('#ff9900')
        neo_pixel.write()
예제 #13
0
파일: main.py 프로젝트: mjka/slacklinebot
import machine
from machine import Pin, PWM, I2C, TouchPad
import mpu6050

en = Pin(32, Pin.OUT)
pm = Pin(33, Pin.OUT)

touch1 = TouchPad(Pin(12))
touch2 = TouchPad(Pin(13))

i2c = I2C(scl=Pin(22), sda=Pin(21))
accel = mpu6050.accel(i2c, 104)

print("WHILE")

while 1:
    t1 = touch1.read() < 300
    t2 = touch2.read() < 300
    if t1 == t2:
        en.off()
    else:
        en.on()
        if t1:
            pm.on()
        else:
            pm.off()

while 0:
    a = accel.get_values()
    if a['GyZ'] > 1000:
        en.on()
예제 #14
0
import timestart



SERVER = '192.168.1.103'  # MQTT Server Address (Change to the IP address of your Pi)
PORT = 1883
CLIENT_ID = 'MPU'
TOPIC = b'test'
USER = '******'
PASSWORD = '******'

client = MQTTClient(CLIENT_ID, SERVER, user=USER, password=PASSWORD)
client.connect()   # Connect to MQTT broker

i2c = I2C(scl=Pin(22), sda=Pin(21))
acc = mpu6050.accel(i2c, "5432")
rtc = RTC()



def send(tim):

    while True:
        try:
    # Confirm sensor results are numeric
            vals = acc.get_values()
            print(vals)
            # vals["sensor_id"] = 5432
            # vals["timestamp"] = str(rtc.datetime())
            # msg = bytearray(str(vals),"utf8")
            # msg = ujson.loads(vals)
예제 #15
0
import mpu6050, machine, neopixel, time

while 1:
    try:
        acl = mpu6050.accel()
        print("accel init")
        break
    except:
        print("accelinit iicerror")

np = neopixel.NeoPixel(machine.Pin(14), 1)
np[0] = (0, 255, 0)
np.write()
max_val = 30000
val = None
while 1:
    try:
        val = acl.get_values()
        break
    except:
        print("getval0 iicerror")

while 1:
    try:
        val = acl.get_values()
    except:
        print("getval1 iicerror")
    if abs(val["GyX"]) > max_val or abs(val["GyY"]) > max_val or abs(
            val["GyZ"]) > max_val:
        print("1:" + str(val["GyX"]) + " " + str(val["GyY"]) + " " +
              str(val["GyZ"]))