最近,我一直试图让我的机器人在检测到前方障碍物时停止。接下来的情况是:当我检测到物体时,汽车会停下来,这没关系,但是当物体消失时,汽车将不会继续移动到其最终目的地。代替此操作,汽车应从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上运行。
这是一种伪代码,因为我不知道您的机器人和wireingPi是如何正常工作的,但是我希望这段代码流能在正确的轨道上为您提供帮助: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
*/
}