qqcc30 發表於 2017-3-15 16:59:22

leap motion傳輸訊息給arduino

目的是以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();             //連結arduino(com3),並com3=serial.list()
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,請大哥大姐們幫忙,小弟目前剛加入這個行列還不是很懂這方面的邏輯。歡迎指教謝謝大家。
頁: [1]
查看完整版本: leap motion傳輸訊息給arduino