Commit 470352ce authored by roangel's avatar roangel
Browse files

added support for saving data in the database. Added cf zone index to...

added support for saving data in the database. Added cf zone index to database. Now need to fix all exceptions
parent 02087bfe
......@@ -15,6 +15,7 @@
#include "CFLinker.h"
#include "d_fall_pps/CrazyflieDB.h"
#include "d_fall_pps/CrazyflieEntry.h"
using namespace d_fall_pps;
......@@ -148,6 +149,17 @@ private:
int getTabIndexFromName(QString name);
CrazyflieDB m_data_base;
void clear_database_file();
void fill_database_file();
int read_database_from_file(CrazyflieDB &read_db);
void save_database_file();
void insert_or_update_entry_database(CrazyflieEntry entry);
};
......
......@@ -34,6 +34,8 @@ public:
void messageCallback(const ptrToMessage& p_msg);
ros::ServiceClient m_read_db_client;
ros::ServiceClient m_update_db_client;
ros::ServiceClient m_command_db_client;
signals:
......
......@@ -100,13 +100,14 @@ void CFLinker::link(int student_id, int cf_zone_index, std::string cf_name)
tmp_link.cf_zone_index = cf_zone_index;
tmp_link.cf_name = cf_name;
ROS_INFO("tmp_link.student_id %d", tmp_link.student_id);
ROS_INFO("tmp_link.cf_zone_index %d", tmp_link.cf_zone_index);
ROS_INFO("tmp_link.cf_name %s", tmp_link.cf_name.c_str());
// (*m_crazyfly_zones)[tmp_link.cf_zone_index]->linkCF(tmp_link.cf_name);
// (*m_crazyflies_vector)[getCFIndexFromName(tmp_link.cf_name)]->assignCFZone(tmp_link.cf_zone_index);
addNewRow(m_ui->spinBox_student_ids->value(), tmp_link.cf_name, tmp_link.cf_zone_index);
addNewRow(tmp_link.student_id, tmp_link.cf_name, tmp_link.cf_zone_index);
links.push_back(tmp_link);
// TODO: remove options linked from available ones
......
......@@ -16,6 +16,9 @@
#include "d_fall_pps/UnlabeledMarker.h"
#include "d_fall_pps/CMRead.h"
#include "d_fall_pps/CrazyflieEntry.h"
#include "d_fall_pps/CMUpdate.h"
#include "d_fall_pps/CMCommand.h"
#include "CentralManagerService.h"
#endif
#include <string>
......@@ -699,7 +702,6 @@ void MainGUIWindow::on_save_in_DB_button_clicked()
for(int i = 0; i < cf_linker->links.size(); i++)
{
CrazyflieEntry tmp_entry;
ROS_INFO("for loop iteration");
tmp_entry.crazyflieContext.crazyflieName = cf_linker->links[i].cf_name;
tmp_entry.crazyflieContext.localArea.crazyfly_zone_index = cf_linker->links[i].cf_zone_index;
tmp_entry.studentID = cf_linker->links[i].student_id;
......@@ -708,11 +710,16 @@ void MainGUIWindow::on_save_in_DB_button_clicked()
{
if(cf_linker->links[i].cf_zone_index == scene->crazyfly_zones[j]->getIndex())
{
QRectF rect = scene->crazyfly_zones[j]->rect();
tmp_entry.crazyflieContext.localArea.xmin = rect.x() * FROM_UNITS_TO_METERS;
tmp_entry.crazyflieContext.localArea.xmax = (rect.x() + rect.width()) * FROM_UNITS_TO_METERS;
tmp_entry.crazyflieContext.localArea.ymin = (rect.y() - rect.height()) * FROM_UNITS_TO_METERS;
tmp_entry.crazyflieContext.localArea.ymax = rect.y() * FROM_UNITS_TO_METERS;
double x_min = scene->crazyfly_zones[j]->sceneBoundingRect().bottomLeft().x();
double y_min = - scene->crazyfly_zones[j]->sceneBoundingRect().bottomLeft().y();
double x_max = scene->crazyfly_zones[j]->sceneBoundingRect().topRight().x();
double y_max = -scene->crazyfly_zones[j]->sceneBoundingRect().topRight().y();
tmp_entry.crazyflieContext.localArea.xmin = x_min * FROM_UNITS_TO_METERS;
tmp_entry.crazyflieContext.localArea.xmax = x_max * FROM_UNITS_TO_METERS;
tmp_entry.crazyflieContext.localArea.ymin = y_min * FROM_UNITS_TO_METERS;
tmp_entry.crazyflieContext.localArea.ymax = y_max * FROM_UNITS_TO_METERS;
}
}
tmp_db.crazyflieEntries.push_back(tmp_entry);
......@@ -721,17 +728,96 @@ void MainGUIWindow::on_save_in_DB_button_clicked()
m_data_base = tmp_db;
ROS_INFO_STREAM("database:\n" << m_data_base);
// save the database in the file
fill_database_file();
}
void MainGUIWindow::on_load_from_DB_button_clicked()
void MainGUIWindow::clear_database_file()
{
CrazyflieDB tmp_db;
if(read_database_from_file(tmp_db) == 0)
{
for(int i = 0; i < tmp_db.crazyflieEntries.size(); i++)
{
CMUpdate updateCall;
updateCall.request.mode = ENTRY_REMOVE;
updateCall.request.crazyflieEntry.crazyflieContext.crazyflieName = tmp_db.crazyflieEntries[i].crazyflieContext.crazyflieName;
if(_rosNodeThread->m_update_db_client.call(updateCall))
{
ROS_INFO("database changed in central manager service");
}
else
{
ROS_ERROR("Failed to remove entry in DB");
}
}
save_database_file();
}
else
{
ROS_INFO("Failed to read DB");
}
}
void MainGUIWindow::fill_database_file()
{
clear_database_file();
ROS_INFO("cleared data base file");
ROS_INFO_STREAM("database:\n" << m_data_base);
for(int i = 0; i < m_data_base.crazyflieEntries.size(); i++)
{
ROS_INFO("inserted 1 item in DB");
insert_or_update_entry_database(m_data_base.crazyflieEntries[i]);
}
save_database_file();
}
void MainGUIWindow::save_database_file()
{
CMCommand commandCall;
commandCall.request.command = CMD_SAVE;
if(_rosNodeThread->m_command_db_client.call(commandCall))
{
ROS_INFO("successfully saved db");
}
else
{
ROS_ERROR("failed to save db");
}
}
void MainGUIWindow::insert_or_update_entry_database(CrazyflieEntry entry)
{
CMUpdate updateCall;
updateCall.request.mode = ENTRY_INSERT_OR_UPDATE;
updateCall.request.crazyflieEntry = entry;
_rosNodeThread->m_update_db_client.call(updateCall);
}
int MainGUIWindow::read_database_from_file(CrazyflieDB &read_db)
{
// need to reload and then read?
CMRead getDBCall;
_rosNodeThread->m_read_db_client.waitForExistence(ros::Duration(-1));
if(_rosNodeThread->m_read_db_client.call(getDBCall))
{
m_data_base = getDBCall.response.crazyflieDB;
ROS_INFO_STREAM("database:\n" << m_data_base);
read_db = getDBCall.response.crazyflieDB;
return 0;
}
else
{
return -1;
}
}
void MainGUIWindow::on_load_from_DB_button_clicked()
{
CrazyflieDB tmp_db;
if(read_database_from_file(tmp_db) == 0)
{
ROS_INFO_STREAM("database:\n" << tmp_db);
m_data_base = tmp_db;
// TODO: update links table
cf_linker->clear_all_links();
for(int i = 0; i < m_data_base.crazyflieEntries.size(); i++)
......@@ -743,7 +829,7 @@ void MainGUIWindow::on_load_from_DB_button_clicked()
qreal width = m_data_base.crazyflieEntries[i].crazyflieContext.localArea.xmax - m_data_base.crazyflieEntries[i].crazyflieContext.localArea.xmin;
qreal height = m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymax - m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymin;
QRectF tmp_rect(m_data_base.crazyflieEntries[i].crazyflieContext.localArea.xmin * FROM_METERS_TO_UNITS,
m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymax * FROM_METERS_TO_UNITS,
- m_data_base.crazyflieEntries[i].crazyflieContext.localArea.ymax * FROM_METERS_TO_UNITS, // minus sign because qt has y-axis inverted
width * FROM_METERS_TO_UNITS,
height * FROM_METERS_TO_UNITS);
int found_j;
......@@ -762,7 +848,8 @@ void MainGUIWindow::on_load_from_DB_button_clicked()
}
else
{
scene->crazyfly_zones[found_j]->setRect(tmp_rect);
scene->crazyfly_zones[found_j]->setPos(tmp_rect.topLeft());
// scene->crazyfly_zones[found_j]->setRect(tmp_rect);
scene->crazyfly_zones[found_j]->rectSizeChanged();
}
int student_id = m_data_base.crazyflieEntries[i].studentID;
......
#include "rosNodeThread.h"
#include "d_fall_pps/CMRead.h"
#include "d_fall_pps/CMUpdate.h"
#include "d_fall_pps/CMCommand.h"
rosNodeThread::rosNodeThread(int argc, char** pArgv, const char * topic, QObject* parent)
......@@ -43,6 +45,8 @@ bool rosNodeThread::init()
// clients for db services:
m_read_db_client = nh.serviceClient<CMRead>("/CentralManagerService/Read", false);
m_update_db_client = nh.serviceClient<CMUpdate>("/CentralManagerService/Update", false);
m_command_db_client = nh.serviceClient<CMCommand>("/CentralManagerService/Command", false);
m_pThread->start();
return true;
......
3,cfTwo,0/69/2M,-2,-2,0,2,2,2
5,cfThree,0/72/2M,-2,-2,0,2,2,2
2,CF1,,0,-1.18203,0.803384,0,-0.372033,1.94338,0
1,CF2,,1,-1.70283,2.03901,0,-1.15283,2.96901,0
......@@ -91,7 +91,7 @@ void readCrazyflieDB(CrazyflieDB& db) {
vector<string> dataRow = nextLine(dbFile);
if(dataRow.size() == 0) {
} else if(dataRow.size() != 9) {
} else if(dataRow.size() != 10) {
ROS_ERROR_STREAM("row in csv file has not the right amount of data fields, skipped");
} else {
......@@ -103,14 +103,14 @@ void readCrazyflieDB(CrazyflieDB& db) {
context.crazyflieAddress = unescape(dataRow[2]);
AreaBounds area;
area.crazyfly_zone_index = stof(dataRow[3]);
area.xmin = stof(dataRow[4]);
area.ymin = stof(dataRow[5]);
area.zmin = stof(dataRow[6]);
area.xmin = stof(dataRow[3]);
area.ymin = stof(dataRow[4]);
area.zmin = stof(dataRow[5]);
area.xmax = stof(dataRow[6]);
area.ymax = stof(dataRow[7]);
area.zmax = stof(dataRow[8]);
area.xmax = stof(dataRow[7]);
area.ymax = stof(dataRow[8]);
area.zmax = stof(dataRow[9]);
context.localArea = area;
entry.crazyflieContext = context;
......@@ -133,6 +133,8 @@ void writeCrazyflieDB(CrazyflieDB& db) {
AreaBounds area = context.localArea;
dbFile << area.crazyfly_zone_index << ',';
dbFile << area.xmin << ',';
dbFile << area.ymin << ',';
dbFile << area.zmin << ',';
......
Supports Markdown
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