迁移分支

This commit is contained in:
zengxiaobin
2023-11-24 17:09:26 +08:00
commit a6919ec672
158 changed files with 30851 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
#--------------------------------------------------------
# The CMakeLists.txt for: pSurfaceSupportComm
# Author(s): Xiaobin Zeng
#--------------------------------------------------------
SET(SRC
SurfaceSupportComm.cpp
SurfaceSupportComm_Info.cpp
main.cpp
)
FIND_LIBRARY(DUNE_LIB dune-core /usr/local/lib /usr/local/lib/DUNE)
FIND_PATH(DUNE_INCLUDE DUNE/IMC.hpp /usr/local/include /usr/local/include/DUNE)
include_directories(${DUNE_INCLUDE})
# include(FindProtobuf)
# find_package(Protobuf REQUIRED)
# include_directories(${Protobuf_INCLUDE_DIR})
# protobuf_generate_cpp(PROTO_SRC PROTO_HEADER Behavior.proto NavigationInfo.proto)
# add_library(proto ${PROTO_HEADER} ${PROTO_SRC})
# include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# find_package (jsoncpp NO_MODULE REQUIRED)
include_directories(/usr/include/jsoncpp/)
link_directories(/usr/local/lib/)
find_package (GeographicLib REQUIRED)
include_directories(${GeographicLib_INCLUDE_DIRS})
ADD_EXECUTABLE(pSurfaceSupportComm ${SRC})
TARGET_LINK_LIBRARIES(pSurfaceSupportComm
${MOOS_LIBRARIES}
${CMAKE_DL_LIBS}
${SYSTEM_LIBS}
${DUNE_LIB}
${GeographicLib_LIBRARIES}
mbutil
m
pthread
jsoncpp
# jsoncpp_lib_static
# protobuf
# protoc
# proto
# ${PROTOBUF_LIBRARY}
)

View File

@@ -0,0 +1,142 @@
#include "PeriodicTCPEvent.h"
#define TCP_RECEIVE_PORT 8000
class PeriodicTCPEvent::Impl
{
public:
Impl()
{
pfn_ = DefaultCallback;
pParamCaller_= NULL;
Period_ = 1.0;
}
static bool DefaultCallback(DUNE::IMC::Message * msg)
{
// UNUSED_PARAMETER(pParamCaller);
// std::cout.setf(std::ios::fixed);
// std::cout<<std::setprecision(4);
// std::cout<<"Timer Callback \n";
// std::cout<<" TimeNow "<<TimeNow<<"\n";
// std::cout<<" TimeScheduled "<<TimeScheduled<<"\n";
// std::cout<<" TimeLastRun "<<TimeLastRun<<"\n";
return true;
}
static bool PeriodicTCPEventDispatch(void * pParam)
{
PeriodicTCPEvent::Impl* pMe = static_cast<PeriodicTCPEvent::Impl*> (pParam);
return pMe->DoWork();
}
void SetCallback(bool (*pfn)(DUNE::IMC::Message * msg), void * pCallerParam)
{
UNUSED_PARAMETER(pCallerParam);
pfn_ = pfn;
//pParamCaller_ = pCallerParam;
}
bool SetPeriod(double PeriodSeconds)
{
if(PeriodSeconds<0)
return false;
Period_ = PeriodSeconds;
return true;
}
bool Stop()
{
Thread_.Stop();
return true;
}
bool Start()
{
if(Thread_.IsThreadRunning())
return false;
else
{
Thread_.Initialise(PeriodicTCPEventDispatch,this);
return Thread_.Start();
}
}
bool DoWork()
{
double TimeLast = MOOS::Time();
MOOS::BoostThisThread();
sock_tcp_receive.bind(TCP_RECEIVE_PORT);
sock_tcp_receive.listen(5);
while(!Thread_.IsQuitRequested())
{
double TimeScheduled=TimeLast+Period_;
size_t size = sock_tcp_receive.read(tcpReceiveBuffer, sizeof(tcpReceiveBuffer)/sizeof(uint8_t));
if (size > 0)
{
DUNE::IMC::Message * msg = DUNE::IMC::Packet::deserialize(tcpReceiveBuffer, size);
if(!(*pfn_)(msg))
{
break;
}
}
}
return true;
}
CMOOSThread Thread_;
bool (*pfn_)(DUNE::IMC::Message * msg);
void * pParamCaller_;
double Period_;
// DUNE::Network::UDPSocket sock_udp_send;
DUNE::Network::TCPSocket sock_tcp_receive;
uint8_t tcpReceiveBuffer[65535];
CMOOSLock m_Lock;
// std::stack<DUNE::IMC::Message *> msgBuffer;
};
void PeriodicTCPEvent::SetCallback(bool (*pfn)(DUNE::IMC::Message * msg), void * pCallerParam)
{
Impl_->SetCallback(pfn,pCallerParam);
}
PeriodicTCPEvent::PeriodicTCPEvent(): Impl_(new PeriodicTCPEvent::Impl )
{
}
bool PeriodicTCPEvent::SetPeriod(double PeriodSeconds)
{
return Impl_->SetPeriod(PeriodSeconds);
}
bool PeriodicTCPEvent::Start()
{
return Impl_->Start();
}
bool PeriodicTCPEvent::Stop()
{
return Impl_->Stop();
}
// bool PeriodicTCPEvent::Push(DUNE::IMC::Message* msg)
// {
// Impl_->msgBuffer.push(msg);
// }

