0

我正在开发一个控制 DIY RC卡车的 Arduino 项目,该卡车读取 RC 接收器的输出引脚,并且应该相应地对几个引脚进行PWM 。这些引脚连接到采用 PWM 的电机控制器上。

这就是问题所在。我的反向工作完美,但在向前的引脚上,我只得到随机活动。我正在使用Arduino Mega 2560

这是代码。该问题已在其下方发布:

#include <Servo.h>

//Create variables for three channels
int RXCH[3];
volatile int RXSG[3];
int RXOK[3];
int PWMSG[3];

byte vooruit;
byte achteruit;

Servo stuur;

int mv = 13;
int ma = 10;

void setup() {
    Serial.begin(115200);
    stuur.attach(8);

    //Assign PPM input pins. The receiver output pins are conected as below to non-PWM Digital connectors:
    RXCH[0] = 6;  //Throttle
    RXCH[1] = 7;  //Steering
    //RXCH[2] = 5;  //Nothing yet
    //RXCH[3] = 2;  //Nothing yet
    //RXCH[4] = 7;  //Nothing yet
    //RXCH[5] = 8;  //Nothing yet

    for (int i = 0; i < 3; i++){
        pinMode(RXCH[i], INPUT);
    }

    //TCCR1B = TCCR1B & 0b11111000 | 0x01;
    //TCCR2B = TCCR2B & 0b11111000 | 0x01;
}

void loop() {
    // Read RX values
    for (int i = 0; i < 3; i++){                 //For each of the 6 channels:
        RXSG[i] = pulseIn(RXCH[i], HIGH, 20000); //Read the receiver signal
        if (RXSG[i] == 0) {                      //Error catching
            RXSG[i] = RXOK[i];
        } else {
            RXOK[i] = RXSG[i];
        }
        //Substitute the high values to a value between -255 and 255
        PWMSG[0] = map(RXSG[0], 1000, 2000, -255, 255);
        //Servo values, calibrated according to my steering servo.
        PWMSG[1] = map(RXSG[1], 1000, 2000, 24, 169);
        //Make sure that the value stays within the desired boundaries.
        constrain (PWMSG[i], -255, 255);

        //For debugginf purposes
        Serial.print(" ||   Ch: ");
        Serial.print(i);
        Serial.print(" / PWMSG: ");
        Serial.print(PWMSG[i]);
    }
    delay (5);

    // Car goes forwards
    if (PWMSG[0] > 40)
    {
        MV();
    }

    // Car goes backwards
    if (PWMSG[0] < -40)
    {
        MA();
    }

    // Car stops
    else
    {
        stopmotor();
    }

    stuur.write(PWMSG[1]);
    Serial.println();
}

void MV()
{
    vooruit = PWMSG[0];
    analogWrite (mv, vooruit);
    digitalWrite (ma, LOW);
    Serial.print("    vooruit: ");
    Serial.print(vooruit);
}

void MA()
{
    achteruit = abs(PWMSG[0]);
    analogWrite (ma, achteruit);
    digitalWrite (mv, LOW);
    Serial.print("    achteruit: ");
    Serial.print(achteruit);
}

void stopmotor()
{
    digitalWrite (ma, LOW);
    digitalWrite (mv, LOW);
}

我真的不知道代码是否被认为是漂亮的,或者我是否为此犯了一些基本错误。

这是我尝试以正确方式进行的第一个项目之一,发表评论等,欢迎所有评论良好的批评。

代码应该做什么:

  • 向前移动发射器上的摇杆,小车前进,速度应根据摇杆的位置。
  • 将发射器上的摇杆向后移动,汽车后退,速度应根据摇杆的位置。
  • 向左或向右移动发射器上的摇杆,车内的舵机应根据 Arduino 计算的值做出反应。您可能想知道为什么我不将伺服直接放在发射器上。嗯,那是因为我对这个项目有更多未来的想法,现在我可以更容易地校准它。

问题:

  • 当我将发射器上的操纵杆向前移动,串行监视器打开时,我在串行监视器上得到正确的值,但引脚 13 上的 LED 只是随机闪烁,我必须说非常暗淡。

我已经尝试用 int 替换 byte 之类的东西,但它没有效果。其余代码工作正常。

使用一些新代码,我从每个“阶段”得到串行响应,除了控制引脚的最后阶段。

#include <Servo.h>

//Create variables for channels

Servo wheel;

int MFORWARD_PIN = 13;
#define MBACKWARD_PIN 10
#define WHEEL_PIN 8

#define THROTTLE_PIN 6
#define STEERING_PIN 7

