Ubuntu+LeapMotion C++环境配置及例子

来源:互联网 发布:淘宝如何快速打造爆款 编辑:程序博客网 时间:2024/06/07 02:22

基础配置见   地址


首先确保能LeapMotion正常运行,打开Visualizer看看是否正常,如果不正常,记得

systemctl daemon-reloadsudo leapd

LeapControlPanel


然后就开始喜闻乐见的Makefile环节了……


官网是提供里Makefile的,使用起来也非常简单,直接cd到目录下

make
就能看到执行文件

./Sample

就可以运行了


-----------------------------------------------

下面提供CMakeLists.txt版本

新建CMakeLists.txt

cmake_minimum_required(VERSION 2.8)project(test_LP)#头文件include_directories(/home/zmd/LeapMotion/LeapDeveloperKit_2.3.1+31549_linux/LeapSDK/include)#库文件目录link_directories("/home/zmd/LeapMotion/LeapDeveloperKit_2.3.1+31549_linux/LeapSDK/lib/x64")add_executable(test_LP Sample.cpp)target_link_libraries(test_LP libLeap.so)

以及sample.cpp

/******************************************************************************\* Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.               ** Leap Motion proprietary and confidential. Not for distribution.              ** Use subject to the terms of the Leap Motion SDK Agreement available at       ** https://developer.leapmotion.com/sdk_agreement, or another agreement         ** between Leap Motion and you, your company or other organization.             *\******************************************************************************/#include <iostream>#include <cstring>#include "Leap.h"using namespace Leap;class SampleListener : public Listener {  public:    virtual void onInit(const Controller&);    virtual void onConnect(const Controller&);    virtual void onDisconnect(const Controller&);    virtual void onExit(const Controller&);    virtual void onFrame(const Controller&);    virtual void onFocusGained(const Controller&);    virtual void onFocusLost(const Controller&);    virtual void onDeviceChange(const Controller&);    virtual void onServiceConnect(const Controller&);    virtual void onServiceDisconnect(const Controller&);  private:};const std::string fingerNames[] = {"Thumb", "Index", "Middle", "Ring", "Pinky"};const std::string boneNames[] = {"Metacarpal", "Proximal", "Middle", "Distal"};const std::string stateNames[] = {"STATE_INVALID", "STATE_START", "STATE_UPDATE", "STATE_END"};void SampleListener::onInit(const Controller& controller) {  std::cout << "Initialized" << std::endl;}void SampleListener::onConnect(const Controller& controller) {  std::cout << "Connected" << std::endl;  controller.enableGesture(Gesture::TYPE_CIRCLE);  controller.enableGesture(Gesture::TYPE_KEY_TAP);  controller.enableGesture(Gesture::TYPE_SCREEN_TAP);  controller.enableGesture(Gesture::TYPE_SWIPE);}void SampleListener::onDisconnect(const Controller& controller) {  // Note: not dispatched when running in a debugger.  std::cout << "Disconnected" << std::endl;}void SampleListener::onExit(const Controller& controller) {  std::cout << "Exited" << std::endl;}void SampleListener::onFrame(const Controller& controller) {  // Get the most recent frame and report some basic information  const Frame frame = controller.frame();  std::cout << "Frame id: " << frame.id()            << ", timestamp: " << frame.timestamp()            << ", hands: " << frame.hands().count()            << ", extended fingers: " << frame.fingers().extended().count()            << ", tools: " << frame.tools().count()            << ", gestures: " << frame.gestures().count() << std::endl;  HandList hands = frame.hands();  for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {    // Get the first hand    const Hand hand = *hl;    std::string handType = hand.isLeft() ? "Left hand" : "Right hand";    std::cout << std::string(2, ' ') << handType << ", id: " << hand.id()              << ", palm position: " << hand.palmPosition() << std::endl;    // Get the hand's normal vector and direction    const Vector normal = hand.palmNormal();    const Vector direction = hand.direction();    // Calculate the hand's pitch, roll, and yaw angles    std::cout << std::string(2, ' ') <<  "pitch: " << direction.pitch() * RAD_TO_DEG << " degrees, "              << "roll: " << normal.roll() * RAD_TO_DEG << " degrees, "              << "yaw: " << direction.yaw() * RAD_TO_DEG << " degrees" << std::endl;    // Get the Arm bone    Arm arm = hand.arm();    std::cout << std::string(2, ' ') <<  "Arm direction: " << arm.direction()              << " wrist position: " << arm.wristPosition()              << " elbow position: " << arm.elbowPosition() << std::endl;    // Get fingers    const FingerList fingers = hand.fingers();    for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {      const Finger finger = *fl;      std::cout << std::string(4, ' ') <<  fingerNames[finger.type()]                << " finger, id: " << finger.id()                << ", length: " << finger.length()                << "mm, width: " << finger.width() << std::endl;      // Get finger bones      for (int b = 0; b < 4; ++b) {        Bone::Type boneType = static_cast<Bone::Type>(b);        Bone bone = finger.bone(boneType);        std::cout << std::string(6, ' ') <<  boneNames[boneType]                  << " bone, start: " << bone.prevJoint()                  << ", end: " << bone.nextJoint()                  << ", direction: " << bone.direction() << std::endl;      }    }  }  // Get tools  const ToolList tools = frame.tools();  for (ToolList::const_iterator tl = tools.begin(); tl != tools.end(); ++tl) {    const Tool tool = *tl;    std::cout << std::string(2, ' ') <<  "Tool, id: " << tool.id()              << ", position: " << tool.tipPosition()              << ", direction: " << tool.direction() << std::endl;  }  // Get gestures  const GestureList gestures = frame.gestures();  for (int g = 0; g < gestures.count(); ++g) {    Gesture gesture = gestures[g];    switch (gesture.type()) {      case Gesture::TYPE_CIRCLE:      {        CircleGesture circle = gesture;        std::string clockwiseness;        if (circle.pointable().direction().angleTo(circle.normal()) <= PI/2) {          clockwiseness = "clockwise";        } else {          clockwiseness = "counterclockwise";        }        // Calculate angle swept since last frame        float sweptAngle = 0;        if (circle.state() != Gesture::STATE_START) {          CircleGesture previousUpdate = CircleGesture(controller.frame(1).gesture(circle.id()));          sweptAngle = (circle.progress() - previousUpdate.progress()) * 2 * PI;        }        std::cout << std::string(2, ' ')                  << "Circle id: " << gesture.id()                  << ", state: " << stateNames[gesture.state()]                  << ", progress: " << circle.progress()                  << ", radius: " << circle.radius()                  << ", angle " << sweptAngle * RAD_TO_DEG                  <<  ", " << clockwiseness << std::endl;        break;      }      case Gesture::TYPE_SWIPE:      {        SwipeGesture swipe = gesture;        std::cout << std::string(2, ' ')          << "Swipe id: " << gesture.id()          << ", state: " << stateNames[gesture.state()]          << ", direction: " << swipe.direction()          << ", speed: " << swipe.speed() << std::endl;        break;      }      case Gesture::TYPE_KEY_TAP:      {        KeyTapGesture tap = gesture;        std::cout << std::string(2, ' ')          << "Key Tap id: " << gesture.id()          << ", state: " << stateNames[gesture.state()]          << ", position: " << tap.position()          << ", direction: " << tap.direction()<< std::endl;        break;      }      case Gesture::TYPE_SCREEN_TAP:      {        ScreenTapGesture screentap = gesture;        std::cout << std::string(2, ' ')          << "Screen Tap id: " << gesture.id()          << ", state: " << stateNames[gesture.state()]          << ", position: " << screentap.position()          << ", direction: " << screentap.direction()<< std::endl;        break;      }      default:        std::cout << std::string(2, ' ')  << "Unknown gesture type." << std::endl;        break;    }  }  if (!frame.hands().isEmpty() || !gestures.isEmpty()) {    std::cout << std::endl;  }}void SampleListener::onFocusGained(const Controller& controller) {  std::cout << "Focus Gained" << std::endl;}void SampleListener::onFocusLost(const Controller& controller) {  std::cout << "Focus Lost" << std::endl;}void SampleListener::onDeviceChange(const Controller& controller) {  std::cout << "Device Changed" << std::endl;  const DeviceList devices = controller.devices();  for (int i = 0; i < devices.count(); ++i) {    std::cout << "id: " << devices[i].toString() << std::endl;    std::cout << "  isStreaming: " << (devices[i].isStreaming() ? "true" : "false") << std::endl;  }}void SampleListener::onServiceConnect(const Controller& controller) {  std::cout << "Service Connected" << std::endl;}void SampleListener::onServiceDisconnect(const Controller& controller) {  std::cout << "Service Disconnected" << std::endl;}int main(int argc, char** argv) {  // Create a sample listener and controller  SampleListener listener;  Controller controller;  // Have the sample listener receive events from the controller  controller.addListener(listener);  if (argc > 1 && strcmp(argv[1], "--bg") == 0)    controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);  // Keep this process running until Enter is pressed  std::cout << "Press Enter to quit..." << std::endl;  std::cin.get();  // Remove the sample listener when done  controller.removeListener(listener);  return 0;}


