我似乎无法让舵机移动。我使用的是 p9_14 引脚,我的伺服系统是 Tower Pro SG92R,我的 beaglebone black 上有 Debian
如果可能的话,我正在尝试不使用库来解决它(用于练习)
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define PWM_PERIOD 20000
#define DUTY_MIN 1000
#define DUTY_MAX 2000
void writeToFile(const char* path, int value) {
FILE* file = fopen(path, "w");
if(file == NULL) {
perror("Error opening file");
printf("My integer is: %d\n", value);
printf(path);
exit(1);
}
fprintf(file, "%d", value);
fclose(file);
}
int main(){
writeToFile("/sys/class/pwm/pwm-0:0/enable", 1);
writeToFile("/sys/class/pwm/pwm-0:0/period", PWM_PERIOD);
while(1){
// Move the servo to the left (minimum position)
writeToFile("/sys/class/pwm/pwm-0:0/duty_cycle", DUTY_MIN);
usleep(2000000);
// Move the servo to the right (maximum position)
writeToFile("/sys/class/pwm/pwm-0:0/duty_cycle", DUTY_MAX);
usleep(2000000);
// Move the servo to the center position
writeToFile("/sys/class/pwm/pwm-0:0/duty_cycle", (DUTY_MIN + DUTY_MAX) / 2);
usleep(2000000);
}
writeToFile("/sys/class/pwm/pwm-0:0/enable", 0);
return 0;
}
我已经尝试了所有的pwm通道,我已经玩过最大/最小占空比和周期
/dev/bone/pwm/
可能会有所帮助。这是否显示在您的文件系统中?