void setup() {
    Serial.begin(115200);
    wheel.attach(WHEEL_PIN);

    pinMode(THROTTLE_PIN, INPUT);
    pinMode(STEERING_PIN, INPUT);

    pinMode(MFORWARD_PIN, OUTPUT);
    pinMode(MBACKWARD_PIN, OUTPUT);

    //TCCR1B = TCCR1B & 0b11111000 | 0x01;
    //TCCR2B = TCCR2B & 0b11111000 | 0x01;
}

void loop() {
    int throttle = read_throttle();
    int steering = read_steering();
    delay (5);
    throttle_handle(throttle);
    steering_handle(steering);
}

// Read RX values
int read_throttle(){
    int throttle = pulseIn(THROTTLE_PIN, HIGH, 20000);
    throttle = map(throttle, 1000, 2000, -255, 255);    //Substitute the high values to a value between -255 and 255.
    constrain (throttle, -255, 255);                    //Make sure that the value stays within the desired boundaries.
    //Serial.println(throttle);
}

int read_steering() {
     int steering = pulseIn(STEERING_PIN, HIGH, 20000);
     steering = map(steering, 1000, 2000, 24, 169);     //Servo values, calibrated according to my steering servo.
     constrain (steering, 24, 169);                     //Make sure that the value stays within the disired boundaries.
     //Serial.println("steering");
}

void move_forward(int val) {
    analogWrite (MFORWARD_PIN, val);
    digitalWrite (MBACKWARD_PIN, LOW);
    Serial.print("    vooruit: ");
    Serial.print(val);
}

void move_backward(int val)
{
    val = abs(val);
    analogWrite (MBACKWARD_PIN, val);
    digitalWrite (MFORWARD_PIN, LOW);
    Serial.print("    achteruit: ");
    Serial.print(val);
}

void move_stop()
{
    digitalWrite (MFORWARD_PIN, LOW);
    digitalWrite (MBACKWARD_PIN, LOW);
}

void throttle_handle(int throttle) {
    //Serial.print("throttle");
    if (throttle > 40) {
        move_forward(throttle);
    }

    if (throttle < -40) {
        move_backward(throttle);
    }
    else {
        move_stop();
    }
}

void steering_handle(int steering) {
    wheel.write(steering);
    // Serial.println("steering:");
    // Serial.print(steering);
}
4

1 回答 1

0
  • 未使用的索引:

你到处循环三个值,而你只使用数组中的两个项目。因此,您最好将所有尺寸更改为 2 而不是 3,或者您可以NB_INPUT在源代码的顶部定义一个常量,您可以轻松更改以获得更大的灵活性:

#define NB_INPUT 2
...

