查看: 2726|回复: 0

[教程] 【一网打尽】LEARN RASPBERRY PI (8 of 13)

[复制链接]
  • TA的每日心情
    开心
    2016-8-4 10:56
  • 签到天数: 242 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2015-3-30 21:45:23 | 显示全部楼层 |阅读模式
    分享到:
    Overview SIMON MONK

    This lesson describes how to control a single servo motor using Python.  

    learn_raspberry_pi_overview (1).jpg
    Servo motors are controlled by pulses of varying lengths. This requires fairly accurate timing. The Raspberry Pi has one pin that generates pulses in hardware, without having to rely on the operating system. Occidentalis includes an interface to make use of this pin for controlling a servo motor.

    Parts SIMON MONK

    To build this project, you will need the following parts.
    PartQty
    learn_raspberry_pi_pi.jpg
    Raspberry Pi1
    learn_raspberry_pi_cobbler.jpg
    Cobbler Breakout Board and 26-pin IDC cable
    1
    learn_raspberry_pi_jumpers_web.jpg
    Set of male to male jumper leads1
    learn_raspberry_pi_breadboard_half_web.jpg
    Half-size breadboard1
    learn_raspberry_pi_motor_servo.jpg
    Servo1
    learn_raspberry_pi_occidentalis.png
    Adafruit Occidentalis 0.2 or later operating system distribution.
    learn_raspberry_pi_batterybox.jpg
    4 x AA or AAA battery holder and batteries.1

    Servo Motors SIMON MONK

    The position of the servo motor is set by the length of a pulse. The servo expects to receive a pulse roughly every 20 milliseconds. If that pulse is high for 1 millisecond, then the servo angle will be zero, if it is 1.5 milliseconds, then it will be at its centre position and if it is 2 milliseconds it will be at 180 degrees.

    learn_raspberry_pi_servos.png
    The end points of the servo can vary and many servos only turn through about 170 degrees. You can also buy 'continuous' servos that can rotate through the full 360 degrees.

    The PWM and Servo Kernel Module SIMON MONK

    Adafruit and Sean Cross have created a kernel module that is included with the Occidentalisdistribution. For details of creating an Occidentalis follow this link. If you want to use the module with Raspbian or some other distribution, then there is help on installing the kernel module into your environment here.
    The module is called PWM and Servo because as well as controlling servo motors, the module can also produce PWM (Pulse Width Modulation) signals that can be used (with extra electronics) to control the power to motors or lights. We will not be using the PWM feature in this lesson.
    The PWM and Servo Module uses a file type of interface, where you control what the output pin and therefore the servo is doing, by reading and writing to special files. This makes it really easy to interface with in Python or other languages.
    The files involved in using the module to drive a servo are listed below. All the files can be found in the directory /sys/class/rpi-pwm/pwm0/ on your Raspberry Pi.

    Hardware SIMON MONK

    There is only one pin on the Pi that is capable of producing pulses in this way (GPIO pin 18). This will be connected to the control pin of the servo. The power to the servo is provided by an external battery as powering the servo from the Pi itself is likely to cause it to crash as the Servo draws too much current as it starts to move. Servos require 4.8-6V DC power to the motor, but the signal level (pulse output) can be 3.3V, which is how its OK to just connect the signal line directly to the GPIO output of the Pi.

    learn_raspberry_pi_fritzing.png
    The Pi Cobbler is used to link the Raspberry Pi to the breadboard. If you have not used the Cobbler before take a look at Lesson 4 in this series.

    Servo motors generally come with three pin sockets attached. The red and brown sockets supply power (positive to red) and the third yellow or orange socket is for the control signal. To link the socket to the breadboard, use the male-to-male jumper wires.

    learn_raspberry_pi_overview.jpg
    Software SIMON MONK

    This project does not require any Python libraries to be installed.

    The Python program to make the servo sweep back and forth is listed below:

    Copy Code


    • # Servo Control
    • import time
    • def set(property, value):
    •         try:
    •                 f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
    •                 f.write(value)
    •                 f.close()       
    •         except:
    •                 print("Error writing to: " + property + " value: " + value)
    • def setServo(angle):
    •         set("servo", str(angle))
    •        
    •                
    • set("delayed", "0")
    • set("mode", "servo")
    • set("servo_max", "180")
    • set("active", "1")
    • delay_period = 0.01
    • while True:
    •         for angle in range(0, 180):
    •                 setServo(angle)
    •                 time.sleep(delay_period)
    •         for angle in range(0, 180):
    •                 setServo(180 - angle)
    •                 time.sleep(delay_period)

    To make writing to the files easier, I have written a utility function called just 'set'. The first argument to this is the file to be written to (property) and the second argument the value to write to it.
    So, the program starts with a few file writes to turn the delay mode off, set the mode to be 'servo', set the maximum servo value to be 180 and finally to make the output pin active.
    A variable (delay_period) is used to contain the time in seconds between each step of the servo.
    The while loop will just continue forever or until the program is interrupted by pressing CTRL-C. Within the loop there are two near identical 'for' loops. The first counts the angle up from 0 to 180 and the second sets the servo angle to 180 – angle, moving the servo arm back from 180 to 0 degrees.
    To install the software, connect to your Pi using SSH and then type the command:

    Copy Code


    • $ nano servo.py

    Paste the code above into the editor and then do CTRL-X and Y to save the file.

    learn_raspberry_pi_install.png
    To run the servo program just type the following command into your SSH window:

    Copy Code


    • $ python servo.py

    The servo should start to move straight away.  

    Test & Configure SIMON MONK

    If you want to make the servo move faster, try changing delay_period to a smaller value, say 0.001. Then to slow it down try increasing it to 0.1.
    If you want to control more than one servo, then the easiest way to do this is to use something like the Adafruit I2C 16 channel servo / pwm controller. This tutorial explains how to use it.















    回复

    使用道具 举报

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

    本版积分规则

    关闭

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



    手机版|小黑屋|与非网

    GMT+8, 2024-4-18 10:14 , Processed in 0.107396 second(s), 16 queries , MemCache On.

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

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.