blmc_drivers
motor_board.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 
17 #include "real_time_tools/threadsafe/threadsafe_object.hpp"
18 #include "real_time_tools/threadsafe/threadsafe_timeseries.hpp"
21 
22 namespace blmc_drivers
23 {
24 
25 //==============================================================================
31 {
32 public:
33 
38 
45  MotorBoardCommand(uint32_t id, int32_t content)
46  {
47  id_ = id;
48  content_ = content;
49  }
50 
54  void print() const
55  {
56  rt_printf("command id: %d, content: %d\n", id_, content_);
57  }
58 
63  enum IDs
64  {
65  ENABLE_SYS = 1,
66  ENABLE_MTR1 = 2,
67  ENABLE_MTR2 = 3,
68  ENABLE_VSPRING1 = 4,
69  ENABLE_VSPRING2 = 5,
70  SEND_CURRENT = 12,
71  SEND_POSITION = 13,
72  SEND_VELOCITY = 14,
73  SEND_ADC6 = 15,
74  SEND_ENC_INDEX = 16,
75  SEND_ALL = 20,
76  SET_CAN_RECV_TIMEOUT = 30,
77  ENABLE_POS_ROLLOVER_ERROR = 31,
78  };
79 
83  enum Contents
84  {
85  ENABLE = 1,
86  DISABLE = 0
87  };
88 
92  uint32_t id_;
93 
97  int32_t content_;
98 };
99 
100 
101 
102 //==============================================================================
108 {
109 public:
117  uint8_t system_enabled:1;
118 
122  uint8_t motor1_enabled:1; // 1
123 
127  uint8_t motor1_ready:1; // 2
128 
132  uint8_t motor2_enabled:1; // 3
133 
137  uint8_t motor2_ready:1; // 4
138 
142  uint8_t error_code:3; // 5-7
143 
148  {
150  NONE = 0,
152  ENCODER = 1,
154  CAN_RECV_TIMEOUT = 2,
157  CRIT_TEMP = 3, // currently unused
159  POSCONV = 4,
161  POS_ROLLOVER = 5,
163  OTHER = 7
164  };
165 
169  void print() const
170  {
171  rt_printf("\tSystem enabled: %d\n", system_enabled);
172  rt_printf("\tMotor 1 enabled: %d\n", motor1_enabled);
173  rt_printf("\tMotor 1 ready: %d\n", motor1_ready);
174  rt_printf("\tMotor 2 enabled: %d\n", motor2_enabled);
175  rt_printf("\tMotor 2 ready: %d\n", motor2_ready);
176  rt_printf("\tError Code: %d\n", error_code);
177  }
178 
185  bool is_ready()
186  {
187  if(system_enabled &&
188  motor1_enabled &&
189  motor1_ready &&
190  motor2_enabled &&
191  motor2_ready &&
192  !error_code)
193  {
194  return true;
195  }
196  else
197  {
198  return false;
199  }
200  }
201 };
202 
203 
204 
205 //==============================================================================
210 {
211 public:
215  virtual ~MotorBoardInterface() {}
216 
220  typedef real_time_tools::ThreadsafeTimeseries<double> ScalarTimeseries;
224  typedef ScalarTimeseries::Index Index;
228  typedef real_time_tools::ThreadsafeTimeseries<Index> IndexTimeseries;
232  typedef real_time_tools::ThreadsafeTimeseries<MotorBoardStatus> StatusTimeseries;
236  typedef real_time_tools::ThreadsafeTimeseries<MotorBoardCommand> CommandTimeseries;
240  template<typename Type> using Ptr = std::shared_ptr<Type>;
244  template<typename Type> using Vector = std::vector<Type>;
245 
249  enum MeasurementIndex {current_0, current_1,
250  position_0, position_1,
251  velocity_0, velocity_1,
252  analog_0, analog_1,
253  encoder_index_0, encoder_index_1,
254  measurement_count};
255 
259  enum ControlIndex {current_target_0, current_target_1, control_count};
260 
272  virtual Ptr<const ScalarTimeseries> get_measurement(
273  const int& index) const = 0;
274 
281  virtual Ptr<const StatusTimeseries> get_status() const = 0;
282 
294  virtual Ptr<const ScalarTimeseries> get_control(const int& index) const = 0;
295 
302  virtual Ptr<const CommandTimeseries> get_command() const = 0;
303 
311  virtual Ptr<const ScalarTimeseries> get_sent_control(
312  const int& index) const = 0;
313 
320  virtual Ptr<const CommandTimeseries> get_sent_command() const = 0;
321 
333  virtual void set_control(const double& control, const int& index) = 0;
334 
341  virtual void set_command(const MotorBoardCommand& command) = 0;
342 
346  virtual void send_if_input_changed() = 0;
347 };
348 
358 template<typename Type>
359 std::vector<std::shared_ptr<Type> > create_vector_of_pointers(
360  const size_t& size, const size_t& length)
361 {
362  std::vector<std::shared_ptr<Type> > vector;
363  vector.resize(size);
364  for(size_t i = 0; i < size; i++)
365  {
366  vector[i] = std::make_shared<Type>(length);
367  }
368  return vector;
369 }
370 
371 //==============================================================================
377 {
378 public:
379 
386  CanBusMotorBoard(std::shared_ptr<CanBusInterface> can_bus,
387  const size_t& history_length = 1000,
388  const int &control_timeout_ms = 100);
389 
393  ~CanBusMotorBoard();
394 
395 
396 
408  virtual Ptr<const ScalarTimeseries> get_measurement(const int& index) const
409  {
410  return measurement_[index];
411  }
412 
419  {
420  return status_;
421  }
422 
430  virtual Ptr<const ScalarTimeseries> get_control(const int& index) const
431  {
432  return control_[index];
433  }
434 
442  {
443  return command_;
444  }
445 
453  const int& index) const
454  {
455  return control_[index];
456  }
457 
464  {
465  return sent_command_;
466  }
467 
478  virtual void set_control(const double& control, const int& index)
479  {
480  control_[index]->append(control);
481  }
482 
488  virtual void set_command(const MotorBoardCommand& command)
489  {
490  command_->append(command);
491  }
492 
496  virtual void send_if_input_changed();
497 
501  void wait_until_ready();
502 
503  bool is_ready();
504 
507  void pause_motors();
508 
512  void disable_can_recv_timeout();
513 
515 private:
516 
528  template<typename T> int32_t bytes_to_int32(T bytes)
529  {
530  return (int32_t) bytes[3] + ((int32_t)bytes[2] << 8) +
531  ((int32_t)bytes[1] << 16) + ((int32_t)bytes[0] << 24);
532  }
533 
540  float q24_to_float(int32_t qval)
541  {
542  return ((float)qval / (1 << 24));
543  }
544 
551  int32_t float_to_q24(float fval)
552  {
553  return ((int)(fval * (1 << 24)));
554  }
555 
563  template<typename T> float qbytes_to_float(T qbytes)
564  {
565  return q24_to_float(bytes_to_int32(qbytes));
566  }
567 
573  void send_newest_controls();
574 
579  void send_newest_command();
580 
588  static THREAD_FUNCTION_RETURN_TYPE loop(void* instance_pointer)
589  {
590  ((CanBusMotorBoard*)(instance_pointer))->loop();
591  return THREAD_FUNCTION_RETURN_VALUE;
592  }
593 
597  void loop();
598 
602  void print_status();
603 
604 private:
605 
609  std::shared_ptr<CanBusInterface> can_bus_;
610 
616  {
617  COMMAND_ID= 0x00,
618  IqRef = 0x05,
619  STATUSMSG = 0x10,
620  Iq = 0x20,
621  POS = 0x30,
622  SPEED = 0x40,
623  ADC6 = 0x50,
624  ENC_INDEX = 0x60
625  };
626 
635  Vector<Ptr<ScalarTimeseries>> measurement_;
636 
641 
649  Vector<Ptr<ScalarTimeseries>> control_;
650 
655 
663  Vector<Ptr<ScalarTimeseries>> sent_control_;
664 
669 
679 
685 
686 
692 
697  real_time_tools::RealTimeThread rt_thread_;
698 
699 };
700 
701 } // namespace blmc_drivers
MotorBoardCommand()
Construct a new MotorBoardCommand object.
Definition: motor_board.hpp:37
virtual ~MotorBoardInterface()
Destroy the MotorBoardInterface object.
Definition: motor_board.hpp:215
MotorBoardCommand(uint32_t id, int32_t content)
Construct a new MotorBoardCommand object.
Definition: motor_board.hpp:45
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: motor_board.hpp:697
real_time_tools::ThreadsafeTimeseries< Index > IndexTimeseries
A useful shortcut.
Definition: motor_board.hpp:228
uint8_t motor2_ready
Bits 4 checks if the motor 2 is ready or not.
Definition: motor_board.hpp:137
virtual Ptr< const CommandTimeseries > get_command() const
Get the commands to be sent.
Definition: motor_board.hpp:441
virtual Ptr< const ScalarTimeseries > get_measurement(const int &index) const
Getters.
Definition: motor_board.hpp:408
bool is_ready()
Check if the all status are green.
Definition: motor_board.hpp:185
MotorBoardInterface declares an API to inacte with a MotorBoard.
Definition: motor_board.hpp:209
int32_t content_
content_ is the value of teh command to be sent to the cards.
Definition: motor_board.hpp:97
std::vector< Type > Vector
A useful shortcut.
Definition: motor_board.hpp:244
uint8_t error_code
This encodes the error codes.
Definition: motor_board.hpp:142
virtual void set_command(const MotorBoardCommand &command)
Set the commands, see MotorBoardInterface::set_command.
Definition: motor_board.hpp:488
This namespace is the standard namespace of the package.
Definition: const_torque_control.cpp:12
bool motors_are_paused_
Are motor in idle mode = 0 torques? update this documentation with the actual behavior.
Definition: motor_board.hpp:684
virtual void set_control(const double &control, const int &index)
Setters.
Definition: motor_board.hpp:478
float q24_to_float(int32_t qval)
Convert from 24-bit normalized fixed-point to float.
Definition: motor_board.hpp:540
uint8_t motor2_enabled
Bits 3 enables/disable of the motor 2.
Definition: motor_board.hpp:132
#define rt_printf
Create a common type_def to wrap xenomai and posix.
Definition: os_interface.hpp:76
This MotorBoardCommand class is a data structurs that defines a command.
Definition: motor_board.hpp:30
Vector< Ptr< ScalarTimeseries > > measurement_
Outputs.
Definition: motor_board.hpp:635
real_time_tools::ThreadsafeTimeseries< MotorBoardCommand > CommandTimeseries
A useful shortcut.
Definition: motor_board.hpp:236
Vector< Ptr< ScalarTimeseries > > sent_control_
Log.
Definition: motor_board.hpp:663
ErrorCodes
This is the list of the error codes.
Definition: motor_board.hpp:147
virtual Ptr< const StatusTimeseries > get_status() const
Get the status of the CAN card.
Definition: motor_board.hpp:418
virtual Ptr< const CommandTimeseries > get_sent_command() const
Get the already sent commands.
Definition: motor_board.hpp:463
real_time_tools::ThreadsafeTimeseries< double > ScalarTimeseries
A useful shortcut.
Definition: motor_board.hpp:220
Contents
Is the different command status.
Definition: motor_board.hpp:83
float qbytes_to_float(T qbytes)
Converts from qbytes to float.
Definition: motor_board.hpp:563
bool is_loop_active_
Loop management.
Definition: motor_board.hpp:678
void print() const
Simply print the status of the motor board.
Definition: motor_board.hpp:169
uint8_t motor1_ready
Bits 2 checks if the motor 1 is ready or not.
Definition: motor_board.hpp:127
Ptr< CommandTimeseries > command_
This is the buffer of the commands to be sent to the card.
Definition: motor_board.hpp:654
int32_t float_to_q24(float fval)
Converts from float to 24-bit normalized fixed-point.
Definition: motor_board.hpp:551
ScalarTimeseries::Index Index
A useful shortcut.
Definition: motor_board.hpp:224
uint32_t id_
id_ is the command to be modifies on the card.
Definition: motor_board.hpp:92
std::shared_ptr< CanBusInterface > can_bus_
This is the pointer to the can bus to communicate with.
Definition: motor_board.hpp:609
int control_timeout_ms_
If no control is sent for more than control_timeout_ms_ the board will shut down. ...
Definition: motor_board.hpp:691
MeasurementIndex
This is the list of the measurement we can access.
Definition: motor_board.hpp:249
this class exists purely for logical reasons, it does not in itself implement anything.
Definition: device_interface.hpp:36
Ptr< StatusTimeseries > status_
This is the status history of the CAN board.
Definition: motor_board.hpp:640
std::vector< std::shared_ptr< Type > > create_vector_of_pointers(const size_t &size, const size_t &length)
Create a vector of pointers.
Definition: motor_board.hpp:359
Ptr< CommandTimeseries > sent_command_
This is the history of the already sent commands.
Definition: motor_board.hpp:668
void print() const
Display on a terminal the status of the message.
Definition: motor_board.hpp:54
std::shared_ptr< Type > Ptr
A useful shortcut.
Definition: motor_board.hpp:240
uint8_t motor1_enabled
Bits 1 enables/disable of the motor 1.
Definition: motor_board.hpp:122
int32_t bytes_to_int32(T bytes)
private methods ========================================================
Definition: motor_board.hpp:528
real_time_tools::ThreadsafeTimeseries< MotorBoardStatus > StatusTimeseries
A useful shortcut.
Definition: motor_board.hpp:232
virtual Ptr< const ScalarTimeseries > get_control(const int &index) const
Get the controls to be sent.
Definition: motor_board.hpp:430
This class represent a 8 bits message that describe the state (enable/disabled) of the card and the t...
Definition: motor_board.hpp:107
CanframeIDs
These are the frame IDs that define the kind of data we acquiere from the CAN bus.
Definition: motor_board.hpp:615
static THREAD_FUNCTION_RETURN_TYPE loop(void *instance_pointer)
This is the helper function used for spawning the real time thread.
Definition: motor_board.hpp:588
IDs
IDs are the different implemented commands that one can send to the MotorBoard.
Definition: motor_board.hpp:63
This class CanBusMotorBoard implements a MotorBoardInterface specific to CAN networks.
Definition: motor_board.hpp:376
Vector< Ptr< ScalarTimeseries > > control_
Inputs.
Definition: motor_board.hpp:649
ControlIndex
This is the list of the controls we can send.
Definition: motor_board.hpp:259
virtual Ptr< const ScalarTimeseries > get_sent_control(const int &index) const
Get the already sent controls.
Definition: motor_board.hpp:452
uint8_t system_enabled
These are the list of bits of the message.
Definition: motor_board.hpp:117