-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathagv.cpp
More file actions
355 lines (301 loc) · 10.3 KB
/
agv.cpp
File metadata and controls
355 lines (301 loc) · 10.3 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
#include <boost/thread.hpp>
#include "agv.h"
#include "mapmap/mapmanager.h"
#include "agvtask.h"
#include "network/tcpclient.h"
#include "userlogmanager.h"
#include "msgprocess.h"
#include "mapmap/mapmanager.h"
#include "mapmap/blockmanager.h"
#include "mapmap/conflictmanager.h"
Agv::Agv(int _id, std::string _name, std::string _ip, int _port) :
currentTask(nullptr),
id(_id),
name(_name),
ip(_ip),
port(_port),
lastStation(0),
nowStation(0),
nextStation(0),
x(0),
y(0),
theta(0),
floor(-1),
agvType(-1),
isPowerLow(false),
isChanging(false),
isManualControl(false),
isEmergencyStop(false)
{
}
Agv::~Agv()
{
}
void Agv::init()
{
}
void Agv::setPosition(int _lastStation, int _nowStation, int _nextStation) {
auto mapmanagerptr = MapManager::getInstance();
if (_nowStation > 0) {
MapPoint *point = mapmanagerptr->getPointById(_nowStation);
if(point == nullptr)return ;
x = point->getRealX();
y = point->getRealY();
theta = -0.1 * point->getRealA();
onArriveStation(_nowStation);
mapmanagerptr->addOccuStation(_nowStation,shared_from_this());
mapmanagerptr->freeAllStationLines(shared_from_this(),_nowStation);
}else if(_lastStation>0 && _nextStation>0){
auto line = mapmanagerptr->getPathByStartEnd(_lastStation,_nextStation);
if(line!=nullptr){
mapmanagerptr->addOccuLine(line->getId(),shared_from_this());
}
}
lastStation = _lastStation;
nowStation = _nowStation;
nextStation = _nextStation;
mapmanagerptr->printGroup();
}
//到达后是否停下,如果不停下,就是不减速。
//是一个阻塞的函数
void Agv::goStation(int station, bool stop)
{
}
void Agv::stop()
{
}
void Agv::onArriveStation(int station)
{
auto mapmanagerptr = MapManager::getInstance();
auto conflictmanagerptr = ConflictManager::getInstance();
//set floor
auto floorid = mapmanagerptr->getFloor(station);
setFloor(floorid);
//add block occu station
MapPoint *point = mapmanagerptr->getPointById(station);
if(point == nullptr)return ;
combined_logger->info("agv id:{0} arrive station:{1}",getId(),point->getName());
bool addOccuResult = conflictmanagerptr->tryAddConflictOccu(station,getId());
if(station>0){
if(nowStation!=nowStation){
lastStation = (int)nowStation;
}
//free last station occu
mapmanagerptr->freeStation(lastStation,shared_from_this());
conflictmanagerptr->freeConflictOccu(lastStation,getId());
nowStation = station;
stationMtx.lock();
for (int i = 0; i < excutestations.size(); ++i) {
if (excutestations[i] == station) {
if (i + 1 < excutestations.size()) {
nextStation = excutestations[i + 1];
}else{
nextStation = 0;
}
break;
}
}
stationMtx.unlock();
}
//TODO:释放之前的线路和站点
std::vector<MapPath *> paths;
stationMtx.lock();
for(auto line:excutespaths){
MapPath *path = mapmanagerptr->getPathById(line);
if(path == nullptr)continue;
paths.push_back(path);
}
stationMtx.unlock();
int findIndex = -1;
for(int i=0;i<paths.size();++i){
auto line = paths[i];
if(line->getEnd() == station){
findIndex = i;
}
}
if(findIndex != -1){
//之前的道路占用全部释放
//之前的站点占用全部释放
for(int i=0;i<=findIndex;++i){
auto line = paths[i];
int start = line->getStart();
int end = line->getEnd();
int lineId = line->getId();
//free start station
mapmanagerptr->freeStation(start,shared_from_this());
conflictmanagerptr->freeConflictOccu(start,getId());
//free last line
mapmanagerptr->freeLine(lineId,shared_from_this());
conflictmanagerptr->freeConflictOccu(lineId,getId());
//free end station
if(i!=findIndex){
mapmanagerptr->freeStation(end,shared_from_this());
conflictmanagerptr->freeConflictOccu(end,getId());
}
}
}
char buf[SQL_MAX_LENGTH];
snprintf(buf, SQL_MAX_LENGTH, "update agv_agv set lastStation=%d,nowStation=%d,nextStation=%d where id = %d;", (int)lastStation, (int)nowStation, (int)nextStation, id);
try {
g_db.execDML(buf);
}
catch (CppSQLite3Exception e) {
combined_logger->error("sqlerr code:{0} msg:{1}", e.errorCode(), e.errorMessage());
}
catch (std::exception e) {
combined_logger->error("sqlerr code:{0} ", e.what());
}
if(addOccuResult){
//判断下一段线路的可行性 [block]
//判断下一个线路 是否在block内,block内是否已经有其他车辆。如果有的话,就暂停 一下当前动作
stationMtx.lock();
auto itr = std::find(excutestations.begin(),excutestations.end(),nowStation);
if(itr!=excutestations.end()){
++itr;
if(itr!=excutestations.end()){
nextStation = *itr;
stationMtx.unlock();
//next path is block free
auto p = mapmanagerptr->getPathByStartEnd(nowStation,nextStation);
if(p!=nullptr){
if(!conflictmanagerptr->conflictPassable(p->getId(),getId())){
pause();
}
}
}else{
stationMtx.unlock();
}
}else{
stationMtx.unlock();
}
}else{
pause();
}
conflictmanagerptr->printConflict();
mapmanagerptr->printGroup();
}
void Agv::onLeaveStation(int stationid)
{
auto mapmanagerptr = MapManager::getInstance();
auto conflictmanagerptr = ConflictManager::getInstance();
conflictmanagerptr->freeConflictOccu(stationid,getId());
auto nextPath = mapmanagerptr->getPathByStartEnd(stationid,nextStation);
bool addOccuResult = true;
if(nextPath!=nullptr){
combined_logger->info("agv id:{0} try add conflict occu path id:{1} name:{2}",nextPath->getId(),nextPath->getName());
addOccuResult = conflictmanagerptr->tryAddConflictOccu(nextPath->getId(),getId());
}
bool b_getNextStation = false;
stationMtx.lock();
for(auto line:excutespaths){
MapPath *path = mapmanagerptr->getPathById(line);
if(path == nullptr)continue;
if(path->getStart() == nowStation){
nextStation = path->getEnd();
b_getNextStation = true;
break;
}
}
stationMtx.unlock();
if(!b_getNextStation){
nextStation = 0;
}
lastStation = stationid;
nowStation = 0;
auto point = mapmanagerptr->getPointById(stationid);
if(point!=nullptr){
combined_logger->info("agv id:{0} leave station:{1}",getId(),point->getName());
}
//释放这个站点的占用
MapManager::getInstance()->freeStation(stationid,shared_from_this());
if(nextStation == 0){
setFloor(mapmanagerptr->getFloor(lastStation));
}else{
auto pp = mapmanagerptr->getPathByStartEnd(lastStation,nextStation);
if(pp!=nullptr){
setFloor(mapmanagerptr->getFloor(pp->getId()));
}else{
setFloor(-1);
}
}
char buf[SQL_MAX_LENGTH];
snprintf(buf, SQL_MAX_LENGTH, "update agv_agv set lastStation=%d,nowStation=%d,nextStation=%d where id = %d;", (int)lastStation, (int)nowStation, (int)nextStation,id);
try {
g_db.execDML(buf);
}
catch (CppSQLite3Exception e) {
combined_logger->error("sqlerr code:{0} msg:{1}", e.errorCode(), e.errorMessage());
}
catch (std::exception e) {
combined_logger->error("sqlerr code:{0} ", e.what());
}
//判断下一个站点的可行性 [block]
//判断下一个站点 是否在block内,block内是否已经有其他车辆。如果有的话,就暂停 一下当前动作
if(addOccuResult){
auto p = mapmanagerptr->getPointById(nextStation);
if(p!=nullptr){
if(!conflictmanagerptr->conflictPassable(p->getId(),getId())){
pause();
}
}
}else{
pause();
}
conflictmanagerptr->printConflict();
mapmanagerptr->printGroup();
}
void Agv::onError(int code, std::string msg)
{
status = AGV_STATUS_ERROR;
char sss[1024];
snprintf(sss, 1024, "Agv id:%d occur error code:%d msg:%s", id, code, msg.c_str());
std::string ss(sss);
//combined_logger->error(ss.c_str());
UserLogManager::getInstance()->push(ss);
MsgProcess::getInstance()->errorOccur(code, msg, true);
}
void Agv::onWarning(int code, std::string msg)
{
char sss[1024];
snprintf(sss, 1024, "Agv id:%d occur warning code:%d msg:%s", id, code, msg.c_str());
std::string ss(sss);
//combined_logger->warn(ss.c_str());
UserLogManager::getInstance()->push(ss);
MsgProcess::getInstance()->errorOccur(code, msg, false);
}
//请求切换地图(呼叫电梯)
void Agv::callMapChange(int station)
{
}
void Agv::cancelTask()
{
auto mapmanagerptr = MapManager::getInstance();
int nowOccu = 0;
//释放其他所有占用
if(nowStation>0){
nowOccu = nowStation;
//占据位置
MapManager::getInstance()->addOccuStation(nowOccu, shared_from_this());
//释放其他所有占用
MapManager::getInstance()->freeAllStationLines(shared_from_this(),nowOccu);
}else if(lastStation>0 && nextStation>0){
auto path = mapmanagerptr->getPathByStartEnd(lastStation,nextStation);
if(path!=nullptr)nowOccu = path->getId();
//占据位置
MapManager::getInstance()->addOccuLine(nowOccu, shared_from_this());
//释放其他所有占用
MapManager::getInstance()->freeAllStationLines(shared_from_this(),nowOccu);
}else if(lastStation>0){
nowOccu = lastStation;
//占据位置
MapManager::getInstance()->addOccuStation(nowOccu, shared_from_this());
//释放其他所有占用
MapManager::getInstance()->freeAllStationLines(shared_from_this(),nowOccu);
}
mapmanagerptr->printGroup();
onTaskCanceled(currentTask);
}
void Agv::reconnect()
{
}