blmc_drivers
spi_bus.hpp
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include "master_board_sdk/master_board_interface.h"
17 #include "real_time_tools/thread.hpp"
18 #include "real_time_tools/threadsafe/threadsafe_timeseries.hpp"
19 
21 
22 namespace blmc_drivers
23 {
24 
25 class SpiBus: public DeviceInterface
26 {
27 
28 public:
41  SpiBus(
42  std::shared_ptr<MasterBoardInterface> main_board_interface,
43  const size_t& nb_udrivers, const size_t& history_length=1000);
44 
49  ~SpiBus();
50 
62  virtual std::shared_ptr<const MotorInterface::ScalarTimeseries>
64  const size_t udriver_id,
65  const MotorBoardInterface::MeasurementIndex& index) const ;
66 
71  virtual std::shared_ptr<const MotorBoardInterface::StatusTimeseries>
72  get_status(const size_t udriver_id) const;
73 
82  virtual std::shared_ptr<const MotorInterface::ScalarTimeseries> get_control(
83  const size_t udriver_id,
84  const MotorBoardInterface::ControlIndex& index) const;
85 
90  virtual std::shared_ptr<const MotorBoardInterface::CommandTimeseries>
91  get_command(const size_t udriver_id) const;
92 
97  virtual std::shared_ptr<const MotorInterface::ScalarTimeseries>
99  const size_t udriver_id,
100  const MotorBoardInterface::ControlIndex& index) const;
101 
106  virtual std::shared_ptr<const MotorBoardInterface::CommandTimeseries>
107  get_sent_command(const size_t udriver_id) const;
108 
117  virtual void set_control(const size_t udriver_id, const double& control,
118  const MotorBoardInterface::ControlIndex& index);
119 
124  virtual void set_command(const size_t udriver_id,
125  const MotorBoardCommand& command);
126 
133  virtual void send_if_input_changed();
134 
138  bool is_ready();
139 
143  void wait_until_ready();
144 
145 private:
157  static THREAD_FUNCTION_RETURN_TYPE loop(void* instance_pointer)
158  {
159  static_cast<SpiBus*>(instance_pointer)->loop();
160  return THREAD_FUNCTION_RETURN_VALUE;
161  }
162 
167  void loop();
168 
174  void send_newest_command();
175 
179  void send_newest_controls();
180 
189  std::shared_ptr<MasterBoardInterface> main_board_interface_;
190 
195  size_t nb_udrivers_;
196 
205  std::vector<std::shared_ptr<MotorInterface::ScalarTimeseries> > measurement_;
206 
210  std::vector<std::shared_ptr<MotorBoardInterface::StatusTimeseries> > status_;
211 
219  std::vector<std::shared_ptr<MotorInterface::ScalarTimeseries> > control_;
220 
224  std::vector<std::shared_ptr<MotorBoardInterface::CommandTimeseries> >command_;
225 
229 
237  std::vector<std::shared_ptr<MotorInterface::ScalarTimeseries> > sent_control_;
238 
242  std::vector<std::shared_ptr<MotorBoardInterface::CommandTimeseries> > sent_command_;
243 
253 
258 
264 
269  real_time_tools::RealTimeThread rt_thread_;
270 
281  std::array<bool, 12> motor_index_toggle_bits_;
282 };
283 
284 }
std::vector< std::shared_ptr< MotorBoardInterface::CommandTimeseries > > sent_command_
This is the history of the already sent commands.
Definition: spi_bus.hpp:242
std::vector< std::shared_ptr< MotorInterface::ScalarTimeseries > > control_
Inputs.
Definition: spi_bus.hpp:219
virtual void send_if_input_changed()
Actually send the commands and the controls.
Definition: spi_bus.cpp:152
bool is_ready()
return s only once board and motors are ready.
Definition: spi_bus.cpp:276
std::array< bool, 12 > motor_index_toggle_bits_
Everytime a motor index is detected the corresponding bit change for the opposite value...
Definition: spi_bus.hpp:281
This namespace is the standard namespace of the package.
Definition: const_torque_control.cpp:12
std::vector< std::shared_ptr< MotorBoardInterface::StatusTimeseries > > status_
This is the status history of the udriver board.
Definition: spi_bus.hpp:210
virtual std::shared_ptr< const MotorInterface::ScalarTimeseries > get_measurement(const size_t udriver_id, const MotorBoardInterface::MeasurementIndex &index) const
Output and status.
Definition: spi_bus.cpp:89
std::shared_ptr< MasterBoardInterface > main_board_interface_
Communication related attributes.
Definition: spi_bus.hpp:189
bool motors_are_paused_
Are motor in idle mode = 0 torques.
Definition: spi_bus.hpp:257
real_time_tools::RealTimeThread rt_thread_
This is the thread object that allow to spwan a real-time thread or not dependening on the current OS...
Definition: spi_bus.hpp:269
bool is_loop_active_
Loop management.
Definition: spi_bus.hpp:252
void wait_until_ready()
Wait until the robot is ready.
Definition: spi_bus.cpp:393
This MotorBoardCommand class is a data structurs that defines a command.
Definition: motor_board.hpp:30
virtual std::shared_ptr< const MotorBoardInterface::CommandTimeseries > get_sent_command(const size_t udriver_id) const
Get the sent commands.
Definition: spi_bus.cpp:130
~SpiBus()
Destroy the SpiBus object.
Definition: spi_bus.cpp:66
int control_timeout_ms_
If no control is sent for more than control_timeout_ms_ the board will shut down. ...
Definition: spi_bus.hpp:263
virtual void set_control(const size_t udriver_id, const double &control, const MotorBoardInterface::ControlIndex &index)
Setters.
Definition: spi_bus.cpp:139
SpiBus(std::shared_ptr< MasterBoardInterface > main_board_interface, const size_t &nb_udrivers, const size_t &history_length=1000)
Construct a new SpiBus object.
Definition: spi_bus.cpp:25
MeasurementIndex
This is the list of the measurement we can access.
Definition: motor_board.hpp:249
virtual std::shared_ptr< const MotorInterface::ScalarTimeseries > get_control(const size_t udriver_id, const MotorBoardInterface::ControlIndex &index) const
input logs
Definition: spi_bus.cpp:108
static THREAD_FUNCTION_RETURN_TYPE loop(void *instance_pointer)
Private methods.
Definition: spi_bus.hpp:157
this class exists purely for logical reasons, it does not in itself implement anything.
Definition: device_interface.hpp:36
size_t history_length_
history_length_ is the length of data buffers in number of iteration.
Definition: spi_bus.hpp:228
std::vector< std::shared_ptr< MotorInterface::ScalarTimeseries > > measurement_
Outputs.
Definition: spi_bus.hpp:205
virtual std::shared_ptr< const MotorInterface::ScalarTimeseries > get_sent_control(const size_t udriver_id, const MotorBoardInterface::ControlIndex &index) const
Get the sent controls.
Definition: spi_bus.cpp:122
virtual std::shared_ptr< const MotorBoardInterface::StatusTimeseries > get_status(const size_t udriver_id) const
Get the status of the motor board.
Definition: spi_bus.cpp:98
void loop()
This is the real time thread that streams the data to/from the main board.
Definition: spi_bus.cpp:292
size_t nb_udrivers_
nb_udrivers_ is the number of the udriver controlled by the main board.
Definition: spi_bus.hpp:195
virtual std::shared_ptr< const MotorBoardInterface::CommandTimeseries > get_command(const size_t udriver_id) const
Get the commands to be send.
Definition: spi_bus.cpp:116
std::vector< std::shared_ptr< MotorBoardInterface::CommandTimeseries > > command_
This is the buffer of the commands to be sent to the card.
Definition: spi_bus.hpp:224
std::vector< std::shared_ptr< MotorInterface::ScalarTimeseries > > sent_control_
Log.
Definition: spi_bus.hpp:237
Definition: spi_bus.hpp:25
virtual void set_command(const size_t udriver_id, const MotorBoardCommand &command)
set_command save the command internally.
Definition: spi_bus.cpp:146
ControlIndex
This is the list of the controls we can send.
Definition: motor_board.hpp:259
void send_newest_command()
Send the newest control stored in the time series.
Definition: spi_bus.cpp:184
void send_newest_controls()
Send the newest control stored in the time series.
Definition: spi_bus.cpp:252