From 5c17356a75f6d48623e66d5ee0fdb441d7fe5c82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergi=20Hern=C3=A0ndez=20Juan?= <shernand@iri.upc.edu> Date: Mon, 20 Jun 2016 09:09:39 +0000 Subject: [PATCH] Added an attribute to chnage the new packet update rate and functions to handle it. --- src/can/virtual_can.cpp | 16 +++++++++++++++- src/can/virtual_can.h | 10 ++++++++++ src/examples/test_virtual_can_rx.cpp | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/can/virtual_can.cpp b/src/can/virtual_can.cpp index 22e0f97..22d4440 100755 --- a/src/can/virtual_can.cpp +++ b/src/can/virtual_can.cpp @@ -27,6 +27,7 @@ CVirtualCAN::CVirtualCAN(const std::string &comm_id) : CComm(comm_id) this->event_server->create_event(this->data_requested_event_id); this->finish_can_thread_event_id=comm_id + "_finish_can_thread_event_id"; this->event_server->create_event(this->finish_can_thread_event_id); + this->update_rate_hz=1000; } void CVirtualCAN::hard_open(void *comm_dev) @@ -94,7 +95,7 @@ int CVirtualCAN::hard_get_num_data(void) int CVirtualCAN::hard_wait_comm_event(void) { - usleep(1000);// publish can packets at 1KHz + usleep(1000000/this->update_rate_hz);// publish can packets at 1KHz return 1; } @@ -155,6 +156,19 @@ void CVirtualCAN::open(const std::string &dump_filename) this->config(); } +void CVirtualCAN::set_update_rate(double rate_hz) +{ + if(rate_hz<10.0 || rate_hz>10000.0) + throw CCommException(_HERE_,"Invalid update rate. Supported rate range between 10 Hz and 10 kHz.",this->comm_id); + else + this->update_rate_hz=rate_hz; +} + +double CVirtualCAN::get_update_rate(void) +{ + return this->update_rate_hz; +} + std::string CVirtualCAN::get_new_frame_event_id(void) { return this->new_frame_event_id; diff --git a/src/can/virtual_can.h b/src/can/virtual_can.h index 83283af..8eb81ec 100755 --- a/src/can/virtual_can.h +++ b/src/can/virtual_can.h @@ -46,6 +46,8 @@ class CVirtualCAN : protected CComm struct can_filter *rx_filters; unsigned int num_filters; CMutex can_access; + // desired rate + double update_rate_hz; protected: /** * \brief Function to actually open the device @@ -198,6 +200,14 @@ class CVirtualCAN : protected CComm * class. */ CVirtualCAN(const std::string &comm_id); + /** + * \brief + */ + void set_update_rate(double rate_hz); + /** + * \brief + */ + double get_update_rate(void); /** * \brief */ diff --git a/src/examples/test_virtual_can_rx.cpp b/src/examples/test_virtual_can_rx.cpp index cb0943c..ec8e5fe 100755 --- a/src/examples/test_virtual_can_rx.cpp +++ b/src/examples/test_virtual_can_rx.cpp @@ -28,6 +28,7 @@ int main(int argc,char *argv[]) events.push_back(can_port.get_new_frame_event_id()); try{ can_port.open(dump_file); + can_port.set_update_rate(100); can_port.add_id_filter(0x010,0x0F0,false); while(1) { -- GitLab