-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoboVision.cpp
144 lines (113 loc) · 3.37 KB
/
RoboVision.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
File: RoboVision.cpp
Date: 9/10/19
Description: Program will allow an e-puck robot to "see" its environment to guide itself through it (eg navigating a cave or tunnel).
At the moment the goal is to "see" colors.
Author: Josh Chica
*/
#include <stdio.h>
#include <ctime>
#include <webots/Robot.hpp>
#include <webots/DistanceSensor.hpp>
#include <webots/Camera.hpp>
#include <webots/Motor.hpp>
#include <opencv2/opencv.hpp>
// Macros
#define TIME_STEP 64
#define MAX_SPEED 6.28
// namespaces
using namespace webots;
using namespace cv;
// forward declare OpenCV functions
int ColorDetect();
// main
int main(int argc, char **argv)
{
/* necessary to initialize webots stuff */
Robot *robot = new Robot();
/*
* Declaring Distance sensor
* Declaring Camera
* Declaring Left+Right Motors
* Declaring clock_t
*/
DistanceSensor *ps[8];
char psNames[8][4] = { "ps0", "ps1", "ps2", "ps3", "ps4", "ps5", "ps6", "ps7" };
for (int i = 0; i < 8; i++) {
ps[i] = robot->getDistanceSensor(psNames[i]);
ps[i]->enable(TIME_STEP);
}
Camera *camera;
camera = robot->getCamera("camera");
camera->enable(TIME_STEP);
Motor *leftMotor = robot->getMotor("left wheel motor");
Motor *rightMotor = robot->getMotor("right wheel motor");
leftMotor->setPosition(INFINITY);
rightMotor->setPosition(INFINITY);
leftMotor->setVelocity(0.0);
rightMotor->setVelocity(0.0);
clock_t pastTime = clock();
double secondsPassed;
/* main loop
* Perform simulation steps of TIME_STEP milliseconds
* and leave the loop when the simulation is over
*/
printf("Everything's loaded, about to move\n");
while (robot->step(TIME_STEP) != -1) {
/* Read the sensors */
// read in distance sensors
double psValues[8];
for (int i = 0; i < 8; i++)
psValues[i] = ps[i]->getValue();
// taking a picture with camera
const unsigned char *image = camera->getImage();
//unsigned char greyImage = camera->imageGetGray(image, camera->getWidth(), 0, 0);
/* Process sensor data here */
/*
* Determines if the robot should turn
* checks if the distance sensors detect anything close by
*/
bool right_obstacle =
psValues[0] > 70.0 ||
psValues[1] > 70.0 ||
psValues[2] > 70.0;
bool left_obstacle =
psValues[5] > 70.0 ||
psValues[6] > 70.0 ||
psValues[7] > 70.0;
/* Actuator commands here */
// setting the speed that will be assigned to the motors
double leftSpeed = .5 * MAX_SPEED;
double rightSpeed = .5 * MAX_SPEED;
// determines if the robot should turn because something's too close
if (left_obstacle) {
// turn right
leftSpeed += 0.5 * MAX_SPEED;
rightSpeed -= 0.5 * MAX_SPEED;
}
else if (right_obstacle) {
// turn left
leftSpeed -= 0.5 * MAX_SPEED;
rightSpeed += 0.5 * MAX_SPEED;
}
// actually setting the motors' velocities based on the above data
// set the motor speeds to leftSpeed & rightSpeed respectively when done testing
leftMotor->setVelocity(0);
rightMotor->setVelocity(0);
// determing how much time has passed & if it should process the image
secondsPassed = (clock() - pastTime) / CLOCKS_PER_SEC;
if (secondsPassed >= 3) {
if (camera->saveImage("image.png", 100) == 0);
else {
printf("couldn't save image\n");
continue;
}
ColorDetect();
pastTime = clock();
}
};
/* Enter your cleanup code here */
/* This is necessary to cleanup webots resources */
delete robot;
return 0;
}