Skip to content

Commit

Permalink
Added Ros.io to allow saving and loading data to yaml files.
Browse files Browse the repository at this point in the history
Refactoring.
  • Loading branch information
StefanFabian committed May 8, 2020
1 parent 2fb3d30 commit 14eee4f
Show file tree
Hide file tree
Showing 20 changed files with 779 additions and 191 deletions.
13 changes: 11 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ find_package(catkin REQUIRED actionlib roscpp image_transport ros_babel_fish tf2

find_package(Qt5 COMPONENTS Core Multimedia Qml Quick REQUIRED)

find_package(PkgConfig REQUIRED)
pkg_check_modules(YAML_CPP yaml-cpp)

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

Expand All @@ -21,7 +24,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
catkin_package(
LIBRARIES qml_ros_plugin
CATKIN_DEPENDS actionlib roscpp image_transport ros_babel_fish tf2_ros
DEPENDS Qt5Core Qt5Multimedia Qt5Qml
DEPENDS Qt5Core Qt5Multimedia Qt5Qml YAML_CPP
)

###########
Expand All @@ -47,6 +50,7 @@ set(SOURCES
include/qml_ros_plugin/goal_handle.h
include/qml_ros_plugin/image_buffer.h
include/qml_ros_plugin/image_transport_subscriber.h
include/qml_ros_plugin/io.h
include/qml_ros_plugin/message_conversions.h
include/qml_ros_plugin/node_handle.h
include/qml_ros_plugin/package.h
Expand All @@ -67,6 +71,7 @@ set(SOURCES
src/goal_handle.cpp
src/image_buffer.cpp
src/image_transport_subscriber.cpp
src/io.cpp
src/message_conversions.cpp
src/node_handle.cpp
src/package.cpp
Expand All @@ -81,7 +86,7 @@ set(SOURCES
src/time.cpp)

add_library(qml_ros_plugin ${SOURCES})
target_link_libraries(qml_ros_plugin ${catkin_LIBRARIES} Qt5::Core Qt5::Multimedia Qt5::Qml Qt5::Quick)
target_link_libraries(qml_ros_plugin ${catkin_LIBRARIES} Qt5::Core Qt5::Multimedia Qt5::Qml Qt5::Quick ${YAML_CPP_LIBRARIES})

#############
## Testing ##
Expand Down Expand Up @@ -128,6 +133,10 @@ if (CATKIN_ENABLE_TESTING)
add_rostest_gtest(${PROJECT_NAME}_test_package test/test_package.test test/package.cpp)
target_link_libraries(${PROJECT_NAME}_test_package ${PROJECT_NAME})
set_target_properties(${PROJECT_NAME}_test_package PROPERTIES OUTPUT_NAME test_package PREFIX "")

add_rostest_gtest(${PROJECT_NAME}_test_io test/test_io.test test/io.cpp)
target_link_libraries(${PROJECT_NAME}_test_io ${PROJECT_NAME})
set_target_properties(${PROJECT_NAME}_test_io PROPERTIES OUTPUT_NAME test_io PREFIX "")
endif ()

# to run: catkin build --this --no-deps -DENABLE_COVERAGE_TESTING=ON -DCMAKE_BUILD_TYPE=Debug -v --catkin-make-args qml_ros_plugin_coverage
Expand Down
21 changes: 21 additions & 0 deletions docs/pages/Ros-Singleton.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ The package property provides a wrapper for ``ros::package``.
// Get plugins for a package as a map [package_name -> [values]]
var plugins = Ros.package.getPlugins("rviz", "plugin")
Console
-------
The Ros singleton also provides access to the ``Ros`` logging functionality.
See `Logging`:ref:.

IO
--
You can also save and read data that can be serialized in the yaml format using:

.. code-block:: qml
var obj = {"key": [1, 2, 3], "other": "value"}
if (!Ros.io.writeYaml("/home/user/file.yaml", obj))
Ros.error("Could not write file!")
// and read it back
obj = Ros.io.readYaml("/home/user/file.yaml")
if (!obj) Ros.error("Failed to load file!")
API
---
.. doxygenclass:: qml_ros_plugin::Package
Expand All @@ -96,5 +114,8 @@ API
.. doxygenclass:: qml_ros_plugin::TopicInfo
:members:

.. doxygenclass:: qml_ros_plugin::IO
:members:

.. doxygenclass:: qml_ros_plugin::RosQmlSingletonWrapper
:members:
43 changes: 25 additions & 18 deletions include/qml_ros_plugin/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#ifndef QML_ROS_PLUGIN_ARRAY_H
#define QML_ROS_PLUGIN_ARRAY_H

#include <QObject>
#include <QVariant>

#include <ros_babel_fish/messages/array_message.h>
Expand All @@ -16,21 +15,24 @@ namespace qml_ros_plugin
/*!
* @brief View on an array field of a message.
* This allows access on array elements with lazy copy mechanism.
* Copies of an Array point to the same data and modifications of one array will be mirrored by the other.
*/
class Array : public QObject
class Array
{
Q_OBJECT
Q_GADGET
// @formatter:off
//! The length of the array, i.e., the number of elements.
Q_PROPERTY( int length READ length WRITE setLength NOTIFY lengthChanged )
Q_PROPERTY( int length READ length WRITE setLength )
// @formatter:on
public:
Array();

Array( ros_babel_fish::TranslatedMessage::ConstPtr translated_message,
const ros_babel_fish::ArrayMessageBase *message );

// Array( const Array &other );

~Array() override = default;
~Array() = default;

int length() const;

Expand Down Expand Up @@ -101,22 +103,27 @@ Q_OBJECT

const ros_babel_fish::ArrayMessageBase *_message() const;

signals:

void lengthChanged();
Q_INVOKABLE QVariantList toVariantList() const;

private:
void enlargeCache( int size );

void fillCache();

ros_babel_fish::TranslatedMessage::ConstPtr translated_message_;
const ros_babel_fish::ArrayMessageBase *message_;
mutable QVariantList cache_;
bool all_in_cache_;
QList<bool> modified_;
int length_;
void enlargeCache( int size ) const;

void fillCache() const;

struct Data
{
QVariantList cache;
QList<bool> modified;
ros_babel_fish::TranslatedMessage::ConstPtr translated_message;
const ros_babel_fish::ArrayMessageBase *message = nullptr;
bool all_in_cache = true;
int length = 0;
};
// Copies of array share the data
std::shared_ptr<Data> p_;
};
} // qml_ros_plugin

Q_DECLARE_METATYPE( qml_ros_plugin::Array )

#endif // QML_ROS_PLUGIN_ARRAY_H

0 comments on commit 14eee4f

Please sign in to comment.