Skip to content

Commit

Permalink
[feature] added JSONSerializable abstract class; added url for json r…
Browse files Browse the repository at this point in the history
…equest lib; added FCSurveillance framework.
  • Loading branch information
cyanine-gi committed Aug 9, 2021
1 parent bd67829 commit b56e7c7
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 18 deletions.
8 changes: 8 additions & 0 deletions algorithms/src/GroundControlStation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
##### Each station contains some small hexagons. Each hexagon contains multiple layers with different height.

##### When a vehicle try to plan its path: the stations in this path are involved, and hexagons with a certain altitude(in other words, 3d cellulars) are distributed to this vehicle by these stations. If path is generated successfully by stations, these 3d cellulars are set status "occupied", which will be maintained by these stations.

# 自动飞行汽车地面控制站

### 这些地面被蜂窝状放置.如同你的手机使用的基站一样.

##### 每个站负责一块小六边形区域.每个区域有多个层

##### 当空中载具尝试规划路径时,经过的基站参与这一过程.小六边形块对应的高度层被分配给这些空中载具.如果路径生成成功,这些带高度的小六边形块在地面基站里被标记为已占据.
4 changes: 4 additions & 0 deletions algorithms/src/SystemManagement/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

主要负责机上飞行状态切换,与地面站通信,飞行前申请空域,宏观上规划/协商飞行器的最优路径,监控飞行器自身状态并向地面汇报;是连接机上状态和地面站通信的桥梁.

使用http传递json的方式向地面站服务器发送自身状态.

System Management module.

In charge of flight status management(FSM), communication with ground control station, negotiation of best path and surveillance of vehicle state.

This module is the bridge between vehicle and ground control station.

This module reports vehicle's state with http request to the http server set on ground control station.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

#include "FlightStage.h"
#include "location_and_coordinates/LocationT.h"
#include "../../json_request_response_lib/src/JSONSerializableAbstract.h"

