File size: 9,079 Bytes
ef17c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Anton Deguet
  Created on: 2019-11-11

  (C) Copyright 2019-2024 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include "robManipulatorTest.h"

#include <cisstCommon/cmnPath.h>
#include <cisstCommon/cmnUnits.h>

#include <sawIntuitiveResearchKit/sawIntuitiveResearchKitConfig.h>


class ManipulatorTestDataECM: public ManipulatorTestData {
public:
    ManipulatorTestDataECM(void)
    {
        Name = "ECM";
        NumberOfLinks = 4;
        Manipulator = new robManipulatorECM;
    };

    void CheckIKResults(void) {
        vctDoubleVec jointErrors(NumberOfLinks), jointErrorsAbsolute(NumberOfLinks);
        jointErrors.DifferenceOf(SolutionJoints, ActualJoints);
        jointErrorsAbsolute.AbsOf(jointErrors);

        // compare joint values
        CPPUNIT_ASSERT_MESSAGE("Joint 0 solution is incorrect",
                               (jointErrorsAbsolute[0] < 0.001 * cmnPI_180)); // asinl is not that precise

        CPPUNIT_ASSERT_MESSAGE("Joint 1 solution is incorrect",
                               (jointErrorsAbsolute[1] < 0.001 * cmnPI_180)); // asinl

        CPPUNIT_ASSERT_MESSAGE("Joint 2 solution is incorrect",
                               (jointErrorsAbsolute[2] < 0.000000001 * cmn_mm)); // just a sqrt, higher precision expected

        CPPUNIT_ASSERT_MESSAGE("Joint 3 solution is incorrect",
                               (jointErrorsAbsolute[3] < 0.001 * cmnPI_180)); // acosl

        // translation
        vct3 positionTranslationError = ActualPose.Translation() - SolutionPose.Translation();
        CPPUNIT_ASSERT_MESSAGE("Cartesian translation error is too high",
                               positionTranslationError.Norm() < 0.005 * cmn_mm);

        // rotation
        vctMatRot3 positionRotationError;
        ActualPose.Rotation().ApplyInverseTo(SolutionPose.Rotation(), positionRotationError);
        CPPUNIT_ASSERT_MESSAGE("Cartesian rotation error is too high",
                               vctAxAnRot3(positionRotationError).Angle() < 0.0000001 * cmnPI_180);
    }
};


class ManipulatorTestDataMTM: public ManipulatorTestData {
public:
    ManipulatorTestDataMTM(void)
    {
        Name = "MTM";
        NumberOfLinks = 7;
        Manipulator = new robManipulatorMTM;
    };

    void CheckIKResults(void) {
        vctDoubleVec jointErrors(NumberOfLinks), jointErrorsAbsolute(NumberOfLinks);
        jointErrors.DifferenceOf(SolutionJoints, ActualJoints);
        jointErrorsAbsolute.AbsOf(jointErrors);

        std::string details =
            "Actual joints: " + (SolutionJoints * cmn180_PI).ToString() + "\n"
            "Solution     : " + (ActualJoints * cmn180_PI).ToString() + "\n"
            "Error        : " + (jointErrors * cmn180_PI).ToString() + "\n";

        // compare joint values
        CPPUNIT_ASSERT_MESSAGE("Joint 0 solution is incorrect\n" + details,
                               (jointErrorsAbsolute[0] < 0.001 * cmnPI_180)); // asinl is not that precise

        CPPUNIT_ASSERT_MESSAGE("Joint 1 solution is incorrect\n" + details,
                               (jointErrorsAbsolute[1] < 0.001 * cmnPI_180)); // asinl

        CPPUNIT_ASSERT_MESSAGE("Joint 2 solution is incorrect\n" + details,
                               (jointErrorsAbsolute[2] < 0.001 * cmnPI_180)); // asinl

        // translation
        vct3 positionTranslationError = ActualPose.Translation() - SolutionPose.Translation();
        CPPUNIT_ASSERT_MESSAGE("Cartesian translation error is too high",
                               positionTranslationError.Norm() < 0.005 * cmn_mm);

        // rotation
        vctMatRot3 positionRotationError;
        ActualPose.Rotation().ApplyInverseTo(SolutionPose.Rotation(), positionRotationError);
        CPPUNIT_ASSERT_MESSAGE("Cartesian rotation error is too high",
                               vctAxAnRot3(positionRotationError).Angle() < 0.0000001 * cmn180_PI);
    }
};


