查看: 4527|回复: 1

[项目] 如何使用Raspberry Pi控制步进电机旋转高清摄像头并拍照

[复制链接]
  • TA的每日心情
    开心
    2016-8-15 09:30
  • 签到天数: 162 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2015-1-30 13:43:46 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 小菜儿 于 2015-1-30 13:45 编辑

    目录:
    • 硬件准备
    • 安装
      • 步进电机电源
      • 步进电机驱动线路
      • 安装摄像头
    • 驱动示例代码
      • 转动效果视频
    • 连续转动拍摄代码实现
    • 拍摄图片效果

    硬件准备

    需要以下硬件:

    • 可以工作的树莓派一个
    • 母对母1P杜邦线6根
    • DC 5V4相28YBJ-48步进电机一个
    • UL2003芯片步进电机驱动板一块
    安装

    按下图将步进电机接到驱动板上,也就是白色的接口

    1.jpg

    2.jpg

    步进电机电源

    步进电机需要5V电压驱动,而树莓派的GPIO接口中已有5V输出,将图中的Pin 2(最右上角那个)5V,接到驱动板的5V正极,Pin 6接到5V负级,电源部分则搞定。

    3.png

    步进电机驱动线路

    驱动板上有IN1, IN2, IN3, IN4四个接口,根据资料得知这四个接口依次设置为低电平就可以驱动,我们分别用杜邦线将GPIO 17(Pin 11),GPIO 18(Pin 12), GPIO 21(Pin 13), GPIO 22(Pin 15)和IN1,IN2,IN3,IN4一一相连。 注意不同的GPIO驱动程序对端口的编号不一定一样(至少有三种叫法:Board,Broadcom,GPIO)

    驱动原理:(每次将四个GPIO端口按下表依次设置好电平后,可以sleep几十毫秒来控制转速)

    QQ截图20150130134327.png

    安装摄像头

    本来是希望用3D打印机来制作齿轮和支架来完成这部分工作的,但因为打印机还没到货,所以先用乐高积木来做了, 刚好乐高积木可以插在步进电机中轴上,而且很牢靠,还不用密封带了。

    摄像头如下图用两根导线固定在乐高积木上:

    4.jpg

    然后用各种积木搭个底座把电机固定起来,并留两个洞口可以将驱动线和摄像头的USB线穿出,这样表面上比较整齐,USB线也不会因为牵扯影响转动。

    5.jpg


    驱动示例代码

    这里使用的是Python GPIO库,注意这里的端口命名是按树莓派的Board叫法(Pin 11, 12, 13, 15)

    1. root@raspberrypi2 ~/projects/step_motor # cat motor.py
    2. import RPi.GPIO as GPIO
    3. import time
    4. import sys
    5. from array import *
    6. GPIO.setwarnings(False)
    7. GPIO.setmode(GPIO.BOARD)
    8. steps    = int(sys.argv[1]);
    9. clockwise = int(sys.argv[2]);
    10. arr = [0,1,2,3];
    11. if clockwise!=1:
    12.     arr = [3,2,1,0];
    13. ports = [11,12,13,15]
    14. for p in ports:
    15.     GPIO.setup(p,GPIO.OUT)
    16. for x in range(0,steps):
    17.     for j in arr:
    18.         time.sleep(0.01)
    19.         for i in range(0,4):
    20.             if i == j:            
    21.                 GPIO.output(ports[i],True)
    22.             else:
    23.                 GPIO.output(ports[i],False)
    复制代码

    执行python motor.py 90 0 可以顺时针转动大约80度。

    执行python motor.py 90 1 则可逆时针转动大约80度。

    转动效果视频
    连续转动拍摄代码实现

    这次使用webiopi把控制程序转换成REST API,这样方便网页调用。


    1. root@raspberrypi2 ~/projects/gpio_server # cat webiopi_custom.py
    2. # Imports
    3. import webiopi
    4. import time
    5. # Retrieve GPIO lib
    6. GPIO = webiopi.GPIO
    7. # -------------------------------------------------- #
    8. # Macro definition part                              #
    9. # -------------------------------------------------- #
    10. # A custom macro which prints out the arg received and return OK
    11. def myMacroWithArgs(arg1, arg2, arg3):
    12.     print("myMacroWithArgs(%s, %s, %s)" % (arg1, arg2, arg3))
    13.     return "OK"
    14. # A custom macro without args which return nothing
    15. def myMacroWithoutArgs():
    16.     print("myMacroWithoutArgs()")
    17. # Example loop which toggle GPIO 7 each 5 seconds
    18. def loop():
    19.     time.sleep(5)        
    20. def turnLed(port_str, ms):
    21.     port = int(port_str)
    22.     GPIO.setFunction(port,GPIO.OUT)   
    23.     GPIO.output(port,GPIO.LOW)
    24.     time.sleep(float(ms)/1000)
    25.     GPIO.output(port,GPIO.HIGH)
    26. def turnWebcam(steps_str, clockwise_str):
    27.     steps = int(steps_str);
    28.     clockwise = int(clockwise_str);
    29.     arr = [0,1,2,3];
    30.     if clockwise!=1:
    31.         arr = [3,2,1,0];
    32.     ports = [17,18,27,22]
    33.     for p in ports:
    34.         GPIO.setFunction(p,GPIO.OUT)
    35.     for x in range(0,steps):
    36.         for j in arr:
    37.             time.sleep(0.01)
    38.             for i in range(0,4):
    39.                 if i == j:            
    40.                     GPIO.output(ports[i],GPIO.LOW)
    41.                 else:
    42.                     GPIO.output(ports[i],GPIO.HIGH)
    43. # -------------------------------------------------- #
    44. # Initialization part                                #
    45. # -------------------------------------------------- #
    46. # Setup GPIOs
    47. # -------------------------------------------------- #
    48. # Main server part                                   #
    49. # -------------------------------------------------- #
    50. # Instantiate the server on the port 8000, it starts immediately in its own thread
    51. server = webiopi.Server(port=8001, login="pi", password="pi")
    52. # or     webiopi.Server(port=8000, passwdfile="/etc/webiopi/passwd")
    53. # Register the macros so you can call it with Javascript and/or REST API
    54. server.addMacro(turnWebcam)
    55. server.addMacro(turnLed)
    56. # -------------------------------------------------- #
    57. # Loop execution part                                #
    58. # -------------------------------------------------- #
    59. # Run our loop until CTRL-C is pressed or SIGTERM received
    60. webiopi.runLoop()
    61. # If no specific loop is needed and defined above, just use
    62. # webiopi.runLoop()
    63. # here instead
    64. # -------------------------------------------------- #
    65. # Termination part                                   #
    66. # -------------------------------------------------- #
    67. # Cleanly stop the server
    68. server.stop()
    复制代码

    执行python webiopi_custom.py 后启动GPIO REST API服务器

    转动命令是:curl --data "" "http://pi:pi@raspberrypi2:8001/macros/turnWebcam/90,0

    拍照命令是:/usr/bin/fswebcam -v -r 640x480 --no-banner /var/www/fswebcam/foo.jpg

    于是我们可以用以下方法来实现连续拍摄:

    • 执行拍照命令, 生成right.jpg
    • 顺时转80度
    • 执行拍照命令, 生成middle.jpg
    • 顺时转80度
    • 执行拍照命令, 生成left.jpg
    • 逆时针转160度归位

    将命令通过网页执行后,就可以在外面看房间里的情况了,今天出去外面采草莓在iPhone上试了一下,结果符合预期。

    拍摄图片效果

    在手机上看到的页面:点Reload会重新连拍三张


    6.jpg

    总共花了不到2小时就可以搞定这个了,还是非常好玩的~

    后面还可以用OpenCV库来合成照片到真正的全景图


    出处:http://hugozhu.myalert.info/2013 ... -rotate-webcam.html


    回复

    使用道具 举报

  • TA的每日心情
    开心
    2015-4-19 12:59
  • 签到天数: 11 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2015-1-30 23:39:47 | 显示全部楼层
    感觉只要角度设置的好完全不用openCV拼图的
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-5-18 19:23 , Processed in 0.121525 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.