1

最近我一直试图让我的机器人在检测到它前面有障碍物时停下来。接下来的情况是:当我检测到一个物体时,汽车会停下来,这没关系,但是当物体消失时,汽车不会继续移动到最终目的地。而不是这样做,汽车应该从 A 点行驶到 B 点 10 秒(10 秒只是一个例子,每次移动的时间不同),当它检测到一个物体时,它应该停下来等待物体消失,之后它必须继续前往最终目的地(B 点)。

我有一个想法,在机器人不移动时计算时间,并添加到达终点所需的时间。但我正在为此苦苦挣扎。

这是代码:

int measureDistance()
{
    if (wiringPiSetup() == -1)
        cout << "Initialization problem - measureDistance() " << endl;

    Sonar sonar;
    sonar.init(trigger, echo);

    int distance = 0;

    distance = sonar.distance(30000);
    sleep_for(nanoseconds(10));

    return distance;
}

bool checkForObstacles() 
{
    wiringPiSetup();

    // Controlling the motors from here
    softPwmCreate(0, 0, 255);
    softPwmCreate(4, 0, 255);

    constexpr int MIN_DISTANCE = 20;

    int distance = measureDistance();
    cout << "Distance: " << distance << endl;

    if (distance >= MIN_DISTANCE)
        return false;

    softPwmWrite(0, LOW);
    softPwmWrite(4, LOW);

    while(distance < MIN_DISTANCE) 
    {
        delay(10); // re-measure after 10ms. Adjust to what you prefer
        distance = measureDistance();
        cout << "Measuring: " << distance << "cm" << endl;
    }


    return false;
}

void move(int t)
{
        // Pins where the motors are connected
    int ena = 0;
    int in1 = 2;
    int in2 = 3;

    int enb = 4;
    int in3 = 5;
    int in4 = 6;
    // Pins setup
    wiringPiSetup ();

    softPwmCreate(ena, 0, 255);
    pinMode(in1, OUTPUT);
    pinMode(in2, OUTPUT);

    softPwmCreate(enb, 0, 255);
    pinMode(in3, OUTPUT);
    pinMode(in4, OUTPUT);

    // Control
    softPwmWrite(ena, 50);
    digitalWrite(in1, 1);
    digitalWrite(in2, 0);

    softPwmWrite(enb, 50);
    digitalWrite(in3, 1);
    digitalWrite(in4, 0);

    cout << "TIME: " << t << endl;
    auto start = chrono::high_resolution_clock::now();
    while(true) 
    {
        auto now = chrono::high_resolution_clock::now();
        auto elapsed = chrono::duration_cast<chrono::milliseconds>(now-start).count();

        cout << "ELAPSED: " << elapsed << endl;

        int remaining = t - (int) elapsed;

        cout << "REMAINING: " << remaining << "ms" << endl;
        if (remaining < 0)
            break;

        if (checkForObstacles())
        {
            continue;
        }   

        delay(min(remaining, 25)); // replace 25 with how often you want to check the distance
    }

    softPwmWrite(ena, LOW);
    softPwmWrite(enb, LOW);
    delay(200);
}

恢复:如果我在它前面放置障碍物,汽车正在移动,但当我移除障碍物时,程序结束 - 之后汽车不再移动。 这不应该发生。

PS:一切都在树莓派上运行。

4

1 回答 1

0

这是一种伪代码,因为我不知道你的机器人和 WiringPi 是如何工作的,但我希望这个代码流可以帮助你走上正确的道路: measureDistance 似乎没什么用,所以我删除了它(它每次都调用初始化一些原因)。在您的主程序中,您应该设置所有针脚和声纳,然后在无限循环中您睡一会儿,检查障碍物,如果没有,您移动,否则跳过移动部分。

int main()
{
    // only initialize everything once, in your main
    if (wiringPiSetup() == -1)
        return -1;

    Sonar sonar;
    sonar.init(trigger, echo); // declare these somewhere first, I don't know the values

    while(1){
        sleep_for(nanoseconds(10)); // or delay(200) or whatever
        // if there are obstacles skip the move() part and go back to sleeping
        // or delay again, and try again
        if (checkForObstacles(sonar))
            continue;
        // No obstacle = move
        move();
    }
}

// this is only responsible for checking obstacles, no pin writing and moving happening
bool checkForObstacles(Sonar sonar) // pass in a reference to sonar
{
    constexpr int MIN_DISTANCE = 20;

    int distance = sonar.distance(30000);
    cout << "Distance: " << distance << endl;

    if (distance >= MIN_DISTANCE)
        return false;

    return true;
}

void move() {
    /*
    *  only move logic goes here
    * like the digitalWrites
    */
}
于 2019-11-02T17:34:15.253 回答