Arduino人间大炮 - 使用Leap Motion控制

2014-01-184.9万
AI 快速预览详细 收起
Arduino人间大炮是一个通过Leap Motion和Processing将手的位置传回,驱动Servo转动的互动项目。主要硬件包括Arduino UNO、两个舵机Servo、一个红LED等。通过特定的数据处理逻辑,将Leap Motion捕捉的手位置映射到舵机的角度,实现精准控制。适合初学者的互动项目,让孩子在玩乐中学习编程和机械控制。


Arduino和Leap motion连接在同一台电脑上,通过Processin将手的位置传回,驱动Servo转动。

Arduino人间大炮 - 使用Leap Motion控制图1




更新 2014-01-25
图中的LED Matrix替换为一个单色二极管。下面的图和代码也是依此更改。



原理:
Leap Motion捕捉手的位置,通过Processing取得部分数据,并传输给Arduino以控制两个舵机和一个LED。

主要硬件:
一个Arduino UNO
两个舵机Servo
一个红LED
一个200欧姆电Resistor
一个面包板
一包连接线
一个Leap Motion


与软硬件有关的网址
Leap Motion Dev社区
https://developer.leapmotion.com/

Processing
http://www.processing.org

LeapMotionForProcessing by Darius Morawiec
https://github.com/voidplus/leap-motion-processing

Arduino Servo
http://arduino.cc/en/reference/servo

连线图
Arduino人间大炮 - 使用Leap Motion控制图2



Arduino代码
  1. #include <Servo.h>
  2. Servo leapservo;
  3. Servo leapservo2;
  4. int servopin=6;
  5. int servo2pin=10;
  6. int breadboardledpin = 4;
  7. void setup(){
  8.   Serial.begin(9600);
  9.   leapservo2.attach(servo2pin);
  10.   leapservo.attach(servopin);
  11.   pinMode(breadboardledpin,OUTPUT);
  12. }
  13. void loop(){
  14.   if(Serial.available()){
  15.     char buffer[3];
  16.     Serial.readBytes(buffer,3);
  17.     int leaphandx = buffer[0];
  18.     if (leaphandx<=-76 && leaphandx >-128){
  19.       leaphandx = 256-abs(leaphandx);
  20.     }
  21.     else if (leaphandx > -76 && leaphandx<0){
  22.       leaphandx = 179;
  23.     }
  24.     else{
  25.       leaphandx = buffer[0];
  26.     }
  27.     int leaphandy = buffer[1];
  28.     if (leaphandy<=-76 && leaphandy >-128){
  29.       leaphandy = 256-abs(buffer[0]);
  30.     }
  31.     else if (leaphandy > -70 && leaphandy<0){
  32.       leaphandy = 179;
  33.     }
  34.     else{
  35.       leaphandy = buffer[1];
  36.     }
  37.     int breadboardled = buffer[2];
  38.     if(breadboardled>=0 && breadboardled<110){
  39.       digitalWrite(breadboardledpin,HIGH);
  40.     }
  41.     else{
  42.       digitalWrite(breadboardledpin,LOW);
  43.     }
  44.     leapservo2.write(leaphandx);   
  45.     leapservo.write(leaphandy);
  46.   }
  47.   Serial.flush();
  48. }
复制代码

