-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
208 lines (196 loc) · 8.44 KB
/
Source.cpp
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
204
205
206
207
208
// 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>
#include <SDL.h>
// 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), 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;
}
// 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.
m_roll = ((roll + (float)M_PI) / (M_PI * 2.0f) * 18);
roll_w = static_cast<int>(m_roll);
m_pitch = ((pitch + (float)M_PI / 2.0f) / M_PI * 18);
pitch_w = static_cast<int>(m_pitch);
m_yaw = ((yaw + (float)M_PI) / (M_PI * 2.0f) * 18);
yaw_w = static_cast<int>(m_yaw);
}
// 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;
// Vibrate the Myo whenever we've detected that the user has made a fist.
if (pose == myo::Pose::fist) {
myo->vibrate(myo::Myo::vibrationMedium);
}
}
// onArmRecognized() 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 onArmRecognized(myo::Myo* myo, uint64_t timestamp, myo::Arm arm, myo::XDirection xDirection)
{
onArm = true;
whichArm = arm;
}
// onArmLost() 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 onArmLost(myo::Myo* myo, uint64_t timestamp)
{
onArm = 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 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 << '[' << (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(14, ' ') << ']';
}
std::cout << std::flush;
}
// These values are set by onArmRecognized() and onArmLost() above.
bool onArm;
myo::Arm whichArm;
// These values are set by onOrientationData() and onPose() above.
int roll_w, pitch_w, yaw_w;
double m_roll, m_pitch, m_yaw;
myo::Pose currentPose;
};
void put_pixel(SDL_Surface* screen, int screen_w, int screen_h, int x, int y, Uint32 val)
{ // TODO: add error checks for invalid values and screen
if (x > screen_w || y > screen_h)
{
std::cout << "error, invalid pixel: out of screen bounds\n";
exit(-1);
}
int index = (screen_w * y) + x;
((Uint32*)screen->pixels)[index] = val;
}
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.
// waitForAnyMyo() 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 waitForAnyMyo() 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);
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
const int wind_width = 640;
const int wind_height = 480;
SDL_Window* wind = SDL_CreateWindow("Fuzzy",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
wind_width, wind_height, SDL_WINDOW_SHOWN);
if (wind == nullptr)
{
std::cout << "SDL_CreateWindow Failure, errorcode: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Renderer* rend = SDL_CreateRenderer(wind, -1, SDL_RENDERER_ACCELERATED);
if (rend == nullptr)
{
std::cout << "SDL_CreateRederer Failure, errorcode: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Surface* surf = SDL_GetWindowSurface(wind);
if (surf == nullptr)
{
std::cout << "SDL_GetWindowSurface Failur, errorcode: " << SDL_GetError() << std::endl;
return 1;
}
// 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();
SDL_UnlockSurface(surf);
Uint32* pix;
put_pixel(surf, wind_width, wind_height, wind_width*(collector.m_yaw/(18.0)), wind_height*(collector.m_pitch/18.0), 0x00ffffff);
SDL_LockSurface(surf);
SDL_UpdateWindowSurface(wind);
}
SDL_Quit();
// 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;
}
}