接着就是喜闻乐见的cmake .

cmake .make



然后运行

./test_LP

就可以看到结果了



如果发现没数据


只要

systemctl daemon-reloadsudo leapd
重新载入leap服务器即可



----------------------------------------------下面要和Opencv串联一下啦,没兴趣自行跳过------------------------------------


惯例先放结果




Cmakelists.txt

cmake_minimum_required(VERSION 2.8)project(test)#INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})#头文件#include_directories(/home/zmd/LeapMotion/Test/include)include_directories(/home/zmd/LeapMotion/LeapDeveloperKit_2.3.1+31549_linux/LeapSDK/include)#库文件目录link_directories("/home/zmd/LeapMotion/LeapDeveloperKit_2.3.1+31549_linux/LeapSDK/lib/x64")find_package(OpenCV REQUIRED)add_executable(test Sample.cpp)target_link_libraries(test libLeap.so)target_link_libraries(test ${OpenCV_LIBS})

Sample.cpp

/******************************************************************************\* Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.               ** Leap Motion proprietary and confidential. Not for distribution.              ** Use subject to the terms of the Leap Motion SDK Agreement available at       ** https://developer.leapmotion.com/sdk_agreement, or another agreement         ** between Leap Motion and you, your company or other organization.             *\******************************************************************************/#include <iostream>#include <cstring>#include "opencv2/opencv.hpp"#include "Leap.h"using namespace Leap;using namespace cv;Vector palm;  class SampleListener : public Listener {  public:    virtual void onInit(const Controller&);    virtual void onConnect(const Controller&);    virtual void onDisconnect(const Controller&);    virtual void onExit(const Controller&);    virtual void onFrame(const Controller&);    virtual void onFocusGained(const Controller&);    virtual void onFocusLost(const Controller&);    virtual void onDeviceChange(const Controller&);    virtual void onServiceConnect(const Controller&);    virtual void onServiceDisconnect(const Controller&);  private:};const std::string fingerNames[] = {"Thumb", "Index", "Middle", "Ring", "Pinky"};const std::string boneNames[] = {"Metacarpal", "Proximal", "Middle", "Distal"};const std::string stateNames[] = {"STATE_INVALID", "STATE_START", "STATE_UPDATE", "STATE_END"};void SampleListener::onInit(const Controller& controller) {  std::cout << "Initialized" << std::endl;}void SampleListener::onConnect(const Controller& controller) {  std::cout << "Connected" << std::endl;  controller.enableGesture(Gesture::TYPE_CIRCLE);  controller.enableGesture(Gesture::TYPE_KEY_TAP);  controller.enableGesture(Gesture::TYPE_SCREEN_TAP);  controller.enableGesture(Gesture::TYPE_SWIPE);}void SampleListener::onDisconnect(const Controller& controller) {  // Note: not dispatched when running in a debugger.  std::cout << "Disconnected" << std::endl;}void SampleListener::onExit(const Controller& controller) {  std::cout << "Exited" << std::endl;}void SampleListener::onFrame(const Controller& controller) {  // Get the most recent frame and report some basic information  const Frame frame = controller.frame();  std::cout << "Frame id: " << frame.id()            << ", timestamp: " << frame.timestamp()            << ", hands: " << frame.hands().count()            << ", extended fingers: " << frame.fingers().extended().count()            << ", tools: " << frame.tools().count()            << ", gestures: " << frame.gestures().count() << std::endl;  HandList hands = frame.hands();  for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {    // Get the first hand    const Hand hand = *hl;    std::string handType = hand.isLeft() ? "Left hand" : "Right hand";    std::cout << std::string(2, ' ') << handType << ", id: " << hand.id()              << ", palm position: " << hand.palmPosition() << std::endl;palm = hand.palmPosition();     // Get the hand's normal vector and direction    const Vector normal = hand.palmNormal();    const Vector direction = hand.direction();    // Calculate the hand's pitch, roll, and yaw angles    std::cout << std::string(2, ' ') <<  "pitch: " << direction.pitch() * RAD_TO_DEG << " degrees, "              << "roll: " << normal.roll() * RAD_TO_DEG << " degrees, "              << "yaw: " << direction.yaw() * RAD_TO_DEG << " degrees" << std::endl;    // Get the Arm bone    Arm arm = hand.arm();    std::cout << std::string(2, ' ') <<  "Arm direction: " << arm.direction()              << " wrist position: " << arm.wristPosition()              << " elbow position: " << arm.elbowPosition() << std::endl;    // Get fingers    const FingerList fingers = hand.fingers();    for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {      const Finger finger = *fl;      std::cout << std::string(4, ' ') <<  fingerNames[finger.type()]                << " finger, id: " << finger.id()                << ", length: " << finger.length()                << "mm, width: " << finger.width() << std::endl;      // Get finger bones      for (int b = 0; b < 4; ++b) {        Bone::Type boneType = static_cast<Bone::Type>(b);        Bone bone = finger.bone(boneType);        std::cout << std::string(6, ' ') <<  boneNames[boneType]                  << " bone, start: " << bone.prevJoint()                  << ", end: " << bone.nextJoint()                  << ", direction: " << bone.direction() << std::endl;      }    }  }  // Get tools  const ToolList tools = frame.tools();  for (ToolList::const_iterator tl = tools.begin(); tl != tools.end(); ++tl) {    const Tool tool = *tl;    std::cout << std::string(2, ' ') <<  "Tool, id: " << tool.id()              << ", position: " << tool.tipPosition()              << ", direction: " << tool.direction() << std::endl;  }  // Get gestures  const GestureList gestures = frame.gestures();  for (int g = 0; g < gestures.count(); ++g) {    Gesture gesture = gestures[g];    switch (gesture.type()) {      case Gesture::TYPE_CIRCLE:      {        CircleGesture circle = gesture;        std::string clockwiseness;        if (circle.pointable().direction().angleTo(circle.normal()) <= PI/2) {          clockwiseness = "clockwise";        } else {          clockwiseness = "counterclockwise";        }        // Calculate angle swept since last frame        float sweptAngle = 0;        if (circle.state() != Gesture::STATE_START) {          CircleGesture previousUpdate = CircleGesture(controller.frame(1).gesture(circle.id()));          sweptAngle = (circle.progress() - previousUpdate.progress()) * 2 * PI;        }        std::cout << std::string(2, ' ')                  << "Circle id: " << gesture.id()                  << ", state: " << stateNames[gesture.state()]                  << ", progress: " << circle.progress()                  << ", radius: " << circle.radius()                  << ", angle " << sweptAngle * RAD_TO_DEG                  <<  ", " << clockwiseness << std::endl;        break;      }      case Gesture::TYPE_SWIPE:      {        SwipeGesture swipe = gesture;        std::cout << std::string(2, ' ')          << "Swipe id: " << gesture.id()          << ", state: " << stateNames[gesture.state()]          << ", direction: " << swipe.direction()          << ", speed: " << swipe.speed() << std::endl;        break;      }      case Gesture::TYPE_KEY_TAP:      {        KeyTapGesture tap = gesture;        std::cout << std::string(2, ' ')          << "Key Tap id: " << gesture.id()          << ", state: " << stateNames[gesture.state()]          << ", position: " << tap.position()          << ", direction: " << tap.direction()<< std::endl;        break;      }      case Gesture::TYPE_SCREEN_TAP:      {        ScreenTapGesture screentap = gesture;        std::cout << std::string(2, ' ')          << "Screen Tap id: " << gesture.id()          << ", state: " << stateNames[gesture.state()]          << ", position: " << screentap.position()          << ", direction: " << screentap.direction()<< std::endl;        break;      }      default:        std::cout << std::string(2, ' ')  << "Unknown gesture type." << std::endl;        break;    }  }  if (!frame.hands().isEmpty() || !gestures.isEmpty()) {    std::cout << std::endl;  }}void SampleListener::onFocusGained(const Controller& controller) {  std::cout << "Focus Gained" << std::endl;}void SampleListener::onFocusLost(const Controller& controller) {  std::cout << "Focus Lost" << std::endl;}void SampleListener::onDeviceChange(const Controller& controller) {  std::cout << "Device Changed" << std::endl;  const DeviceList devices = controller.devices();  for (int i = 0; i < devices.count(); ++i) {    std::cout << "id: " << devices[i].toString() << std::endl;    std::cout << "  isStreaming: " << (devices[i].isStreaming() ? "true" : "false") << std::endl;  }}void SampleListener::onServiceConnect(const Controller& controller) {  std::cout << "Service Connected" << std::endl;}void SampleListener::onServiceDisconnect(const Controller& controller) {  std::cout << "Service Disconnected" << std::endl;}int main(int argc, char** argv) {  // Create a sample listener and controller  SampleListener listener;  Controller controller;  // Have the sample listener receive events from the controller  controller.addListener(listener);  if (argc > 1 && strcmp(argv[1], "--bg") == 0)    controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);    bool paused = false;      //圆心        Point center = Point(255, 255);      //半径        int r = 100;      //承载图像              //参数为:承载的图像、圆心、半径、颜色、粗细、线型                  while (waitKey(30)!=27) {          Mat picture(500, 500, CV_8UC3, Scalar(255, 255, 255));          center = Point(palm[0] + 320, palm[2] + 240);          r = palm[1] / 2;          circle(picture, center, r, Scalar(0, 0, 0));          putText(picture, "Happy 6.1" , Point(center.x-r, center.y), CV_FONT_HERSHEY_COMPLEX,r/50, cvScalar(0, 0, 255, 0));            imshow("控制画图", picture);        //  waitKey(30);      }  return 0;  // Keep this process running until Enter is pressed  std::cout << "Press Enter to quit..." << std::endl;  std::cin.get();  // Remove the sample listener when done  controller.removeListener(listener);  return 0;}


儿童节快乐~~~