当前位置: 首页>>代码示例>>C++>>正文


C++ DataCollector::connectPipe方法代码示例

本文整理汇总了C++中DataCollector::connectPipe方法的典型用法代码示例。如果您正苦于以下问题:C++ DataCollector::connectPipe方法的具体用法?C++ DataCollector::connectPipe怎么用?C++ DataCollector::connectPipe使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataCollector的用法示例。


在下文中一共展示了DataCollector::connectPipe方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main()
{
	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.khalory.puppy");
		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);
		collector.connectPipe();

		myo::Vector3<float> oldAccel;
		myo::Vector3<double> curSpeed = myo::Vector3<double>(0, 0, 0);
		myo::Vector3<double> oldSpeed;
		myo::Vector3<double> avgSpeed;
		myo::Vector3<double> curPos = myo::Vector3<double>(0, 0, 0);
		auto start = std::chrono::high_resolution_clock::now();
		auto curTime = std::chrono::high_resolution_clock::now();
		long numSteps = 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();
			auto finish = std::chrono::high_resolution_clock::now();
			long nanos = std::chrono::duration_cast<std::chrono::microseconds>(finish - curTime).count();
			long nanosTotal = std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count();
			double dt = ((double)nanos) / 1000000.0;
			double timeTotal = ((double)nanosTotal) / 1000000.0;

			/*std::cout << "-----" << endl;
			std::cout << nanos << endl;
			std::cout << nanosTotal << endl;

			std::cout << dt << endl;
			std::cout << timeTotal << endl;*/

			curSpeed = myo::Vector3<double>(oldSpeed.x() + (collector.accel.x()*dt),
				oldSpeed.y() + (collector.accel.y()*dt),
				oldSpeed.z() + (collector.accel.z()*dt));

			avgSpeed = myo::Vector3<double>((numSteps - 1)*avgSpeed.x() + (collector.accel.x()*dt) / numSteps, 
				(numSteps - 1)*avgSpeed.y() + (collector.accel.y()*dt) / numSteps,
				(numSteps - 1)*avgSpeed.z() + (collector.accel.z()*dt) / numSteps);

			/*curPos = myo::Vector3<double>(curPos.x() + (.5*(curSpeed.x() + oldSpeed.x())*dt),
				curPos.y() + (.5*(curSpeed.y() + oldSpeed.y())*dt),
				curPos.z() + (.5*(curSpeed.z() + oldSpeed.z())*dt));*/
			curPos = myo::Vector3<double>(avgSpeed.x() * timeTotal,
				avgSpeed.y() * timeTotal,
				avgSpeed.z() * timeTotal);


			collector.sendData(curPos);
			oldAccel = collector.accel;
			oldSpeed = curSpeed;
			curTime = std::chrono::high_resolution_clock::now();
		}
		// 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;
	}

	return 0;
}
开发者ID:Khalory,项目名称:Ice-Cream-Puppy,代码行数:86,代码来源:RobotDoge.cpp


注:本文中的DataCollector::connectPipe方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。