云天 发表于 2021-4-29 17:18:10

【试用测评】pinpong板——MPU6050控制pgzero角色

本帖最后由 云天 于 2021-4-29 17:18 编辑

【pinpong板】
拿到这块板子,挺喜欢,板子做工比较精致。布局紧凑,合理。各种常用传感器都有,如按键,光线、声音、陀螺仪、红外发发射接收、电位器、温湿。还有RGB灯、OLED显示屏。集成了2路电机驱动、WIFI,引出了Arduino所有端口,还有两IIC接口,一个UART接口。一个7.4V供电口。

【编辑环境】
基于Python编程,提供PinPong使用教程及大量的example,方便学习使用Python驱动硬件。我使用了Mind+的Python模式。
需要把Mind+的pinpong库更新到“pinpong-0.3.5-20210328”。
【测试准备】
1、在Mind+的python模式中安装pgzero库Pygame Zero ,是一个基于 Pygame 的游戏编程框架,他可以更容易的编辑游戏,无需模板,不用编写事件循环,也无需学习复杂的 Pygame API。
2、测试代码pgzero游戏代码
先了解一下常见的项目结构。pgzero的使用过程中,往往直接引用某些对象名称,而不给出全部的路径,那是因为它默认执行的脚本**.py所在目录具有固定的结构,典型的案例如下:
.
├── images/
│   └── ball.png
└── main.py当程序需要创建一个以alien.png图片为对象的游戏角色时,ball = Actor("ball") 这个命令就会直接去找images文件夹,然后将它下面的ball.png图片导入进来,所以如果你的目录结构不符合这样的结构,就会报错喽。

完整的游戏代码(测试用)
import pgzrun

WIDTH = 1300
HEIGHT = 600
NUM = 3
balls = []
for i in range(NUM):
    ball = Actor("ball")
    ball.x = 50 * i + 100
    ball.y = 100
    ball.dx = 5 + i
    ball.dy = 5 + i
    balls.append(ball)

def draw():
    screen.fill((255,255,255))
    for ball in balls:
      ball.draw()
   
def update():
    for ball in balls:
      ball.x += ball.dx
      ball.y += ball.dy
      if ball.right > WIDTH or ball.left < 0:
            ball.dx = - ball.dx
      if ball.bottom > HEIGHT or ball.top < 0:
            ball.dy = -ball.dy
      
pgzrun.go()


3、测试MPU6050代码
# -*- coding: utf-8 -*-

#效果:打印检测到的加速度与角速度
#接线:使用windows电脑连接一块arduino主控板

import time
from pinpong.board import Board
from pinpong.libs.dfrobot_mpu6050 import MPU6050

Board("PinPong Board").begin()#初始化,选择板型和端口号,不输入端口号则进行自动识别

accelgyro = MPU6050()

accelgyro.init()

if accelgyro.connection():
    print("MPU6050 connection successful")
else:
    print("MPU6050 connection failed")

while True:
    buf = accelgyro.get_motion6()
    print("ax:{} ay:{} az:{} gx:{} gy:{} gz:{}".format(buf, buf, buf,buf,buf,buf))
    time.sleep(0.5)



【完整代码】
其中,对于从MPU6050采集的数据进行一阶滤波。
# -*- coding: utf-8 -*-

#效果:打印检测到的加速度与角速度
#接线:使用windows电脑连接一块arduino主控板

import time
from math import *
from pinpong.board import Board
from pinpong.libs.dfrobot_mpu6050 import MPU6050

import pgzrun
K1 =0.05   # 对加速度计取值的权重
dt=20*0.001#注意:dt的取值为滤波器采样时间
angle1=0.0
Board("PinPong Board").begin()#初始化,选择板型和端口号,不输入端口号则进行自动识别

accelgyro = MPU6050()

accelgyro.init()

if accelgyro.connection():
    print("MPU6050 connection successful")
else:
    print("MPU6050 connection failed")


WIDTH = 1300
HEIGHT = 600
ball = Actor("ball")
ball.x = 650
ball.y = 300