View File

@@ -0,0 +1,39 @@
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
#include "MOOS/libMOOS/Utils/ThreadPriority.h"
#include "MOOS/libMOOS/Utils/MOOSThread.h"
#include <DUNE/DUNE.hpp>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
class PeriodicTCPEvent
{
public:
PeriodicTCPEvent();
/**
* this sets the callback you wish to have called
*/
void SetCallback(bool (*pfn)(DUNE::IMC::Message * msg), void * pCallerParam);
/**
* Set the period of the event
*/
bool SetPeriod(double PeriodSeconds);
/** start the service*/
bool Start();
/** stop the service */
bool Stop();
private:
class Impl;
Impl * Impl_;
};

View File

@@ -0,0 +1,156 @@
#include "PeriodicUDPEvent.h"
#define UDP_SEND_PORT 6001
#define DEST_IP_ADDRESS "127.0.0.1"
class PeriodicUDPEvent::Impl
{
public:
Impl()
{
pfn_ = DefaultCallback;
pParamCaller_= NULL;
Period_ = 1.0;
}
static bool DefaultCallback(double TimeNow,double TimeLastRun,double TimeScheduled, void * pParamCaller)
{
UNUSED_PARAMETER(pParamCaller);
std::cout.setf(std::ios::fixed);
std::cout<<std::setprecision(4);
std::cout<<"Timer Callback \n";
std::cout<<" TimeNow "<<TimeNow<<"\n";
std::cout<<" TimeScheduled "<<TimeScheduled<<"\n";
std::cout<<" TimeLastRun "<<TimeLastRun<<"\n";
return true;
}
static bool PeriodicUDPEventDispatch(void * pParam)
{
PeriodicUDPEvent::Impl* pMe = static_cast<PeriodicUDPEvent::Impl*> (pParam);
return pMe->DoWork();
}
void SetCallback(bool (*pfn)(double TimeNow,double TimeLastRun,double TimeScheduled, void * pParamCaller), void * pCallerParam)
{
UNUSED_PARAMETER(pCallerParam);
pfn_ = pfn;
//pParamCaller_ = pCallerParam;
}
bool SetPeriod(double PeriodSeconds)
{
if(PeriodSeconds<0)
return false;
Period_ = PeriodSeconds;
return true;
}
bool Stop()
{
Thread_.Stop();
return true;
}
bool Start()
{
if(Thread_.IsThreadRunning())
return false;
else
{
Thread_.Initialise(PeriodicUDPEventDispatch,this);
return Thread_.Start();
}
}
bool DoWork()
{
double TimeLast = MOOS::Time();
MOOS::BoostThisThread();
while(!Thread_.IsQuitRequested())
{
double TimeScheduled=TimeLast+Period_;
std::string addr = DEST_IP_ADDRESS;
int size = -1;
int type = -1;
m_Lock.Lock();
if (!msgBuffer.empty())
{
DUNE::Utils::ByteBuffer bb;
DUNE::IMC::Message *msg = msgBuffer.top();
DUNE::IMC::Packet::serialize(msg, bb);
size = sock_udp_send.write(bb.getBuffer(), msg->getSerializationSize(),
DUNE::Network::Address(addr.c_str()), UDP_SEND_PORT);
type = msg->getId();
// delete msg;
msgBuffer.pop();
}
m_Lock.UnLock();
double TimeNow = MOOS::Time();
if(!(*pfn_)(MOOS::Time(), TimeLast,TimeScheduled,pParamCaller_))
{
break;
}
TimeLast = TimeNow;
}
return true;
}
CMOOSThread Thread_;
bool (*pfn_)(double TimeNow,double TimeLastRun,double TimeScheduled, void* pParam);
void * pParamCaller_;
double Period_;
DUNE::Network::UDPSocket sock_udp_send;
uint8_t udpReceiveBuffer[65535];
CMOOSLock m_Lock;
std::stack<DUNE::IMC::Message *> msgBuffer;
};
void PeriodicUDPEvent::SetCallback(bool (*pfn)(double Now,double LastRun,double Scheduled, void * pParamCaller), void * pCallerParam)
{
Impl_->SetCallback(pfn,pCallerParam);
}
PeriodicUDPEvent::PeriodicUDPEvent(): Impl_(new PeriodicUDPEvent::Impl )
{
}
bool PeriodicUDPEvent::SetPeriod(double PeriodSeconds)
{
return Impl_->SetPeriod(PeriodSeconds);
}
bool PeriodicUDPEvent::Start()
{
return Impl_->Start();
}
bool PeriodicUDPEvent::Stop()
{
return Impl_->Stop();
}
bool PeriodicUDPEvent::Push(DUNE::IMC::Message* msg)
{
Impl_->msgBuffer.push(msg);
}

