qbmove_research_api.cpp
Go to the documentation of this file.
1 /***
2  * Software License Agreement: BSD 3-Clause License
3  *
4  * Copyright (c) 2015-2021, 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 
29 
30 using namespace qbrobotics_research_api;
31 
32 void qbmoveResearch::Params::initParams(const std::vector<int8_t> &param_buffer) {
33  Device::Params::initParams(param_buffer);
34  getParameter<uint8_t>(14, param_buffer, rate_limiter);
35 }
36 
37 qbmoveResearch::qbmoveResearch(std::shared_ptr<Communication> communication, std::string name, std::string serial_port, uint8_t id)
38  : Device(std::move(communication), std::move(name), std::move(serial_port), id, true, std::make_unique<qbmoveResearch::Params>()) {}
39 
40 qbmoveResearch::qbmoveResearch(std::shared_ptr<Communication> communication, std::string name, std::string serial_port, uint8_t id, bool init_params)
41  : Device(std::move(communication), std::move(name), std::move(serial_port), id, init_params, std::make_unique<qbmoveResearch::Params>()) {}
42 
43 qbmoveResearch::qbmoveResearch(std::shared_ptr<Communication> communication, std::string name, std::string serial_port, uint8_t id, bool init_params, std::unique_ptr<Device::Params> params)
44  : Device(std::move(communication), std::move(name), std::move(serial_port), id, init_params, std::move(params)) {}
45 
47  std::vector<int8_t> data_in;
48  if (communication_->sendCommandAndParse(serial_port_, params_->id, CMD_CALIBRATE, data_in) < 0) {
49  return -1;
50  }
51  return 0;
52 }
53 
54 int qbmoveResearch::setPositionAndStiffnessReferences(int16_t position, int16_t stiffness) {
55  auto const data_out = Communication::vectorSwapAndCast<int8_t, int16_t>({position, stiffness});
56  if (communication_->sendCommand(serial_port_, params_->id, CMD_SET_POS_STIFF, data_out) < 0) {
57  return -1;
58  }
59  return 0;
60 }
61 
63  return getParamRateLimiter(params_->rate_limiter);
64 }
65 
66 int qbmoveResearch::getParamRateLimiter(uint8_t &rate_limiter) {
67  std::vector<int8_t> param_buffer;
68  if (getParameters(param_buffer) != 0) {
69  return -1;
70  }
71  Params::getParameter<uint8_t>(14, param_buffer, rate_limiter);
72  return 0;
73 }
74 
75 int qbmoveResearch::setParamRateLimiter(uint8_t rate_limiter) {
76  int set_fail = setParameter(14, Communication::vectorSwapAndCast<int8_t, uint8_t>({rate_limiter}));
77  if (!set_fail) {
78  params_->rate_limiter = rate_limiter;
79  }
80  return set_fail;
81 }
82 
83 
84 // ----------------------------------------------------------------
85 
86 
87 void qbmoveLegacyResearch::Params::initParams(const std::vector<int8_t> &param_buffer) {
89  // Legacy firmware needs inverted signs
90  std::for_each(encoder_offsets.begin(), encoder_offsets.end(), [](int16_t &x){ x *= -1; } );
91 }
92 
93 qbmoveLegacyResearch::qbmoveLegacyResearch(std::shared_ptr<Communication> communication, std::string name, std::string serial_port, uint8_t id)
94  : qbmoveResearch(std::move(communication), std::move(name), std::move(serial_port), id, true, std::make_unique<qbmoveLegacyResearch::Params>()) {}
95 
96 qbmoveLegacyResearch::qbmoveLegacyResearch(std::shared_ptr<Communication> communication, std::string name, std::string serial_port, uint8_t id, bool init_params)
97  : qbmoveResearch(std::move(communication), std::move(name), std::move(serial_port), id, init_params, std::make_unique<qbmoveLegacyResearch::Params>()) {}
98 
99 int qbmoveLegacyResearch::setParameter(uint16_t param_type, const std::vector<int8_t> &param_data) {
100  int set_fail = qbmoveResearch::setParameter(param_type, param_data);
101  if (!set_fail && param_type != 1) { // WARN: the storeUserDataMemory for ID change is called by the specific method
102  set_fail = qbmoveResearch::storeUserDataMemory(); // WARN: a store user memory is required on legacy devices!
103  }
104  return set_fail;
105 }
106 
107 int qbmoveLegacyResearch::getControlReferences(std::vector<int16_t> &control_references) {
108  int set_fail = qbmoveResearch::getControlReferences(control_references);
109  // Legacy firmware needs inverted signs
110  std::for_each( control_references.begin(), control_references.end(), [](int16_t &x){ x *= -1; } );
111  return set_fail;
112 }
113 
115  int set_fail = qbmoveResearch::setMotorStates(motor_state);
116  std::this_thread::sleep_for(std::chrono::milliseconds(3));
117  return set_fail;
118 }
119 
121  return getParamEncoderOffsets(params_->encoder_offsets);
122 }
123 
124 int qbmoveLegacyResearch::getParamEncoderOffsets(std::vector<int16_t> &encoder_offsets) {
125  int set_fail = qbmoveResearch::getParamEncoderOffsets(encoder_offsets);
126  // Legacy firmware needs inverted signs
127  std::for_each( encoder_offsets.begin(), encoder_offsets.end(), [](int16_t &x){ x *= -1; } );
128  return set_fail;
129 }
130 
131 int qbmoveLegacyResearch::setParamId(uint8_t id) { // old method only for legacy devices
132  uint8_t previous_id = params_->id;
133  int set_fail = qbmoveResearch::setParamId(id);
134  if (!set_fail) {
135  params_->id = previous_id; // WARN: need to send the command to the previous ID on legacy devices!
136  set_fail = qbmoveResearch::storeUserDataMemory(); // WARN: a store user memory is required on legacy devices!
137  params_->id = id;
138  }
139  return set_fail;
140 }
141 
142 int qbmoveLegacyResearch::setParamEncoderOffsets(const std::vector<int16_t> &encoder_offsets) {
143  std::vector<int16_t> legacy_offsets = encoder_offsets;
144  // Legacy firmware needs inverted signs
145  legacy_offsets.at(2) *= -1;
146  return qbmoveResearch::setParamEncoderOffsets(legacy_offsets);
147 }
148 
149 int qbmoveLegacyResearch::setParamZeros() { // old method only for legacy devices
150  std::vector<int16_t> positions;
151  if (getPositions(positions)) {
152  return -1;
153  }
154  std::vector<int16_t> offsets;
155  if (getParamEncoderOffsets(offsets)) {
156  return -1;
157  }
158 
159  std::transform(offsets.begin(), offsets.end(), positions.begin(), offsets.begin(), std::plus<int16_t>()); // inverted signs for legacy leads to "plus" operation
160 
161  return setParamEncoderOffsets(offsets);
162 }
qbrobotics_research_api::qbmoveLegacyResearch
Definition: qbmove_research_api.h:84
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::qbmoveLegacyResearch::setParamZeros
int setParamZeros() override
Set motor(s) zero(s) to actual encoder reading.
Definition: qbmove_research_api.cpp:149
qbrobotics_research_api::qbmoveLegacyResearch::setMotorStates
int setMotorStates(bool motor_state) override
Activate or deactivate the motor(s)
Definition: qbmove_research_api.cpp:114
qbrobotics_research_api::Device::params_
std::shared_ptr< Params > params_
Definition: qbrobotics_research_api.h:852
qbrobotics_research_api::qbmoveResearch::getParamRateLimiter
int getParamRateLimiter()
Definition: qbmove_research_api.cpp:62
qbrobotics_research_api::qbmoveLegacyResearch::qbmoveLegacyResearch
qbmoveLegacyResearch(std::shared_ptr< Communication > communication, std::string name, std::string serial_port, uint8_t id)
Definition: qbmove_research_api.cpp:93
qbrobotics_research_api::qbmoveResearch::computeAndStoreMaximumStiffness
int computeAndStoreMaximumStiffness()
Definition: qbmove_research_api.cpp:46
qbrobotics_research_api::qbmoveLegacyResearch::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: qbmove_research_api.cpp:99
qbrobotics_research_api::qbmoveLegacyResearch::Params::initParams
void initParams(const std::vector< int8_t > &param_buffer) override
Definition: qbmove_research_api.cpp:87
qbrobotics_research_api::qbmoveResearch::Params
Definition: qbmove_research_api.h:87
qbrobotics_research_api::qbmoveLegacyResearch::getControlReferences
int getControlReferences(std::vector< int16_t > &control_references) override
Get the reference command sent to the motor(s) of the device.
Definition: qbmove_research_api.cpp:107
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
Definition: qbmove_research_api.h:33
CMD_SET_POS_STIFF
@ CMD_SET_POS_STIFF
Not used in the softhand firmware.
Definition: qbrobotics_research_commands.h:70
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::qbmoveResearch::qbmoveResearch
qbmoveResearch(std::shared_ptr< Communication > communication, std::string name, std::string serial_port, uint8_t id)
Definition: qbmove_research_api.cpp:37
qbrobotics_research_api::qbmoveLegacyResearch::setParamEncoderOffsets
int setParamEncoderOffsets(const std::vector< int16_t > &encoder_offsets) override
Set the encoder offsets parameters of the device.
Definition: qbmove_research_api.cpp:142
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::qbmoveLegacyResearch::setParamId
int setParamId(uint8_t id) override
Set the ID of the device.
Definition: qbmove_research_api.cpp:131
qbrobotics_research_api::Device::communication_
std::shared_ptr< Communication > communication_
Definition: qbrobotics_research_api.h:853
qbrobotics_research_api::Device::getControlReferences
virtual int getControlReferences(std::vector< int16_t > &control_references)
Get the reference command sent to the motor(s) of the device.
Definition: qbrobotics_research_api.cpp:639
qbrobotics_research_api::Device::Params::rate_limiter
uint8_t rate_limiter
Definition: qbrobotics_research_api.h:312
qbrobotics_research_api::Device::Params::encoder_offsets
std::vector< int16_t > encoder_offsets
Definition: qbrobotics_research_api.h:306
qbmove_research_api.h
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::qbmoveResearch::Params::initParams
void initParams(const std::vector< int8_t > &param_buffer) override
Definition: qbmove_research_api.cpp:32
CMD_CALIBRATE
@ CMD_CALIBRATE
Starts the stiffness calibration of the qbMove.
Definition: qbrobotics_research_commands.h:59
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::qbmoveLegacyResearch::Params
Definition: qbmove_research_api.h:86
qbrobotics_research_api::qbmoveResearch::setPositionAndStiffnessReferences
int setPositionAndStiffnessReferences(int16_t position, int16_t stiffness)
Definition: qbmove_research_api.cpp:54
qbrobotics_research_api::qbmoveResearch
Definition: qbmove_research_api.h:60
qbrobotics_research_api::qbmoveLegacyResearch::getParamEncoderOffsets
int getParamEncoderOffsets() override
Update the device encoder offsets in the class variable param_.
Definition: qbmove_research_api.cpp:120
qbrobotics_research_api::qbmoveResearch::setParamRateLimiter
int setParamRateLimiter(uint8_t rate_limiter)
Definition: qbmove_research_api.cpp:75


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