RoboDK Plug-In Interface
Loading...
Searching...
No Matches
joints.cpp
1/****************************************************************************
2**
3** Copyright (c) 2015-2025 RoboDK Inc.
4** Contact: https://robodk.com/
5**
6** This file is part of the RoboDK API.
7**
8** Permission is hereby granted, free of charge, to any person obtaining a copy
9** of this software and associated documentation files (the "Software"), to deal
10** in the Software without restriction, including without limitation the rights
11** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12** copies of the Software, and to permit persons to whom the Software is
13** furnished to do so, subject to the following conditions:
14**
15** The above copyright notice and this permission notice shall be included in all
16** copies or substantial portions of the Software.
17**
18** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24** SOFTWARE.
25**
26** RoboDK is a registered trademark of RoboDK Inc.
27**
28****************************************************************************/
29
30#include "joints.h"
31
32#include <cstring>
33#include <algorithm>
34
35#include <QStringList>
36
37#include "legacymatrix2d.h"
38
39
40namespace robodk
41{
42
44 : _dofCount(0)
45{
46 std::memset(_joints, 0, sizeof(_joints));
47 std::memset(_jointsFloat, 0, sizeof(_jointsFloat));
48}
49
51{
52 _dofCount = std::min(ndofs, MaximumJoints);
53 for (int i = 0; i < _dofCount; i++)
54 {
55 _joints[i] = 0.0;
56 }
57}
58
59Joints::Joints(const double *joints, int ndofs)
60{
61 SetValues(joints, ndofs);
62}
63
64Joints::Joints(const float *joints, int ndofs)
65{
66 int ndofs_ok = std::min(ndofs, MaximumJoints);
67 double jnts[MaximumJoints];
68
69 for (int i=0; i < ndofs_ok; i++)
70 {
71 jnts[i] = joints[i];
72 }
73 SetValues(jnts, ndofs_ok);
74}
75
76Joints::Joints(const legacy::Matrix2D *mat2d, int column, int ndofs)
77{
78 if (column >= Matrix2D_GetDimension(mat2d, 2))
79 {
80 _dofCount = 0;
81 }
82 if (ndofs < 0)
83 {
84 ndofs = Matrix2D_GetDimension(mat2d, 1);
85 }
86 _dofCount = qMin(ndofs, MaximumJoints);
87
88 double *ptr = Matrix2D_GetColumn(mat2d, column);
89 SetValues(ptr, _dofCount);
90}
91
92Joints::Joints(const QString &str)
93{
94 _dofCount = 0;
95 FromString(str);
96}
97
98const double* Joints::ValuesD() const
99{
100 return _joints;
101}
102
103const float* Joints::ValuesF() const
104{
105 for (int i = 0; i < MaximumJoints; i++)
106 {
107 _jointsFloat[i] = _joints[i];
108 }
109
110 return _jointsFloat;
111}
112
113#ifdef ROBODK_API_FLOATS
114const float* Joints::Values() const
115{
116 return ValuesF();
117}
118#else
119const double* Joints::Values() const
120{
121 return _joints;
122}
123#endif
124
125double Joints::Compare(const Joints &other) const
126{
127 double sum_diff = 0.0;
128 for (int i = 0; i <qMin(_dofCount, other.Length()); i++)
129 {
130 sum_diff += qAbs(_joints[i] - other.Values()[i]);
131 }
132 return sum_diff;
133}
134
136{
137 return _joints;
138}
139
140int Joints::Length() const
141{
142 return _dofCount;
143}
144
145void Joints::setLength(int new_length)
146{
147 if (new_length >= 0 && new_length < _dofCount)
148 {
149 _dofCount = new_length;
150 }
151}
152
154{
155 return _dofCount > 0;
156}
157
158void Joints::SetValues(const double *values, int ndofs)
159{
160 if (ndofs >= 0)
161 {
162 _dofCount = qMin(ndofs, MaximumJoints);
163 }
164
165 for (int i = 0; i < _dofCount; i++)
166 {
167 _joints[i] = values[i];
168 }
169}
170
171void Joints::SetValues(const float *values, int ndofs)
172{
173 if (ndofs >= 0)
174 {
175 _dofCount = qMin(ndofs, MaximumJoints);
176 }
177
178 for (int i = 0; i < _dofCount; i++)
179 {
180 _joints[i] = values[i];
181 }
182}
183
184int Joints::GetValues(double *values)
185{
186 for (int i = 0; i < _dofCount; i++)
187 {
188 values[i] = _joints[i];
189 }
190
191 return _dofCount;
192}
193
194#ifdef QT_GUI_LIB
195QString Joints::ToString(const QString &separator, int precision) const
196{
197 QString values;
198 for (int i = 0; i < _dofCount; i++)
199 {
200 if (i > 0)
201 {
202 values.append(separator);
203 }
204 values.append(QString::number(_joints[i], 'f', precision));
205 }
206 return values;
207}
208
209bool Joints::FromString(const QString &str)
210{
211 const QLatin1Char separator(',');
212
213 QString s = str;
214 s.replace(QLatin1Char(';'), separator);
215 s.replace(QLatin1Char('\t'), separator);
216
217#if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
218 const QString::SplitBehavior behavior = QString::SkipEmptyParts;
219#else
220 const Qt::SplitBehavior behavior = Qt::SkipEmptyParts;
221#endif
222
223 QStringList jointList = s.split(separator, behavior);
224 _dofCount = qMin(jointList.length(), MaximumJoints);
225 for (int i = 0; i < _dofCount; i++)
226 {
227 _joints[i] = jointList[i].trimmed().toDouble();
228 }
229 return true;
230}
231#endif // QT_GUI_LIB
232
233} // namespace robodk
The Joints class represents a joint position of a robot (robot axes).
Definition joints.h:51
int GetValues(double *joints)
GetValues.
Definition joints.cpp:184
float _jointsFloat[MaximumJoints]
joint values (floats, used to return a copy as a float pointer)
Definition joints.h:168
int Length() const
Number of joint axes of the robot (or degrees of freedom)
Definition joints.cpp:140
double _joints[MaximumJoints]
joint values (doubles, used to store the joint values)
Definition joints.h:165
const double * Values() const
Joint values.
Definition joints.cpp:119
bool Valid()
Check if the joints are valid. For example, when we request the Inverse kinematics and there is no so...
Definition joints.cpp:153
int _dofCount
number of degrees of freedom
Definition joints.h:162
double * Data()
Definition joints.cpp:135
const double * ValuesD() const
Joint values.
Definition joints.cpp:98
void setLength(int new_length)
Set the length of the array (only shrinking the array is allowed)
Definition joints.cpp:145
Joints()
Joints.
Definition joints.cpp:43
const float * ValuesF() const
Joint values.
Definition joints.cpp:103
void SetValues(const double *joints, int ndofs=-1)
Set the joint values in deg or mm. You can also important provide the number of degrees of freedom (6...
Definition joints.cpp:158
The Matrix2D struct represents a variable size 2D matrix.