namespace GAASManagement {

struct GAASFlightStatusT//用于和地面同步飞行状态.
class GAASFlightStatusT:public JSONSerializableAbstract//用于和地面同步飞行状态.
{
public:
string current_map_name;
FlightStageT current_mode = FlightStage::IDLE_STAGE;
LocationT currentLocation,TargetLocation;
Expand All @@ -19,6 +21,7 @@ struct GAASFlightStatusT//用于和地面同步飞行状态.
LocationT getTargetLocation();
void serialize();
void deserialize();
json getJSONObject();
};


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#ifndef GAAS_VEHICLE_STATIC_INFO_HEADER
#define GAAS_VEHICLE_STATIC_INFO_HEADER
#include "../../json_request_response_lib/src/JSONSerializableAbstract.h"




struct VehicleStaticInfo;
struct VehicleStaticInfo
class VehicleStaticInfo;
class VehicleStaticInfo:public JSONSerializableAbstract
{
public:
string vehicle_name;//飞行器名称
double vehicle_size_x,vehicle_size_y,vehicle_size_z;// 尺寸
bool isVTOL = true;//支持垂直起降.
Expand All @@ -15,5 +16,6 @@ struct VehicleStaticInfo
void serialize();
void deserialize();
void loadFromConfigFile();//从配置文件加载.
json getJSONObject();
};
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ cmake_minimum_required(VERSION 2.8.3)
project(json_request_response_lib)

## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++17 -O0)
#add_compile_options(-std=c++17 -O3)
#add_compile_options(-std=c++17 -O0)
add_compile_options(-std=c++17 -O3)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
set(CMAKE_BUILD_TYPE DEBUG)
#set(CMAKE_BUILD_TYPE RELEASE)
#set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_BUILD_TYPE RELEASE)
#find_package(OpenCV 3.4.5 REQUIRED)
find_package(Glog REQUIRED)
find_package(catkin REQUIRED COMPONENTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ struct EncryptedJSONResponse
std::shared_ptr<json> pResponse_json;
};

static std::string request_function(const json& j_)//,std::promise<EncryptedJSONResponse::Ptr>& promise)
static std::string request_function(const json& j_,const string& api_url)//,std::promise<EncryptedJSONResponse::Ptr>& promise)
{
try{
httplib::Client cli("localhost", 8080);
std::string json_str = j_.dump(4);
httplib::Headers header;
auto res = cli.Post("/api/fetchJSONObjectResponse",header,json_str,"text/plain; charset=utf-8");
auto res = cli.Post(api_url.c_str(),header,json_str,"text/plain; charset=utf-8");
if(res)
{
LOG(INFO) << "Received HTTP response with status:"<< res->status << endl;
Expand Down Expand Up @@ -78,15 +78,15 @@ class EncryptedJSONRequest
{
public:
typedef std::chrono::time_point<std::chrono::high_resolution_clock> ChronoTimeT;
void start_request(const json& request_json)
void start_request(const json& request_json,const string& api_url)
{
// this->request_future = this->request_promise.get_future();
// //this->pThread = std::shared_ptr<std::thread>(new std::thread(request_function,std::cref(request_json),std::ref(this->request_promise)));
// this->pAsync = std::shared_ptr<std::async>( new std::async(std::launch::async,request_function,std::cref(request_json))//,std::ref(this->request_promise))
// );

this->api_url = api_url;
this->t_start = std::chrono::system_clock::now();
this->request_future = std::async(std::launch::async,request_function,std::cref(request_json));
this->request_future = std::async(std::launch::async,request_function,std::cref(request_json),std::cref(api_url));
}

bool getResponseSafe(EncryptedJSONResponse::Ptr& ptr_output)
Expand Down Expand Up @@ -115,25 +115,31 @@ class EncryptedJSONRequest
}
return false;
}
std::string getCurrentApiUrl()
{
return this->api_url;
}


private:
std::string api_url;
std::future<std::string> request_future;
ChronoTimeT t_start;
//std::shared_ptr<std::async> pAsync;
bool queryIfFinished()
{
std::future_status status;
status = this->request_future.wait_for(std::chrono::seconds(0));
if (status == std::future_status::deferred)
if(status == std::future_status::deferred)
{
return false;//未开始.
}
else if(status == std::future_status::timeout)
{
return false;//尚未结束.
}
else if (status == std::future_status::ready) {
else if(status == std::future_status::ready)
{
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef JSON_SERIALIZABLE_ABSTRACT_H
#define JSON_SERIALIZABLE_ABSTRACT_H
#include <string>
#include <memory>
#include "third_party/nlohmann_json/single_include/nlohmann/json.hpp"
using json = nlohmann::json;

class JSONSerializableAbstract{

public:
virtual json getJSONObject() = 0;
};


#endif
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main(int argc,char** argv)
json j;
std::stringstream ss("{\"function\": \"query\"}");
ss>>j;
json_req.start_request(j);
json_req.start_request(j,"/api/ping");
EncryptedJSON::EncryptedJSONResponse::Ptr pRes;
while(!json_req.getResponseSafe(pRes)&&!json_req.everTimeout())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ int start_server()
svr.Get("/hi", [](const httplib::Request& req, httplib::Response& res) {
res.set_content("Hello World!", "text/plain");
});
svr.Post("/api/fetchJSONObjectResponse",[](const httplib::Request& req, httplib::Response& res){
res.set_content("{\"state\":\"correct\"}", "text/plain");
svr.Post("/api/ping",[](const httplib::Request& req, httplib::Response& res){
res.set_content("{\"state\":\"correct\",\"data\":\"pong!\" }", "text/plain");
});
cout<<"Server start..."<<endl;
svr.listen("localhost", 8080);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef FLIGHT_CONTROLLER_SURVEILLANCE_H
#define FLIGHT_CONTROLLER_SURVEILLANCE_H

#include "SurveillanceModuleAbstract.h"
#include "gaas_msgs/GAASSystemManagementFlightControllerState.h"
#include <geometry_msgs/PoseStamped.h>

class FlightControllerModule:SurveillanceModuleAbstract
{
int runPipelineAndGetState(const gaas_msgs::GAASSystemManagementFlightControllerStateConstPtr& pFCState)
//int runPipelineAndGetState(const geometry_msgs::PoseStampedConstPtr& pFCPose)
{
if(!this->ever_init)
{
this->lastFCState = *pFCPose;
this->current_status = this->STATUS_NOT_SURE;
this->ever_init = true;
return this->current_status;
}
//已初始化:
//bool speed_and_angular_rate_correct = checkSpeedAndAngularRate();
//if(!speed_and_angular_rate_correct)
//{
// this->current_status = this->STATUS_ERROR;
//}
//else
//{
// this->current_status = this->STATUS_CORRECT;
//}
return this->current_status;

}
void initSurveillanceModule()
{
setModuleName("Flight Controller Module");
}
int checkStateLegal()
{
return this->current_status;
}
private:
bool ever_init = false;
gaas_msgs::GAASSystemManagementFlightControllerState lastFCState;
geometry_msgs::PoseStamped lastPose;//手动计算....
bool checkSpeedAndAngularRate(const geometry_msgs::PoseStamped& pose1,const geometry_msgs::PoseStamped& pose2);

};



#endif

0 comments on commit b56e7c7

Please sign in to comment.