int main() {
int up = 0;
int down = 0;
int left = 0;
int right = 0;
int duty_cycle6 = DUTY_90_DEG;
int duty_cycle7 = DUTY_90_DEG;
int adcChannels[] = {1, 2, 3, 4};
int adcValues[4] = {0};
init_pwm();
write_file(PWM6_DUTY_CYCLE, duty_cycle6);
write_file(PWM7_DUTY_CYCLE, duty_cycle7);
while (1) {
for (int i = 0; i < 4; ++i) {
adcValues = read_adc_value(adcChannels);
}
up = (adcValues[3] + adcValues[0]) ;
down = (adcValues[2] + adcValues[1]) ;
left = (adcValues[3] + adcValues[2]) ;
right = (adcValues[0] + adcValues[1]) ;
printf("up: %d\n", up);
printf("down: %d\n", down);
printf("left: %d\n", left);
printf("right: %d\n", right);
if ((down - up >= 200) && (duty_cycle6 >= DUTY_0_DEG)) {
duty_cycle6 -= 100000;
write_file(PWM6_DUTY_CYCLE, duty_cycle6);
usleep(10000);
}
if ((up - down >= 200) && (duty_cycle6 <= DUTY_180_DEG)) {
duty_cycle6 += 100000;
write_file(PWM6_DUTY_CYCLE, duty_cycle6);
usleep(10000);
}
if ((right - left >= 200) && (duty_cycle7 >= DUTY_0_DEG)) {
duty_cycle7 -= 100000;
write_file(PWM7_DUTY_CYCLE, duty_cycle7);
usleep(10000);
}
if ((left - right >= 200) && (duty_cycle7 <= DUTY_180_DEG)) {
duty_cycle7 += 100000;
write_file(PWM7_DUTY_CYCLE, duty_cycle7);
usleep(10000);
}
usleep(100000);
}
return 0;
}
配置pwm。
int init_pwm()
{
write_file(PWM6_EXPORT, 0);
if (-1 == write_file(PWM6_PERIOD, PWM_PERIOD))
return -1;
if (-1 == write_file(PWM6_ENABLE, 1))
return -1;
write_file(PWM7_EXPORT, 0);
if (-1 == write_file(PWM7_PERIOD, PWM_PERIOD))
return -1;
if (-1 == write_file(PWM7_ENABLE, 1))
return -1;
return 0;
}