void robManipulatorTest::SetupTestData(ManipulatorTestData & data,
                                       const std::string & filename)
{
    // find the file
    cmnPath path;
    path.Add(std::string(sawIntuitiveResearchKit_SOURCE_CONFIG_DIR) + "/kinematic", cmnPath::TAIL);
    const std::string configFile = path.Find(filename);
    CPPUNIT_ASSERT_MESSAGE("Can't find full path for " + filename,
                           configFile != std::string(""));

    // json parsing
    std::ifstream jsonStream;
    Json::Value jsonConfig;
    Json::Reader jsonReader;
    jsonStream.open(configFile.c_str());
    bool fileParsed = jsonReader.parse(jsonStream, jsonConfig);
    CPPUNIT_ASSERT_MESSAGE("Failed to parse JSON file " + configFile + ": " + jsonReader.getFormattedErrorMessages(),
                           fileParsed);

    // look for DH in file
    const Json::Value jsonDH = jsonConfig["DH"];
    CPPUNIT_ASSERT_MESSAGE("Can't find \"DH\" in " + configFile,
                           !jsonDH.isNull());

    // try to load the DH parameters
    CPPUNIT_ASSERT_MESSAGE("Failed while loading from JSON \"DH\" value in " + configFile,
                           data.Manipulator->LoadRobot(jsonDH) == robManipulator::ESUCCESS);

    // verify number of links in robManipulator
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Expected number of links for " + filename,
                                 data.NumberOfLinks, data.Manipulator->links.size());

    // allocate test data
    data.LowerLimits.SetSize(data.NumberOfLinks);
    data.UpperLimits.SetSize(data.NumberOfLinks);
    data.Manipulator->GetJointLimits(data.LowerLimits,
                                     data.UpperLimits);
    data.Increments.SetSize(data.NumberOfLinks);
    data.ActualJoints.SetSize(data.NumberOfLinks);
    data.PreviousActualJoints.SetSize(data.NumberOfLinks);
    data.SolutionJoints.SetSize(data.NumberOfLinks);
}

void robManipulatorTest::ComputeAndTestIK(ManipulatorTestData & data)
{
    // compute FK
    data.ActualPose = data.Manipulator->ForwardKinematics(data.ActualJoints);
    // compute IK
    data.SolutionJoints.Assign(data.ActualJoints);
    robManipulator::Errno result = data.Manipulator->InverseKinematics(data.SolutionJoints,
                                                                       data.ActualPose);
    // make sure IK didn't complain
    CPPUNIT_ASSERT_EQUAL_MESSAGE("robManipulator::InverseKinematics result for "
                                 + data.Name + ": " + data.Manipulator->LastError(),
                                 robManipulator::ESUCCESS,
                                 result);

    // compute solution pose
    data.SolutionPose = data.Manipulator->ForwardKinematics(data.SolutionJoints);

    // arm specific testing
    data.CheckIKResults();
}

void robManipulatorTest::TestSampleJointSpace(ManipulatorTestData & data)
{
    vctDoubleVec directions(data.NumberOfLinks, 1.0);

    // initial position
    data.ActualJoints.Assign(data.LowerLimits);
    data.PreviousActualJoints.Assign(data.LowerLimits);
    ComputeAndTestIK(data);

    bool allReached = false;
    while (!allReached) {
        bool nextDimensionIncrement = true;
        for (size_t index = 0; index < data.NumberOfLinks; ++index) {
            if (nextDimensionIncrement) {
                double future = data.ActualJoints[index] + directions[index] * data.Increments[index];
                if (future > data.UpperLimits[index]) {
                    directions[index] = -1.0;
                    if (index == (data.NumberOfLinks - 1)) {
                        allReached = true;
                    }
                } else if (future < data.LowerLimits[index]) {
                    directions[index] = 1.0;
                } else {
                    // valid position
                    data.PreviousActualJoints.Assign(data.ActualJoints);
                    data.ActualJoints[index] = future;
                    nextDimensionIncrement = false;
                    ComputeAndTestIK(data);
                }
            }
        }
    }
}

void robManipulatorTest::TestECMIKSampleJointSpace(void)
{
    // load manipulator
    ManipulatorTestDataECM data;
    SetupTestData(data, "ecm.json");

    data.Increments.SetAll(3.0 * cmnPI_180); // use 3 degrees sampling
    data.Increments.at(2) = 2.0 * cmn_cm; // except for the translation stage

    TestSampleJointSpace(data);
}


void robManipulatorTest::TestMTMIKSampleJointSpace(void)
{
    // load manipulator
    ManipulatorTestDataMTM data;
    SetupTestData(data, "mtmr.json");

    data.Increments.SetAll(15.0 * cmnPI_180); // use 15 degrees sampling
    data.LowerLimits.at(6) = -cmnPI;
    data.UpperLimits.at(6) =  cmnPI;

    TestSampleJointSpace(data);
}