for (int i = 0; i<NB_INPUT; ++i) {
...
  • RXOKsetup()

您对数组的评论是有道理的,我在您的代码中看到的第一个错误是您从RXOK数组中读取,而您没有在其中放入任何值。如果您确定在循环的第一次通过RXSG时只得到零,那可能没问题,但我怀疑它是。例如:pulseIn()Read RX values

for (int i=0; i<3; ++i)
    RXOK[i] = 0;

所以你应该把值RXOK放在setup().

  • for循环中的常量索引:

然后你在 for 循环中和内部map()从 1000->2000 到 -255->255 的值,这将在三个迭代中完成。我不确定你想要什么,但如果你想为常量索引做这件事,你最好在循环之外做。但是当您为迭代循环的每个值检查 -255->255 域的约束时,我认为您可能希望对相对值执行此操作:RXSG[0]RXSG[1]

PWMSG[i] = map(RXSG[i], 1000, 2000, -255, 255);

但似乎域取决于索引,因此您可能希望在源代码的顶部进行一些定义:

#define THROTTLE_IDX 0
#define STEERING_IDX 1

并把你map()放在一个 if 语句中:

if (i == THROTTLE_IDX)
    PWMSG[i] = map(RXSG[i], 1000, 2000, -255, 255);
elif (i == STEERING_IDX)
    PWMSG[i] = map(RXSG[i], 1000, 2000, 24, 169);
# add a else statement if you need to do a map for the other values of the array
constrain(PWMSG[i], -255, 255)
  • 通用算法

我不确定您的用例是否真的需要一个数组。您最好保留一些变量,并更好地使用函数以使您的代码可读且不易出错:

#define THROTTLE_PIN 6
#define STEERING_PIN 7
#define WHEEL_PIN    8
#define MFORWARD_PIN 13
#define MBACKWARD_PIN 10

Servo wheel;

// sets up the arduino
void setup() {
    Serial.begin(115200);
    wheel.attach(WHEEL_PIN);
    pinMode(THROTTLE_PIN, INPUT);
    pinMode(STEERING_PIN, INPUT);
}

// input data handling
int read_throttle() {
    int throttle = pulseIn(THROTTLE_PIN, HIGH, 20000);
    return map(throttle, 1000, 2000, -255, 255);
}

int read_steering() {
    int steering = pulseIn(STEERING_PIN, HIGH, 20000);
    return map(throttle, 1000, 2000, 24, 169);
}

// output actions handling
void move_forward(int val) {
    analogWrite(MFORWARD_PIN, val);
    digitalWrite(MBACKWARD_PIN, LOW);
    // Serial.print...
}

void move_backward(int val) {
    analogWrite(MFORWARD_PIN, val);
    digitalWrite(MBACKWARD_PIN, LOW);
    // Serial.print...
}

void stop_motor() {
    digitalWrite(MFORWARD_PIN, LOW);
    digitalWrite(MBACKWARD_PIN, LOW);
}

void handle_throttle(int throttle) {
    if (throttle > 40)
        move_forward(throttle);
    elif (throttle < -40)
        move_backward(throttle);
    else
        stop_motor();
}

// general algorithm
void loop() {
    int throttle = read_throttle();
    delay(5);
    handle_throttle(throttle);
}

有更多的代码重复,但有时复制代码比使代码几乎不可读、难以调试但不提供任何灵活性/模块化更好。以下是我在您的代码中发现的其他一些应该更正的内容:

  • 命名约定:尝试为您的函数使用好的名称(两个字母变量或荷兰语变量不是一个好主意,我不是英语本地人,我总是避免在代码中使用我自己的基于语言的名称,即使是我不使用的代码' t 分享,你永远不知道谁会在 2 天、2 个月或 2 年内阅读你的代码)。

  • globals:尽量避免使用全局变量。仅在全局范围内声明常量:const int foo = 1;或预处理器定义#define foo 1,这样您就不会在 arduino 上花费太多的小 RAM 空间。该规则的唯一例外是 arduino 开发非常特定的对象(例如Servo在您的代码中),您需要全局声明这些对象,以便您可以在函数中设置它们setup(),并在函数中使用它们loop()

如果我推断您所写的内容,您可能需要添加一个handle_steering()函数,例如:

void handle_steering(int steering) {
    if (steering > NN)
        turn_left(steering);
    elif (steering < NN)
        turn_right(steering);
    else
        keep_straight();
}

并将循环()更改为:

void loop() {
    int throttle = read_throttle();
    int steering = read_steering();
    delay(5);
    handle_throttle(throttle);
    handle_steering(steering);
}

为了使其成为动态灵活地处理许多功能的更一般的情况,您可以保留一些数组:

  • PIN[]:包含引脚,
  • DOM_MIN[]:包含特征域的最小值(对于地图),
  • DOM_MAX[]:包含特征域的最大值(对于地图),
  • BOUND_MIN[]: 包含最小边界(对于 handle_steering 条件),
  • BOUND_MAX[]: 包含最大边界(对于 handle_steering 条件),
  • ACTION[]: 包含指向函数的指针。

然后你的算法看起来像:

int read_input(int i) {
    int value = pulseIn(PIN[i], HIGH, 20000);
    return map(value, 1000, 2000, DOM_MIN[i], DOM_MAX[i]);
}

int handle_action(int i, int value) {
    if (value > BOUND_MIN[i])
        *(ACTION[i])(value);
    elif (value < BOUND_MAX[i])
        *(ACTION[i])(value);
    else
        *(ACTION[i])(-1);
}

void loop() {
    for (int i=0; i<NB_INPUTS; ++i) {
        int value = read_input(i);
        delay(5);
        handle_action(i, value);
    }
}

但是由于您仍然对数组不满意(而且我也假设使用指针),所以我暂时不建议进一步这样做。首先做简单并使其工作,然后您可以尝试按照我在这里公开的想法对其进行分解。但是你正在制作一个嵌入式软件,其中 RAM 很少,处理能力很低,而你的代码空间很便宜。因此,这是为数不多的情况之一,您最好在程序空间中编写更多冗余代码,而您希望在 RAM 中操作尽可能少的符号。这就是为什么我没有向您展示如何声明/定义数组以及如何处理函数指针,因为我认为该解决方案(您的目标)不是做您想做的事情的正确方法。

永远记住,越简单越好!

高温高压

于 2013-06-20T12:55:53.220 回答