blmc_drivers
can_bus.hpp
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include <memory>
11 #include <string>
12 
13 #include "real_time_tools/timer.hpp"
14 #include "real_time_tools/thread.hpp"
15 #include "real_time_tools/iostream.hpp"
16 #include "real_time_tools/spinner.hpp"
17 
18 #include "real_time_tools/threadsafe/threadsafe_object.hpp"
19 #include "real_time_tools/threadsafe/threadsafe_timeseries.hpp"
22 
23 
24 namespace blmc_drivers
25 {
26 
32 {
33 public:
37  std::array<uint8_t, 8> data;
41  uint8_t dlc;
46 
47  void print() const
48  {
49  rt_printf("---------------------------\n");
50  rt_printf("can bus frame data");
51  for(auto& datum : data)
52  {
53  rt_printf(" :%d", datum);
54  }
55  rt_printf("\n");
56 
57  rt_printf("id: %d\n", id);
58 
59  rt_printf("dlc: %d\n", dlc);
60 
61  rt_printf("---------------------------\n");
62  }
63 };
64 
70 {
71 public:
75  struct sockaddr_can send_addr;
79  int socket;
80 };
81 
87 {
88 public:
92  virtual ~CanBusInterface() {}
93 
94 
98  typedef real_time_tools::ThreadsafeTimeseries<CanBusFrame> CanframeTimeseries;
99 
109  virtual std::shared_ptr<const CanframeTimeseries> get_output_frame()
110  const = 0;
111 
117  virtual std::shared_ptr<const CanframeTimeseries> get_input_frame() = 0;
118 
124  virtual std::shared_ptr<const CanframeTimeseries> get_sent_input_frame() = 0;
125 
135  virtual void set_input_frame(const CanBusFrame& input_frame) = 0;
136 
144  virtual void send_if_input_changed() = 0;
145 };
146 
150 class CanBus: public CanBusInterface
151 {
152 public:
159  CanBus(const std::string& can_interface_name,
160  const size_t& history_length = 1000);
161 
165  virtual ~CanBus();
166 
176  std::shared_ptr<const CanframeTimeseries> get_output_frame() const
177  {
178  return output_;
179  }
180 
186  virtual std::shared_ptr<const CanframeTimeseries> get_input_frame()
187  {
188  return input_;
189  }
190 
196  virtual std::shared_ptr<const CanframeTimeseries> get_sent_input_frame()
197  {
198  return sent_input_;
199  }
200 
210  virtual void set_input_frame(const CanBusFrame& input_frame)
211  {
212  input_->append(input_frame);
213  }
214 
222  virtual void send_if_input_changed();
223 
227 private:
228 
237  static THREAD_FUNCTION_RETURN_TYPE loop(void* instance_pointer)
238  {
239  ((CanBus*)(instance_pointer))->loop();
240  return THREAD_FUNCTION_RETURN_VALUE;
241  }
242 
246  void loop();
247 
253  void send_frame(const CanBusFrame& unstamped_can_frame);
254 
260  CanBusFrame receive_frame();
261 
270  CanBusConnection setup_can(std::string name, uint32_t err_mask);
271 
280  real_time_tools::SingletypeThreadsafeObject<CanBusConnection, 1> can_connection_;
281 
286  std::shared_ptr<real_time_tools::ThreadsafeTimeseries<CanBusFrame> > input_;
287 
291  std::shared_ptr<real_time_tools::ThreadsafeTimeseries<CanBusFrame> > sent_input_;
292 
296  std::shared_ptr<real_time_tools::ThreadsafeTimeseries<CanBusFrame> > output_;
297 
303 
308  real_time_tools::RealTimeThread rt_thread_;
309 
313  std::string log_dir_;
314 
318  std::string name_;
319 };
320 
321 }
322 
CanBusConnection is a data structure that contains the hardware details for the connection between to...
Definition: can_bus.hpp:69
CanBusFrame is a class that contains a fixed sized amount of data to be send or received via the can ...
Definition: can_bus.hpp:31
std::array< uint8_t, 8 > data
data is the acutal data to be sent/received.
Definition: can_bus.hpp:37
uint8_t dlc
dlc is the size of the message.
Definition: can_bus.hpp:41
This namespace is the standard namespace of the package.
Definition: const_torque_control.cpp:12
static THREAD_FUNCTION_RETURN_TYPE loop(void *instance_pointer)
private attributes and methods
Definition: can_bus.hpp:237
virtual std::shared_ptr< const CanframeTimeseries > get_input_frame()
Get the input frame.
Definition: can_bus.hpp:186
std::shared_ptr< real_time_tools::ThreadsafeTimeseries< CanBusFrame > > sent_input_
sent_inupt_ is the list of the input already sent to the network.
Definition: can_bus.hpp:291
CanBus is the implementation of the CanBusInterface.
Definition: can_bus.hpp:150
canid_t can_id_t
Create a common type_def to wrap xenomai and posix.
Definition: os_interface.hpp:63
#define rt_printf
Create a common type_def to wrap xenomai and posix.
Definition: os_interface.hpp:76
real_time_tools::ThreadsafeTimeseries< CanBusFrame > CanframeTimeseries
CanframeTimeseries is a simple sohortcut.
Definition: can_bus.hpp:98
CanBusInterface is an abstract class that defines an API for the communication via Can bus...
Definition: can_bus.hpp:86
bool is_loop_active_
This boolean makes sure that the loop is not active upon destruction of the current object...
Definition: can_bus.hpp:302
can_id_t id
id is the id number return by the CAN bus.
Definition: can_bus.hpp:45
int socket
socket is the port through which the messages will be processed
Definition: can_bus.hpp:79
this class exists purely for logical reasons, it does not in itself implement anything.
Definition: device_interface.hpp:36
virtual ~CanBusInterface()
Destroy the CanBusInterface object.
Definition: can_bus.hpp:92
std::shared_ptr< const CanframeTimeseries > get_output_frame() const
Getters.
Definition: can_bus.hpp:176
virtual std::shared_ptr< const CanframeTimeseries > get_sent_input_frame()
Get the input frame thas has been sent.
Definition: can_bus.hpp:196
real_time_tools::SingletypeThreadsafeObject< CanBusConnection, 1 > can_connection_
Attributes.
Definition: can_bus.hpp:280
std::shared_ptr< real_time_tools::ThreadsafeTimeseries< CanBusFrame > > output_
output_ is the list of the frames received from the can network.
Definition: can_bus.hpp:296
virtual void set_input_frame(const CanBusFrame &input_frame)
Setters.
Definition: can_bus.hpp:210
real_time_tools::RealTimeThread rt_thread_
rt_thread_ is the thread object allowing us to spawn real-time threads.
Definition: can_bus.hpp:308
std::string name_
time_log_name is the name of the loggin
Definition: can_bus.hpp:318
std::shared_ptr< real_time_tools::ThreadsafeTimeseries< CanBusFrame > > input_
input_ is a list of time stamped frame to be send to the can network.
Definition: can_bus.hpp:286
std::string log_dir_
Log directory.
Definition: can_bus.hpp:313