qbsofthand2_research_api.cpp
Go to the documentation of this file.
2 #include <iostream>
3 
4 using namespace qbrobotics_research_api;
5 
6 void qbSoftHand2MotorsResearch::Params::initParams(const std::vector<int8_t> &param_buffer) {
7  Device::Params::initParams(param_buffer);
8  // Legacy firmware needs inverted signs for second motor encoders (encoder 3 and encoder 4)
9  std::for_each(encoder_offsets.begin()+2, encoder_offsets.end(), [](int16_t &x){ x *= -1; } );
10 
11  getParameter<uint8_t>(25, param_buffer, hand_side);
12 }
13 
14 qbSoftHand2MotorsResearch::qbSoftHand2MotorsResearch(std::shared_ptr<Communication> communication, std::string name, std::string serial_port, uint8_t id)
15  : Device(std::move(communication), std::move(name), std::move(serial_port), id, true, std::make_unique<qbSoftHand2MotorsResearch::Params>()) {}
16 
17 qbSoftHand2MotorsResearch::qbSoftHand2MotorsResearch(std::shared_ptr<Communication> communication, std::string name, std::string serial_port, uint8_t id, bool init_params)
18  : Device(std::move(communication), std::move(name), std::move(serial_port), id, init_params, std::make_unique<qbSoftHand2MotorsResearch::Params>()) {}
19 
20 /*------ Methods for 6.X.X firmware ------*/
22  int set_fail = Device::setMotorStates(motor_state);
23  std::this_thread::sleep_for(std::chrono::milliseconds(1));
24  return set_fail;
25 }
26 
27 int qbSoftHand2MotorsResearch::setParameter(uint16_t param_type, const std::vector<int8_t> &param_data) {
28  int set_fail = Device::setParameter(param_type, param_data);
29  std::this_thread::sleep_for(std::chrono::milliseconds(1));
30  if (!set_fail && param_type != 1) { // WARN: the storeUserDataMemory for ID change is called by the specific method
31  set_fail = Device::storeUserDataMemory(); // WARN: a store user memory is required on legacy devices!
32  }
33  std::this_thread::sleep_for(std::chrono::milliseconds(1));
34  return set_fail;
35 }
36 
38  int set_fail = setParameter(25, Communication::vectorSwapAndCast<int8_t, uint8_t>({hand_side}));
39  return set_fail;
40 }
41 
42 int qbSoftHand2MotorsResearch::setParamId(uint8_t id) { // old method only for legacy devices
43  uint8_t previous_id = params_->id;
44  int set_fail = Device::setParamId(id);
45  std::this_thread::sleep_for(std::chrono::milliseconds(1));
46  if (!set_fail) {
47  params_->id = previous_id; // WARN: need to send the command to the previous ID on legacy devices!
48  set_fail = Device::storeUserDataMemory(); // WARN: a store user memory is required on legacy devices!
49  params_->id = id;
50  }
51  std::this_thread::sleep_for(std::chrono::milliseconds(1));
52  return set_fail;
53 }
54 
55 int qbSoftHand2MotorsResearch::setParamZeros() { // old method only for legacy devices
56  std::vector<int16_t> positions;
57  if (getPositions(positions)) {
58  return -1;
59  }
60  std::this_thread::sleep_for(std::chrono::milliseconds(200));
61  std::vector<int16_t> offsets;
62  if (getParamEncoderOffsets(offsets)) {
63  return -1;
64  }
65  std::this_thread::sleep_for(std::chrono::milliseconds(200));
66  std::transform(offsets.begin(), offsets.end(), positions.begin(), offsets.begin(), std::minus<int16_t>());
67  return setParamEncoderOffsets(offsets);
68 }
69 
70 /*------ Methods for SoftHand 2 ------*/
71 
73  return getParamEncoderOffsets(params_->encoder_offsets);
74 }
75 
76 int qbSoftHand2MotorsResearch::getParamEncoderOffsets(std::vector<int16_t> &encoder_offsets) {
77  int set_fail = Device::getParamEncoderOffsets(encoder_offsets);
78  // Legacy firmware needs inverted signs for second motor encoders (encoder 3 and encoder 4)
79  std::for_each( encoder_offsets.begin()+2, encoder_offsets.end(), [](int16_t &x){x *= -1;} );
80  return set_fail;
81 }
82 
84  return getParamHandSide(std::dynamic_pointer_cast<qbrobotics_research_api::qbSoftHand2MotorsResearch::Params>(params_)->hand_side);
85 }
86 
88  std::vector<int8_t> param_buffer;
89  if (getParameters(param_buffer) != 0) {
90  return -1;
91  }
92  Params::getParameter<uint8_t>(25, param_buffer, hand_side);
93  return 0;
94 }
95 
96 int qbSoftHand2MotorsResearch::getSynergies(std::vector<int16_t> &synergies) {
97  std::vector<int8_t> data_in;
98  if (communication_->sendCommandAndParse(serial_port_, params_->id, CMD_GET_SYNERGIES, data_in) < 0) {
99  return -1;
100  }
101  synergies = Communication::vectorCastAndSwap<int16_t>(data_in);
102  return 0;
103 }
104 
105 int qbSoftHand2MotorsResearch::setParamEncoderOffsets(const std::vector<int16_t> &encoder_offsets) {
106  std::vector<int16_t> legacy_offsets = encoder_offsets;
107  // Legacy firmware needs inverted signs for second motor encoders (encoder 3 and encoder 4)
108  std::for_each( legacy_offsets.begin()+2, legacy_offsets.end(), [](int16_t &x){ x *= -1; } );
109  return Device::setParamEncoderOffsets(legacy_offsets);
110 }
111 
112 int qbSoftHand2MotorsResearch::setAdditiveSynergiesReferences(int16_t synergy_1, int16_t synergy_2) {
113  auto const data_out = Communication::vectorSwapAndCast<int8_t, int16_t>({synergy_1, synergy_2});
114  if (communication_->sendCommand(serial_port_, params_->id, CMD_SET_SYNERGIES, data_out) < 0) {
115  return -1;
116  }
117  return 0;
118 }
119 
121  if(synergy_1 < 0 || synergy_1 > 1 || synergy_2 < -1 || synergy_2 > 1) {
122  return -2;
123  }
124  int32_t l = getParams()->position_limits[0];
125  int32_t L = getParams()->position_limits[1];
126  int32_t Lr = (L + l);
127  float Sf = (2/(float)Lr);
128  float mf = (1/(float)l);
129  // multiplicative synergies formulas
130  int16_t q1 = (1 + synergy_2)*synergy_1/Sf - synergy_2/mf;
131  int16_t q2 = (1 - synergy_2)*synergy_1/Sf + synergy_2/mf;
132 
133  std::vector<int16_t> refs{q1, q2};
134 
135  setControlReferences(refs);
136 }
137 
139  // Get the params to return to home position
140  std::vector<int16_t> synergies;
141  int result = getSynergies(synergies);
142  std::vector<int32_t> limits;
143  getParamPositionLimits(limits);
144 
145  if(synergies.size() <= 0 || result < 0 || limits.size() < 4){ // something went wrong
146  return result;
147  }
148  std::vector<std::vector<int>> synergies_references{{limits.at(0), 0}, {synergies.at(0), 0}}; //|synergies references to cancel synergy 1 | synergies references to cancel synergy 2|
149  std::vector<int> synergies_to_reach {-1000, 200};
150 
151  int synergy_to_cancel = 1;
152  while(!synergies_references.empty()) {
153  if(setAdditiveSynergiesReferences(synergies_references.back().at(0), synergies_references.back().at(1)) != 0){ // cancel the contribution of synergies
154  return -1;
155  }
156  auto t_start = std::chrono::high_resolution_clock::now();
157  auto t_end = t_start;
158  double elapsed_time_ms;
159  bool condition = true;
160  while(condition){
161  getSynergies(synergies);
162  if(synergy_to_cancel == 1) {
163  condition = std::abs(synergies.at(synergy_to_cancel)) > synergies_to_reach.back();
164  } else {
165  condition = synergies.at(synergy_to_cancel) > synergies_to_reach.back();
166  }
167  //condition = synergy_to_cancel == 1 ? std::abs(synergies.at(synergy_to_cancel)) > synergies_to_reach.back() : synergies.at(synergy_to_cancel) > synergies_to_reach.back();
168  std::this_thread::sleep_for(std::chrono::milliseconds(1)); //sleep 1 ms in order to not clog the communication with the device
169  t_end = std::chrono::high_resolution_clock::now();
170  elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end-t_start).count();
171  if(elapsed_time_ms > 2000.0) {
172  result = -2;
173  break;
174  }
175  }
176  synergies_references.pop_back();
177  synergies_to_reach.pop_back();
178  synergy_to_cancel --;
179  }
180  if(setControlReferences({0, 0}) != 0){
181  return -1;
182  }
183  return result;
184 }
qbrobotics_research_api::Device::setParameter
virtual int setParameter(uint16_t param_type, const std::vector< int8_t > &param_data)
Set the Parameter specified by param_type with the values stored in param_data.
Definition: qbrobotics_research_api.cpp:941
qbrobotics_research_api::Device::getParameters
virtual int getParameters(std::vector< int8_t > &param_buffer)
Get the parameters from the device.
Definition: qbrobotics_research_api.cpp:759
qbrobotics_research_api::qbSoftHand2MotorsResearch::Params::initParams
void initParams(const std::vector< int8_t > &param_buffer) override
Definition: qbsofthand2_research_api.cpp:6
qbrobotics_research_api::Device::params_
std::shared_ptr< Params > params_
Definition: qbrobotics_research_api.h:852
qbrobotics_research_api::qbSoftHand2MotorsResearch::setParamId
int setParamId(uint8_t id) override
Set the ID of the device.
Definition: qbsofthand2_research_api.cpp:42
qbrobotics_research_api::qbSoftHand2MotorsResearch::Params
Definition: qbsofthand2_research_api.h:10
qbrobotics_research_api::qbSoftHand2MotorsResearch::setParamHandSide
int setParamHandSide(uint8_t hand_side)
Set the hand_side parameter for RIGHT or LEFT Hand for SoftHand 2.
Definition: qbsofthand2_research_api.cpp:37
qbrobotics_research_api::qbSoftHand2MotorsResearch::qbSoftHand2MotorsResearch
qbSoftHand2MotorsResearch(std::shared_ptr< Communication > communication, std::string name, std::string serial_port, uint8_t id)
Definition: qbsofthand2_research_api.cpp:14
qbrobotics_research_api::Device::setParamEncoderOffsets
virtual int setParamEncoderOffsets(const std::vector< int16_t > &encoder_offsets)
Set the encoder offsets parameters of the device.
Definition: qbrobotics_research_api.cpp:1015
qbrobotics_research_api::qbSoftHand2MotorsResearch::getSynergies
int getSynergies(std::vector< int16_t > &synergies)
Get the synergies values of the SoftHand 2.
Definition: qbsofthand2_research_api.cpp:96
qbrobotics_research_api::Device::getParams
std::shared_ptr< Params > getParams()
Definition: qbrobotics_research_api.h:844
qbrobotics_research_api::qbSoftHand2MotorsResearch::setMultiplicativeSynergiesReferences
int setMultiplicativeSynergiesReferences(float synergy_1, float synergy_2)
Set the Multiplicative Synergies References to SoftHand 2.
Definition: qbsofthand2_research_api.cpp:120
qbrobotics_research_api
Definition: qbmove_research_api.h:33
qbrobotics_research_api::Device::getParamPositionLimits
virtual int getParamPositionLimits()
Update the position limits parameters in the class variable param_.
Definition: qbrobotics_research_api.cpp:902
qbrobotics_research_api::qbSoftHand2MotorsResearch::getParamHandSide
int getParamHandSide()
Update the SoftHand 2 hand_side parameter in the class variable param_.
Definition: qbsofthand2_research_api.cpp:83
qbrobotics_research_api::Device::setMotorStates
virtual int setMotorStates(bool motor_state)
Activate or deactivate the motor(s)
Definition: qbrobotics_research_api.cpp:737
qbrobotics_research_api::Device::storeUserDataMemory
virtual int storeUserDataMemory()
Store the changed parameters on 7.X.X firmware devices in user memory.
Definition: qbrobotics_research_api.cpp:1124
qbrobotics_research_api::Device::serial_port_
std::string serial_port_
Definition: qbrobotics_research_api.h:851
qbrobotics_research_api::Device::getParamEncoderOffsets
virtual int getParamEncoderOffsets()
Update the device encoder offsets in the class variable param_.
Definition: qbrobotics_research_api.cpp:863
qbrobotics_research_api::Device
General class that allow to get/set parameters from/to qbrobotics devices.
Definition: qbrobotics_research_api.h:268
qbrobotics_research_api::Device::communication_
std::shared_ptr< Communication > communication_
Definition: qbrobotics_research_api.h:853
qbrobotics_research_api::qbSoftHand2MotorsResearch::setParamZeros
int setParamZeros() override
Set motor(s) zero(s) to actual encoder reading.
Definition: qbsofthand2_research_api.cpp:55
qbsofthand2_research_api.h
qbrobotics_research_api::Device::Params::encoder_offsets
std::vector< int16_t > encoder_offsets
Definition: qbrobotics_research_api.h:306
qbrobotics_research_api::qbSoftHand2MotorsResearch::setAdditiveSynergiesReferences
int setAdditiveSynergiesReferences(int16_t synergy_1, int16_t synergy_2)
Set the Additive Synergies References to SoftHand 2.
Definition: qbsofthand2_research_api.cpp:112
qbrobotics_research_api::qbSoftHand2MotorsResearch::getParamEncoderOffsets
int getParamEncoderOffsets() override
Update the SoftHand 2 encoder offsets in the class variable param_.
Definition: qbsofthand2_research_api.cpp:72
qbrobotics_research_api::Device::setParamId
virtual int setParamId(uint8_t id)
Set the ID of the device.
Definition: qbrobotics_research_api.cpp:959
qbrobotics_research_api::qbSoftHand2MotorsResearch
SoftHand 2 class that allows to control and get/set parameters.
Definition: qbsofthand2_research_api.h:8
qbrobotics_research_api::qbSoftHand2MotorsResearch::setParameter
int setParameter(uint16_t param_type, const std::vector< int8_t > &param_data) override
Set the Parameter specified by param_type with the values stored in param_data.
Definition: qbsofthand2_research_api.cpp:27
CMD_SET_SYNERGIES
@ CMD_SET_SYNERGIES
Command to set the synergies to SoftHand 2.
Definition: qbrobotics_research_commands.h:86
qbrobotics_research_api::qbSoftHand2MotorsResearch::setHomePosition
int setHomePosition()
Blocking function that moves SoftHand 2 motors to home position.
Definition: qbsofthand2_research_api.cpp:138
qbrobotics_research_api::Device::Params::initParams
virtual void initParams(const std::vector< int8_t > &param_buffer)
Definition: qbrobotics_research_api.cpp:1079
std
qbrobotics_research_api::Device::getPositions
virtual int getPositions(std::vector< int16_t > &positions)
Get the Positions given by the encoders mounted on the device.
Definition: qbrobotics_research_api.cpp:669
qbrobotics_research_api::qbSoftHand2MotorsResearch::Params::hand_side
uint8_t hand_side
Definition: qbsofthand2_research_api.h:17
qbrobotics_research_api::qbSoftHand2MotorsResearch::setMotorStates
int setMotorStates(bool motor_state) override
Activate or deactivate the motor(s)
Definition: qbsofthand2_research_api.cpp:21
CMD_GET_SYNERGIES
@ CMD_GET_SYNERGIES
Command to get the synergies of SoftHand 2.
Definition: qbrobotics_research_commands.h:87
qbrobotics_research_api::Device::setControlReferences
virtual int setControlReferences(const std::vector< int16_t > &control_references)
Set motor(s) control reference - position[tick].
Definition: qbrobotics_research_api.cpp:717
qbrobotics_research_api::qbSoftHand2MotorsResearch::setParamEncoderOffsets
int setParamEncoderOffsets(const std::vector< int16_t > &encoder_offsets) override
Set the encoder offsets parameters of the SoftHand 2.
Definition: qbsofthand2_research_api.cpp:105


qb_device_driver
Author(s): qbroboticsĀ®
autogenerated on Thu Apr 27 2023 02:36:32