forked from Schniz/robotikka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.cpp
154 lines (121 loc) · 3.15 KB
/
Robot.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
145
146
147
148
149
150
151
152
153
154
#include "Robot.h"
#include <stdexcept>
Robot::Robot(char* ip, int port) {
_pc = new PlayerClient(ip, port);
_pp = new Position2dProxy(_pc);
_lp = new LaserProxy(_pc);
_pp->SetMotorEnable(true);
robot_XPos = 0;
robot_YPos = 0;
robot_Yaw = 0;
//For fixing Player's reading BUG
for (int i = 0; i < 15; i++)
Read();
}
double Robot::getXPosition() {
return _pp->GetXPos();
}
double Robot::getYPosition() {
return _pp->GetYPos();
}
double Robot::getYawPosition() {
return _pp->GetYaw();
}
void Robot::calcLocationDeltas(double &DelX,double &DelY,double &DelYaw)
{
// Getting the new position of the robot
double currRobotX = _pp->GetXPos();
double currRobotY = _pp->GetYPos();
// Getting the robot yaw and casting it from radians to degrees
double x = _pp->GetYaw();
double currRobotYaw = x * 180 / M_PI;
// Calculating the deltas
DelX = currRobotX - robot_XPos;
DelY = currRobotY - robot_YPos;
DelYaw = fmod(currRobotYaw - robot_Yaw ,360);
// Updating the robot location
robot_XPos = currRobotX;
robot_YPos = currRobotY;
robot_Yaw = currRobotYaw;
}
bool Robot::canTurnRight()
{
return canTurnInDirection(START_RIGHT_RAYS_RANGE_ANGLE, END_RIGHT_RAYS_RANGE_ANGLE);
}
bool Robot::canTurnLeft()
{
return canTurnInDirection(START_LEFT_RAYS_RANGE_ANGLE, END_LEFT_RAYS_RANGE_ANGLE);
}
bool Robot::canTurnInDirection(double startRangeAngle, double endRangeAngle)
{
int rangeStartIndex = angleToIndex(startRangeAngle);
int rangeEndIndex = angleToIndex(endRangeAngle);
for (int i = rangeStartIndex; i < rangeEndIndex; i++)
{
if (getDistanceFromObstacle(i) <= 0.5)
{
return false;
}
}
return true;
}
bool Robot::canMoveForward()
{
double dis;
int rightCorner = angleToIndex(-30);
int leftCorner = angleToIndex(30);
for (int i = rightCorner; i <= leftCorner; i++)
{
dis = getDistanceFromObstacle(i);
if (dis <= 0.8)
{
return false;
}
}
return true;
}
// This function summarize the "rays range" distances from obstacles
double Robot::getRaysRangeSum(double rangeStartAngle,double rangeEndAngle)
{
double sum = 0;
int rangeStartIndex = angleToIndex(rangeStartAngle);
int rangeEndIndex = angleToIndex(rangeEndAngle);
for (int i = rangeStartIndex; i <= rangeEndIndex; i++)
{
sum += getDistanceFromObstacle(i);
}
return sum;
}
double Robot::getRangeLaser(unsigned index) {
if (index < 0 || index > LASER_SLEASER_ARRAY_SIZE) {
return -1;
}
return _lp->GetRange(index);;
}
double Robot::getRangeLaser(double angle) {
return getRangeLaser(angleToIndex(angle));
}
double Robot::indexToAngle(int index) {
double angular_resolution = LASER_FOV_DEGREE
/ (double) LASER_SLEASER_ARRAY_SIZE;
double min_angle = LASER_FOV_DEGREE / 2;
double angle = index * angular_resolution - min_angle;
return angle;
}
unsigned Robot::angleToIndex(double angle) {
double min_angle = LASER_FOV_DEGREE / 2;
int index = ((double) LASER_SLEASER_ARRAY_SIZE / LASER_FOV_DEGREE)
* (angle + min_angle);
if (index >= LASER_SLEASER_ARRAY_SIZE) {
throw new out_of_range("index larger than possible.");
}
return index;
}
double Robot::getDistanceFromObstacle(int index) {
return (*_lp)[index];
}
Robot::~Robot() {
delete _pc;
delete _pp;
delete _lp;
}