Unity3D使用串口与单片机等进行数据传输
一个简单的例子:(接收一个字节)
using UnityEngine;
using System.Collections;
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Text;
public class tmp : MonoBehaviour
{
private SerialPort sp;
private Thread recvThread;
// Use this for initialization
void Start()
{
sp = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
//串口初始化
if (!sp.IsOpen)
{
sp.Open();
}
recvThread = new Thread(ReceiveOneByte); //该线程接收一个字节
recvThread.Start();
}
void Update()
{
//...
}
private void ReceiveOneByte()
{
try
{
Byte[] buf = new Byte[1];
string sbReadline2str = string.Empty;
if (sp.IsOpen)
sp.Read(buf, 0, 1);
}
catch (Exception ex)
{
Debug.Log(ex);
}
}
/* private void SendSerialPortData(string data) //发送一个字节
{
if(sp.IsOpen)
{
sp.WriteLine(data);
}
}
*/
void OnApplicationQuit()
{
sp.Close();
}
}
应用:
Arduino+MPU6050提供姿态数据由Unity3D实时显示
Arduino端:
1.准备:
与MPU6050的物理连接:A4---SDA
A5---SCL
VCC---3V3
GND---GND
GND---ADD
Arduino IDE(我这用的是1.0.5)
库文件:https://github.com/jrowberg/i2cdevlib 下载后本次需要的是MPU6050和I2Cdev两个文件夹,将它们拷贝进IDE的安装目录下的libraries\文件夹下
2.代码:
代码可由MPU6050库自带的Example中的“MPU6050_DMP6”改写(该Example还可用于测试连接或元件是否正常工作)
改写内容有:
1.选择输出欧拉角功能
2.去除其它不必要输出和输入
3.修改输出格式如下:
每一组欧拉角前加“!”,欧拉角内以“#”分隔
注:由该Example可获得MPU6050经DMP处理后的数据,包括四元数,欧拉角,加速度,具体获得那种数据,靠预处理来选择
最终代码:
// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class using DMP (MotionApps v2.0)
// 6/21/2012 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
// 2013-05-08 - added seamless Fastwire support
// - added note about gyro calibration
// 2012-06-21 - added note about Arduino 1.0.1 + Leonardo compatibility error
// 2012-06-20 - improved FIFO overflow handling and simplified read process
// 2012-06-19 - completely rearranged DMP initialization code and simplification
// 2012-06-13 - pull gyro and accel data from FIFO packet instead of reading directly
// 2012-06-09 - fix broken FIFO read sequence and change interrupt detection to RISING
// 2012-06-05 - add gravity-compensated initial reference frame acceleration output
// - add 3D math helper file to DMP6 example sketch
// - add Euler output and Yaw/Pitch/Roll output formats
// 2012-06-04 - remove accel offset clearing for better results (thanks Sungon Lee)
// 2012-06-01 - fixed gyro sensitivity to be 2000 deg/sec instead of 250
// 2012-05-30 - basic DMP initialization working
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2012 Jeff Rowberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limit
Unity3D与单片机串口通信实现实时数据展示

这篇博客介绍了如何利用Unity3D通过串口与Arduino及MPU6050传感器进行数据交互,实现从Arduino获取姿态数据并在Unity3D中实时显示。
最低0.47元/天 解锁文章
745

被折叠的 条评论
为什么被折叠?