Processing代码
  1. import processing.serial.*;
  2. import de.voidplus.leapmotion.*;
  3. import development.*;
  4. Serial port;
  5. LeapMotion leap;
  6. float processhandx;
  7. float processhandy;
  8. float processhandz;
  9. void setup() {
  10.   size(800, 500, P3D);
  11.   background(255);
  12.   noStroke();
  13.   fill(50);
  14.   port = new Serial(this, Serial.list()[2], 9600);   
  15.   leap = new LeapMotion(this);
  16. }
  17. void draw() {
  18.   background(100);
  19.   int fps = leap.getFrameRate();
  20.   for (Hand hand : leap.getHands()) {
  21.     hand.draw();
  22.     PVector hand_position    = hand.getPosition();
  23.     PVector hand_dynamics = hand.getDynamics() ;
  24.     int finger_count    = hand.countFingers();
  25.     boolean  get_finger = hand.hasFingers();
  26.     if (hand_position.x<20) {
  27.       processhandx = 0;
  28.     }
  29.     else if (hand_position.x>=780) {
  30.       processhandx = 179;
  31.     }
  32.     else {
  33.       processhandx = map(hand_position.x, 20, 780, 0, 179);
  34.     }
  35.     if (hand_position.y<50) {
  36.       processhandy = 0;
  37.     }
  38.     else if (hand_position.y>=450) {
  39.       processhandy = 179;
  40.     }
  41.     else {
  42.       processhandy = map(hand_position.y, 50, 450, 0, 179);
  43.     }
  44.     if (hand_position.z< -50) {
  45.       processhandz = 0;
  46.     }
  47.     else if (hand_position.z>=50) {
  48.       processhandz = 179;
  49.     }
  50.     else {
  51.       processhandz = map(hand_position.z, -50, 50, 0, 179);
  52.     }
  53.    
  54.     byte[] q = {
  55.       byte(processhandx), byte(processhandy), byte(processhandz)
  56.     };
  57.     println(q);
  58.    
  59.     port.write(q);
  60.   }
  61. }
复制代码




创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(15)
让生活更有趣
让生活更有趣
楼主你好,我的情况同19楼,能否告知一下到哪里下载development这个库文件,万分感谢
红明
红明
作者
回复让生活更有趣
抱歉,时间太久,我也记不清楚了,刚才查了leapmotion和processing社区,没有发现对应的模块。
修正液
修正液
楼主你好,processing中找不到development这个库,这个能在哪里下载呢?
DEEOBOT萌新
DEEOBOT萌新回复修正液
哥们儿,你找到那个库在哪儿下了吗?
小冬527
小冬527
我最近在用leap motion控制机械臂,processing做上位机,arduino做下位机,和楼主你这个很像,所以就借用了您的代码进行测试,但是就是找不到这个development的库文件,能否告知一下,我应该怎么进行添加,谢谢啦
小冬527
小冬527
楼主你好,找不到你的development的库
toooo477
toooo477
楼主你码代码都不注释一下嘛~:$
红明
红明
作者
回复toooo477
你好,最近再写代码,我都加注释了。因为过一段时间,我自己都不知道某一段的含义,哈哈。
b1045851730
b1045851730
import development.*;
这个development包不存在,在哪里可以找到
hdc
hdc
Leap Motion 听说真正的使用效果不怎么样?是不是
冰禾
冰禾
有意思有前途
社区活动向导
社区活动向导
您好,您的项目已被推荐至 DF创客社区“梦幻3D打印机大奖”,有机会赢取3D打印机大奖。
https://mc.dfrobot.com.cn/thread-941-1-1.html
何处不江南
何处不江南
用这个控制机械手 更有搞头,楼主怎么看 哈哈
我爱小宇宙
我爱小宇宙
如果可以的话,能否同时检测到全身的动作数据,这样就可以同时控制一个人形机器人,就像铁甲钢拳的电影一样,一定很爽啊!~~~
红明
红明
作者
回复我爱小宇宙
Leap Motion监测手的活动。
全身动作数据,得想别的办法,比如试试从Kinect取数据。
cj2q
cj2q
大户啊 那个感应装置买来多少钱
cj2q
cj2q回复红明
楼主果然是技术型人才;P
红明
红明
作者
回复cj2q
Leap Motion内测期间,提交了一个开发计划,审核通过后就能免费获得一个开发版,去年年初的事。现在淘宝上也有做代购的,5、6百块。
红明
红明
作者
更新好了。能演示,不过也仅仅是能演示。
下午尝试同时取手的位置、姿态和手指数量,一堆舵机,后来发现速度变慢。
年后再细细研究。
BoBo
BoBo
能公布一下代码和连接图吗?:)
红明
红明
作者
回复BoBo
你好,Bobo,我这几天在进行。。。算是程序员常说的“重构”吗,哈哈
之前只能传输两个值过来,舵机也会突然翻转,这几天慢慢可以稳定地重现手的位置、手指数量等的数量,身边的程序员都愿意帮忙,没有比这个更好的了。
明天下午,如果事情做完了,我就整理一下,分享给大家。
谢谢你。
R2D2
R2D2
这个非常有意思。
- 没有更多了 -