def draw():
    screen.fill((255,255,255))
    ball.draw()
   
def update():
    global K1 ,dt,angle1
    buf = accelgyro.get_motion6()
    ax=buf
    az=buf
    gy=buf
    angleAx=atan2(ax,az)*180/3.1415926 #加速度计算角度
    gyroGy=-gy/131.00 #陀螺仪角速度,注意正负号与放置有关
    #一阶互补滤波
    angle1 = K1 * angleAx +(1-K1) * (angle1 +gyroGy * dt)
    #print("ax:{} ay:{} az:{} gx:{} gy:{} gz:{}".format(buf, buf, buf,buf,buf,buf))
    print(angle1)
    ball.x -= angle1
    time.sleep(20*0.001)
pgzrun.go()


【演示视频】

https://v.youku.com/v_show/id_XNTE0NjQ3MTM0NA==.html


云天 发表于 2021-4-29 19:28:56

from pyphysicssandbox import *
# -*- coding: utf-8 -*-

#效果:打印检测到的加速度与角速度
#接线:使用windows电脑连接一块arduino主控板

import time
from math import *
from pinpong.board import Board
from pinpong.libs.dfrobot_mpu6050 import MPU6050

K1 =0.05   # 对加速度计取值的权重
dt=20*0.001#注意:dt的取值为滤波器采样时间
angle1=0.0
Board("PinPong Board").begin()#初始化,选择板型和端口号,不输入端口号则进行自动识别

accelgyro = MPU6050()

accelgyro.init()

if accelgyro.connection():
    print("MPU6050 connection successful")
else:
    print("MPU6050 connection failed")


window("Python物理沙盒测试程序", 1024, 600)
gravity(0.0, 500.0)                     #设定重力参数


floor       = static_rounded_box((0,570), 1024, 25, 3) # 最下面的地板,静止的
floor.color = Color('brown')
floor.friction=1.0 #摩擦力
floor.elasticity=0.5 #弹性

box2 = static_rounded_box((974,320), 50, 250, 5 ) # 右下品红色的圆角正方形
box2.color = Color('magenta')
box3 = static_rounded_box((0,320), 50, 250, 5 ) # 左下品红色的圆角正方形
box3.color = Color('magenta')

wheel1               = ball((500, 130), 30)      
wheel1.color         = Color('green')
wheel1.draw_radius_line=True
wheel1.friction      = 0.25 #摩擦力
wheel1.elasticity=0.5 #弹性
wheel1.wrap= False

wheel2               = ball((600, 130), 30)      
wheel2.color         = Color('green')
wheel2.draw_radius_line=True
wheel2.friction      = 0.25 #摩擦力
wheel2.elasticity=0.5 #弹性
wheel2.wrap= False

chassis=box((470,0),160,100)
chassis.color=Color("blue")

wheel2.elasticity=0.0 #弹性
pin((500,130),wheel1,(470,0),chassis)
pin((500,130),wheel1,(470,100),chassis)
pin((600,130),wheel2,(630,0),chassis)
pin((600,130),wheel2,(630,100),chassis)
while True:

    buf = accelgyro.get_motion6()
    ax=buf
    az=buf
    gy=buf
    angleAx=atan2(ax,az)*180/3.1415926 #加速度计算角度
    gyroGy=-gy/131.00 #陀螺仪角速度,注意正负号与放置有关
    #一阶互补滤波
    angle1 = K1 * angleAx +(1-K1) * (angle1 +gyroGy * dt)
    #print("ax:{} ay:{} az:{} gx:{} gy:{} gz:{}".format(buf, buf, buf,buf,buf,buf))
    print(angle1)
   
    motor(wheel1,angle1)
    motor(wheel2,angle1)
    time.sleep(20*0.001)
run()

print("测试完成")

修远 发表于 2021-5-7 12:37:08

赞,想法好新颖
页: [1]
查看完整版本: 【试用测评】pinpong板——MPU6050控制pgzero角色