Configuring the servo library for Python

To control a servo, we need to send some signals through the PWM pins. We opt for using a library for controlling the servo motors.

Use the following link to get access to a Servo.py Python script from a GitHub repository:

https://github.com/MakersTeam/Edison/blob/master/Python-Examples/Servo/Servo.py

Download the file and push it to your Edison device. After that, just execute the file similar to executing a Python script:

python Servo.py

Now once you have done, you are ready to use the servo with your Intel Edison using Python.

Getting back to the hardware, the servo must be connected to digital pin 6, which is a PWM pin. Let's write a Python script that will test if the library is functioning or not:

from Servo import * 
import time
myServo = Servo("Servo")
myServo.attach(6)
while True:
# From 0 to 180 degrees
for angle in range(0,180):
myServo.write(angle)
time.sleep(0.005)
# From 180 to 0 degrees
for angle in range(180,-1,-1):
myServo.write(angle)
time.sleep(0.005)

The preceding code basically sweeps from 0 to 180 degrees and back to 0 degrees. The circuit remains the same as discussed before. Initially, we attach the servo to the servo pin. Then as the standard goes, we put the entire logic in an infinite loop. To rotate the servo to a specific angle, we use .write(angle). In the two for loops, initially we rotate from 0 to 180 degrees and in the second one, we rotate from 180 to 0 degrees.

It is also to be noted that time.sleep(time_interval) is used to pause the code for some miliseconds. When you execute the preceding code, the servo should rotate and come back to the initial position.

Now, we have all the things in place. We'll just put them in the right place and your voice controlled door will be ready. Initially, we controlled an LED and then we learned how we can operate a servo using Python. Now let's create a language model using the Sphinx knowledge base tool.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.195.225