0

这是我的代码,它运行,没有错误,但没有轮子旋转。我的设置: 1. Raspberry Pi B+ 2. L293D 用于直流电机 3. 两个直流电机 4. 使用 WiringPi 库

示例 1:

#include <iostream>
#include <wiringPi.h>
#include <softPwm.h>

using namespace std;

// motor pins (pwm)
// motor left
int motor_l_u = 26;
int motor_l_v = 27;

// motor right
int motor_r_u = 28;
int motor_r_v = 29;

// pwm
int pwmValue = 1023;
int pwmValueInit = 0;

int main(void) {

if (wiringPiSetup() == -1)
    return -1;

if (wiringPiSetupSys() == -1)
    return -1;

pinMode(motor_l_u, OUTPUT);
pinMode(motor_l_v, OUTPUT);
pinMode(motor_r_u, OUTPUT);
pinMode(motor_r_v, OUTPUT);
digitalWrite(motor_l_u, LOW); // at start turn off the GPIO
digitalWrite(motor_l_v, LOW); // at start turn off the GPIO
digitalWrite(motor_r_u, LOW); // at start turn off the GPIO
digitalWrite(motor_r_v, LOW); // at start turn off the GPIO
pinMode(motor_l_u, PWM_OUTPUT);
pinMode(motor_l_v, PWM_OUTPUT);
pinMode(motor_r_u, PWM_OUTPUT);
pinMode(motor_r_v, PWM_OUTPUT);

// prepare GPIOs for motors
softPwmCreate(motor_l_u, pwmValueInit, pwmValue);
softPwmCreate(motor_l_v, pwmValueInit, pwmValue);

softPwmCreate(motor_r_u, pwmValueInit, pwmValue);
softPwmCreate(motor_r_v, pwmValueInit, pwmValue);

// acceleration forward
for (int var = 0; var < pwmValue; ++var) {
    if (debug == 1) {
        cout << "set speed to " << var << endl;
    }
    softPwmWrite(motor_r_u, (pwmValue - var));
    softPwmWrite(motor_r_v, var);

    softPwmWrite(motor_l_u, var);
    softPwmWrite(motor_l_v, (pwmValue - var));
    delay(10);
}
// acceleration backward
for (int var = 0; var < pwmValue; ++var) {
    if (debug == 1) {
        cout << "set speed to " << var << endl;
    }
    softPwmWrite(motor_r_u, var);
    softPwmWrite(motor_r_v, (pwmValue - var));

    softPwmWrite(motor_l_u, (pwmValue - var));
    softPwmWrite(motor_l_v, var);
    delay(10);
}
return -1;
}

我的另一个例子是这样,但是编译轮子上的 do 旋转没有错误。

#include <iostream>
#include <wiringPi.h>
#include <softPwm.h>

using namespace std;

// motor pins (pwm)
// motor left
int motor_l_u = 26;
int motor_l_v = 27;

// motor right
int motor_r_u = 28;
int motor_r_v = 29;

// pwm
int pwmValue = 1023;
int pwmValueInit = 0;

int main(void) {

if (wiringPiSetup() == -1)
    return -1;

if (wiringPiSetupSys() == -1)
    return -1;

pinMode(motor_l_u, OUTPUT);
pinMode(motor_l_v, OUTPUT);
pinMode(motor_r_u, OUTPUT);
pinMode(motor_r_v, OUTPUT);
digitalWrite(motor_l_u, LOW); // at start turn off the GPIO
digitalWrite(motor_l_v, LOW); // at start turn off the GPIO
digitalWrite(motor_r_u, LOW); // at start turn off the GPIO
digitalWrite(motor_r_v, LOW); // at start turn off the GPIO
pinMode(motor_l_u, PWM_OUTPUT);
pinMode(motor_l_v, PWM_OUTPUT);
pinMode(motor_r_u, PWM_OUTPUT);
pinMode(motor_r_v, PWM_OUTPUT);

// prepare GPIOs for motors
pwmWrite(motor_l_u, pwmValueInit);
pwmWrite(motor_l_v, pwmValueInit);

pwmWrite(motor_r_u, pwmValueInit);
pwmWrite(motor_r_v, pwmValueInit);

// acceleration forward
for (int var = 0; var < pwmValue; ++var) {
    if (debug == 1) {
        cout << "set speed to " << var << endl;
    }
    pwmWrite(motor_r_u, (pwmValue - var));
    pwmWrite(motor_r_v, var);

    pwmWrite(motor_l_u, var);
    pwmWrite(motor_l_v, (pwmValue - var));
    delay(10);
}
// acceleration backward
for (int var = 0; var < pwmValue; ++var) {
    if (debug == 1) {
        cout << "set speed to " << var << endl;
    }
    pwmWrite(motor_r_u, var);
    pwmWrite(motor_r_v, (pwmValue - var));

    pwmWrite(motor_l_u, (pwmValue - var));
    pwmWrite(motor_l_v, var);
    delay(10);
}
return -1;
}

我没有看到我的问题:-(。请不要发布 python 代码。

4

1 回答 1

0

这部分错了

pinMode(motor_l_u, OUTPUT);
pinMode(motor_l_v, OUTPUT);
pinMode(motor_r_u, OUTPUT);
pinMode(motor_r_v, OUTPUT);
digitalWrite(motor_l_u, LOW); // at start turn off the GPIO
digitalWrite(motor_l_v, LOW); // at start turn off the GPIO
digitalWrite(motor_r_u, LOW); // at start turn off the GPIO
digitalWrite(motor_r_v, LOW); // at start turn off the GPIO
pinMode(motor_l_u, PWM_OUTPUT);
pinMode(motor_l_v, PWM_OUTPUT);
pinMode(motor_r_u, PWM_OUTPUT);
pinMode(motor_r_v, PWM_OUTPUT);
于 2017-03-03T02:04:16.193 回答