qb_device_driver.h
Go to the documentation of this file.
1 /***
2  * Software License Agreement: BSD 3-Clause License
3  *
4  * Copyright (c) 2016-2018, qbrobotics®
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
8  * following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this list of conditions and the
11  * following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
14  * following disclaimer in the documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
17  * products derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef QB_DEVICE_DRIVER_H
29 #define QB_DEVICE_DRIVER_H
30 
31 // Standard libraries
32 #include <array>
33 
34 // internal libraries
35 #include <qbmove_communications.h>
36 
37 namespace qb_device_driver {
41 class qbDeviceAPI {
42  public:
51  virtual void activate(comm_settings *file_descriptor, const int &id, bool activate_command) {
52  commActivate(file_descriptor, id, static_cast<char>(activate_command));
53  ros::Duration(0.001).sleep();
54  }
55 
62  virtual void close(comm_settings *file_descriptor) {
63  closeRS485(file_descriptor);
64  }
65 
76  virtual int getCurrents(comm_settings *file_descriptor, const int &id, std::vector<short int> &currents) {
77  currents.resize(2);
78  return commGetCurrents(file_descriptor, id, currents.data());
79  }
80 
88  virtual int getDeviceIds(comm_settings *file_descriptor, std::array<char, 255> &device_ids) {
89  return RS485ListDevices(file_descriptor, device_ids.data());
90  }
91 
99  virtual std::string getInfo(comm_settings *file_descriptor, const int &id) {
100  char info[5000];
101  commGetInfo(file_descriptor, id, INFO_ALL, info); // actually only INFO_ALL is supported
102  return info;
103  }
104 
117  virtual void getParameters(comm_settings *file_descriptor, const int &id, std::vector<int> &input_mode, std::vector<int> &control_mode, std::vector<int> &resolutions, std::vector<int> &limits) {
118  unsigned char parameter_buffer[5000];
119  commGetParamList(file_descriptor, id, 0, NULL, 0, 0, parameter_buffer);
120  ros::Duration(0.001).sleep(); // unexpected behaviour with no sleep
121 
122  getParameter<uint8_t>(5, parameter_buffer, input_mode); // hardcoded input mode parameter id
123  getParameter<uint8_t>(6, parameter_buffer, control_mode); // hardcoded control mode parameter id
124  getParameter<uint8_t>(7, parameter_buffer, resolutions); // hardcoded encoder resolutions parameter id
125  getParameter<int32_t>(11, parameter_buffer, limits); // hardcoded position limits parameter id
126  }
127 
142  virtual int getMeasurements(comm_settings *file_descriptor, const int &id, std::vector<short int> &measurements) {
143  measurements.resize(5);
144  return commGetCurrAndMeas(file_descriptor, id, measurements.data());
145  }
146 
158  virtual int getPositions(comm_settings *file_descriptor, const int &id, std::vector<short int> &positions) {
159  positions.resize(3);
160  return commGetMeasurements(file_descriptor, id, positions.data());
161  }
162 
170  virtual int getSerialPorts(std::array<char[255], 10> &serial_ports) {
171  return RS485listPorts(serial_ports.data());
172  }
173 
182  virtual bool getStatus(comm_settings *file_descriptor, const int &id, bool &activate_status) {
183  char status;
184  int result = commGetActivate(file_descriptor, id, &status);
185  activate_status = status != 0;
186  return result == 0;
187  }
188 
197  virtual bool getStatus(comm_settings *file_descriptor, const int &id) {
198  char status;
199  return commGetActivate(file_descriptor, id, &status) == 0;
200  }
201 
209  virtual void open(comm_settings *file_descriptor, const std::string &serial_port) {
210  openRS485(file_descriptor, serial_port.c_str());
211  }
212 
223  virtual int setCommandsAndWait(comm_settings *file_descriptor, const int &id, std::vector<short int> &commands) {
224  ROS_ASSERT(commands.size() == 2);
225  return commSetInputsAck(file_descriptor, id, commands.data());
226  }
227 
238  virtual int setCommandsAsync(comm_settings *file_descriptor, const int &id, std::vector<short int> &commands) {
239  ROS_ASSERT(commands.size() == 2);
240  commSetInputs(file_descriptor, id, commands.data());
241  ros::Duration(0.00001).sleep(); // several calls may introduce serial communication errors without a wait (setCommandsAndWait is a more reliable tool)
242  return 0;
243  }
244 
252  virtual int setPID(comm_settings *file_descriptor, const int &id, std::vector<float> &pid) {
253  ROS_ASSERT(pid.size() == 3);
254  commGetParamList(file_descriptor, id, 2, pid.data(), sizeof(float), pid.size(), NULL); // actually set the parameter (despite of its name)
255  return 0;
256  }
257 
258  private:
266  template<class T>
267  void getParameter(const int &parameter_id, unsigned char *parameter_buffer, std::vector<int> &parameter_vector) {
268  parameter_vector.clear();
269  int number_of_values = parameter_buffer[(parameter_id-1)*PARAM_BYTE_SLOT + 7];
270  int value_size = sizeof(T);
271  for (int i=0; i<number_of_values; i++) {
272  T parameter_field = 0;
273  for (int j=0; j<value_size; j++) {
274  parameter_field += parameter_buffer[(parameter_id-1)*PARAM_BYTE_SLOT + 8 + i*value_size + value_size - j - 1] << (8 * j);
275  }
276  parameter_vector.push_back(parameter_field);
277  }
278  }
279 
287  template<class T>
288  void getParameter(const int &parameter_id, unsigned char *parameter_buffer, std::vector<float> &parameter_vector) {
289  parameter_vector.clear();
290  int number_of_values = parameter_buffer[(parameter_id-1)*PARAM_BYTE_SLOT + 7];
291  int value_size = sizeof(T);
292  for (int i=0; i<number_of_values; i++) {
293  std::vector<uint8_t> parameter_field(value_size, 0);
294  for (int j=0; j<value_size; j++) {
295  parameter_field.at(j) = parameter_buffer[(parameter_id-1)*PARAM_BYTE_SLOT + 8 + i*value_size + value_size - j - 1];
296  }
297  parameter_vector.push_back(*(reinterpret_cast<T*>(parameter_field.data())));
298  }
299  }
300 };
301 typedef std::shared_ptr<qbDeviceAPI> qbDeviceAPIPtr;
302 } // namespace qb_device_driver
303 
304 #endif // QB_DEVICE_DRIVER_H
int commGetCurrents(comm_settings *comm_settings_t, int id, short int currents[2])
This function gets currents from a qbMove or a qbHand connected to the serial port.
void openRS485(comm_settings *comm_settings_t, const char *port_s, int BAUD_RATE=B2000000)
This function is used to open a serial port for using with the qbMove or the qbHand.
virtual int getPositions(comm_settings *file_descriptor, const int &id, std::vector< short int > &positions)
Retrieve the motor positions of the given device.
int RS485ListDevices(comm_settings *comm_settings_t, char list_of_ids[255])
This function is used to list the number of devices connected to the serial port and get their relati...
void closeRS485(comm_settings *comm_settings_t)
This function is used to close a serial port being used with the qbMove or an qbHand.
This class wraps the qbrobotics device-independent API to easily use it within the Communication Hand...
#define INFO_ALL
All system information.
Definition: commands.h:257
bool sleep() const
int commGetParamList(comm_settings *comm_settings_t, int id, unsigned short index, void *values, unsigned short value_size, unsigned short num_of_values, uint8_t *buffer)
This function gets all the parameters that are stored in the qbMove or qbHand memory and sets one of ...
int commGetInfo(comm_settings *comm_settings_t, int id, short int info_type, char *info)
This function is used to ping the qbMove or the qbHand and get information about the device...
int commGetCurrAndMeas(comm_settings *comm_settings_t, int id, short int *values)
This function gets currents and position measurements from a qbMove or a qbHand connected to the seri...
virtual int getSerialPorts(std::array< char[255], 10 > &serial_ports)
Retrieve the list of serial ports connected to the system.
void getParameter(const int &parameter_id, unsigned char *parameter_buffer, std::vector< int > &parameter_vector)
Extract the specified parameter from the given buffer where all the device parameters are stored...
virtual bool getStatus(comm_settings *file_descriptor, const int &id)
Retrieve the connection status of the given device.
#define PARAM_BYTE_SLOT
Definition: commands.h:247
virtual void close(comm_settings *file_descriptor)
Close the communication with all the devices connected to the serial port relative to the given file ...
virtual void open(comm_settings *file_descriptor, const std::string &serial_port)
Open the serial communication on the given serial port.
virtual int setPID(comm_settings *file_descriptor, const int &id, std::vector< float > &pid)
Set the position control PID parameters of the given device, temporarily (until power off)...
virtual void getParameters(comm_settings *file_descriptor, const int &id, std::vector< int > &input_mode, std::vector< int > &control_mode, std::vector< int > &resolutions, std::vector< int > &limits)
Retrieve some of the parameters from the given device.
virtual bool getStatus(comm_settings *file_descriptor, const int &id, bool &activate_status)
Retrieve the motor activation status of the given device.
virtual std::string getInfo(comm_settings *file_descriptor, const int &id)
Retrieve the printable configuration setup of the given device.
void commSetInputs(comm_settings *comm_settings_t, int id, short int inputs[])
This function send reference inputs to a qbMove or a qbHand connected to the serial port...
Library of functions for SERIAL PORT communication with a qbMove.
virtual int setCommandsAndWait(comm_settings *file_descriptor, const int &id, std::vector< short int > &commands)
Send the reference command to the motors of the given device and wait for acknowledge.
virtual int getMeasurements(comm_settings *file_descriptor, const int &id, std::vector< short int > &measurements)
Retrieve the motor positions of the given device.
void getParameter(const int &parameter_id, unsigned char *parameter_buffer, std::vector< float > &parameter_vector)
Extract the specified parameter from the given buffer where all the device parameters are stored...
virtual int setCommandsAsync(comm_settings *file_descriptor, const int &id, std::vector< short int > &commands)
Send the reference command to the motors of the given device in a non-blocking fashion.
int RS485listPorts(char list_of_ports[10][255])
This function is used to return a list of available serial ports.
virtual int getCurrents(comm_settings *file_descriptor, const int &id, std::vector< short int > &currents)
Retrieve the motor currents of the given device.
int commGetActivate(comm_settings *comm_settings_t, int id, char *activate)
This function gets the activation status of a qbMove or a qbHand connected to the serial port...
virtual void activate(comm_settings *file_descriptor, const int &id, bool activate_command)
Activate (or deactivate, according to the given command) the motors of the device connected to the se...
virtual int getDeviceIds(comm_settings *file_descriptor, std::array< char, 255 > &device_ids)
Retrieve the list of devices connected to the serial port of the given file descriptor.
#define ROS_ASSERT(cond)
int commGetMeasurements(comm_settings *comm_settings_t, int id, short int measurements[3])
This function gets position measurements from a qbMove or a qbHand connected to the serial port...
int commSetInputsAck(comm_settings *comm_settings_t, int id, short int inputs[])
This function send reference inputs to a qbMove or a qbHand connected to the serial port and expects ...
std::shared_ptr< qbDeviceAPI > qbDeviceAPIPtr
void commActivate(comm_settings *comm_settings_t, int id, char activate)
This function activates or deactivates a qbMove or a qbHand connected to the serial port...


qb_device_driver
Author(s): qbrobotics®
autogenerated on Thu Jun 6 2019 19:46:35