level 7
高大上61
楼主
import processing.serial.*;
import de.voidplus.leapmotion.*;
LeapMotion leap;
Serial serial1,serfial2;
int flag=0;
PFont font;
int r=255, g=255, b=255;
Robot bot1;
Robot bot2;
int px1=0,px2=0;
void setup() {
serial1=new Serial(this,"COM11", 57600);
//serial2=new Serial(this,"COM8", 57600);
size(1024, 768);
bot1 = new Robot("robot1.svg", 10, 10);
bot2 = new Robot("robot2.svg", 640, 30);
leap = new LeapMotion(this).withGestures();
}
void draw() {
leap_draw();
background(0, 130, 128);
// Update and display first robot
bot1.update();
bot1.display();
//bot1.xpos = px1;
// Update and display second robot
bot2.update();
bot2.display();
//bot2.xpos = px2;
}
class Robot {
float xpos;
float ypos;
float angle;
PShape botShape;
float yoffset = 0.0;
// Set initial values in constructor
Robot(String svgName, float tempX, float tempY) {
botShape = loadShape(svgName);
xpos = tempX;
ypos = tempY;
angle = random(0, TWO_PI);
}
// Update the fields
void update() {
angle += 0.05;
yoffset = sin(angle) * 20;
}
// Draw the robot to the screen
void display() {
shape(botShape, xpos, ypos + yoffset);
}
}
String bf=""; //接收字符串
boolean Is_Start=false; //串口连续接收标志
void serialEvent(Serial s) {
char a=(char)s.read();
if (a=='[') { //接收开始
Is_Start=true;
bf="";
}
else if (a==']') { //接收结束
Is_Start=false;
String[] val=split(bf, ","); //拆分字符串,生成数组
px1=int(val[0]); //red
px2=int(val[1]); //green
b=int(val[2]); //blue
print(" R:");print(r); //print
print(" G:");print(g);
print(" B:");print(b);
println(); //空行
}
else if (Is_Start) {
bf += a; //连续接收,串接字符串
}
}
void leap_draw(){
int sum1=0,sum2=0;
char LorR='0';
int count=0;
background(255);
// ...
// ...
int fps = leap.getFrameRate();
// ========= HANDS =========
for (Hand hand : leap.getHands ()) {
// ----- BASICS -----
int hand_id = hand.getId();
PVector hand_position = hand.getPosition();
PVector hand_stabilized = hand.getStabilizedPosition();
PVector hand_direction = hand.getDirection();
PVector hand_dynamics = hand.getDynamics();
float hand_roll = hand.getRoll();
float hand_pitch = hand.getPitch();
float hand_yaw = hand.getYaw();
boolean hand_is_left = hand.isLeft();
boolean hand_is_right = hand.isRight();
float hand_grab = hand.getGrabStrength();
float hand_pinch = hand.getPinchStrength();
float hand_time = hand.getTimeVisible();
PVector sphere_position = hand.getSpherePosition();
float sphere_radius = hand.getSphereRadius();
// ----- SPECIFIC FINGER -----
println(hand_direction);
Finger finger_thumb = hand.getThumb();
// or hand.getFinger("thumb");
// or hand.getFinger(0);
Finger finger_index = hand.getIndexFinger();
// or hand.getFinger("index");
// or hand.getFinger(1);
Finger finger_middle = hand.getMiddleFinger();
// or hand.getFinger("middle");
// or hand.getFinger(2);
Finger finger_ring = hand.getRingFinger();
// or hand.getFinger("ring");
// or hand.getFinger(3);
Finger finger_pink = hand.getPinkyFinger();
// or hand.getFinger("pinky");
// or hand.getFinger(4);
// ----- DRAWING -----
//hand.draw();
// hand.drawSphere();
// ========= ARM =========
if (hand.hasArm()) {
Arm arm = hand.getArm();
float arm_width = arm.getWidth();
PVector arm_wrist_pos = arm.getWristPosition();
PVector arm_elbow_pos = arm.getElbowPosition();
}
// ========= FINGERS =========
//ArrayList FingerC= hand.getOutstrechtedFingersByAngel(60);
for (Finger finger : hand.getOutstrechtedFingersByAngel(90)){ // hand.getFingers()) {
// Alternatives:
//fingerC.add( hand.getOutstrechtedFingers());
if(hand.getOutstrechtedFingers()!=null)
{
count++;
}
//hand.getOutstrechtedFingersByAngle();
// ----- BASICS -----
int finger_id = finger.getId();
PVector finger_position = finger.getPosition();
PVector finger_stabilized = finger.getStabilizedPosition();
PVector finger_velocity = finger.getVelocity();
PVector finger_direction = finger.getDirection();
float finger_time = finger.getTimeVisible();
// ----- SPECIFIC FINGER -----
if(hand.isLeft())
{
LorR='L';
switch(finger.getType()) {
case 0:
System.out.println("L1");
flag=1;
break;
case 1:
System.out.println("L2");
flag=2;
break;
case 2:
System.out.println("L3");
flag=4;
break;
case 3:
System.out.println("L4");
flag=8;
break;
case 4:
System.out.println("L5");
flag=16;
break;
}
}
// ----- SPECIFIC FINGER -----
if(hand.isRight())
{
LorR='R';
switch(finger.getType()) {
case 0:
System.out.println("R1");
flag=1;
break;
case 1:
System.out.println("R2");
flag=2;
break;
case 2:
System.out.println("R3");
flag=4;
break;
case 3:
System.out.println("R4");
flag=8;
break;
case 4:
System.out.println("R5");
flag=16;
break;
}
}
if(LorR=='L')
{
sum1=sum1+flag;
}
if(LorR=='R')
{
sum2=sum2+flag;
}
// ----- SPECIFIC BONE -----
Bone bone_distal = finger.getDistalBone();
// or finger.get("distal");
// or finger.getBone(0);
Bone bone_intermediate = finger.getIntermediateBone();
// or finger.get("intermediate");
// or finger.getBone(1);
Bone bone_proximal = finger.getProximalBone();
// or finger.get("proximal");
// or finger.getBone(2);
Bone bone_metacarpal = finger.getMetacarpalBone();
// or finger.get("metacarpal");
// or finger.getBone(3);
// ----- DRAWING -----
//finger.draw(); // = drawLines()+drawJoints()
//finger.drawLines();
//finger.drawJoints();
//textFont(font);
//fill(0);
//PVector v2 = hand_direction.get();
//String a = "(" + str(hand_direction.x) + "," + hand_direction.y + "," + hand_direction.z + ")";
//text(a,0,40);
// ----- TOUCH EMULATION -----
int touch_zone = finger.getTouchZone();
float touch_distance = finger.getTouchDistance();
switch(touch_zone) {
case -1: // None
break;
case 0: // Hovering
//println("Hovering (#"+finger_id+"): "+touch_distance);
break;
case 1: // Touching
//println("Touching (#"+finger_id+")");
break;
}
}
serial1.write("L");
serial1.write((byte)sum1);
serial1.write("R");
serial1.write((byte)sum2);
serial1.write(0x0d);
serial1.write(0x0a);
// ========= TOOLS =========
for (Tool tool : hand.getTools ()) {
// ----- BASICS -----
int tool_id = tool.getId();
PVector tool_position = tool.getPosition();
PVector tool_stabilized = tool.getStabilizedPosition();
PVector tool_velocity = tool.getVelocity();
PVector tool_direction = tool.getDirection();
float tool_time = tool.getTimeVisible();
// ----- DRAWING -----
//tool.draw();
// ----- TOUCH EMULATION -----
int touch_zone = tool.getTouchZone();
float touch_distance = tool.getTouchDistance();
switch(touch_zone) {
case -1: // None
break;
case 0: // Hovering
//println("Hovering (#"+tool_id+"): "+touch_distance);
break;
case 1: // Touching
//println("Touching (#"+tool_id+")");
break;
}
}
}
// ========= DEVICES =========
for (Device device : leap.getDevices ()) {
float device_horizontal_view_angle = device.getHorizontalViewAngle();
float device_verical_view_angle = device.getVerticalViewAngle();
float device_range = device.getRange();
}
println("The Number of outstrech Fingers:",count);
}
// ----- SWIPE GESTURE -----
void leapOnSwipeGesture(SwipeGesture g, int state){
int id = g.getId();
Finger finger = g.getFinger();
PVector position = g.getPosition();
PVector position_start = g.getStartPosition();
PVector direction = g.getDirection();
float speed = g.getSpeed();
long duration = g.getDuration();
float duration_seconds = g.getDurationInSeconds();
switch(state){
case 1: // Start
break;
case 2: // Update
break;
case 3: // Stop
//println("SwipeGesture: "+id);
break;
}
}
// ----- CIRCLE GESTURE -----
void leapOnCircleGesture(CircleGesture g, int state){
int id = g.getId();
Finger finger = g.getFinger();
PVector position_center = g.getCenter();
float radius = g.getRadius();
float progress = g.getProgress();
long duration = g.getDuration();
float duration_seconds = g.getDurationInSeconds();
int direction = g.getDirection();
switch(state){
case 1: // Start
break;
case 2: // Update
break;
case 3: // Stop
//println("CircleGesture: "+id);
break;
}
switch(direction){
case 0: // Anticlockwise/Left gesture
break;
case 1: // Clockwise/Right gesture
break;
}
}
// ----- SCREEN TAP GESTURE -----
void leapOnScreenTapGesture(ScreenTapGesture g){
int id = g.getId();
Finger finger = g.getFinger();
PVector position = g.getPosition();
PVector direction = g.getDirection();
long duration = g.getDuration();
float duration_seconds = g.getDurationInSeconds();
//println("ScreenTapGesture: "+id);
}
// ----- KEY TAP GESTURE -----
void leapOnKeyTapGesture(KeyTapGesture g){
int id = g.getId();
Finger finger = g.getFinger();
PVector position = g.getPosition();
PVector direction = g.getDirection();
long duration = g.getDuration();
float duration_seconds = g.getDurationInSeconds();
//println("KeyTapGesture: "+id);
}
void leapOnInit() {
//println("Leap Motion Init");
}
void leapOnConnect() {
//println("Leap Motion Connect");
}
void leapOnFrame() {
//println("Leap Motion Frame");
}
void leapOnDisconnect() {
//println("Leap Motion Disconnect");
}
void leapOnExit() {
//println("Leap Motion Exit");
}
2016年01月16日 08点01分
1
import de.voidplus.leapmotion.*;
LeapMotion leap;
Serial serial1,serfial2;
int flag=0;
PFont font;
int r=255, g=255, b=255;
Robot bot1;
Robot bot2;
int px1=0,px2=0;
void setup() {
serial1=new Serial(this,"COM11", 57600);
//serial2=new Serial(this,"COM8", 57600);
size(1024, 768);
bot1 = new Robot("robot1.svg", 10, 10);
bot2 = new Robot("robot2.svg", 640, 30);
leap = new LeapMotion(this).withGestures();
}
void draw() {
leap_draw();
background(0, 130, 128);
// Update and display first robot
bot1.update();
bot1.display();
//bot1.xpos = px1;
// Update and display second robot
bot2.update();
bot2.display();
//bot2.xpos = px2;
}
class Robot {
float xpos;
float ypos;
float angle;
PShape botShape;
float yoffset = 0.0;
// Set initial values in constructor
Robot(String svgName, float tempX, float tempY) {
botShape = loadShape(svgName);
xpos = tempX;
ypos = tempY;
angle = random(0, TWO_PI);
}
// Update the fields
void update() {
angle += 0.05;
yoffset = sin(angle) * 20;
}
// Draw the robot to the screen
void display() {
shape(botShape, xpos, ypos + yoffset);
}
}
String bf=""; //接收字符串
boolean Is_Start=false; //串口连续接收标志
void serialEvent(Serial s) {
char a=(char)s.read();
if (a=='[') { //接收开始
Is_Start=true;
bf="";
}
else if (a==']') { //接收结束
Is_Start=false;
String[] val=split(bf, ","); //拆分字符串,生成数组
px1=int(val[0]); //red
px2=int(val[1]); //green
b=int(val[2]); //blue
print(" R:");print(r); //print
print(" G:");print(g);
print(" B:");print(b);
println(); //空行
}
else if (Is_Start) {
bf += a; //连续接收,串接字符串
}
}
void leap_draw(){
int sum1=0,sum2=0;
char LorR='0';
int count=0;
background(255);
// ...
// ...
int fps = leap.getFrameRate();
// ========= HANDS =========
for (Hand hand : leap.getHands ()) {
// ----- BASICS -----
int hand_id = hand.getId();
PVector hand_position = hand.getPosition();
PVector hand_stabilized = hand.getStabilizedPosition();
PVector hand_direction = hand.getDirection();
PVector hand_dynamics = hand.getDynamics();
float hand_roll = hand.getRoll();
float hand_pitch = hand.getPitch();
float hand_yaw = hand.getYaw();
boolean hand_is_left = hand.isLeft();
boolean hand_is_right = hand.isRight();
float hand_grab = hand.getGrabStrength();
float hand_pinch = hand.getPinchStrength();
float hand_time = hand.getTimeVisible();
PVector sphere_position = hand.getSpherePosition();
float sphere_radius = hand.getSphereRadius();
// ----- SPECIFIC FINGER -----
println(hand_direction);
Finger finger_thumb = hand.getThumb();
// or hand.getFinger("thumb");
// or hand.getFinger(0);
Finger finger_index = hand.getIndexFinger();
// or hand.getFinger("index");
// or hand.getFinger(1);
Finger finger_middle = hand.getMiddleFinger();
// or hand.getFinger("middle");
// or hand.getFinger(2);
Finger finger_ring = hand.getRingFinger();
// or hand.getFinger("ring");
// or hand.getFinger(3);
Finger finger_pink = hand.getPinkyFinger();
// or hand.getFinger("pinky");
// or hand.getFinger(4);
// ----- DRAWING -----
//hand.draw();
// hand.drawSphere();
// ========= ARM =========
if (hand.hasArm()) {
Arm arm = hand.getArm();
float arm_width = arm.getWidth();
PVector arm_wrist_pos = arm.getWristPosition();
PVector arm_elbow_pos = arm.getElbowPosition();
}
// ========= FINGERS =========
//ArrayList FingerC= hand.getOutstrechtedFingersByAngel(60);
for (Finger finger : hand.getOutstrechtedFingersByAngel(90)){ // hand.getFingers()) {
// Alternatives:
//fingerC.add( hand.getOutstrechtedFingers());
if(hand.getOutstrechtedFingers()!=null)
{
count++;
}
//hand.getOutstrechtedFingersByAngle();
// ----- BASICS -----
int finger_id = finger.getId();
PVector finger_position = finger.getPosition();
PVector finger_stabilized = finger.getStabilizedPosition();
PVector finger_velocity = finger.getVelocity();
PVector finger_direction = finger.getDirection();
float finger_time = finger.getTimeVisible();
// ----- SPECIFIC FINGER -----
if(hand.isLeft())
{
LorR='L';
switch(finger.getType()) {
case 0:
System.out.println("L1");
flag=1;
break;
case 1:
System.out.println("L2");
flag=2;
break;
case 2:
System.out.println("L3");
flag=4;
break;
case 3:
System.out.println("L4");
flag=8;
break;
case 4:
System.out.println("L5");
flag=16;
break;
}
}
// ----- SPECIFIC FINGER -----
if(hand.isRight())
{
LorR='R';
switch(finger.getType()) {
case 0:
System.out.println("R1");
flag=1;
break;
case 1:
System.out.println("R2");
flag=2;
break;
case 2:
System.out.println("R3");
flag=4;
break;
case 3:
System.out.println("R4");
flag=8;
break;
case 4:
System.out.println("R5");
flag=16;
break;
}
}
if(LorR=='L')
{
sum1=sum1+flag;
}
if(LorR=='R')
{
sum2=sum2+flag;
}
// ----- SPECIFIC BONE -----
Bone bone_distal = finger.getDistalBone();
// or finger.get("distal");
// or finger.getBone(0);
Bone bone_intermediate = finger.getIntermediateBone();
// or finger.get("intermediate");
// or finger.getBone(1);
Bone bone_proximal = finger.getProximalBone();
// or finger.get("proximal");
// or finger.getBone(2);
Bone bone_metacarpal = finger.getMetacarpalBone();
// or finger.get("metacarpal");
// or finger.getBone(3);
// ----- DRAWING -----
//finger.draw(); // = drawLines()+drawJoints()
//finger.drawLines();
//finger.drawJoints();
//textFont(font);
//fill(0);
//PVector v2 = hand_direction.get();
//String a = "(" + str(hand_direction.x) + "," + hand_direction.y + "," + hand_direction.z + ")";
//text(a,0,40);
// ----- TOUCH EMULATION -----
int touch_zone = finger.getTouchZone();
float touch_distance = finger.getTouchDistance();
switch(touch_zone) {
case -1: // None
break;
case 0: // Hovering
//println("Hovering (#"+finger_id+"): "+touch_distance);
break;
case 1: // Touching
//println("Touching (#"+finger_id+")");
break;
}
}
serial1.write("L");
serial1.write((byte)sum1);
serial1.write("R");
serial1.write((byte)sum2);
serial1.write(0x0d);
serial1.write(0x0a);
// ========= TOOLS =========
for (Tool tool : hand.getTools ()) {
// ----- BASICS -----
int tool_id = tool.getId();
PVector tool_position = tool.getPosition();
PVector tool_stabilized = tool.getStabilizedPosition();
PVector tool_velocity = tool.getVelocity();
PVector tool_direction = tool.getDirection();
float tool_time = tool.getTimeVisible();
// ----- DRAWING -----
//tool.draw();
// ----- TOUCH EMULATION -----
int touch_zone = tool.getTouchZone();
float touch_distance = tool.getTouchDistance();
switch(touch_zone) {
case -1: // None
break;
case 0: // Hovering
//println("Hovering (#"+tool_id+"): "+touch_distance);
break;
case 1: // Touching
//println("Touching (#"+tool_id+")");
break;
}
}
}
// ========= DEVICES =========
for (Device device : leap.getDevices ()) {
float device_horizontal_view_angle = device.getHorizontalViewAngle();
float device_verical_view_angle = device.getVerticalViewAngle();
float device_range = device.getRange();
}
println("The Number of outstrech Fingers:",count);
}
// ----- SWIPE GESTURE -----
void leapOnSwipeGesture(SwipeGesture g, int state){
int id = g.getId();
Finger finger = g.getFinger();
PVector position = g.getPosition();
PVector position_start = g.getStartPosition();
PVector direction = g.getDirection();
float speed = g.getSpeed();
long duration = g.getDuration();
float duration_seconds = g.getDurationInSeconds();
switch(state){
case 1: // Start
break;
case 2: // Update
break;
case 3: // Stop
//println("SwipeGesture: "+id);
break;
}
}
// ----- CIRCLE GESTURE -----
void leapOnCircleGesture(CircleGesture g, int state){
int id = g.getId();
Finger finger = g.getFinger();
PVector position_center = g.getCenter();
float radius = g.getRadius();
float progress = g.getProgress();
long duration = g.getDuration();
float duration_seconds = g.getDurationInSeconds();
int direction = g.getDirection();
switch(state){
case 1: // Start
break;
case 2: // Update
break;
case 3: // Stop
//println("CircleGesture: "+id);
break;
}
switch(direction){
case 0: // Anticlockwise/Left gesture
break;
case 1: // Clockwise/Right gesture
break;
}
}
// ----- SCREEN TAP GESTURE -----
void leapOnScreenTapGesture(ScreenTapGesture g){
int id = g.getId();
Finger finger = g.getFinger();
PVector position = g.getPosition();
PVector direction = g.getDirection();
long duration = g.getDuration();
float duration_seconds = g.getDurationInSeconds();
//println("ScreenTapGesture: "+id);
}
// ----- KEY TAP GESTURE -----
void leapOnKeyTapGesture(KeyTapGesture g){
int id = g.getId();
Finger finger = g.getFinger();
PVector position = g.getPosition();
PVector direction = g.getDirection();
long duration = g.getDuration();
float duration_seconds = g.getDurationInSeconds();
//println("KeyTapGesture: "+id);
}
void leapOnInit() {
//println("Leap Motion Init");
}
void leapOnConnect() {
//println("Leap Motion Connect");
}
void leapOnFrame() {
//println("Leap Motion Frame");
}
void leapOnDisconnect() {
//println("Leap Motion Disconnect");
}
void leapOnExit() {
//println("Leap Motion Exit");
}