回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

Wii手柄+arduino+processing创建虚拟现实 简单

头像 Anders项勇 2021.12.16 916 0

步骤1 项目介绍

Meta(元)现在很热门,虚拟与现实显得无法分隔。我这里使用Wii手柄+arduino+processing创建一个虚拟现实的案例抛砖引玉,相信大家可以做出更加精彩的项目。

步骤2 Wii Nunchuck手柄

任天堂的Wii游戏机的Wii Nunchuck手柄集成了1个3轴加速度传感器,其提供的I2C接口能方便的获取手柄上各个传感器的数据,包括2轴摇杆,2个按钮按钮,3轴加速度传感器等。使用Wii Nunchuck手柄和Arduino能做出非常酷的互动作品。WiiChuck转接板能将Wii手柄的I2C端口引出,从而使Wii Nunchuck手柄方便与Arduino连接而不需任何焊接和连线,不过这个WiiChuck转接板DF停产了。这里我们用arduino使用sparrow主板获取加速度的值。WiiChuck转接板和Wii手柄连接的参考资料见:https://wiki.dfrobot.com.cn/_SKU_DFR0062_WiiChuck%E8%BD%AC%E6%8E%A5%E5%99%A8

project-image
代码
#include <WiiChuck.h>


    #include <math.h>
    #include <stdlib.h>
    #include "Wire.h"
    #include "WiiChuck.h"

    WiiChuck wii = WiiChuck();

    void setup()
    {
     wii.initWithPower();
     Serial.begin(19200);
    }

    void loop()
    {
      if (true == wii.read()) {
        
        Serial.print(wii.getAccelAxisX(), DEC);
        Serial.print(",");
        Serial.print(wii.getAccelAxisY(), DEC);
        Serial.print(",");
        Serial.print(wii.getAccelAxisZ(), DEC);
        Serial.print(" \n");

        
      }
      delay(100);

    }

步骤3 processing软件

processing是一个方便的软件,通过写代码来画出图案和2D、3D动画,并且可以通过串口和其他硬件进行交互通讯。软件下载地址:https://processing.org/网站里面也有很多案例参考学习。这里我们接收wii手柄传过来的加速度值对创建的3d图形进行同步控制。注意com口和波特率与arduino里面保持一致。

代码
/**
 * Mixture
 * by Simon Greenwold. 
 * 
 * Display a box with three different kinds of lights. 
 */
import processing.serial.*; 
int x = 0;
int y = 0;
int z = 0;
Serial myPort;
void setup() {
  size(640, 360, P3D);
  noStroke();
  myPort = new Serial(this,"/dev/cu.usbmodem1421",19200);
  myPort.bufferUntil('\n');
}
void serialEvent(Serial p){
  String inString =p.readString();
  String[] list =split(inString,',');
  x = int(list[0]);
  y = int(list[1]);
}



void draw() {
  background(0);
  translate(width / 2, height / 2);
  
  // Orange point light on the right
  pointLight(150, 100, 0, // Color
             200, -150, 0); // Position

  // Blue directional light from the left
  directionalLight(0, 102, 255, // Color
                   1, 0, 0); // The x-, y-, z-axis direction

  // Yellow spotlight from the front
  spotLight(255, 255, 109, // Color
            0, 40, 200, // Position
            0, -0.5, -0.5, // Direction
            PI / 2, 2); // Angle, concentration
  
  rotateY(map(x, 0, width, 0, PI));
  rotateX(map(y, 0, height, 0, PI));
  box(150);
}
project-image

步骤4 总结

通过这个项目创建了实体与虚拟3d图形的随动联系,交互动作。当然你可以把2轴摇杆,2个按钮的值也传出,创造更加复杂的图形和同步动作和功能,还可以控制其他硬件,比如机械臂,还可以控制两个差异图形使用3d眼镜观看等等。

步骤5 视频演示

评论

user-avatar