2016年8月9日星期二

Servo Motor Control with AdndroiDAQ

Servo motors are common motion control actuators that are used in robotics, radio controlled devices, 3D printers, and other motion drive systems. Using servo motors with AndroiDAQ is easy. This article contains a brief history of the servo motor, some basic operational background for servo motors, and how to connect and use a servo motor with the AndroiDAQ data acquisition and control module.

The steam engine governor is considered to be the first servo mechanism using a powered feedback system. Its first recorded use was in 1868, by JJL Farcot, who described steam engines and hydraulics for use in steering a ship. Servo mechanisms are considered to be closed-loop systems, as the mechanism uses position feedback to control its motion and final position.


Solution

Use PWM to control the width of pulses to a servo motor to change its angle. Although this will work, the PWM generated is not completely stable, so there will be a little bit of jitter with the servo.

You should also power the servo from a separate 5V power supply because peaks in the load current are likely to crash or overload the Raspberry Pi.

To make this recipe, you will need:

    5V servo motor (see “Miscellaneous”)

    Breadboard and jumper wires (see “Prototyping Equipment”)

    1kΩ resistor (see “Resistors and Capacitors”)

    5V 1A power supply or 4.8V battery pack (see “Miscellaneous”)

The breadboard layout for this is shown in Figure 5-1.

The 1kΩ resistor is not essential, but it does protect the GPIO pin from unexpectedly high currents in the control signal, which could occur if a fault developed on the servo.


The leads of the servo may not be the same as the colors indicated in Figure 5-2. It is common for the 5V wire to be red, the ground brown, and the control lead orange.

You can, if you prefer, power the servo from a battery pack rather than a power supply. Using a four-cell AA battery holder with rechargeable batteries will provide around 4.8V and work well with a servo. Using four alkali AA cells to provide 6V will be fine for many servos, but check the datasheet of your servo to make sure it is OK with 6V.

The user interface for setting the angle of the servo is based on the gui_slider.py program intended for controlling the brightness of an LED (“Controlling the Brightness of an LED”). However, you can modify it so that the slider sets the angle, between 0 and 180 degrees (Figure 5-2).

Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called servo.py.

Note that this program uses a graphical user interface, so you cannot run it from SSH.

You must run it from the windowing environment on the Pi itself or via remote control using VNC (“Controlling the Pi Remotely with VNC”). You also need to run it as superuser, so run it with the command sudo python servo.py:

from Tkinter import *
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 100)
pwm.start(5)

class App:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        scale = Scale(frame, from_=0, to=180,
              orient=HORIZONTAL, command=self.update)
        scale.grid(row=0)


    def update(self, angle):
        duty = float(angle) / 10.0 + 2.5
        pwm.ChangeDutyCycle(duty)

root = Tk()
root.wm_title('Servo Control')
app = App(root)
root.geometry("200x50+0+0")
root.mainloop()

A Servo Motor consists of three major parts: a motor, a controller circuit, and a feedback system, which usually consists of a potentiometer which is connected to the motor’s output shaft. The motor typically drives a set of gears, which turns the output shaft of the motor and the potentiometer simultaneously. The potentiometer’s measured resistance controls the output angle of the motor shaft. This resistance is fed into the servo controller circuit and when the controller circuit detects that the motor position is correct, it stops the servo motor. If the controller circuit detects that the angle is not correct, for whatever the motor it trying to control, it will turn the servo motor the correct direction until the angle is correct. Normally a servo motor is used to control an angular motion of between 0 and 180 degrees. They typically can not turn any further unless they are modified, due to a mechanical stop which is build onto the main output gear or the potentiometer itself. Servo motors are generally used as a high performance alternative to the stepper motor.

Servo motors are controlled by sending to the servo’s control wire a pulse train of variable width, or better, a pulse width modulated signal. This pulse train has to have specific parameters such as a minimum pulse, a maximum pulse, and a repetition rate. This signal is typically derived and sent to the servo control wire by a microcontroller such as the one on AndroiDAQ. The shaft angle of the servo is determined by the duration of the pulse train sent. Given the pulse train’s constraints of having a minimum pulse, a maximum pulse, and a repetition rate, a neutral position is defined. This neutral position is where the servo has exactly the same amount of potential rotation in the clockwise direction as it does in the counterclockwise direction. This neutral position is typically always around 1.5 milliseconds.

没有评论:

发表评论