-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvirtualrosagv.cpp
More file actions
469 lines (423 loc) · 15.8 KB
/
virtualrosagv.cpp
File metadata and controls
469 lines (423 loc) · 15.8 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include "virtualrosagv.h"
#include "mapmap/onemap.h"
#include "mapmap/mapmanager.h"
#include "mapmap/blockmanager.h"
#include "mapmap/conflictmanager.h"
#include "agvtask.h"
#include "bezierarc.h"
#include <limits>
#define _USE_MATH_DEFINES
#include "math.h"
#include <float.h>
#define VIRTUAL_AGV_SPEED 30.0
VirtualRosAgv::VirtualRosAgv(int id,std::string name):
VirtualAgv(id,name),
lastStationOdometer(0),
isPaused(false)
{
}
VirtualRosAgv::~VirtualRosAgv()
{
}
void VirtualRosAgv::init()
{
//do nothing
}
bool VirtualRosAgv::pause()
{
isPaused = true;
return true;
}
bool VirtualRosAgv::resume()
{
isPaused = false;
return true;
}
bool VirtualRosAgv::setInitPos(int station)
{
auto mapmanagerptr = MapManager::getInstance();
MapPoint *point = mapmanagerptr->getPointById(station);
if(point == nullptr)return false;
setPosition(0, station, 0);
//占据初始位置
mapmanagerptr->addOccuStation(station, shared_from_this());
//释放其他所有占用
mapmanagerptr->freeAllStationLines(shared_from_this(),station);
}
void VirtualRosAgv::excutePath(std::vector<int> lines)
{
auto conflictmanagerptr = ConflictManager::getInstance();
auto mapmanagerptr = MapManager::getInstance();
isStop = false;
std::vector<int> spirits;
stationMtx.lock();
excutespaths = lines;
excutestations.clear();
for (auto line : lines) {
spirits.push_back(line);
MapPath *path = mapmanagerptr->getPathById(line);
if(path == nullptr)continue;
int endId = path->getEnd();
spirits.push_back(endId);
excutestations.push_back(endId);
}
stationMtx.unlock();
conflictmanagerptr->addAgvExcuteStationPath(spirits,getId());
int next = 0;//下一个要去的位置
MapPoint *currentPoint = mapmanagerptr->getPointById(nowStation);
MapPoint *nextPoint = mapmanagerptr->getPointById(nextStation);
MapPoint *lastPoint = mapmanagerptr->getPointById(lastStation);
if(currentPoint==nullptr && lastPoint!=nullptr){
currentPoint = lastPoint;
}
if(currentPoint == nullptr &&lastPoint==nullptr && nextPoint!=nullptr){
currentPoint = nextPoint;
}
if(currentPoint == nullptr){
combined_logger->debug("agv position lost! can not excute path");
return ;
}
bool in_elevator = currentPoint->getMapChange();
for(unsigned int i=0;i<excutestations.size();++i)
{
if(currentTask!=nullptr && currentTask->getIsCancel())break;//任务取消
if(status == AGV_STATUS_HANDING)break;//手动控制
if(status == AGV_STATUS_ERROR)break;//发生错误
floor = mapmanagerptr->getFloor(nowStation);
int now = excutestations[i];//接下来要去的位置
if(i+1<excutestations.size())
next = excutestations[i+1];
else
next = 0;
if(next == 0){
//没有下一站了
goStation(now,true);
continue;
}
MapPoint *nowPoint = mapmanagerptr->getPointById(now);
MapPoint *nextPoint = mapmanagerptr->getPointById(next);
if(nowPoint == nullptr)continue;
if(nextPoint == nullptr){
goStation(now, true);
continue;
}
//还有下一站。
//分类讨论
if(!nowPoint->getMapChange() && !nextPoint->getMapChange())
{
//两个都不是地图切换点
goStation(now,false);
in_elevator = false;
}
else if(!in_elevator)
{
goStation(now, true);
in_elevator = true;
}
else
{
callMapChange(now);
}
}
//if(currentTask!=nullptr && currentTask->getIsCancel())cancelTask();
if(status == AGV_STATUS_HANDING || status == AGV_STATUS_ERROR)cancelTask();
}
void VirtualRosAgv::cancelTask()
{
if(currentTask!=nullptr)
currentTask->cancel();
//是否保存到数据库呢?
stop();
}
void VirtualRosAgv::goStation(int station, bool stop)
{
auto conflictmanagerptr = ConflictManager::getInstance();
//BlockManagerPtr blockmanagerptr = BlockManager::getInstance();
MapManagerPtr mapmanagerptr = MapManager::getInstance();
//看是否是写特殊点
MapPoint *endPoint = mapmanagerptr->getPointById(station);
if(endPoint == nullptr)return ;
//获取当前线路和当前站点
MapPoint *startPoint = nullptr;
if(nowStation > 0){
startPoint = mapmanagerptr->getPointById(nowStation);
}else{
startPoint = mapmanagerptr->getPointById(lastStation);
}
if(startPoint==nullptr){
//初始位置未知!
return ;
}
//获取当前走的线路
MapPath *path = mapmanagerptr->getMapPathByStartEnd(startPoint->getId(),station);
if(path==nullptr){
//根本不存在该线路
return ;
}
//计算线路长度[以图像上的长度为长度]
PointF a(startPoint->getX(),startPoint->getY());
PointF b(path->getP1x(),path->getP1y());
PointF c(path->getP2x(),path->getP2y());
PointF d(endPoint->getX(),endPoint->getY());
double currentT = 0.;
double path_length = 100;
if(path->getPathType() == MapPath::Map_Path_Type_Line){
path_length = sqrt((endPoint->getY()-startPoint->getY())*(endPoint->getY()-startPoint->getY())+(endPoint->getX()-startPoint->getX())*(endPoint->getX()-startPoint->getX()));
double minDistance = DBL_MAX;
for (double tt = 0.0; tt <= 1.0; tt += 0.01) {
double distance = getDistance(PointF(startPoint->getX()+(endPoint->getX()-startPoint->getX())*tt,startPoint->getY()+(endPoint->getY()-startPoint->getY())*tt), PointF(x, y));
if (distance<minDistance) {
minDistance = distance;
currentT = tt;
}
}
}else if(path->getPathType() == MapPath::Map_Path_Type_Quadratic_Bezier){
path_length = BezierArc::BezierArcLength(a,b,d);
//获取当前位置在曲线上的位置
double minDistance = DBL_MAX;
for(double tt = 0.0;tt<=1.0;tt+=0.01){
BezierArc::POSITION_POSE pp = BezierArc::BezierArcPoint(a,b,d,tt);
double distance = getDistance(pp.pos,PointF(x,y));
if(distance<minDistance){
minDistance = distance;
currentT = tt;
}
}
}else if(path->getPathType() == MapPath::Map_Path_Type_Cubic_Bezier){
path_length = BezierArc::BezierArcLength(a,b,c,d);
//计算当前位置到曲线上最近的点的距离
//获取当前位置在曲线上的位置
double minDistance = DBL_MAX;
for(double tt = 0.0;tt<=1.0;tt+=0.01){
BezierArc::POSITION_POSE pp = BezierArc::BezierArcPoint(a,b,c,d,tt);
double distance = getDistance(pp.pos,PointF(x,y));
if(distance<minDistance){
minDistance = distance;
currentT = tt;
}
}
}
if(fabs(path_length) < 0.1)
{
path_length = 100;
combined_logger->info("path_length=0 error");
}
while (!g_quit && currentTask != nullptr && !currentTask->getIsCancel()) {
sleep_for_us(500000);
bool canGo = true;
if (nowStation > 0) {
if (!conflictmanagerptr->tryAddConflictOccu(nowStation, getId())) {
canGo = false;
}
}
else {
if(lastStation>0){
auto path = mapmanagerptr->getPathByStartEnd(lastStation, nextStation);
if (path != nullptr) {
if (!conflictmanagerptr->tryAddConflictOccu(path->getId(), getId())) {
canGo = false;
}
}else{
if (!conflictmanagerptr->tryAddConflictOccu(lastStation, getId())) {
canGo = false;
}
}
}
}
if (!canGo)continue;
//could occu next station or path block?
int iip;
if (nowStation > 0) {
int pId = -1;
stationMtx.lock();
for (int i = 0; i < excutespaths.size(); ++i) {
auto path = mapmanagerptr->getPathById(excutespaths[i]);
if (path != nullptr && path->getStart() == nowStation) {
pId = path->getId();
break;
}
}
stationMtx.unlock();
iip = pId;
}
else {
iip = nextStation;
}
if (!conflictmanagerptr->conflictPassable(iip, getId())) {
canGo = false;
}
else {
canGo = conflictmanagerptr->tryAddConflictOccu(iip, getId());
}
if (canGo)
break;
}
if(g_quit || currentTask == nullptr || currentTask->getIsCancel())return ;
//进行模拟 移动位置
bool firstMove = true;
while(true){
if(isStop)break;
if(isPaused){
//TODO:check can resume
bool canResume = true;
//could occu current station or path block?
//combined_logger->debug("agv {0} check conflick can resume! nowStation = {1},lastStation = {2},nextStation={3}",getId(),nowStation,lastStation,nextStation);
if (nowStation > 0) {
//auto nowStationPtr = mapmanagerptr->getPointById(nowStation);
if (!conflictmanagerptr->tryAddConflictOccu(nowStation, getId())) {
//combined_logger->debug("agv {0} paused,check current station {1},result={2}!",id,nowStationPtr->getName(),false);
canResume = false;
}else{
//combined_logger->debug("agv {0} paused,check current station {1},result={2}!",id,nowStationPtr->getName(),true);
}
}
else {
if(lastStation>0){
auto path = mapmanagerptr->getPathByStartEnd(lastStation, nextStation);
if (path != nullptr) {
if (!conflictmanagerptr->tryAddConflictOccu(path->getId(), getId())) {
//combined_logger->debug("agv {0} paused,check current line {1},result={2}!",getId(),path->getName(),false);
canResume = false;
}else{
//combined_logger->debug("agv {0} paused,check current line {1},result={2}!",getId(),path->getName(),true);
}
}else{
//auto lastStationPtr = mapmanagerptr->getPointById(lastStation);
if (!conflictmanagerptr->tryAddConflictOccu(lastStation, getId())) {
//combined_logger->debug("agv {0} paused,check last station {1},result={2}!",id,lastStationPtr->getName(),false);
canResume = false;
}else{
//combined_logger->debug("agv {0} paused,check last station {1},result={2}!",id,lastStationPtr->getName(),true);
}
}
}
}
if (!canResume)continue;
//could occu next station or path block?
int iip;
if (nowStation > 0) {
int pId = -1;
stationMtx.lock();
for (int i = 0; i < excutespaths.size(); ++i) {
auto path = mapmanagerptr->getPathById(excutespaths[i]);
if (path != nullptr && path->getStart() == nowStation) {
pId = path->getId();
break;
}
}
stationMtx.unlock();
iip = pId;
}
else {
iip = nextStation;
}
if (!conflictmanagerptr->conflictPassable(iip, getId())) {
//combined_logger->debug("agv {0} paused,check next station {1},result={2}!",id,iip,false);
canResume = false;
}
else {
//combined_logger->debug("agv {0} paused,check next station {1},result={2}!",id,iip,true);
canResume = conflictmanagerptr->tryAddConflictOccu(iip, getId());
}
if (canResume)
resume();
sleep_for_ms(500);
continue;
}
//1.向目标前进100ms的距离 假设每次前进10 //3.重新计算当前位置
if(path->getPathType() == MapPath::Map_Path_Type_Line){
currentT += VIRTUAL_AGV_SPEED/ path_length;
//前移10
x = startPoint->getX()+(endPoint->getX()-startPoint->getX()) * currentT;
y = startPoint->getY() + (endPoint->getY() - startPoint->getY()) * currentT;
if(path->getSpeed()<0){
int angle = 180 +atan2(endPoint->getY()-y,endPoint->getX()-x)*180/M_PI;
theta = std::min(angle, abs(360-angle));
}else{
theta = atan2(endPoint->getY()-y,endPoint->getX()-x)*180/M_PI;
}
}else if(path->getPathType() == MapPath::Map_Path_Type_Quadratic_Bezier){
//前移10
currentT += VIRTUAL_AGV_SPEED/path_length;
if(currentT<0)currentT = 0.;
if(currentT>1)currentT = 1.;
BezierArc::POSITION_POSE pp = BezierArc::BezierArcPoint(a,b,d,currentT);
x = pp.pos.x();
y = pp.pos.y();
if(path->getSpeed()<0){
int angle = 180 + pp.angle;
theta = std::min(angle, abs(360-angle));
}else{
theta = pp.angle;
}
}else if(path->getPathType() == MapPath::Map_Path_Type_Cubic_Bezier){
//前移10
currentT += VIRTUAL_AGV_SPEED/path_length;
if(currentT<0)currentT = 0.;
if(currentT>1)currentT = 1.;
BezierArc::POSITION_POSE pp = BezierArc::BezierArcPoint(a,b,c,d,currentT);
x = pp.pos.x();
y = pp.pos.y();
if(path->getSpeed()<0){
int angle = 180 + pp.angle;
theta = std::min(angle, abs(360-angle));
}else{
theta = pp.angle;
}
}
//2.初次移动,调用离开上一站
if(firstMove){
if(nowStation>0){
onLeaveStation(nowStation);
}
firstMove = false;
}
//4.判断是否到达下一站 如果到达,break
if(currentT>=1.0){
onArriveStation(station);
lastStation = (int)nowStation;
nowStation = station;
floor = mapmanagerptr->getFloor(station);
break;
}
sleep_for_ms(500);
}
}
void VirtualRosAgv::stop()
{
isStop = true;
}
void VirtualRosAgv::callMapChange(int station)
{
//模拟电梯运行,这里只做等待即可
sleep_for_ms(5000);
lastStation = (int)nowStation;
nowStation = station;
floor = MapManager::getInstance()->getFloor(station);
combined_logger->debug("go elvator:current floor={0}", floor);
}
void VirtualRosAgv::onTaskStart(AgvTaskPtr _task)
{
if(_task != nullptr)
{
status = Agv::AGV_STATUS_TASKING;
}
}
void VirtualRosAgv::onTaskCanceled(AgvTaskPtr _task){
auto mapmanagerptr = MapManager::getInstance();
if(currentTask!=nullptr){
auto nodes = currentTask->getTaskNodes();
auto index = currentTask->getDoingIndex();
if(index<nodes.size())
{
auto node = nodes[index];
mapmanagerptr->freeStation(node->getStation(),shared_from_this());
auto paths = currentTask->getPath();
for(auto p:paths){
mapmanagerptr->freeLine(p,shared_from_this());
}
}
}
auto conflictmanagerptr = ConflictManager::getInstance();
conflictmanagerptr->freeAgvOccu(getId(),lastStation,nowStation,nextStation);
}