forked from rishabh1b/RealTimePathPlanning
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRobot.cpp
199 lines (185 loc) · 4.55 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "Robot.h"
void Robot::setup()
{
alive = true; mass = 5.0; scanRadius = sensorRadius; accuracy = accur;
//battery = 100;
//float x = ofRandom(0, ofGetWindowWidth()); float y = ofRandom(0, ofGetWindowHeight());`
location.set(0.0,0.0); HOME = location;
velocity.set(0.0, 0.0);
accelaration.set(0.0, 0.0);
maxVelocity.set(mVal, mVal);
maxForce.set(mForce, mForce);
//color = { ofRandom(0,255),ofRandom(0,255) ,ofRandom(0,255) };
color = {50,145,80};
}
void Robot::setup(ofVec2f loc)
{
alive = true; mass = 5.0; scanRadius = sensorRadius; accuracy = accur;
location = loc;
HOME = location;
velocity.set(0.0, 0.0);
accelaration.set(0.0, 0.0);
maxVelocity.set(mVal, mVal);
maxForce.set(mForce, mForce);
color = { 50,145,80 };
}
void Robot::update()
{
velocity += accelaration;
velocity = (velocity.length() <= maxVelocity.length()) ? velocity : (velocity.normalized() *mVal);
location += velocity;
accelaration *= 0.0;
pt.set(location.x, location.y);
line.addVertex(pt);
}
void Robot::render()
{
int r = 6;
ofEnableAlphaBlending();
ofFill();
ofSetColor(color);
ofSetLineWidth(3);
this->line.draw();
ofSetLineWidth(1);
ofNoFill();
ofSetColor(color);
ofDrawCircle(location.x,location.y,scanRadius);
ofPushMatrix();
ofTranslate(location.x,location.y);
ofRotate(ofRadToDeg(atan2(velocity.y, velocity.x) + PI / 2));
ofFill();
ofBeginShape();
ofVertex(0, -r * 2);
ofVertex(-r, r * 2);
ofVertex(r, r * 2);
ofEndShape(true);
ofPopMatrix();
ofSetColor(color, 80);
ofDrawCircle(location.x, location.y, ofGetFrameNum() % int(scanRadius));
ofNoFill();
ofDisableAlphaBlending();
}
void Robot::addForce(ofVec2f force)
{
accelaration += (force / mass);
}
void Robot::controller(ofVec2f target)
{
ofVec2f error = (target - location);
//error.normalize();
//error *= 1.5;
//accelaration = error;
float m;
if (error.length() < converge) {
m = ofMap(error.length(), 0, converge, 0, mVal);
}
else {
m = mVal;
}
ofVec2f temp = error.normalized()*m;
ofVec2f steer = (temp - velocity);
steer = (steer.length() <= maxForce.length()) ? steer : (steer.normalized() *mForce);
addForce(steer);
}
void Robot::fillEnviroment(const list<obstacles*> obst, list<Nodes>& node)
{
//check for enviroment
for (auto index : obst) {
float dist = this->location.distance(index->loc());
if (dist <= this->scanRadius + index->rad()) {
updateEnviroment(node, index);
}
}
}
void Robot::updateEnviroment(list<Nodes>& node,obstacles *obst)
{
std::list<Nodes>::iterator it = node.begin();
while (it != node.end())
{
float dist = it->location.distance(obst->loc());
if (dist <= obst->rad()) {
it->costToStart = inf;
it->alive = false;
}
it++;
}
}
//
//inline void quadCopter::fly(Nodes *& nodes)
//{
// if (!wTraj.empty())
// {
// int index = 0;
// bool prio = false;
// Vector2f temp('inf', 'inf');
// for (auto i : wTraj)
// {
// if (nodes->waypoint[i.index].alive)
// {
// if (!nodes->waypoint[i.index].stress) nodes->waypoint[i.index].color = color;
// else nodes->waypoint[i.index].color = { 255,0,0 };
// Vector2f min = this->location - nodes->waypoint[i.index].location;
// if (min.norm()<temp.norm() && nodes->waypoint[i.index].priority)
// {
// temp = min;
// index = i.index;
// prio = true;
// }
// }
// }
// if (prio) {
// controller(nodes->waypoint[index].location);
// }
// //else if (!prio) {
// // int index = 0;
// // Vector2f temp('inf', 'inf');
// // for (auto i : wTraj)
// // {
// // if (nodes->waypoint[i.index].alive)
// // {
// // if (!nodes->waypoint[i.index].stress) nodes->waypoint[i.index].color = color;
// // else nodes->waypoint[i.index].color = { 255,0,0 };
// // Vector2f min = this->location - nodes->waypoint[i.index].location;
// // if (min.norm()<temp.norm() && nodes->waypoint[i.index].priority)
// // {
// // temp = min;
// // index = i.index;
// // prio = true;
// // }
// // }
// // }
// // controller(nodes->waypoint[index].location);
// //}
//
// //list<waypoints>::iterator it;
// //waypoints target = findAlive(wTraj,nodes);
// //controller(target.location);
//
// }
// else
// {
// //Vector2f mouse(ofGetMouseX(), ofGetMouseY());
// //controller(mouse);
// Vector2f HOME(0, 0);
// controller(HOME);
// }
//}
//
//inline waypoints quadCopter::findAlive(list<waypoints> &TRAJ, node *nodes)
//{
// list<waypoints>::iterator it;
// it = TRAJ.begin();
// if (TRAJ.size() <= 1)
// {
// return *it;
// }
// if (nodes->waypoint[it->index].alive)
// {
// return *it;
// }
// else
// {
// TRAJ.pop_front();
// return findAlive(TRAJ, nodes);
// }
//}