-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_detection.cpp
More file actions
54 lines (47 loc) · 1.37 KB
/
object_detection.cpp
File metadata and controls
54 lines (47 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <webots/DistanceSensor.hpp>
#include <webots/Motor.hpp>
#include <webots/Robot.hpp>
#include <webots/Camera.hpp>
#define TIME_STEP 64
using namespace webots;
int main(int argc, char **argv) {
Robot *robot = new Robot();
DistanceSensor *ds[2];
char dsNames[2][10] = {"DS_right", "DS_left"};
for (int i = 0; i < 2; i++) {
ds[i] = robot->getDistanceSensor(dsNames[i]);
ds[i]->enable(TIME_STEP);
}
Camera *cm;
cm= robot -> getCamera("camera");
cm -> enable (TIME_STEP);
cm ->recognitionEnable(TIME_STEP);
Motor *wheels[4];
char wheels_names[4][8] = {"wheel1", "wheel2", "wheel3", "wheel4"};
for (int i = 0; i < 4; i++) {
wheels[i] = robot->getMotor(wheels_names[i]);
wheels[i]->setPosition(INFINITY);
wheels[i]->setVelocity(0.0);
}
int avoidObstacleCounter = 0;
while (robot->step(TIME_STEP) != -1) {
double leftSpeed = 1.0;
double rightSpeed = 1.0;
if (avoidObstacleCounter > 0) {
avoidObstacleCounter--;
leftSpeed = 1.0;
rightSpeed = -1.0;
} else { // read sensors
for (int i = 0; i < 2; i++) {
if (ds[i]->getValue() < 950.0)
avoidObstacleCounter = 100;
}
}
wheels[0]->setVelocity(leftSpeed);
wheels[1]->setVelocity(rightSpeed);
wheels[2]->setVelocity(leftSpeed);
wheels[3]->setVelocity(rightSpeed);
}
delete robot;
return 0; // EXIT_SUCCESS
}