手中的myo几乎快生灰了,由于近期更新的固件使得myo手势识别有了质的提升,可以作为大家开发的一个器件了,所以今天想到还是些一篇文章来简要描述下myo的开发吧。就像LeapMotion的api从1.x到2.x,手指识别有了质的提升,myo也是。
首先从myo官网下载驱动(http://developer.thalmic.com/downloads)、sdk和固件,然后安装他们。驱动的安装(Install结尾的那个exe)是普通软件安装,安装参见[转]windows下Myo腕带的驱动安装 这篇文章,sdk安装就是把下载的sdk解压到你电脑上的某个位置。这里依旧使用常用的windows版本的sdk。当然我还提供了备用下载地址(外国网站很卡):http://brightguo.com/sdk,你可以在这里下载最新的myo相关软件。解压sdk压缩包后,可以见到如下文件:
打开samples下的hello-myo-VisualStudio2012.sln或者hello-myo-VisualStudio2013.sln即可运行示例程序(C++),目前例子只有c++的。还有个unity的包,在第一层目录可以看到,也就是官方还支持unity的。其他语言,可以在这里找到 https://developer.thalmic.com/downloads (右下角连接Unofficial tools and language bindings)。
Myo的使用和LeapMotion差不多,而编程比LeapMotion简单很多,实际可以获取到的数据是十分有限的。因此网上也不会有多少相关教程。
下面是例子代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
// Copyright (C) 2013-2014 Thalmic Labs Inc. // Distributed under the Myo SDK license agreement. See LICENSE.txt for details. #define _USE_MATH_DEFINES #include <cmath> #include <iostream> #include <iomanip> #include <stdexcept> #include <string> #include <algorithm> // The only file that needs to be included to use the Myo C++ SDK is myo.hpp. #include <myo/myo.hpp> // Classes that inherit from myo::DeviceListener can be used to receive events from Myo devices. DeviceListener // provides several virtual functions for handling different kinds of events. If you do not override an event, the // default behavior is to do nothing. class DataCollector : public myo::DeviceListener { public: DataCollector() : onArm(false), isUnlocked(false), roll_w(0), pitch_w(0), yaw_w(0), currentPose() { } // onUnpair() is called whenever the Myo is disconnected from Myo Connect by the user. void onUnpair(myo::Myo* myo, uint64_t timestamp) { // We've lost a Myo. // Let's clean up some leftover state. roll_w = 0; pitch_w = 0; yaw_w = 0; onArm = false; isUnlocked = false; } // onOrientationData() is called whenever the Myo device provides its current orientation, which is represented // as a unit quaternion. void onOrientationData(myo::Myo* myo, uint64_t timestamp, const myo::Quaternion<float>& quat) { using std::atan2; using std::asin; using std::sqrt; using std::max; using std::min; // Calculate Euler angles (roll, pitch, and yaw) from the unit quaternion. float roll = atan2(2.0f * (quat.w() * quat.x() + quat.y() * quat.z()), 1.0f - 2.0f * (quat.x() * quat.x() + quat.y() * quat.y())); float pitch = asin(max(-1.0f, min(1.0f, 2.0f * (quat.w() * quat.y() - quat.z() * quat.x())))); float yaw = atan2(2.0f * (quat.w() * quat.z() + quat.x() * quat.y()), 1.0f - 2.0f * (quat.y() * quat.y() + quat.z() * quat.z())); // Convert the floating point angles in radians to a scale from 0 to 18. roll_w = static_cast<int>((roll + (float)M_PI)/(M_PI * 2.0f) * 18); pitch_w = static_cast<int>((pitch + (float)M_PI/2.0f)/M_PI * 18); yaw_w = static_cast<int>((yaw + (float)M_PI)/(M_PI * 2.0f) * 18); } // onPose() is called whenever the Myo detects that the person wearing it has changed their pose, for example, // making a fist, or not making a fist anymore. void onPose(myo::Myo* myo, uint64_t timestamp, myo::Pose pose) { currentPose = pose; if (pose != myo::Pose::unknown && pose != myo::Pose::rest) { // Tell the Myo to stay unlocked until told otherwise. We do that here so you can hold the poses without the // Myo becoming locked. myo->unlock(myo::Myo::unlockHold); // Notify the Myo that the pose has resulted in an action, in this case changing // the text on the screen. The Myo will vibrate. myo->notifyUserAction(); } else { // Tell the Myo to stay unlocked only for a short period. This allows the Myo to stay unlocked while poses // are being performed, but lock after inactivity. myo->unlock(myo::Myo::unlockTimed); } } // onArmSync() is called whenever Myo has recognized a Sync Gesture after someone has put it on their // arm. This lets Myo know which arm it's on and which way it's facing. void onArmSync(myo::Myo* myo, uint64_t timestamp, myo::Arm arm, myo::XDirection xDirection, float rotation, myo::WarmupState warmupState) { onArm = true; whichArm = arm; } // onArmUnsync() is called whenever Myo has detected that it was moved from a stable position on a person's arm after // it recognized the arm. Typically this happens when someone takes Myo off of their arm, but it can also happen // when Myo is moved around on the arm. void onArmUnsync(myo::Myo* myo, uint64_t timestamp) { onArm = false; } // onUnlock() is called whenever Myo has become unlocked, and will start delivering pose events. void onUnlock(myo::Myo* myo, uint64_t timestamp) { isUnlocked = true; } // onLock() is called whenever Myo has become locked. No pose events will be sent until the Myo is unlocked again. void onLock(myo::Myo* myo, uint64_t timestamp) { isUnlocked = false; } // There are other virtual functions in DeviceListener that we could override here, like onAccelerometerData(). // For this example, the functions overridden above are sufficient. // We define this function to print the current values that were updated by the on...() functions above. void print() { // Clear the current line std::cout << '\r'; // Print out the orientation. Orientation data is always available, even if no arm is currently recognized. std::cout << '[' << std::string(roll_w, '*') << std::string(18 - roll_w, ' ') << ']' << '[' << std::string(pitch_w, '*') << std::string(18 - pitch_w, ' ') << ']' << '[' << std::string(yaw_w, '*') << std::string(18 - yaw_w, ' ') << ']'; if (onArm) { // Print out the lock state, the currently recognized pose, and which arm Myo is being worn on. // Pose::toString() provides the human-readable name of a pose. We can also output a Pose directly to an // output stream (e.g. std::cout << currentPose;). In this case we want to get the pose name's length so // that we can fill the rest of the field with spaces below, so we obtain it as a string using toString(). std::string poseString = currentPose.toString(); std::cout << '[' << (isUnlocked ? "unlocked" : "locked ") << ']' << '[' << (whichArm == myo::armLeft ? "L" : "R") << ']' << '[' << poseString << std::string(14 - poseString.size(), ' ') << ']'; } else { // Print out a placeholder for the arm and pose when Myo doesn't currently know which arm it's on. std::cout << '[' << std::string(8, ' ') << ']' << "[?]" << '[' << std::string(14, ' ') << ']'; } std::cout << std::flush; } // These values are set by onArmSync() and onArmUnsync() above. bool onArm; myo::Arm whichArm; // This is set by onUnlocked() and onLocked() above. bool isUnlocked; // These values are set by onOrientationData() and onPose() above. int roll_w, pitch_w, yaw_w; myo::Pose currentPose; }; int main(int argc, char** argv) { // We catch any exceptions that might occur below -- see the catch statement for more details. try { // First, we create a Hub with our application identifier. Be sure not to use the com.example namespace when // publishing your application. The Hub provides access to one or more Myos. myo::Hub hub("com.example.hello-myo"); std::cout << "Attempting to find a Myo..." << std::endl; // Next, we attempt to find a Myo to use. If a Myo is already paired in Myo Connect, this will return that Myo // immediately. // waitForMyo() takes a timeout value in milliseconds. In this case we will try to find a Myo for 10 seconds, and // if that fails, the function will return a null pointer. myo::Myo* myo = hub.waitForMyo(10000); // If waitForMyo() returned a null pointer, we failed to find a Myo, so exit with an error message. if (!myo) { throw std::runtime_error("Unable to find a Myo!"); } // We've found a Myo. std::cout << "Connected to a Myo armband!" << std::endl << std::endl; // Next we construct an instance of our DeviceListener, so that we can register it with the Hub. DataCollector collector; // Hub::addListener() takes the address of any object whose class inherits from DeviceListener, and will cause // Hub::run() to send events to all registered device listeners. hub.addListener(&collector); // Finally we enter our main loop. while (1) { // In each iteration of our main loop, we run the Myo event loop for a set number of milliseconds. // In this case, we wish to update our display 20 times a second, so we run for 1000/20 milliseconds. hub.run(1000/20); // After processing events, we call the print() member function we defined above to print out the values we've // obtained from any events that have occurred. collector.print(); } // If a standard exception occurred, we print out its message and exit. } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Press enter to continue."; std::cin.ignore(); return 1; } } |
运行结果截图: