gpt4 book ai didi

c++ - 当它用超声波传感器(C++)检测到物体时停止机器人车

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:55 28 4
gpt4 key购买 nike

最近我一直在尝试让我的机器人在检测到前方有障碍物时停下来。接下来的情况是:当我检测到一个物体时,汽车停下来没关系,但是当物体消失时,汽车不会继续移动到最终目的地。而不是这样做,汽车应该从 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);
}

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

P.S:一切都在 Raspberry Pi 上运行。

最佳答案

这是一种伪代码,因为我不知道你的机器人和 wiringPi 是如何工作的,但我希望这段代码流能帮助你走上正轨:measureDistance 似乎没什么用,所以我删除了它(并且由于某种原因它每次都调用初始化)。在你的主体中,你应该设置所有的 pin 东西和声纳,然后在无限循环中你睡一会儿,检查障碍物,如果没有,你就移动,否则跳过移动部分。

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
*/
}

关于c++ - 当它用超声波传感器(C++)检测到物体时停止机器人车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58673053/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com