From 21472310d8a037702834835cbe638e25c3bdebc5 Mon Sep 17 00:00:00 2001 From: Yu Asabe Date: Fri, 1 Apr 2022 15:07:11 +0900 Subject: [PATCH 1/9] Create functions for DB manipulation --- include/autoware_v2x/cpm_application.hpp | 13 +++++++++++++ src/cpm_application.cpp | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/include/autoware_v2x/cpm_application.hpp b/include/autoware_v2x/cpm_application.hpp index ea3aab5..82fa2e0 100644 --- a/include/autoware_v2x/cpm_application.hpp +++ b/include/autoware_v2x/cpm_application.hpp @@ -7,6 +7,7 @@ #include #include "autoware_auto_perception_msgs/msg/predicted_objects.hpp" #include "autoware_v2x/positioning.hpp" +#include namespace v2x { @@ -28,6 +29,18 @@ namespace v2x void printObjectsList(int); void send(); + /** Creates the database schema. + * Creates tables for cpm_sent, cpm_received + */ + void createTables(); + + /** Inserts CPM into the database. + * Constructs and executes queries for inserting the specified CPM data. + * @param cpm The CPM to be inserted + * @param table_name The table to insert the CPM into (cpm_sent or cpm_received) + */ + void insertCpmToCpmTable(vanetza::asn1::Cpm, char* table_name); + struct Object { std::string uuid; int objectID; // 0-255 for CPM diff --git a/src/cpm_application.cpp b/src/cpm_application.cpp index 6cf4cf9..5a42302 100644 --- a/src/cpm_application.cpp +++ b/src/cpm_application.cpp @@ -561,4 +561,12 @@ namespace v2x { ++cpm_num_; } } + + void CpmApplication::createTables() { + + } + + void CpmApplication::insertCpmToCpmTable(vanetza::asn1::Cpm cpm, char* table_name) { + + } } \ No newline at end of file From a499172f1974e1f29df615cfd6065ffc2678b9ad Mon Sep 17 00:00:00 2001 From: Yu Asabe Date: Fri, 1 Apr 2022 15:33:00 +0900 Subject: [PATCH 2/9] Add db placeholder --- db/.gitplaceholder | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 db/.gitplaceholder diff --git a/db/.gitplaceholder b/db/.gitplaceholder new file mode 100644 index 0000000..e69de29 From 8673dc7b2353a83a1d73e28d8f3be65f72bc024e Mon Sep 17 00:00:00 2001 From: Yu Asabe Date: Fri, 1 Apr 2022 15:33:06 +0900 Subject: [PATCH 3/9] Add gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..008692a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +db/*.db \ No newline at end of file From 2db31ab9f0660d7bc1c891f41b2aa0413eb6be1b Mon Sep 17 00:00:00 2001 From: Yu Asabe Date: Fri, 1 Apr 2022 15:33:19 +0900 Subject: [PATCH 4/9] Add sqlite3 to target_link_libraries --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5227c07..f80c7f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,7 +45,7 @@ ament_auto_add_library(autoware_v2x SHARED src/security.cpp ) -target_link_libraries(autoware_v2x Vanetza::vanetza ${GeographicLib_LIBRARIES} Boost::thread) +target_link_libraries(autoware_v2x Vanetza::vanetza ${GeographicLib_LIBRARIES} Boost::thread sqlite3) rclcpp_components_register_node(autoware_v2x PLUGIN "v2x::V2XNode" From eb7790e7ce1727e7541ead889d09469149487677 Mon Sep 17 00:00:00 2001 From: Yu Asabe Date: Fri, 1 Apr 2022 15:33:41 +0900 Subject: [PATCH 5/9] Implement createTables --- src/cpm_application.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/cpm_application.cpp b/src/cpm_application.cpp index 5a42302..1394bdb 100644 --- a/src/cpm_application.cpp +++ b/src/cpm_application.cpp @@ -24,6 +24,8 @@ #include #include +#include + #define _USE_MATH_DEFINES #include @@ -49,6 +51,7 @@ namespace v2x { { RCLCPP_INFO(node_->get_logger(), "CpmApplication started. is_sender: %d", is_sender_); set_interval(milliseconds(100)); + createTables(); } void CpmApplication::set_interval(Clock::duration interval) { @@ -563,7 +566,39 @@ namespace v2x { } void CpmApplication::createTables() { + sqlite3 *db = NULL; + char* err = NULL; + int ret = sqlite3_open("./src/autoware_v2x/db/autoware_v2x.db", &db); + if (ret != SQLITE_OK) { + RCLCPP_INFO(node_->get_logger(), "DB File Open Error"); + return; + } + + char* sql_command; + + sql_command = "create table if not exists cpm_sent(id INTEGER PRIMARY KEY, timestamp INTEGER, perceivedObjectCount INTEGER);"; + + ret = sqlite3_exec(db, sql_command, NULL, NULL, &err); + if (ret != SQLITE_OK) { + RCLCPP_INFO(node_->get_logger(), "DB Execution Error"); + sqlite3_close(db); + sqlite3_free(err); + return; + } + + sql_command = "create table if not exists cpm_received(id INTEGER PRIMARY KEY, timestamp INTEGER, perceivedObjectCount INTEGER);"; + + ret = sqlite3_exec(db, sql_command, NULL, NULL, &err); + if (ret != SQLITE_OK) { + RCLCPP_INFO(node_->get_logger(), "DB Execution Error"); + sqlite3_close(db); + sqlite3_free(err); + return; + } + + sqlite3_close(db); + RCLCPP_INFO(node_->get_logger(), "CpmApplication::createTables Finished"); } void CpmApplication::insertCpmToCpmTable(vanetza::asn1::Cpm cpm, char* table_name) { From c0b38d46f30dd66a980052dae21f16502607427e Mon Sep 17 00:00:00 2001 From: Yu Asabe Date: Fri, 1 Apr 2022 16:37:53 +0900 Subject: [PATCH 6/9] Fix insertCpmToCpmTable --- include/autoware_v2x/cpm_application.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/autoware_v2x/cpm_application.hpp b/include/autoware_v2x/cpm_application.hpp index 82fa2e0..c345de7 100644 --- a/include/autoware_v2x/cpm_application.hpp +++ b/include/autoware_v2x/cpm_application.hpp @@ -39,7 +39,7 @@ namespace v2x * @param cpm The CPM to be inserted * @param table_name The table to insert the CPM into (cpm_sent or cpm_received) */ - void insertCpmToCpmTable(vanetza::asn1::Cpm, char* table_name); + void insertCpmToCpmTable(vanetza::asn1::Cpm, char*); struct Object { std::string uuid; From 8482cacc842d257012b3683a6b4662116b15a540 Mon Sep 17 00:00:00 2001 From: Yu Asabe Date: Fri, 1 Apr 2022 16:39:00 +0900 Subject: [PATCH 7/9] Implement insertCpmToCpmTable, Fix numberOfPerceivedObjects --- src/cpm_application.cpp | 44 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/cpm_application.cpp b/src/cpm_application.cpp index 1394bdb..143aa22 100644 --- a/src/cpm_application.cpp +++ b/src/cpm_application.cpp @@ -446,7 +446,6 @@ namespace v2x { management.stationType = StationType_passengerCar; management.referencePosition.latitude = ego_.latitude * 1e7; management.referencePosition.longitude = ego_.longitude * 1e7; - cpm.cpmParameters.numberOfPerceivedObjects = objectsList.size(); StationDataContainer_t *&sdc = cpm.cpmParameters.stationDataContainer; sdc = vanetza::asn1::allocate(); @@ -462,6 +461,8 @@ namespace v2x { ovc.heading.headingValue = heading; ovc.heading.headingConfidence = 1; + int perceivedObjectsCount = 0; + if (objectsList.size() > 0) { PerceivedObjectContainer_t *&poc = cpm.cpmParameters.perceivedObjectContainer; poc = vanetza::asn1::allocate(); @@ -471,6 +472,7 @@ namespace v2x { // RCLCPP_INFO(node_->get_logger(), "object.to_send: %d", object.to_send); if (object.to_send) { + PerceivedObject *pObj = vanetza::asn1::allocate(); // Update CPM-specific values for Object @@ -531,15 +533,23 @@ namespace v2x { object.to_send = false; // RCLCPP_INFO(node_->get_logger(), "Sending object: %s", object.uuid.c_str()); + + ++perceivedObjectsCount; + } else { // Object.to_send is set to False // RCLCPP_INFO(node_->get_logger(), "Object: %s not being sent.", object.uuid.c_str()); } } + + cpm.cpmParameters.numberOfPerceivedObjects =perceivedObjectsCount; + } else { cpm.cpmParameters.perceivedObjectContainer = NULL; // RCLCPP_INFO(node_->get_logger(), "[CpmApplication::send] Empty POC"); } + + insertCpmToCpmTable(message, (char*) "cpm_sent"); Application::DownPacketPtr packet{new DownPacket()}; std::unique_ptr payload{new geonet::DownPacket()}; @@ -577,7 +587,7 @@ namespace v2x { char* sql_command; - sql_command = "create table if not exists cpm_sent(id INTEGER PRIMARY KEY, timestamp INTEGER, perceivedObjectCount INTEGER);"; + sql_command = (char*) "create table if not exists cpm_sent(id INTEGER PRIMARY KEY, timestamp BIGINT, perceivedObjectCount INTEGER);"; ret = sqlite3_exec(db, sql_command, NULL, NULL, &err); if (ret != SQLITE_OK) { @@ -587,7 +597,7 @@ namespace v2x { return; } - sql_command = "create table if not exists cpm_received(id INTEGER PRIMARY KEY, timestamp INTEGER, perceivedObjectCount INTEGER);"; + sql_command = (char*) "create table if not exists cpm_received(id INTEGER PRIMARY KEY, timestamp BIGINT, perceivedObjectCount INTEGER);"; ret = sqlite3_exec(db, sql_command, NULL, NULL, &err); if (ret != SQLITE_OK) { @@ -602,6 +612,34 @@ namespace v2x { } void CpmApplication::insertCpmToCpmTable(vanetza::asn1::Cpm cpm, char* table_name) { + sqlite3 *db = NULL; + char* err = NULL; + int ret = sqlite3_open("./src/autoware_v2x/db/autoware_v2x.db", &db); + if (ret != SQLITE_OK) { + RCLCPP_INFO(node_->get_logger(), "DB File Open Error"); + return; + } + + int64_t timestamp = duration_cast(runtime_.now().time_since_epoch()).count(); + int perceivedObjectCount = 0; + if (cpm->cpm.cpmParameters.numberOfPerceivedObjects) { + perceivedObjectCount = cpm->cpm.cpmParameters.numberOfPerceivedObjects; + } + + std::stringstream sql_command; + + sql_command << "insert into " << table_name << " (timestamp, perceivedObjectCount) values (" << timestamp << ", " << perceivedObjectCount << ");"; + + ret = sqlite3_exec(db, sql_command.str().c_str(), NULL, NULL, &err); + if (ret != SQLITE_OK) { + RCLCPP_INFO(node_->get_logger(), "DB Execution Error"); + sqlite3_close(db); + sqlite3_free(err); + return; + } + + sqlite3_close(db); + // RCLCPP_INFO(node_->get_logger(), "CpmApplication::insertCpmToCpmTable Finished"); } } \ No newline at end of file From 8d09bfccf62cbf74e51593881745ebde8ef4bb6a Mon Sep 17 00:00:00 2001 From: Yu Asabe Date: Fri, 1 Apr 2022 16:42:16 +0900 Subject: [PATCH 8/9] Add insertCpmToCpmTable in indicate --- src/cpm_application.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cpm_application.cpp b/src/cpm_application.cpp index 143aa22..9c52167 100644 --- a/src/cpm_application.cpp +++ b/src/cpm_application.cpp @@ -169,6 +169,8 @@ namespace v2x { // RCLCPP_INFO(node_->get_logger(), "[INDICATE] Empty POC"); } + insertCpmToCpmTable(message, (char*) "cpm_received"); + if (reflect_packet_) { Application::DownPacketPtr packet{new DownPacket()}; std::unique_ptr payload{new geonet::DownPacket()}; From 87a3a8297cf36e0cda3d05802c64cb795bbd2e93 Mon Sep 17 00:00:00 2001 From: Yu Asabe Date: Wed, 6 Apr 2022 11:10:25 +0900 Subject: [PATCH 9/9] Change timestamp to unix, remove unnecessary logging --- src/cpm_application.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cpm_application.cpp b/src/cpm_application.cpp index 9c52167..2504f98 100644 --- a/src/cpm_application.cpp +++ b/src/cpm_application.cpp @@ -138,7 +138,7 @@ namespace v2x { if (poc != NULL) { for (int i = 0; i < poc->list.count; ++i) { - RCLCPP_INFO(node_->get_logger(), "[INDICATE] Object: #%d", poc->list.array[i]->objectID); + // RCLCPP_INFO(node_->get_logger(), "[INDICATE] Object: #%d", poc->list.array[i]->objectID); CpmApplication::Object object; double x1 = poc->list.array[i]->xDistance.value; @@ -427,7 +427,7 @@ namespace v2x { sending_ = true; - printObjectsList(cpm_num_); + // printObjectsList(cpm_num_); // RCLCPP_INFO(node_->get_logger(), "[CpmApplication::send] Sending CPM..."); @@ -623,7 +623,8 @@ namespace v2x { return; } - int64_t timestamp = duration_cast(runtime_.now().time_since_epoch()).count(); + int64_t timestamp = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + int perceivedObjectCount = 0; if (cpm->cpm.cpmParameters.numberOfPerceivedObjects) { perceivedObjectCount = cpm->cpm.cpmParameters.numberOfPerceivedObjects;