0001-new-rci-converter-for-joint-velocities-with-updated-.patch

N. Dehio, 06/02/2015 09:21 AM

Download (8.48 KB)

View differences:

cpp/src/CMakeLists.txt
64 64
    LIST(APPEND SOURCES "rst/converters/rci/JointAnglesConverter.cpp"
65 65
                        "rst/converters/rci/JointImpedanceConverter.cpp"
66 66
                        "rst/converters/rci/JointTorquesConverter.cpp"
67
                        "rst/converters/rci/JointVelocitiesConverter.cpp"
67 68
                        "rst/converters/rci/LengthConverter.cpp"
68 69
                        "rst/converters/rci/PhaseConverter.cpp"
69 70
                        "rst/converters/rci/PoseConverter.cpp"
......
75 76
    LIST(APPEND HEADERS "rst/converters/rci/JointAnglesConverter.h"
76 77
                        "rst/converters/rci/JointImpedanceConverter.h"
77 78
                        "rst/converters/rci/JointTorquesConverter.h"
79
                        "rst/converters/rci/JointVelocitiesConverter.h"
78 80
                        "rst/converters/rci/LengthConverter.h"
79 81
                        "rst/converters/rci/PhaseConverter.h"
80 82
                        "rst/converters/rci/PoseConverter.h"
......
224 226
    LIST(APPEND RCI_CONVERTERS "rst::converters::rci::JointAnglesConverter"
225 227
                               "rst::converters::rci::JointImpedanceConverter"
226 228
                               "rst::converters::rci::JointTorquesConverter"
229
                               "rst::converters::rci::JointVelocitiesConverter"
227 230
                               "rst::converters::rci::LengthConverter"
228 231
                               "rst::converters::rci::PhaseConverter"
229 232
                               "rst::converters::rci::PoseConverter"
cpp/src/rst/converters/rci/JointVelocitiesConverter.cpp
1
/* ============================================================
2
 *
3
 * This file is part of the rst-converters project.
4
 *
5
 * Copyright (C) 2015 by Niels Dehio <ndehio at techfak dot uni-bielefeld dot de>
6
 *
7
 * This file may be licensed under the terms of the
8
 * GNU Lesser General Public License Version 3 (the ``LGPL''),
9
 * or (at your option) any later version.
10
 *
11
 * Software distributed under the License is distributed
12
 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13
 * express or implied. See the LGPL for the specific language
14
 * governing rights and limitations.
15
 *
16
 * You should have received a copy of the LGPL along with this
17
 * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18
 * or write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
 *
21
 * The development of this software was supported by:
22
 *   CoR-Lab, Research Institute for Cognition and Robotics
23
 *     Bielefeld University
24
 *
25
 * ============================================================ */
26

  
27
#include "JointVelocitiesConverter.h"
28

  
29
#include <rsb/converter/SerializationException.h>
30
#include <rsb/converter/ProtocolBufferConverter.h>
31

  
32
#include <rst/kinematics/JointVelocities.pb.h>
33

  
34
#include <rci/dto/JointVelocities.h>
35

  
36
using namespace std;
37
using namespace nemo;
38
using namespace rci;
39

  
40
namespace rst {
41
namespace converters {
42
namespace rci {
43

  
44
JointVelocitiesConverter::JointVelocitiesConverter() :
45
        rsb::converter::Converter<string>(
46
                rsc::runtime::typeName<rst::kinematics::JointVelocities>(),
47
                RSB_TYPE_TAG( ::rci::JointVelocities)) {
48
    converter = boost::shared_ptr<rsb::converter::Converter<string> >(
49
            new rsb::converter::ProtocolBufferConverter<
50
                    rst::kinematics::JointVelocities>);
51
}
52

  
53
JointVelocitiesConverter::~JointVelocitiesConverter() {
54
}
55

  
56
string JointVelocitiesConverter::getWireSchema() const {
57
    return converter->getWireSchema();
58
}
59

  
60
string JointVelocitiesConverter::serialize(const rsb::AnnotatedData &data,
61
        string &wire) {
62

  
63
    // Cast to original domain type
64
    boost::shared_ptr< ::rci::JointVelocities> domain = boost::static_pointer_cast<
65
    ::rci::JointVelocities>(data.second);
66

  
67
    // Fill protocol buffer object
68
    rst::kinematics::JointVelocities proto;
69

  
70
    //    1. reserve()
71
    //    2. std::copy(begin, begin+n) // oder memcpy
72

  
73
    // TODO Check if this can be done more efficiently (see protobuf C++ docs for repeated numeric fields)
74
    for (unsigned int i = 0; i < domain->getDimension(); ++i) {
75
        proto.add_velocities(domain->asDouble(i));
76
    }
77

  
78
    // Use embedded ProtoBuf converter for serialization to wire
79
    return converter->serialize(
80
            make_pair(rsc::runtime::typeName<rst::kinematics::JointVelocities>(),
81
                    boost::shared_ptr<void>(&proto, rsc::misc::NullDeleter())),
82
            wire);
83
}
84

  
85
rsb::AnnotatedData JointVelocitiesConverter::deserialize(
86
        const std::string &wireType, const std::string &wire) {
87

  
88
    // Deserialize and cast to specific ProtoBuf type
89
    boost::shared_ptr<rst::kinematics::JointVelocities> proto =
90
            boost::static_pointer_cast<rst::kinematics::JointVelocities>(
91
                    converter->deserialize(wireType, wire).second);
92

  
93
    // Read domain data from ProtoBuf
94
    RealVector values = RealVector(dim(proto->velocities_size()));
95
    for (int i = 0; i < proto->velocities_size(); ++i) {
96
        values[i] = proto->velocities(i);
97
    }
98

  
99
    // Instantiate domain object
100
    boost::shared_ptr< ::rci::JointVelocities> domain(
101
            new ::rci::JointVelocities(values));
102

  
103
    return rsb::AnnotatedData(getDataType(), domain);
104

  
105
}
106

  
107
}
108
}
109
}
cpp/src/rst/converters/rci/JointVelocitiesConverter.h
1
/* ============================================================
2
 *
3
 * This file is part of the rst-converters project.
4
 *
5
 * Copyright (C) 2015 by Niels Dehio <ndehio at techfak dot uni-bielefeld dot de>
6
 *
7
 * This file may be licensed under the terms of the
8
 * GNU Lesser General Public License Version 3 (the ``LGPL''),
9
 * or (at your option) any later version.
10
 *
11
 * Software distributed under the License is distributed
12
 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13
 * express or implied. See the LGPL for the specific language
14
 * governing rights and limitations.
15
 *
16
 * You should have received a copy of the LGPL along with this
17
 * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18
 * or write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
 *
21
 * The development of this software was supported by:
22
 *   CoR-Lab, Research Institute for Cognition and Robotics
23
 *     Bielefeld University
24
 *
25
 * ============================================================ */
26

  
27
#pragma once
28

  
29
#include <string>
30
#include <boost/shared_ptr.hpp>
31

  
32
#include <rsb/converter/Converter.h>
33

  
34
#include "rst/rstconvertersexports.h"
35

  
36
namespace rst {
37
namespace converters {
38
namespace rci {
39

  
40
/**
41
 * Converter for rci::JointVelocities to JointVelocities type.
42
 *
43
 * @author swrede
44
 */
45
class RST_CONVERTERS_EXPORT JointVelocitiesConverter: public rsb::converter::Converter<std::string> {
46
public:
47

  
48
    JointVelocitiesConverter();
49
    virtual ~JointVelocitiesConverter();
50

  
51
    std::string getWireSchema() const;
52

  
53
    std::string serialize(const rsb::AnnotatedData &data, std::string &wire);
54
    rsb::AnnotatedData deserialize(const std::string &wireType,
55
            const std::string &wire);
56

  
57
private:
58

  
59
    ::boost::shared_ptr<rsb::converter::Converter<std::string> > converter;
60

  
61
};
62

  
63
}
64
}
65
}
66

  
0
-