diff --git a/src/can/virtual_can.cpp b/src/can/virtual_can.cpp index 22d444021626047debc8a8681045ea122e0b94a8..f3c0fff85b29215d2b6869b087cc8c907009aab6 100755 --- a/src/can/virtual_can.cpp +++ b/src/can/virtual_can.cpp @@ -5,6 +5,9 @@ #include <math.h> #include <errno.h> #include <sys/time.h> +#include <stdlib.h> /* srand, rand */ +#include <time.h> /* time */ +#include <iomanip> CVirtualCAN::CVirtualCAN(const std::string &comm_id) : CComm(comm_id) { @@ -12,6 +15,7 @@ CVirtualCAN::CVirtualCAN(const std::string &comm_id) : CComm(comm_id) this->req_can_id=-1; this->rx_filters=NULL; this->num_filters=0; + srand(time(NULL)); // initialize the thread attributes this->thread_server=CThreadServer::instance(); this->can_thread_id=comm_id + "_can_thread"; @@ -28,6 +32,7 @@ CVirtualCAN::CVirtualCAN(const std::string &comm_id) : CComm(comm_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; + this->err_rate = 0; } void CVirtualCAN::hard_open(void *comm_dev) @@ -137,9 +142,19 @@ void *CVirtualCAN::can_thread(void *param) else { //puch the new frame into the queue - can->rx_frames.push(new_frame); // activate the event - can->event_server->set_event(can->new_frame_event_id); + if ((rand() % 100 + 1) > can->err_rate) + { + can->rx_frames.push(new_frame); + can->event_server->set_event(can->new_frame_event_id); + } + else + { + std::cout << "lost : " << std::uppercase << std::hex << (int) new_frame.can_id << " "; + for (int ii = 0; ii < new_frame.can_dlc; ii++) + std::cout << std::uppercase << std::setw(2) << std::setfill('0') << std::hex << std::hex << (int) new_frame.data[ii] << " "; + std::cout << std::dec << std::endl; + } } num-=sizeof(struct can_frame); } @@ -307,6 +322,26 @@ void CVirtualCAN::clear_id_filters(void) this->num_filters=0; } } +void CVirtualCAN::set_error_rate(double err_rate) +{ + if (err_rate >= 0 && err_rate <= 100) + { + this->can_access.enter(); + this->err_rate = err_rate; + this->can_access.exit(); + } + else + throw CCommException(_HERE_,"Invalid error rate. It must be a value between 0 and 100",this->comm_id); +} + +double CVirtualCAN::get_error_rate(void) +{ + double err_rate; + this->can_access.enter(); + err_rate = this->err_rate; + this->can_access.exit(); + return err_rate; +} void CVirtualCAN::close(void) { diff --git a/src/can/virtual_can.h b/src/can/virtual_can.h index 8eb81ecc5d10a222025fb51122eda0f6d65bf693..741a6f37af30c70a74effd9127224e03a0095d39 100755 --- a/src/can/virtual_can.h +++ b/src/can/virtual_can.h @@ -48,6 +48,8 @@ class CVirtualCAN : protected CComm CMutex can_access; // desired rate double update_rate_hz; + // desired % of messages to "lost" + double err_rate; protected: /** * \brief Function to actually open the device @@ -208,6 +210,14 @@ class CVirtualCAN : protected CComm * \brief */ double get_update_rate(void); + /** + * \brief + */ + void set_error_rate(double err_rate); + /** + * \brief + */ + double get_error_rate(void); /** * \brief */ diff --git a/src/examples/CMakeLists.txt b/src/examples/CMakeLists.txt index 6829ae5e74bb0d2427f8a417b72b8be3d2637040..1ab4dba0d337b858b58b31eea339bc991d9f410b 100644 --- a/src/examples/CMakeLists.txt +++ b/src/examples/CMakeLists.txt @@ -69,3 +69,9 @@ ADD_EXECUTABLE(test_udp_tx test_simple_client_udp_tx.cpp) # edit the following line to add the necessary libraries TARGET_LINK_LIBRARIES(test_udp_tx ${IRIUTILS_LIBRARY} comm pthread) + +# edit the following line to add the source code for the example and the name of the executable +ADD_EXECUTABLE(test_candump test_candump.cpp) + +# edit the following line to add the necessary libraries +TARGET_LINK_LIBRARIES(test_candump ${IRIUTILS_LIBRARY} comm pthread) diff --git a/src/examples/test_virtual_can_rx.cpp b/src/examples/test_virtual_can_rx.cpp index ec8e5fe7ccb1356923b699c18be562f299b3f2ef..36e2e2b987f1f61a5d06ba6f70399db246b7a369 100755 --- a/src/examples/test_virtual_can_rx.cpp +++ b/src/examples/test_virtual_can_rx.cpp @@ -10,7 +10,7 @@ #include <linux/can/raw.h> #include <linux/can.h> -const std::string dump_file="/home/shernand/Work/pas/Sensors/radar/radar_308_idiada/candumpobjects.txt"; +const std::string dump_file="/home/alopez/candumpobjects.txt"; /** * \example test_can.cpp @@ -28,19 +28,20 @@ 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.set_update_rate(2800); + can_port.set_error_rate(1); can_port.add_id_filter(0x010,0x0F0,false); while(1) { event_server->wait_all(events); - std::cout << "[" << std::dec << can_port.get_last_rx_timestamp() << "]" << std::endl; + //std::cout << "[" << std::dec << can_port.get_last_rx_timestamp() << "]" << std::endl; can_port.read_frame(&can_id,data,&len); - std::cout << "can id: 0x" << std::hex << can_id << std::endl; + /*std::cout << "can id: 0x" << std::hex << can_id << std::endl; std::cout << "length: " << len << std::endl; std::cout << "data: "; for(i=0;i<len;i++) std::cout << "0x" << std::hex << (int)data[i] << ","; - std::cout << std::endl; + std::cout << std::endl;*/ } }catch(CCommException &e){ std::cout << e.what() << std::endl;