Commit 2f16e510 authored by Christopher Reis's avatar Christopher Reis

Sends Vehical information

parent fadc330e
# Copyright 2007-2016 United States Government as represented by the
# Administrator of The National Aeronautics and Space Administration.
# No copyright is claimed in the United States under Title 17, U.S. Code.
# All Rights Reserved.
SRCS = gm_kerbal_adaptor.cpp PublishThread.cpp SubscribeThread.cpp
TARGET = gm_kerbal_adaptor
include ../AppMake.mf
INCS += -I ./websocketpp -I ./curlpp-0.8.1/include/
LDFLAGS += -lboost_system -lcurl
CXXFLAGS += -std=c++11
/*
* Copyright 2007-2016 United States Government as represented by the
* Administrator of The National Aeronautics and Space Administration.
* No copyright is claimed in the United States under Title 17, U.S. Code.
* All Rights Reserved.
*/
/**
* @file PublishThread.cpp
*
* This file contains a simple example publisher using the GMSEC standard C++ API.
*
*/
#include "PublishThread.hpp"
#include <string>
#include <curl/curl.h>
#include "json.hpp"
using json = nlohmann::json;
using namespace gmsec::api;
json dictionary;
std::string url = "http://192.168.1.4:8085/telemachus/datalink?";
std::string v_url;
std::vector<std::string> v_meas;
std::string o_url;
std::vector<std::string> o_meas;
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
PublishThread::PublishThread(Config &c)
: config(c),
connection(0)
{
example::initialize(config);
}
PublishThread::~PublishThread()
{
teardown();
}
void CALL_TYPE PublishThread::run()
{
try
{
setup();
//o Output GMSEC API and middleware version information
GMSEC_INFO << Connection::getAPIVersion();
GMSEC_INFO << connection->getLibraryVersion();
//o Delay to allow subscriber thread to set up
example::millisleep(100);
std::ifstream myfile ("dictionary.json");
std::string str((std::istreambuf_iterator<char>(myfile)), (std::istreambuf_iterator<char>()));
dictionary = json::parse(str);
for (size_t i = 0; i < dictionary["subsystems"].size(); i++) {
if(dictionary["subsystems"][i]["name"] == "Vehicle"){
v_url += url;
for (size_t j = 0; j < dictionary["subsystems"][i]["measurements"].size(); j++) {
std::string id(dictionary["subsystems"][i]["measurements"][j]["identifier"].get<std::string>());
v_url += id + "=" + id + "&";
}
}
}
std::cout << v_url << '\n';
while (true) {
get_v();
//get_o();
//publish("GMSEC.TEST.PUBLISH");
example::millisleep(250);
}
teardown();
}
catch (Exception& e)
{
GMSEC_ERROR << "Failure: " << e.what();
}
}
void PublishThread::publish(const char* subject)
{
//o Create message
Message message(subject, Message::PUBLISH);
//o Add fields
int i = 123;
message.addField("CHAR-FIELD", (GMSEC_CHAR) 'c');
message.addField("BOOL-FIELD", false);
message.addField("I32-FIELD", (GMSEC_I32) i);
message.addField("U16-FIELD", (GMSEC_U16) i);
message.addField("STRING-FIELD", "This is a test");
message.addField("F64-FIELD", GMSEC_F64(123 / .1));
message.addField("BIN-FIELD", (GMSEC_BIN) "JLMNOPQ", 7);
//o Publish Message
connection->publish(message);
//o Display XML representation of message
GMSEC_INFO << "Published:\n" << message.toXML();
}
// void PublishThread::get_test(){
// std::string filename = "dictionary.json";
// FILE* pFile = fopen(fileName.c_str(), "rb");
// char buffer[65536];
// rapidjson::FileReadStream is(pFile, buffer, sizeof(buffer));
// rapidjson::Document document;
// document.rapidjson::ParseStream(is);
//
// }
void PublishThread::get_v(){
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, v_url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
json js = json::parse(readBuffer);
//std::cout << readBuffer << std::endl;
std::string subject = "GMSEC.KSP.MSG.TLM.PROCESSED.V";
Message message(subject.c_str(), Message::PUBLISH);
for (json::iterator it = js.begin(); it != js.end(); ++it) {
if (it.value().is_string()) {
//std::cout << it.key() << " : " << it.value().get<std::string>() << "\n";
std::string field(it.key());
message.addField(field.c_str(), it.value().get<std::string>().c_str());
}
if (it.value().is_number()) {
//std::cout << it.key() << " : " << it.value().get<float>() << "\n";
std::string field(it.key());
message.addField(field.c_str(), it.value().get<float>());
}
//message.addField(std::string(it.key()).c_str(), std::string(it.value()).c_str());
}
// for (size_t i = 0; i < js.size(); i++) {
// std::cout << js[i].get<std::string>() << '\n';
// }
connection->publish(message);
// float msnTime = js.at("v.missionTime");
// long alt = js.at("v.altitude");
// float latitude = js.at("v.lat");
// float longitude = js.at("v.long");
// std::string name = js.at("v.name");
// std::string subject = "GMSEC.KSP.MSG.TLM.PROCESSED.V";
// Message message(subject.c_str(), Message::PUBLISH);
// message.addField("ALTITUDE", (GMSEC_U32) alt);
// message.addField("MISSION-TIME", msnTime);
// message.addField("LONGITUDE", longitude);
// message.addField("LATITUDE", latitude);
// connection->publish(message);
// std::cout << msnTime << " " << alt << std::endl;
}
}
void PublishThread::get_o(){
std::string url = "http://192.168.1.4:8085/telemachus/datalink?";
url += "name=v.name&";
url += "orbit=o.orbitPatches";
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
json js = json::parse(readBuffer);
std::cout << readBuffer << std::endl;
json jo = js["orbit"][0];
//std::cout << jo.dump() << std::endl;
//std::string name = js.at("name");
float inclination = jo.at("inclination");
std::string subject = "GMSEC.KSP.MSG.TLM.PROCESSED.O";
Message message(subject.c_str(), Message::PUBLISH);
message.addField("INCLINATION", inclination);
connection->publish(message);
}
}
void PublishThread::setup()
{
//o Create the Connection
connection = Connection::create(config);
//o Connect to the GMSEC bus
connection->connect();
}
void PublishThread::teardown()
{
if (connection)
{
connection->disconnect();
Connection::destroy(connection);
}
}
/*
* Copyright 2007-2016 United States Government as represented by the
* Administrator of The National Aeronautics and Space Administration.
* No copyright is claimed in the United States under Title 17, U.S. Code.
* All Rights Reserved.
*/
/**
* @file PublishThread.hpp
*
* This file contains a simple example publisher using the GMSEC standard C++ API.
*
*/
#ifndef PUBLISH_THREAD_HPP
#define PUBLISH_THREAD_HPP
#include "../example.h"
class PublishThread
{
public:
PublishThread(gmsec::api::Config& c);
~PublishThread();
void publish(const char* subject);
void get_v();
void get_o();
void CALL_TYPE run();
void CALL_TYPE shutdown();
private:
// Declared, but not implemented
PublishThread(const PublishThread& pt);
PublishThread& operator=(const PublishThread& pt);
void setup();
void teardown();
gmsec::api::Config& config;
gmsec::api::Connection* connection;
};
#endif
/*
* Copyright 2007-2016 United States Government as represented by the
* Administrator of The National Aeronautics and Space Administration.
* No copyright is claimed in the United States under Title 17, U.S. Code.
* All Rights Reserved.
*/
/**
* @file SubscribeThread.cpp
*
* This file contains a simple example subscriber using the GMSEC standard C++ API.
*
*/
#include "SubscribeThread.hpp"
#include <string>
using namespace gmsec::api;
SubscribeThread::SubscribeThread(Config &c)
: config(c),
connection(0)
{
example::initialize(config);
}
SubscribeThread::~SubscribeThread()
{
teardown();
}
void CALL_TYPE SubscribeThread::run()
{
try
{
setup();
//o Output API and middelware version information
GMSEC_INFO << Connection::getAPIVersion();
GMSEC_INFO << connection->getLibraryVersion();
//o Listen
bool done = false;
while (!done)
{
Message* message = connection->receive(GMSEC_WAIT_FOREVER);
if (!message)
{
GMSEC_WARNING << "Timeout occurred";
continue;
}
// Display XML representation of the message
GMSEC_INFO << "Received:\n" << message->toXML();
// Examine the message's subject to check if we should quit
std::string subject = message->getSubject();
done = (subject == "GMSEC.TEST.QUIT");
connection->release(message);
}
GMSEC_INFO << "SubscribeThread GMSEC.TEST.QUIT message found.";
teardown();
}
catch (Exception& e)
{
GMSEC_ERROR << "Failure: " << e.what();
}
}
void SubscribeThread::setup()
{
//o Create the Connection
connection = Connection::create(config);
//o Connect
connection->connect();
//o Subscribe
info = connection->subscribe("GMSEC.>");
}
void SubscribeThread::teardown()
{
if (connection)
{
GMSEC_INFO << "Unsubscribing from " << info->getSubject();
connection->unsubscribe(info);
connection->disconnect();
Connection::destroy(connection);
}
}
/*
* Copyright 2007-2016 United States Government as represented by the
* Administrator of The National Aeronautics and Space Administration.
* No copyright is claimed in the United States under Title 17, U.S. Code.
* All Rights Reserved.
*/
/**
* @file SubscribeThread.hpp
*
* This file contains a simple example subscriber using the GMSEC standard C++ API.
*
*/
#ifndef SUBSCRIBE_THREAD_HPP
#define SUBSCRIBE_THREAD_HPP
#include "../example.h"
class SubscribeThread
{
public:
SubscribeThread(gmsec::api::Config& c);
~SubscribeThread();
void CALL_TYPE run();
void CALL_TYPE shutdown();
private:
// Defined, but not implemented
SubscribeThread(const SubscribeThread& st);
SubscribeThread& operator=(const SubscribeThread& st);
void setup();
void teardown();
gmsec::api::Config& config;
gmsec::api::Connection* connection;
gmsec::api::SubscriptionInfo* info;
};
#endif
{
"name": "Example Spacecraft",
"identifier": "sc",
"subsystems": [
{
"name": "Vehicle",
"identifier": "v",
"measurements": [
{
"name": "Altitude",
"identifier": "v.altitude",
"units": "meters",
"type": "float"
},
{
"name": "Height From Terrain",
"identifier": "v.heightFromTerrain",
"units": "meters",
"type": "float"
},
{
"name": "Terrain Height",
"identifier": "v.terrainHeight",
"units": "meters",
"type": "float"
},
{
"name": "Mission Time",
"identifier": "v.missionTime",
"units": "seconds",
"type": "float"
},
{
"name": "Surface Velocity",
"identifier": "v.surfaceVelocity",
"units": "m/s",
"type": "float"
},
{
"name": "Surface Velocity X",
"identifier": "v.surfaceVelocityx",
"units": "m/s",
"type": "float"
},
{
"name": "Surface Velocity Y",
"identifier": "v.surfaceVelocityy",
"units": "m/s",
"type": "float"
},
{
"name": "Surface Velocity Z",
"identifier": "v.surfaceVelocityz",
"units": "m/s",
"type": "float"
},
{
"name": "Angular Velocity",
"identifier": "v.angularVelocity",
"units": "m/s",
"type": "float"
},
{
"name": "Orbital Velocity",
"identifier": "v.orbitalVelocity",
"units": "m/s",
"type": "float"
},
{
"name": "Surface Speed",
"identifier": "v.surfaceSpeed",
"units": "m/s",
"type": "float"
},
{
"name": "Vertical Speed",
"identifier": "v.verticalSpeed",
"units": "m/s",
"type": "float"
},
{
"name": "G-Force",
"identifier": "v.geeForce",
"units": "gravity",
"type": "float"
},
{
"name": "Atmospheric Density",
"identifier": "v.atmosphericDensity",
"units": "kPa",
"type": "float"
},
{
"name": "Longitude",
"identifier": "v.long",
"units": "degree",
"type": "float"
},
{
"name": "Latitude",
"identifier": "v.lat",
"units": "degree",
"type": "float"
},
{
"name": "Dynamic Pressure",
"identifier": "v.dynamicPressure",
"units": "kPa",
"type": "float"
},
{
"name": "Name",
"identifier": "v.name",
"units": "string",
"type": "string"
},
{
"name": "Angle to Prograde",
"identifier": "v.angleToPrograde",
"units": "degree",
"type": "float"
}
]
},
{
"name": "Communications",
"identifier": "comms",
"measurements": [
{
"name": "Received",
"identifier": "comms.recd",
"units": "bytes",
"type": "integer"
},
{
"name": "Sent",
"identifier": "comms.sent",
"units": "bytes",
"type": "integer"
}
]
},
{
"name": "Power",
"identifier": "pwr",
"measurements": [
{
"name": "Generator Temperature",
"identifier": "pwr.temp",
"units": "\u0080C",
"type": "float"
},
{
"name": "Generator Current",
"identifier": "pwr.c",
"units": "A",
"type": "float"
},
{
"name": "Generator Voltage",
"identifier": "pwr.v",
"units": "V",
"type": "float"
}
]
},
{
"name": "System Information",
"identifier": "sys",
"measurements": [
{
"name": "System Uptime",
"identifier": "sys.uptime",
"units": "s",
"type": "float"
},
{
"name": "Processor Percent",
"identifier": "sys.proc_percent",
"units": "%",
"type": "float"
},
{
"name": "Memory Used",
"identifier": "sys.memused",
"units": "Kbytes",
"type": "float"
},
{
"name": "System Idle",
"identifier": "sys.idle",
"units": "%",
"type": "float"
},
{
"name": "Memory Free",
"identifier": "sys.memfree",
"units": "Kbytes",
"type": "float"
},
{
"name": "Memory Total",
"identifier": "sys.memtotal",
"units": "Kbytes",
"type": "float"
},
{
"name": "Memory Active",
"identifier": "sys.memactive",
"units": "Kbytes",
"type": "float"
},
{
"name": "Memory Available",
"identifier": "sys.memavailable",
"units": "Kbytes",
"type": "float"
},
{
"name": "Received Bytes",
"identifier": "sys.rxbytes",
"units": "Bytes",
"type": "float"
},
{
"name": "Received Packets",
"identifier": "sys.rxpackets",
"units": "Bytes",
"type": "float"
},
{
"name": "Transmitted Bytes",
"identifier": "sys.txbytes",
"units": "Bytes",
"type": "float"
},
{
"name": "Transmitted Packets",
"identifier": "sys.txpackets",
"units": "Bytes",
"type": "float"
}
]
}
]
}
/*
* Copyright 2007-2016 United States Government as represented by the
* Administrator of The National Aeronautics and Space Administration.
* No copyright is claimed in the United States under Title 17, U.S. Code.
* All Rights Reserved.
*/
/**
* @file GMSECThreads.cpp
*
* This file contains a simple example of using separate threads
* for separate connections.
*
*/
#include "PublishThread.hpp"
#include "SubscribeThread.hpp"
#include <gmsec4/util/StdSharedPtr.h>
#include <gmsec4/util/StdThread.h>
#include <memory>
using namespace gmsec::api;
using namespace gmsec::api::util;
void runSubscriber(StdSharedPtr<SubscribeThread> shared)
{
shared->run();
}
void runPublisher(StdSharedPtr<PublishThread> shared)
{
shared->run();
}
int main(int argc, char* argv[])
{
Config config(argc, argv);
example::addToConfigFromFile(config);
if (example::isOptionInvalid(config, argc))
{
example::printUsage("gmthreads");
return -1;
}
try
{
StdSharedPtr<SubscribeThread> subscribeThreadShared(new SubscribeThread(config));
std::auto_ptr<StdThread> gmsub(new StdThread(&runSubscriber, subscribeThreadShared));
StdSharedPtr<PublishThread> publishThreadShared(new PublishThread(config));
std::auto_ptr<StdThread> gmpub(new StdThread(&runPublisher, publishThreadShared));
GMSEC_INFO << "Now beginning Threads test";
gmsub->start();
gmpub->start();
gmpub->join();
gmsub->join();
GMSEC_INFO << "Now ending Threads test";
}
catch (Exception& e)
{
GMSEC_ERROR << "Failed: " << e.what();
return -1;
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment