-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
114 lines (105 loc) · 4.28 KB
/
main.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
#include <QCoreApplication>
#include "communication/udpreceiver.h"
#include "communication/udpsender.h"
#include "communication/serialsender.h"
#include "utils/datamanager.h"
#include "utils/mymath.h"
#include "algorithm/pathplanner.h"
#include "algorithm/rrt.h"
#include "algorithm/rrtstar.h"
#include "algorithm/artifical_potential.h"
#include "algorithm/obstacles.h"
#include "utils/visualizationmodule.h"
#include <thread>
#include <iostream>
serialSender serial;
PathPlanner localPlanner;
// set Goals
std::deque<MyPoint> goals = { MyPoint(-200, -100), MyPoint(200, 100)}; // -300是边界
bool updateRRT()
{
bool update = false;
// have arrived && target change && no path
if(localPlanner.pathSize() == 0)
update = true;
// meet obstacles
RobotInfo& me = MyDataManager::instance()->ourRobot();
if(localPlanner.pathSize() != 0){
MyPoint target = localPlanner.path.front();
if(ObstaclesInfo::instance()->hasObstacle(me.x, me.y, target.x(), target.y(), PARAMS::OBSTACLE::OBSTACLETYPE))
update = true;
}
// 到了下一个点重新更新
// can reach my goal directly
MyPoint goal = goals.front();
if(!ObstaclesInfo::instance()->hasObstacle(me.x, me.y, goal.x(), goal.y(), PARAMS::OBSTACLE::OBSTACLETYPE))
update = true;
return update;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UDPReceiver::instance();
// open serial
if(!PARAMS::IS_SIMULATION){
// serial.sendStartPacket();
serial.openSerialPort();
}
// vel sending
while(true){
if ( PARAMS::DEBUG::kMainDebug ){
std::cout << "[main.cpp] send velocity" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(30));
bool if_use_artifical = ApPlanner::instance()->plan(goals.front());
if ( !if_use_artifical || true){
// update RRT
if(updateRRT()){
// qDebug() << "path planning!";
RRTPlanner::instance()->plan(MyDataManager::instance()->ourRobot().x, MyDataManager::instance()->ourRobot().y, goals.front().x(), goals.front().y());
localPlanner.updatePath(RRTPlanner::instance()->smoothPath);
}
// change goals
if(localPlanner.hasArrived(goals.front())){
goals.push_back(goals.front());
goals.pop_front();
localPlanner.stopMoving();
localPlanner.clearPath();
// qDebug() << "Change Goal to " << goals.front().x() << goals.front().y();
}
localPlanner.plan();
if(PARAMS::IS_SIMULATION)
CommandSender::instance()->sendToSim(PARAMS::our_id, localPlanner.velX, -localPlanner.velY, localPlanner.velW);
else
// 从1开始
// serial.sendToReal(PARAMS::our_id, 180 * localPlanner.velX, 180 * localPlanner.velY, -40*localPlanner.velW);
serial.sendToReal(PARAMS::our_id, 140 * localPlanner.velX, 200 * localPlanner.velY, 0);
// qDebug() << "vel: "<< localPlanner.velX << localPlanner.velY << localPlanner.velW;
// qDebug() << MyDataManager::instance()->ourRobot().x << MyDataManager::instance()->ourRobot().y;
}
else {
if(PARAMS::IS_SIMULATION){
CommandSender::instance()->sendToSim(PARAMS::our_id,
ApPlanner::instance()->v_x / 1000,
ApPlanner::instance()->v_y / 1000,
ApPlanner::instance()->v_w);
}
else{
serial.sendToReal(PARAMS::our_id,
ApPlanner::instance()->v_x / 10,
ApPlanner::instance()->v_y / 10,
ApPlanner::instance()->v_w);
}
}
if ( !if_use_artifical )
{
VisualModule::instance()->drawAll(goals);
// VisualModule::instance()->drawLines(RRTPlanner::instance()->smoothPath);
}
else{
// VisualModule::instance()->drawPoint(MyPoint(0, -0));
VisualModule::instance()->drawAll(goals);
}
}
return a.exec();
}