怎样用Arduino和Wekinator创建传感器控制接口

输入指令
在这个项目的输入方面,我们需要将mpu6050传感器与arduino uno连接。
参考下图,获取有关如何将传感器连接到arduino的帮助。
详细说明了mpu6050传感器与arduino uno之间的连接。
安装arduino库
首先,从github下载i2c和mpu6050库,以便与arduino接口。
解压缩或解压缩下载文件后,导航到arduino文件夹,复制i2c和mpu6050文件夹并将它们放在arduino ide库文件夹中。
arduino ide库文件夹中i2c和mpu6050文件夹的位置。
上传代码流程
撒哈拉title title
启动arduino ide。
查找mpu6050文件夹下的示例文件。
打开mpu6050_dmp6文件。
示例下的mpu6050_dmp6文件的位置和mpu6050文件夹。
现在,上传arduino ide代码并显示串口监视器。
如果显示输出,那么这表示你‘已成功将传感器与arduino连接。
输出数据显示在arduino中。
要将数据发送到processing,需要对代码进行一些更改。
首先取消注释117代码行并注释100代码行。
上传代码再次和它笑uld在串口监视器中显示为不可读的字符。
处理代码说明
为了从arduino接收数据并移动3d模型,需要从bitbucket下载’toxiclibs‘库.org。
复制zip文件中的所有文件夹并将其粘贴到processing library文件夹中。
processing library文件夹可以可在以下位置找到:处理文件夹》模式》 java》库。
现在,将下面的代码粘贴到processing然后上传。
代码是mpu6050库中包含的示例的修改版本。
import processing.serial.*;
import processing.opengl.*;
import toxi.geom.*;
import toxi.processing.*;
import oscp5.*;
import netp5.*;
oscp5 oscp5;
netaddress dest;
toxiclibssupport gfx;
serial port; // the serial port
char[] teapotpacket = new char[14]; // invensense teapot packet
int serialcount = 0; // current packet byte position
int synced = 0;
int interval = 0;
float[] q = new float[4];
quaternion quat = new quaternion(1, 0, 0, 0);
float[] gravity = new float[3];
float[] euler = new float[3];
float[] ypr = new float[3];
void setup() {
// 300px square viewport using opengl rendering
size(300, 300, opengl);
gfx = new toxiclibssupport(this);
// setup lights and antialiasing
lights();
smooth();
// display serial port list for debugging/clarity
println(serial.list());
// get the first available port (use either this or the specific port code below)
string portname = serial.list()[0];
// get a specific serial port (use either this or the first-available code above)
//string portname = “com4”;
// open the serial port
port = new serial(this, portname, 115200);
// send single character to trigger dmp init/start
// (expected by mpu6050_dmp6 example arduino sketch)
port.write(’r‘);
/* start oscp5, sending messages at port 9000 */
oscp5 = new oscp5(this,9000);
dest = new netaddress(“127.0.0.1”,6448);
}
void draw() {
if (millis() - interval 》 1000) {
// resend single character to trigger dmp init/start
// in case the mpu is halted/reset while applet is running
port.write(’r‘);
interval = millis();
}
// black background
background(0);
// translate everything to the middle of the viewport
pushmatrix();
translate(width / 2, height / 2);
// 3-step rotation from yaw/pitch/roll angles (gimbal lock!)
// 。..and other weirdness i haven’t figured out yet
//rotatey(-ypr[0]);
//rotatez(-ypr[1]);
//rotatex(-ypr[2]);
// toxiclibs direct angle/axis rotation from quaternion (no gimbal lock!)
// (axis order [1, 3, 2] and inversion [-1, +1, +1] is a consequence of
// different coordinate system orientation assumptions between processing
// and invensense dmp)
float[] axis = quat.toaxisangle();
rotate(axis[0], -axis[1], axis[3], axis[2]);
// draw main body in red
fill(255, 0, 0, 200);
box(10, 10, 200);
// draw front-facing tip in blue
fill(0, 0, 255, 200);
pushmatrix();
translate(0, 0, -120);
rotatex(pi/2);
drawcylinder(0, 20, 20, 8);
popmatrix();
// draw wings and tail fin in green
fill(0, 255, 0, 200);
beginshape(triangles);
vertex(-100, 2, 30); vertex(0, 2, -80); vertex(100, 2, 30); // wing top layer
vertex(-100, -2, 30); vertex(0, -2, -80); vertex(100, -2, 30); // wing bottom layer
vertex(-2, 0, 98); vertex(-2, -30, 98); vertex(-2, 0, 70); // tail left layer
vertex( 2, 0, 98); vertex( 2, -30, 98); vertex( 2, 0, 70); // tail right layer
endshape();
beginshape(quads);
vertex(-100, 2, 30); vertex(-100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80);
vertex( 100, 2, 30); vertex( 100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80);
vertex(-100, 2, 30); vertex(-100, -2, 30); vertex(100, -2, 30); vertex(100, 2, 30);
vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, -30, 98); vertex(-2, -30, 98);
vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, 0, 70); vertex(-2, 0, 70);
vertex(-2, -30, 98); vertex(2, -30, 98); vertex(2, 0, 70); vertex(-2, 0, 70);
endshape();
popmatrix();
//send the osc message
sendosc();
}
void serialevent(serial port) {
interval = millis();
while (port.available() 》 0) {
int ch = port.read();
if (synced == 0 && ch != ‘$’) return; // initial synchronization - also used to resync/realign if needed
synced = 1;
print ((char)ch);
if ((serialcount == 1 && ch != 2)
|| (serialcount == 12 && ch != ‘ ’)
|| (serialcount == 13 && ch != ‘ ’)) {
serialcount = 0;
synced = 0;
return;
}
if (serialcount 》 0 || ch == ‘$’) {
teapotpacket[serialcount++] = (char)ch;
if (serialcount == 14) {
serialcount = 0; // restart packet byte position
// get quaternion from data packet
q[0] = ((teapotpacket[2] 《《 8) | teapotpacket[3]) / 16384.0f;
q[1] = ((teapotpacket[4] 《《 8) | teapotpacket[5]) / 16384.0f;
q[2] = ((teapotpacket[6] 《《 8) | teapotpacket[7]) / 16384.0f;
q[3] = ((teapotpacket[8] 《《 8) | teapotpacket[9]) / 16384.0f;
for (int i = 0; i 《 4; i++) if (q[i] 》= 2) q[i] = -4 + q[i];
// set our toxilibs quaternion to new data
quat.set(q[0], q[1], q[2], q[3]);
// below calculations unnecessary for orientation only using toxilibs
// calculate gravity vector
gravity[0] = 2 * (q[1]*q[3] - q[0]*q[2]);
gravity[1] = 2 * (q[0]*q[1] + q[2]*q[3]);
gravity[2] = q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3];
// calculate euler angles
euler[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
euler[1] = -asin(2*q[1]*q[3] + 2*q[0]*q[2]);
euler[2] = atan2(2*q[2]*q[3] - 2*q[0]*q[1], 2*q[0]*q[0] + 2*q[3]*q[3] - 1);
// calculate yaw/pitch/roll angles
ypr[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
ypr[1] = atan(gravity[0] / sqrt(gravity[1]*gravity[1] + gravity[2]*gravity[2]));
ypr[2] = atan(gravity[1] / sqrt(gravity[0]*gravity[0] + gravity[2]*gravity[2]));
// output various components for debugging
//println(“q: ” + round(q[0]*100.0f)/100.0f + “ ” + round(q[1]*100.0f)/100.0f + “ ” + round(q[2]*100.0f)/100.0f + “ ” + round(q[3]*100.0f)/100.0f);
//println(“euler: ” + euler[0]*180.0f/pi + “ ” + euler[1]*180.0f/pi + “ ” + euler[2]*180.0f/pi);
println(“ypr: ” + ypr[0]*180.0f/pi + “ ” + ypr[1]*180.0f/pi + “ ” + ypr[2]*180.0f/pi);
}
}
}
}
void drawcylinder(float topradius, float bottomradius, float tall, int sides) {
float angle = 0;
float angleincrement = two_pi / sides;
beginshape(quad_strip);
for (int i = 0; i 《 sides + 1; ++i) {
vertex(topradius*cos(angle), 0, topradius*sin(angle));
vertex(bottomradius*cos(angle), tall, bottomradius*sin(angle));
angle += angleincrement;
}
endshape();
// if it is not a cone, draw the circular top cap
if (topradius != 0) {
angle = 0;
beginshape(triangle_fan);
// center point
vertex(0, 0, 0);
for (int i = 0; i 《 sides + 1; i++) {
vertex(topradius * cos(angle), 0, topradius * sin(angle));
angle += angleincrement;
}
endshape();
}
// if it is not a cone, draw the circular bottom cap
if (bottomradius != 0) {
angle = 0;
beginshape(triangle_fan);
// center point
vertex(0, tall, 0);
for (int i = 0; i 《 sides + 1; i++) {
vertex(bottomradius * cos(angle), tall, bottomradius * sin(angle));
angle += angleincrement;
}
endshape();
}
}
void sendosc() {
oscmessage msg = new oscmessage(“/wek/inputs”);
msg.add((float)ypr[2]); // x-axis
msg.add((float)ypr[1]); // y -axis
oscp5.send(msg, dest);
}
上传后代码,窗口应该如下所示。
输出代码指令
就输出过程而言,一个简单的界面将被设置为从wekinator接收一个dtw输出。
在界面内,一个方框根据收到的wekinator输入向左或向右移动。
你可以找到并下载加工草图在wekinator网站上。
下载‘simple dtw-controlled-game’文件并在processing中运行后,它应该如下例所示。
wekinator说明
启动wekinator软件并按照以下步骤操作:
设置输入值为2.
将输出值设置为1.
将输出类型保留为默认设置“所有动态时间扭曲”并指定3种手势类型。
‘创建新项目’窗口,显示wekinator中的输入,输出和手势类型字段。
单击“下一步”,弹出“新建项目”窗口。
‘新项目’窗口,在wekinator中包含输出行字段。
然后,单击输出1行上的“加号”按钮并向左倾斜传感器。输出将沿该方向移动框。
‘新项目’窗口,带有添加/删除按钮。
现在,单击输出2行上的“加号”按钮,然后向右倾斜传感器。输出将相应地移动框。
‘new project’窗口,在wekinator中用输出2行中的添加/删除按钮。
最后,单击输出3行中的加号按钮并向后倾斜传感器。输出将导致框跳转。
‘new project’窗口,带有在wekinator中圈出的添加/删除按钮。
‘新建项目’窗口,输出3行中的添加/删除按钮被圈起来。
录制完成后,根据样本训练wekinator并运行程序。
然后方框会响应传感器倾斜的方向移动

开发一种基于气溶胶沉积的单轴压电MEMS加速度计
火币为什么要停止CDC交易
小米8曝光:最快下月上市 搭载骁龙845处理器
机床领域的数字孪生体应用及实践简介
科氏质量流量计的驱动方式
怎样用Arduino和Wekinator创建传感器控制接口
电压互感器和电流互感器的二次侧的接地原因
工厂3D可视化建模代做的好处
宝马国内销量创下新纪录,今年会推出6款新能源车型
LLC-NCP1399业界首款采用电流控制模式的LLC AC-DC控制IC
TI ZigBee协议栈本身软件bug的修复详解
分布式光伏逆变器与组串式光伏逆变器优缺点分析
154BSD-300PA-3BSCL汽车压力传感器融合系统
VR交通安全体验系统实操式助力交通教育课程
这些科技巨头的联合创始人是你的榜样吗?有人欢喜有人愁
差异化分析:断路器不等于空开
MC1563设计的音频功率放大电路
专为增程系统设计的动力电池有何优势
三大运营商相关领导分析了在提速降费方面的工作成果
NVIDIA Grace Hopper超级芯片横扫MLPerf推理基准测试