Myo臂环Unity3D开发说明文档

最近公司需求Myo做一个Demo,我到网上查了一下,国内竟然没有任何关于Myo臂环的开发文档。所以就把我的一些经验记录下来跟大家分享。

Myo共支持五种手势输入

Fist: 握拳
Wave in:内摆手
Wave out:外摆手
Double tap:双击
Fingers spread:伸开五指
在官方提供的例子中,ColorBoxByPose 中提供了实现以上五种手势的代码。

 void Update ()
    {
        // Access the ThalmicMyo component attached to the Myo game object.
        ThalmicMyo thalmicMyo = myo.GetComponent ();

        // Check if the pose has changed since last update.
        // The ThalmicMyo component of a Myo game object has a pose property that is set to the
        // currently detected pose (e.g. Pose.Fist for the user making a fist). If no pose is currently
        // detected, pose will be set to Pose.Rest. If pose detection is unavailable, e.g. because Myo
        // is not on a user's arm, pose will be set to Pose.Unknown.
        if (thalmicMyo.pose != _lastPose) {
            _lastPose = thalmicMyo.pose;

            // Vibrate the Myo armband when a fist is made.
            if (thalmicMyo.pose == Pose.Fist) {
                 thalmicMyo.Vibrate (VibrationType.Long);

                ExtendUnlockAndNotifyUserAction (thalmicMyo);

            // Change material when wave in, wave out or double tap poses are made.
            } else if (thalmicMyo.pose == Pose.WaveIn) {
                this.GetComponent().material = waveInMaterial;
                cube.transform.position += new Vector3(-1, 0, 0);

                ExtendUnlockAndNotifyUserAction (thalmicMyo);
            } else if (thalmicMyo.pose == Pose.WaveOut) {
                this.GetComponent().material = waveOutMaterial;
                cube.transform.position += new Vector3(1, 0, 0);

                ExtendUnlockAndNotifyUserAction (thalmicMyo);
            } else if (thalmicMyo.pose == Pose.DoubleTap) {
                this.GetComponent().material = doubleTapMaterial;
               cube.transform.localScale *= 1.5f;

                ExtendUnlockAndNotifyUserAction (thalmicMyo);
            }
        }
    }

分别对比当前的手势是否等同于已经定义好的五种手势。通过这个脚本就能对Myo进行简单的开发了。关于这几种手势官方的例子已经很详细,就不多做赘述。

ThalmicMyo

ThalmicMyo是核心脚本,里面的关键参数如下;
1、 armSynced是一个bool参数,为true表示Myo已经识别到手臂。注意在官方自带的例子中只有armSynced为true时才能调用Myo定义好的几种手势。
2、 arm定义右手臂、左手臂。
3、 pose里面存储着Myo探测到的手势,UnKnow表示当前手势-----。
4、 Accelerometer Myo当前加速度计数器,大概0.8 m/s^2
5、 Gyroscope 陀螺仪 轴的速度/秒
6、 isPaired 是否配对
7、 Vibrate Myo所提供的震动类型(Short\VibrationType),转到定义发现Myo定义了三种震动类型

public enum VibrationType
    {
        Short,
        Medium,
        Long
    }

8、 Unlock 解锁类型(Timed/Hold)
Timed=0; 开启一段固定的时间
Hold=1:一直到通知解锁

注意官方并不推荐"import Thalmic.Myo"这样引入命名空间,因为Thalmic.Myo包含了一些类型(如Vector3)会与Unity内置的类型相冲突。
Myo中你最常用的是手势跟震动类型,要访问这些功能,你需要添加如下声明:
Using Pose = Thalmic.Myo.Pose;
Using VibrationType = Thalmic.Myo.VibrationType;

另外我发现在官方调用中,每次使用手势都调用一下ExtendUnlockAndNotifyUserAction这个函数,拓展解锁并通知用户操作。实际测试我发现所谓通知用户操作只是短震一下,黑人问号脸.jpeg。至于基本锁定策列,没有发现不同。

// Extend the unlock if ThalmcHub's locking policy is standard, and notifies the given myo that a user action was
    // recognized.
    void ExtendUnlockAndNotifyUserAction (ThalmicMyo myo)
    {
        ThalmicHub hub = ThalmicHub.instance;

        if (hub.lockingPolicy == LockingPolicy.Standard) {
            myo.Unlock (UnlockType.Timed);
        }

        myo.NotifyUserAction ();
    }

ThalmicHub
这个脚本中ResetHub()方法,是用来初始化的。在用户摘下Myo后会调用这个函数初始化Myo。所以我们是不是可以关闭这个方法的调用来实现用户摘下Myo来给另一个用户不需要重新训练Myo呢?
并不能。
jointOrientation
这个脚本主要实现了,怎么把Myo的方向转化为用户的方向以及震动。

本文的内容是原创的,未经作者允许禁止任何形式的转载。

你可能感兴趣的:(Myo臂环Unity3D开发说明文档)