学习记录
手上有一套电赛时候买的舵机云台。扭矩15KG。型号为LD-1501MG。
舵机
两个LD-1501MG舵机角度范围都是180度。
控制的PWM波周期为20ms。
角度与正脉冲宽度映射如下所示。
0.5ms--------------0度;
1.0ms------------45度;
1.5ms------------90度;
2.0ms-----------135度;
2.5ms-----------180度;
工作电压6-7.4V,实际使用中,空转时5V可驱动。
代码
直接上代码。
/*************************************************************************
> File Name: sopwm.c
> Author: admin
> Mail:
> Created Time: 2020年01月03日 星期五 18时43分25秒
> Modified Time:2020年01月03日 星期五 21时34分38秒
************************************************************************/
#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>
#include <stdlib.h>
#define pwm_pin 1
int main (void)
{
wiringPiSetup ();
int temp = 0;
if(0 !=softPwmCreate (pwm_pin, 0, 200))
{
printf("WP!\n");
exit(0);
}
temp = 10;
while(1)
{
temp = temp + 5;
if(temp > 25)
temp = 5;
softPwmWrite(pwm_pin,temp);
printf("%d\n", temp);
delay(1000);
}
return 0 ;
}
分析
代码功能很简单,产生一个周期为20ms,正脉冲宽度从0.5ms到2.5ms变化的PWM波。
关于PWM波的使用可以看我的另一篇博客树莓派 PWM输出。