Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 3286|回復: 0

leap motion傳輸訊息給arduino

[複製鏈接]
發表於 2017-3-15 16:59:22 | 顯示全部樓層 |閱讀模式
目的是以leap motion掃描手部動作,並傳入arduino讓arduino端的伺服馬達能夠動作
小弟我所使用的程式是Processing , 插入的是 de.voidplus.leapmotion.*;
目標是由leap motion傳輸訊息給arduino讓arudino上的伺服馬達變化角度,達到手指伸直跟彎曲
但是leap motion伸直時也會偵測到彎曲的手指的switch,但彎曲時為正常,試了很多方試都沒辦法讓伸直時彎曲的switch不動作

import processing.serial.*;
import de.voidplus.leapmotion.*;

LeapMotion leap;
Serial myPort;

void setup() {
  size(800,800,P3D);                                       //螢幕大小
  background(255);                                        //白色背景
  String arduinoPort = Serial.list()[1];             //連結arduino(com3),並com3=serial.list()[1]
  leap = new LeapMotion(this);
  println(Serial.list());
  myPort = new Serial(this, arduinoPort, 9600);
}

void draw() {
  background(255);
   
    for (Hand hand : leap.getHands ()) {                                   //偵測leap的手
    hand.draw();                                                                  //繪製手
    for (Finger finger : hand.getOutstretchedFingers()) {           //偵測leap伸出的手指
    finger.draw();

switch(finger.getType()) {
         
      case 0:  // 0-4 = Thumb -> Pinky
         System.out.println("thumb");   //print出thumb
         myPort.write("T");                   //向arduino輸入"T"這個字元

        break;
      case 1:
         System.out.println("index");
         myPort.write("I");

        break;
      case 2:
         System.out.println("middle");
         myPort.write("M");

        break;
      case 3:
         System.out.println("ring");
         myPort.write("R");

        break;
      case 4:
         System.out.println("pinky");
         myPort.write("P");

        break;
       }      
     }
   }
        for (Hand hand : leap.getHands ()) {

         for (Finger finger : hand.getOutstretchedFingersByAngel(0)){


      switch(finger.getType()) {
         
      case 0:  // 0-4 = Thumb -> Pinky
         System.out.println("t");
         myPort.write("t");
        break;
      case 1:
         System.out.println("i");
         myPort.write("i");
        break;
      case 2:
         System.out.println("m");
         myPort.write("m");;
        break;
      case 3:
         System.out.println("r");
         myPort.write("r");
        break;
      case 4:
         System.out.println("p");
         myPort.write("p");
        break;
      //}
    }
   }
  }
}
}

不知道這樣解釋清不清楚,小弟嘗試了一些辦法,包括定義五個變數為0 在第一次的switch觸發case後改為1
在限制下個switch這樣也不行,或是在for (Finger finger : hand.getOutstretchedFingersByAngel(0)){上面加上if(hand.getOutstretchedFingers()==null){
也不行,手指合起來時會直接不讀取下面的switch,請大哥大姐們幫忙,小弟目前剛加入這個行列還不是很懂這方面的邏輯。歡迎指教謝謝大家。
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-3-29 04:44 , Processed in 0.198639 second(s), 7 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表