Commit 2d112843 authored by Ian McInerney's avatar Ian McInerney

Added logger for the network communications

parent fdafdf7b
#ifndef LOCALIZATIONLOGGER_H_
#define LOCALIZATIONLOGGER_H_
#include <iostream>
#include <fstream>
#include <string.h>
#include "CCRTPPacket.h"
typedef struct computation_localization_shared_node_data {
float measuredRadius;
float estimatedX;
float estimatedY;
float estimatedZ;
float alpha;
float beta;
float gamma;
} computation_localization_node_data;
class LocalizationLogger{
private:
computation_localization_node_data *nodeData;
int numAgents;
float targetX;
float targetY;
float targetZ;
double startTime;
std::ofstream file_log;
public:
LocalizationLogger(int numAgents, char* filename);
~LocalizationLogger();
/*
* Functions to get different computation values from an agent
*/
float getMeasuredRadius(int agent);
float getEstimatedX(int agent);
float getEstimatedY(int agent);
float getEstimatedZ(int agent);
float getAlpha(int agent);
float getBeta(int agent);
float getGamma(int agent);
void setTargetPosition(float x, float y, float z);
void logData();
void packetReceived(CCRTPPacket *packet);
};
#endif
\ No newline at end of file
......@@ -6,6 +6,8 @@
#include "../CCRTPPacket.h"
#include "LocalizationLogger.h"
typedef struct edge{
int node1;
int node2;
......@@ -23,6 +25,8 @@ private:
// Array to hold pointers to the queues storing the packets to send to each quadcopter
std::queue<CCRTPPacket> **packetQueues;
LocalizationLogger *networkLogger;
public:
// The constructor
......@@ -33,6 +37,8 @@ public:
// The destructor
~NetworkForwarding();
void setNetworkLogger(LocalizationLogger *logger);
/*
* Add an edge to the graph
*
......
#include <iostream>
#include <fstream>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "computations/LocalizationLogger.h"
#include "CCRTPPacket.h"
#define NSEC_PER_SEC 1000000000L
LocalizationLogger::LocalizationLogger(int numAgents, char* fileName) {
this->numAgents = numAgents;
// Init the target position to 0
this->targetX = 0;
this->targetY = 0;
this->targetZ = 0;
// Save the start time
struct timespec tsTime;
clock_gettime(CLOCK_MONOTONIC, &tsTime);
this->startTime = (tsTime.tv_sec + double(tsTime.tv_nsec) / NSEC_PER_SEC);
// Create the array to hold the data from the nodes
this->nodeData = new computation_localization_node_data[numAgents]();
// Open the file
try {
char logFileName[40];
char timeString[40];
time_t rawtime;
time( &rawtime );
struct tm *timeinfo = localtime( &rawtime );
strftime(timeString, 40, "%Y_%m_%d_%H:%M:%S", timeinfo);
// Create the log file name (including date)
sprintf(logFileName, "%s_%s.txt", fileName, timeString);
this->file_log.open(fileName, std::ios_base::out);
} catch (...) {
std::cout << "Error opening log file for localization logger";
exit(-1);
}
// Create the header for the logfile
this->file_log << "#Computation:Localization" << std::endl;
// Create the field names
this->file_log << "\%Time\t\tTargetX\t\tTargetY\t\tTargetZ";
for (int i = 0; i < numAgents; i++) {
this->file_log << "\t\tNode" << i << "_measuredRadius";
this->file_log << "\t\tNode" << i << "_estimatedX";
this->file_log << "\t\tNode" << i << "_estimatedY";
this->file_log << "\t\tNode" << i << "_estimatedZ";
this->file_log << "\t\tNode" << i << "_alpha";
this->file_log << "\t\tNode" << i << "_beta";
this->file_log << "\t\tNode" << i << "_gamma";
}
this->file_log << std::endl;
// Create the field units
this->file_log << "&sec\t\tmeters\t\tmeters\t\tmeters";
for (int i = 0; i < numAgents; i++) {
this->file_log << "\t\tmeters";
this->file_log << "\t\tmeters";
this->file_log << "\t\tmeters";
this->file_log << "\t\tmeters";
this->file_log << "\t\traw";
this->file_log << "\t\traw";
this->file_log << "\t\traw";
}
this->file_log << std::endl;
}
LocalizationLogger::~LocalizationLogger() {
// Delete the array holding the data
delete[] nodeData;
// Close the logger file
this->file_log.close();
}
void LocalizationLogger::setTargetPosition(float x, float y, float z) {
this->targetX = x;
this->targetY = y;
this->targetZ = z;
}
void LocalizationLogger::logData() {
// Get the timestamp to log
struct timespec tsTime;
clock_gettime(CLOCK_MONOTONIC, &tsTime);
double currentTime = (tsTime.tv_sec + double(tsTime.tv_nsec) / NSEC_PER_SEC) - this->startTime;
// Log the time since start
this->file_log << currentTime;
// Log the target data
this->file_log << "\t\t" << this->targetX;
this->file_log << "\t\t" << this->targetY;
this->file_log << "\t\t" << this->targetZ;
// Log the agent data
for (int i = 0; i < numAgents; i++) {
this->file_log << "\t\t" << this->nodeData[i].measuredRadius;
this->file_log << "\t\t" << this->nodeData[i].estimatedX;
this->file_log << "\t\t" << this->nodeData[i].estimatedY;
this->file_log << "\t\t" << this->nodeData[i].estimatedZ;
this->file_log << "\t\t" << this->nodeData[i].alpha;
this->file_log << "\t\t" << this->nodeData[i].beta;
this->file_log << "\t\t" << this->nodeData[i].gamma;
}
// Add the newline
this->file_log << std::endl;
}
float LocalizationLogger::getMeasuredRadius(int agent) {
// The agent doesn't exist, return 0
if (agent >= this->numAgents) {
return (0.0);
}
// Otherwise just return the data
return (nodeData[agent].measuredRadius);
}
float LocalizationLogger::getEstimatedX(int agent) {
// The agent doesn't exist, return 0
if (agent >= this->numAgents) {
return (0.0);
}
// Otherwise just return the data
return (nodeData[agent].estimatedX);
}
float LocalizationLogger::getEstimatedY(int agent) {
// The agent doesn't exist, return 0
if (agent >= this->numAgents) {
return (0.0);
}
// Otherwise just return the data
return (nodeData[agent].estimatedY);
}
float LocalizationLogger::getEstimatedZ(int agent) {
// The agent doesn't exist, return 0
if (agent >= this->numAgents) {
return (0.0);
}
// Otherwise just return the data
return (nodeData[agent].estimatedZ);
}
float LocalizationLogger::getAlpha(int agent) {
// The agent doesn't exist, return 0
if (agent >= this->numAgents) {
return (0.0);
}
// Otherwise just return the data
return (nodeData[agent].alpha);
}
float LocalizationLogger::getBeta(int agent) {
// The agent doesn't exist, return 0
if (agent >= this->numAgents) {
return (0.0);
}
// Otherwise just return the data
return (nodeData[agent].beta);
}
float LocalizationLogger::getGamma(int agent) {
// The agent doesn't exist, return 0
if (agent >= this->numAgents) {
return (0.0);
}
// Otherwise just return the data
return (nodeData[agent].gamma);
}
void LocalizationLogger::packetReceived(CCRTPPacket *packet) {
// Retrieve the agent number
char* packetData = packet->data();
int agent = packetData[0];
// Copy the data from the packet into the data struct
memcpy(&(nodeData[agent]), &(packetData[1]), sizeof(computation_localization_node_data));
}
\ No newline at end of file
......@@ -7,6 +7,8 @@
NetworkForwarding::NetworkForwarding(int numNodes) {
this->numNodes = numNodes;
this->networkLogger = NULL;
// Create the queues for the nodes and the vectors for the edges
this->packetQueues = (std::queue<CCRTPPacket>**) malloc( sizeof(std::queue<CCRTPPacket>*)*numNodes );
this->edges = (std::vector<int>**) malloc( sizeof(std::vector<int>)*numNodes );
......@@ -44,6 +46,10 @@ NetworkForwarding::~NetworkForwarding() {
delete[] edges;
}
void NetworkForwarding::setNetworkLogger(LocalizationLogger *logger) {
this->networkLogger = logger;
}
/*
* Add an edge to the graph
......@@ -159,4 +165,9 @@ void NetworkForwarding::packetReceived(int quadNum, CCRTPPacket *pk) {
// Add it to the queue of the adjacent quadcopter
packetQueues[*it]->push(*pk);
}
// Pass the packet to the logger
if (this->networkLogger != NULL) {
this->networkLogger->packetReceived(pk);
}
}
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