View File

@@ -0,0 +1,42 @@
#include "MOOS/libMOOS/Utils/MOOSUtilityFunctions.h"
#include "MOOS/libMOOS/Utils/ThreadPriority.h"
#include "MOOS/libMOOS/Utils/MOOSThread.h"
#include <DUNE/DUNE.hpp>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
class PeriodicUDPEvent
{
public:
PeriodicUDPEvent();
/**
* this sets the callback you wish to have called
*/
// void SetCallback(bool (*pfn)(int type, int size), void * pCallerParam);
void SetCallback(bool (*pfn)(double Now,double LastRun,double Scheduled, void * pParamCaller), void * pCallerParam);
/**
* Set the period of the event
*/
bool SetPeriod(double PeriodSeconds);
/** start the service*/
bool Start();
/** stop the service */
bool Stop();
bool Push(DUNE::IMC::Message* msg);
private:
class Impl;
Impl * Impl_;
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,155 @@
/************************************************************/
/* NAME: Xiaobin Zeng */
/* ORGN: MIT */
/* FILE: SurfaceSupportComm.h */
/* DATE: */
/************************************************************/
#ifndef SurfaceSupportComm_HEADER
#define SurfaceSupportComm_HEADER
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <thread>
#include "MOOS/libMOOS/MOOSLib.h"
#include "MOOS/libMOOS/Thirdparty/AppCasting/AppCastingMOOSApp.h"
#include <DUNE/DUNE.hpp>
#include <GeographicLib/LocalCartesian.hpp>
#include <json/json.h>
// #include "PeriodicUDPEvent.h"
struct Landmark {
float lon;
float lat;
float depth;
float speed;
};
struct EstimatedState {
float referenceLon;
float referenceLat;
float referenceAltitude;
float currentLon;
float currentLat;
float currentAltitude;
float offsetNorth;
float offsetEast;
float offsetDown;
float roll;
float pitch;
float yaw;
float linearVelocityNorth;
float linearVelocityEast;
float linearVelocityDown;
float height;
float depth;
};
struct DeviceStatus {
unsigned int batteryVoltage;
unsigned int batteryLevel;
float batteryTemp;
double controllerTemp;
int thrustRpm;
unsigned int lightEnable;
unsigned int throwingLoadEnable;
unsigned int dvlStatus;
};
class SurfaceSupportComm : public CMOOSApp
// class SurfaceSupportComm : public AppCastingMOOSApp
{
public:
SurfaceSupportComm();
~SurfaceSupportComm();
protected: // Standard MOOSApp functions to overload
bool OnNewMail(MOOSMSG_LIST &NewMail);
bool Iterate();
bool OnConnectToServer();
bool OnStartUp();
void RegisterVariables();
// bool buildReport();
private:
enum MissionStatus{FAULT=0, UNRUN=1, MANUAL=2 ,RUN=3, CONFIG=5};
char recv_buf[2048];
DUNE::Network::UDPSocket sock_udp_send;
DUNE::Network::TCPSocket sock_tcp_receive;
int tcpSockFD;
int tcpBindRet;
int tcpListenRet;
int tcpSockConnect;
DUNE::IO::Poll m_poll;
uint16_t header_src, header_dst;
uint8_t header_src_ent, header_dst_ent;
std::string planConfigPath;
struct EstimatedState estimatedState;
struct DeviceStatus deviceStatus;
struct Client
{
DUNE::Network::TCPSocket* socket; // Socket handle.
DUNE::Network::Address address; // Client address.
uint16_t port; // Client port.
DUNE::IMC::Parser parser; // Parser handle
};
typedef std::list<Client> ClientList;
ClientList m_clients;
bool udpSendToServer(DUNE::IMC::Message * msg, std::string addr, int port);
void tcpReceiveFromClient(char* buf, unsigned int cap, double timeout);
void tcpSendToClient(char* buf, unsigned int cap);
void acceptNewClient(void);
void handleClients(char* buf, unsigned int cap);
void closeConnection(Client& c, std::exception& e);
void processMessage(DUNE::IMC::Message * message);
void ConvertLLAToNED(std::vector<double> init_lla,
std::vector<double> point_lla,
std::vector<double>& point_ned);
void ConvertNEDToLLA(std::vector<double> init_lla,
std::vector<double> point_ned,
std::vector<double> &point_lla);
void ConvertLLAToENU(std::vector<double> init_lla,
std::vector<double> point_lla,
std::vector<double>& point_enu);
void ConvertENUToLLA(std::vector<double> init_lla,
std::vector<double> point_enu,
std::vector<double> &point_lla);
void BatchConvertCoordinate(Json::Value &object);
int retrieveEntityStatus(DUNE::IMC::MsgList& equipmentMsgList);
template <typename Type>
inline int getEntityStatus(DUNE::IMC::EntityParameters& entityParameter, std::string name, Type& value);
void getAllFiles(const char * dir_name, std::vector<std::string>& filePath);
// void tcpProcessThread(std::vector<std::string>& downloadFileArray);
void tcpProcessThread(const std::string& downloadFile);
double getFileSize(std::string filePath);
int testFlag = 0;
double TimeLast;
int sendCount = 0;
// PeriodicUDPEvent udpEvent;
// PeriodicTCPEvent tcpEvent;
std::string missionStatusString;
std::string vehicleName;
unsigned int c_block_size;
std::list<DUNE::Network::TCPSocket*> m_sockets;
};
#endif

View File

@@ -0,0 +1,115 @@
/****************************************************************/
/* NAME: Xiaobin Zeng */
/* ORGN: MIT Cambridge MA */
/* FILE: SurfaceSupportComm_Info.cpp */
/* DATE: Dec 29th 1963 */
/****************************************************************/
#include <cstdlib>
#include <iostream>
#include "SurfaceSupportComm_Info.h"
#include "ColorParse.h"
#include "ReleaseInfo.h"
using namespace std;
//----------------------------------------------------------------
// Procedure: showSynopsis
void showSynopsis()
{
blk("SYNOPSIS: ");
blk("------------------------------------ ");
blk(" The pSurfaceSupportComm application is used for ");
blk(" ");
blk(" ");
blk(" ");
blk(" ");
}
//----------------------------------------------------------------
// Procedure: showHelpAndExit
void showHelpAndExit()
{
blk(" ");
blu("=============================================================== ");
blu("Usage: pSurfaceSupportComm file.moos [OPTIONS] ");
blu("=============================================================== ");
blk(" ");
showSynopsis();
blk(" ");
blk("Options: ");
mag(" --alias","=<ProcessName> ");
blk(" Launch pSurfaceSupportComm with the given process name ");
blk(" rather than pSurfaceSupportComm. ");
mag(" --example, -e ");
blk(" Display example MOOS configuration block. ");
mag(" --help, -h ");
blk(" Display this help message. ");
mag(" --interface, -i ");
blk(" Display MOOS publications and subscriptions. ");
mag(" --version,-v ");
blk(" Display the release version of pSurfaceSupportComm. ");
blk(" ");
blk("Note: If argv[2] does not otherwise match a known option, ");
blk(" then it will be interpreted as a run alias. This is ");
blk(" to support pAntler launching conventions. ");
blk(" ");
exit(0);
}
//----------------------------------------------------------------
// Procedure: showExampleConfigAndExit
void showExampleConfigAndExit()
{
blk(" ");
blu("=============================================================== ");
blu("pSurfaceSupportComm Example MOOS Configuration ");
blu("=============================================================== ");
blk(" ");
blk("ProcessConfig = pSurfaceSupportComm ");
blk("{ ");
blk(" AppTick = 4 ");
blk(" CommsTick = 4 ");
blk(" ");
blk("} ");
blk(" ");
exit(0);
}
//----------------------------------------------------------------
// Procedure: showInterfaceAndExit
void showInterfaceAndExit()
{
blk(" ");
blu("=============================================================== ");
blu("pSurfaceSupportComm INTERFACE ");
blu("=============================================================== ");
blk(" ");
showSynopsis();
blk(" ");
blk("SUBSCRIPTIONS: ");
blk("------------------------------------ ");
blk(" NODE_MESSAGE = src_node=alpha,dest_node=bravo,var_name=FOO, ");
blk(" string_val=BAR ");
blk(" ");
blk("PUBLICATIONS: ");
blk("------------------------------------ ");
blk(" Publications are determined by the node message content. ");
blk(" ");
exit(0);
}
//----------------------------------------------------------------
// Procedure: showReleaseInfoAndExit
void showReleaseInfoAndExit()
{
showReleaseInfo("pSurfaceSupportComm", "gpl");
exit(0);
}

View File

@@ -0,0 +1,18 @@
/****************************************************************/
/* NAME: Xiaobin Zeng */
/* ORGN: MIT Cambridge MA */
/* FILE: SurfaceSupportComm_Info.h */
/* DATE: Dec 29th 1963 */
/****************************************************************/
#ifndef SurfaceSupportComm_INFO_HEADER
#define SurfaceSupportComm_INFO_HEADER
void showSynopsis();
void showHelpAndExit();
void showExampleConfigAndExit();
void showInterfaceAndExit();
void showReleaseInfoAndExit();
#endif

View File

@@ -0,0 +1,52 @@
/************************************************************/
/* NAME: Xiaobin Zeng */
/* ORGN: MIT */
/* FILE: main.cpp */
/* DATE: */
/************************************************************/
#include <string>
#include "MBUtils.h"
#include "ColorParse.h"
#include "SurfaceSupportComm.h"
#include "SurfaceSupportComm_Info.h"
using namespace std;
int main(int argc, char *argv[])
{
string mission_file;
string run_command = argv[0];
for(int i=1; i<argc; i++) {
string argi = argv[i];
if((argi=="-v") || (argi=="--version") || (argi=="-version"))
showReleaseInfoAndExit();
else if((argi=="-e") || (argi=="--example") || (argi=="-example"))
showExampleConfigAndExit();
else if((argi == "-h") || (argi == "--help") || (argi=="-help"))
showHelpAndExit();
else if((argi == "-i") || (argi == "--interface"))
showInterfaceAndExit();
else if(strEnds(argi, ".moos") || strEnds(argi, ".moos++"))
mission_file = argv[i];
else if(strBegins(argi, "--alias="))
run_command = argi.substr(8);
else if(i==2)
run_command = argi;
}
if(mission_file == "")
showHelpAndExit();
cout << termColor("green");
cout << "pSurfaceSupportComm launching as " << run_command << endl;
cout << termColor() << endl;
SurfaceSupportComm SurfaceSupportComm;
SurfaceSupportComm.Run(run_command.c_str(), mission_file.c_str());
return(0);
}

View File

@@ -0,0 +1,9 @@
//------------------------------------------------
// pSurfaceSupportComm config block
ProcessConfig = pSurfaceSupportComm
{
AppTick = 4
CommsTick = 4
}