forked from JoshuaKeegan3/AVC-Team-36
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cpp
More file actions
56 lines (51 loc) · 1.57 KB
/
Copy pathcore.cpp
File metadata and controls
56 lines (51 loc) · 1.57 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
55
#include "robot.hpp"
double calculateOffset(){
/// Returns a double between -1 and 1
/// Representing the distance the centre
/// Of the white line is away from the robot
/// If the number is positive the line is to the left
/// If the number is negitive the line is to the right
int offset = 0;//distances away
int amount = 0;//for finding average
int mid_col = cameraView.width/2;//center of fov
int mid_row = cameraView.height/2;
int total_cols = cameraView.width;
for(int col=0; col<cameraView.height;col++){
int whiteness = get_pixel(cameraView,mid_row,col,3);
if(whiteness>250){//if it isline
amount++;
offset += mid_col-col;
}
}
std::cout<<(double)offset/amount/total_cols*2<<std::endl;
return (double)offset/amount/total_cols*2;// divide by half the camera view to normalise.
}
double * calculateWheelSpeeds(double offset,double speed){
double lv=0;
double rv=0;
lv+=-offset*speed+speed;
rv+=offset*speed+speed;
double speeds[2] = { lv,rv };
return speeds;
}
int main(){
if (initClientRobot() !=0){
std::cout<<" Error initializing robot"<<std::endl;
}
double vLeft = 5.0;
double vRight = 5.0;
double speed = 50.0;
takePicture();
SavePPMFile("i0.ppm",cameraView);
while(1){
takePicture();
double offset = calculateOffset();
double * speeds = calculateWheelSpeeds(offset, speed);
vLeft = speeds[0];
vRight = speeds[1];
setMotors(vLeft,vRight);
std::cout<<" Offset="<<offset<<std::endl;
std::cout<<" vLeft="<<vLeft<<" vRight="<<vRight<<std::endl;
usleep(1000);
} //while
} // main