概述
在本课中我们将介绍如何用树莓派控制SG90舵机。
所需器件
1 * Raspberry Pi
1 * Breadboard
1 * SG90舵机
Jumper wires
1 * T-Extension Board
1 * 40-Pin Cable
工作原理
SG90是一款180度舵机,因此SG90舵机轴可以在0-180度范围内旋转。舵机内部直流电机和定位器组成,外部有3条线,红色线是电源正极,棕色线是电源负极,SG90舵机要求电压在4.8V-6V之间电压,黄色线是信号线。信号线引脚需要一个PWM信号控制,舵机旋转角度是由PWM信号的占空比(DutyCycle)决定的。
占空比(DutyCycle)=脉冲宽度(PulseWidth)/周期(Period)
其中
周期(Period)=1/频率(Frequency)
所以
占空比=脉冲宽度/(1/频率)=脉冲宽度*频率

软件
for C language user
在Raspbian系统中有个程序模块叫ServoBlaster,这个软件模块可以很好地控制舵机,同时它还可以产生PWM信号,在本课中我们将使用ServoBlaster驱动舵机,更多关于ServoBlaster的信息请看这里。ServoBlaster模块需要自己手动安装,具体步骤请看下面
cd ~
git clone /github.com/richardghirst/PiBits.git
cd PiBits/ServoBlaster/user
make
sudo make install
执行完以上命令就能安装ServoBalster模块,安装成功后会在/dev/下面生成一个叫servolaster的设备文件,运行下面的命令检查ServoBalster是否安装成功
注意:如果出现”servod:cannot parse the hardware name string”,请将/PiBits/ServoBlaster/user/servod.c文件的第960行中的else if (strstr(modelstr, “BCM2709”) 改成
else if (strstr(modelstr, “BCM2709”) || strstr(modelstr, “BCM2835”)),再重启Pi
ls /dev/servoblaster
安装好ServoBalster接下来就可以使用了。
1) 在/home/pi下新建一个.文件,文件名随意(你爱咋咋地)
cd ~
sudo nano servo.c
向新建的文件中键入如下代码
#include
#include
#include
int main()
{
// run servo program
system("sudo /home/pi/PiBits/ServoBlaster/user/servod");
printf("\n");
while(1)
{
printf("\n");
printf("--------------------------------------->\n");
system("echo 0=50 > /dev/servoblaster");//go to 0 degree
printf("0 degree\n");
sleep(1);
system("echo 0=150 > /dev/servoblaster");//go to 90 degree
printf(" 90 degree\n");
sleep(1);
system("echo 0=250 > /dev/servoblaster");//go to 180 degree
printf(" 180 degree\n");
sleep(1);
}
return 0;
}
完整的源代码通过下面命令可以获取
gwet http:osoyoo.com/driver/pi3_start_learning_kit_lesson_12/servo.c
3) 编译
gcc -Wall -o servo servo.c
4) 执行程序
sudo ./servo
5) 最终结果
程序运行起来后,舵机会旋转到中间位置,也就是90度位置
其实,你还可以写一个shell脚本,在脚本里面调用ServoBalster更好控制舵机,具体步骤请看下面
1) 在/home/pi下新建一个shell脚本文件
cd ~
sudo nano servo.sh
2) 向新建的shell脚本中写入如下代码
#!/bin/bash
echo 'Program is running,Press Ctrl+C to end the program'
i=50
#run servod program
sudo ~/PiBits/ServoBlaster/user/servod
#go to centre position
echo 0=150 > /dev/servoblaster
#delay 1s
sleep 1
#hile loop
while [ $i -ne 250 ]
do
#go to $i position
echo 0=$i > /dev/servoblaster
sleep 0.1
#i+2
i=$(($i + 2))
done
#finally go to centre position
echo 0=150 > /dev/servoblaster
保存退出
注意:完整的shell脚本程序可以通过运行这个命令下载
wget http:osoyoo.com/driver/pi3_start_learning_kit_lesson_12/servo.sh
更多关于shell脚本编程的知识请看这里。
3) 修改shell脚本的执行权限
sudo 777 servo.sh
4) 执行shell脚本
./servo.sh
5) 最终结果
运行上面的shell脚本后,舵机首先会转到90度位置,然后转到0度,从0度位置慢慢旋转到180度位置,再从180度转到90,最后停在90度位置。
for python user
1) 在/home/pi下新建一个.py脚本文件,文件名随意(你爱咋咋地)
cd ~
sudo nano servo.py
2) 编码
向新建的python脚本文件中写入如下代码
import os
import time
#run servod program
os.system('sudo /home/pi/PiBits/ServoBlaster/user/servod')
#print message at the beginning ---custom function
def print_message():
print ('|**********************************|')
print ('| Servo test |')
print ('| ---------------------------- |')
print ('| Servo Connect to GPIO 7 |')
print ('| ---------------------------- |')
print ('| |')
print ('| OSOYOO|')
print ('|**********************************|\n')
print ('Program is running...')
print ('Please press Ctrl+C to end the program...')
pass
def main():
#print info
print_message()
while True:
#go to 0 degree(left position)
os.system("echo 0=50 > /dev/servoblaster")
print('\n')
print('------------------------------------->')
print('0 degree')
time.sleep(0.5)
#go to 90 degree(middle position)
os.system("echo 0=150 > /dev/servoblaster")
print(' 90 degree')
time.sleep(0.5)
#go to 180 degree(right position)
os.system("echo 0=250 > /dev/servoblaster")
print(' 180 degree')
time.sleep(0.5)
# if run this script directly ,do:
if __name__ == '__main__':
try:
main()
#when 'Ctrl+C' is pressed,child program destroy() will be executed.
except KeyboardInterrupt:
pass
程序运行起来后,舵机将在0度、90度、180度之间来回旋转
3) 执行脚本
sudo python servo.py
4) 最终结果
运行上面的脚本后,舵机先转到90度位置,然后在0、90、180三个位置来回移动,同时屏幕上打印出舵机位置
DownLoad Url osoyoo.com
You must be logged in to post a comment.
hi can you help me I have checked and dont see any /dev/servoblaster in my file
hi can you help me I have checked and don’t see any “/dev/servoblaster” in my file