1 |
|
|
/** |
2 |
|
|
* \file |
3 |
|
|
* \brief Wrapper for reading new-line terminated list of values from serial port. |
4 |
|
|
* \author Julian Viereck |
5 |
|
|
* \date 2020 |
6 |
|
|
* \copyright Copyright (c) 2020, New York University and Max Planck |
7 |
|
|
* Gesellschaft. |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#pragma once |
11 |
|
|
|
12 |
|
|
#include <cstdlib> |
13 |
|
|
#include <iostream> |
14 |
|
|
#include <mutex> |
15 |
|
|
#include <unistd.h> |
16 |
|
|
#include <vector> |
17 |
|
|
|
18 |
|
|
#include "real_time_tools/thread.hpp" |
19 |
|
|
|
20 |
|
|
namespace blmc_drivers |
21 |
|
|
{ |
22 |
|
|
|
23 |
|
|
class SerialReader |
24 |
|
|
{ |
25 |
|
|
public: |
26 |
|
|
/** |
27 |
|
|
* @param serial_port The address of the serial port to use. |
28 |
|
|
* @pparam num_values The number of values to read in each line. |
29 |
|
|
*/ |
30 |
|
|
SerialReader(const std::string &serial_port, const int &num_values); |
31 |
|
|
|
32 |
|
|
~SerialReader(); |
33 |
|
|
|
34 |
|
|
/** |
35 |
|
|
* @brief Fills a vector with the latest values. |
36 |
|
|
* @param values Vector to place values into. |
37 |
|
|
* @return How often fill_vector was called without new data. |
38 |
|
|
*/ |
39 |
|
|
int fill_vector(std::vector<int>& values); |
40 |
|
|
|
41 |
|
|
private: |
42 |
|
|
/** |
43 |
|
|
* @brief This is the helper function used for spawning the real time |
44 |
|
|
* thread. |
45 |
|
|
* |
46 |
|
|
* @param instance_pointer is the current object in this case. |
47 |
|
|
* @return THREAD_FUNCTION_RETURN_TYPE depends on the current OS. |
48 |
|
|
*/ |
49 |
|
|
static THREAD_FUNCTION_RETURN_TYPE loop(void* instance_pointer) |
50 |
|
|
{ |
51 |
|
|
static_cast<SerialReader*>(instance_pointer)->loop(); |
52 |
|
|
return THREAD_FUNCTION_RETURN_VALUE; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* @brief This is the real time thread that streams the data to/from the |
57 |
|
|
* main board. |
58 |
|
|
*/ |
59 |
|
|
void loop(); |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* @brief This boolean makes sure that the loop is stopped upon destruction |
63 |
|
|
* of this object. |
64 |
|
|
*/ |
65 |
|
|
bool is_loop_active_; |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* @brief This is the thread object that allow to spwan a real-time thread |
69 |
|
|
* or not dependening on the current OS. |
70 |
|
|
*/ |
71 |
|
|
real_time_tools::RealTimeThread rt_thread_; |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* @brief Holds the device serial port. |
75 |
|
|
*/ |
76 |
|
|
int fd_; |
77 |
|
|
|
78 |
|
|
/** |
79 |
|
|
* @brief If false, the communication is workinng as expected. |
80 |
|
|
*/ |
81 |
|
|
bool has_error_; |
82 |
|
|
|
83 |
|
|
/** |
84 |
|
|
* @brief If the communication is active. |
85 |
|
|
*/ |
86 |
|
|
bool is_active_; |
87 |
|
|
|
88 |
|
|
/** |
89 |
|
|
* |
90 |
|
|
*/ |
91 |
|
|
int new_data_counter_; |
92 |
|
|
|
93 |
|
|
int missed_data_counter_; |
94 |
|
|
|
95 |
|
|
/** |
96 |
|
|
* @brief Holds vector with the latest double values. |
97 |
|
|
*/ |
98 |
|
|
std::vector<int> latest_values_; |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* @brief mutex_ multithreading safety |
102 |
|
|
*/ |
103 |
|
|
std::mutex mutex_; |
104 |
|
|
}; |
105 |
|
|
|
106 |
|
|
} // namespace blmc_robots |