forked from mrl-hsl/Soccer-Robot-Playground
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRobot.cpp
175 lines (156 loc) · 4.21 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "Robot.hpp"
#include "Util.hpp"
//-------------------------
//--| Movements Configs |--
//-------------------------
//-- Robot Move (in Pixels) -- Should be 1, Change it for Making Time Faster
double robotMoveValue = 1;
//-- Robot Rotation (in Degree) -- Should be 1, Change it for Making Time Faster
double robotRotationValue = 1;
//-- Max Movement Speed (in m/s)
double maxMovementSpeed = 0.3;
//-- Max Rotation Speed (in Degree/s)
double maxRotationSpeed = 0.5;
//-- Time Speed, Normal is Equal to 1
double timeSpeed = 1;
//-- Set Function
//-- Spawn X, Spawn Y, Spawn Rotation, Model Scale, Model Length, Model Width
void Robot::setPosition(double inputX, double inputY, double inputTheta) {
x = inputX;
y = inputY;
theta = inputTheta;
}
void Robot::storePosition(double inputX, double inputY, double inputTheta, double inputVX, double inputVY, double inputVTheta) {
tempX = inputX;
tempY = inputY;
tempTheta = inputTheta;
tempVX = inputVX;
tempVY = inputVY;
tempVTheta = inputVTheta;
}
//-- Access to X Cordinate of Robot
double Robot::accessX() {
return x;
}
//-- Access to Y Cordinate of Robot
double Robot::accessY() {
return y;
}
//-- Access to Rotation of Robot
double Robot::accessTheta() {
return theta;
}
//-- Access to Movement Speed (X Vector)
double Robot::accessVX() {
return vX;
}
//-- Access to Movement Speed (Y Vector)
double Robot::accessVY() {
return vY;
}
//-- Access to Rotation Speed
double Robot::accessVTheta() {
return vTheta;
}
//-- Check Impact with Border
int Robot::borderCheck() {
//-- if Impaction then Move to Last Position
if (x > fieldLength / 2){
x = tempX - 0.01;
checkValue = -1;
} else if (x < -(fieldLength / 2)){
x = tempX + 0.01;
checkValue = 1;
} else if (y < -(fieldWidth / 2)){
y = tempY + 0.01;
checkValue = 2;
} else if (y > fieldWidth / 2){
y = tempY - 0.01;
checkValue = 2;
} else {
checkValue = 0;
// cout << " tx : " << tempX << " ty : " << tempY << " tx : " << tempTheta << endl;
}
switch(checkValue){
case -1:
errorInfo = "Can't Go Left More !";
return 1;
break;
case 1:
errorInfo = "Can't Go Right More !";
return 1;
break;
case -2:
errorInfo = "Can't Go Down More !";
return 1;
break;
case 2:
errorInfo = "Can't Go Up More !";
return 1;
break;
}
return 0;
}
//-- Reset Check Value
void Robot::resetCheck() {
checkValue = 0;
}
//-- Output Error
string Robot::error() {
return errorInfo;
}
//-- Seek Movement and Rotation Changes
int Robot::state() {
int output;
if (tempVX != vX) {
output = 1;
} else if (tempVY != vY) {
output = 2;
}
if (tempVTheta != vTheta){
output = 3;
}
if (tempVTheta == vTheta && tempVX == vX && tempVY == vY){
output = 0;
}
return output;
}
//-- Set Robot's Velocity (Movement Velocity, Rotation Velocity) //set
void Robot::setVelocity(double inputX, double inputY, double inputTheta) {
bool inLimit = true;
if (inputX > maxMovementSpeed || inputY > maxMovementSpeed || inputX < -maxMovementSpeed || inputY < -maxMovementSpeed || inputTheta > maxRotationSpeed || inputTheta < -maxRotationSpeed) {
inLimit = false;
}
if (inLimit == true) {
vX = inputX;
vY = inputY;
vTheta = inputTheta;
} else {
cout << "Maximum Speed Reached !" << endl;
errorInfo = "Maximum Speed Reached !";
}
}
//-- Updates Robot's Position
void Robot::update() {
//-- Movement Part
double globalVX = vX * cos(theta) + vY * sin(-theta);
double globalVY = vY * cos(-theta) + vX * sin(theta);
x = x + globalVX * refreshRate;
y = y + globalVY * refreshRate;
// -- Rotation Part
// cout << theta * 180 / M_PI << endl;
theta += vTheta * refreshRate;
theta = modAngle(theta);
}
//-- Robot X Value Implementor
void Robot::setX(double input){
x = input;
}
//-- Robot Y Value Implementor
void Robot::setY(double input){
y = input;
}
//-- Robot Theta Value Implementor
void Robot::setTheta(double input){
theta = input;
}