Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROS2: Add samples and other fixups #109

Merged
merged 33 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
72d1b64
Fix MSVC compiler warning in test_zivid_camera
micragz Jun 26, 2024
67da85a
Add Capture 2D sample
micragz Jun 25, 2024
f18292a
Add capture assistant sample
micragz Jun 25, 2024
575be01
Add sample capture and save
micragz Jun 26, 2024
baf291c
Add `sample_capture.py` sample
micragz Jul 11, 2024
4d30cfd
Make `sample_capture_with_settings_from_file` sample
micragz Jul 12, 2024
44e3e03
Log exceptions related to Settings from Zivid API
micragz Jul 12, 2024
cee20af
Add remaining Python samples
micragz Jul 12, 2024
d3b27d0
Clean up and improve C++ samples
micragz Jul 12, 2024
4e3bfee
Use native line endings on .sh files
micragz Jul 12, 2024
6b873ff
Format samples in docker container (gives different results from loca…
micragz Jul 15, 2024
c962f84
Enable `zivid_samples` linting and fix resulting issues
micragz Jul 15, 2024
544746c
Remove copyright notice from individual C++ sample files
micragz Jul 15, 2024
6c28a00
Use clang-format instead of uncrustify
micragz Jul 15, 2024
e8c592c
Remove g++-14 from test matrix
micragz Jul 16, 2024
0d392ad
Fix building with other compilers
micragz Jul 16, 2024
8a5e65e
Add `ament_clang_tidy` and fix resulting issues
micragz Jul 16, 2024
f869396
Make Python samples executable using `ros2 launch`
micragz Jul 17, 2024
9950c8c
CMake: Use functions to add samples
micragz Jul 18, 2024
d56f456
Add more context to exception error
micragz Jul 18, 2024
bf51ea2
Make `settings` method naming consistent between C++ and Python samples
micragz Jul 18, 2024
d295a99
fixup! Add more context to exception error
micragz Jul 18, 2024
7263319
Remove AsyncParameterClient from Python samples, add timeout when set…
micragz Jul 18, 2024
db7a727
fixup! Remove AsyncParameterClient from Python samples, add timeout w…
micragz Jul 18, 2024
f9c59b4
Throw on out-of-scope enum
micragz Jul 18, 2024
db411d2
Smaller wording improvements
micragz Jul 18, 2024
3cec69c
Don't spin on end of `sample_capture_and_save_cpp`
micragz Jul 18, 2024
deb57e0
fixup! Smaller wording improvements
micragz Jul 19, 2024
baad826
fixup! CMake: Use functions to add samples
micragz Jul 19, 2024
c0e765c
Spin at end on all samples, log info message where it was missing
micragz Jul 19, 2024
8074d80
Catch all `std::exception`
micragz Jul 19, 2024
5ae7bf6
Log param updates as early as possible
apartridge Jul 19, 2024
12835f6
fixup! Log param updates as early as possible
apartridge Jul 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ Using ros2 run (when `zivid_camera` node is already running):
ros2 run zivid_samples sample_capture_2d_cpp
```

### Suggest Settings with Capture Assistant

This sample shows how to invoke the [capture_assistant/suggest_settings](#capture_assistantsuggest_settings) service to
suggest and set capture settings. Then, it shows how to subscribe to the [points/xyzrgba](#pointsxyzrgba) and
[color/image_color](#colorimage_color) topics, and finally invoke the [capture](#capture) service.

Source code: [C++](./zivid_samples/src/sample_capture_assistant.cpp)

```bash
ros2 launch zivid_samples sample.launch sample:=sample_capture_assistant
```
Using ros2 run (when `zivid_camera` node is already running):
```bash
ros2 run zivid_samples sample_capture_assistant
```

_More samples will soon be provided_.

## Frequently Asked Questions
Expand Down
4 changes: 4 additions & 0 deletions zivid_samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ add_executable(sample_capture_2d_cpp src/sample_capture_2d.cpp)
ament_target_dependencies(sample_capture_2d_cpp rclcpp zivid_interfaces sensor_msgs std_srvs ament_index_cpp)
install(TARGETS sample_capture_2d_cpp DESTINATION lib/${PROJECT_NAME})

add_executable(sample_capture_assistant_cpp src/sample_capture_assistant.cpp)
ament_target_dependencies(sample_capture_assistant_cpp rclcpp zivid_interfaces sensor_msgs std_srvs ament_index_cpp)
install(TARGETS sample_capture_assistant_cpp DESTINATION lib/${PROJECT_NAME})
apartridge marked this conversation as resolved.
Show resolved Hide resolved

if(BUILD_TESTING)
# TODO enable linting of the samples
#find_package(ament_lint_auto REQUIRED)
Expand Down
127 changes: 127 additions & 0 deletions zivid_samples/src/sample_capture_assistant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2024 Zivid AS
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Zivid AS nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include <rclcpp/rclcpp.hpp>

#include <ament_index_cpp/get_package_share_directory.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <std_srvs/srv/trigger.hpp>

#include <zivid_interfaces/srv/capture_assistant_suggest_settings.hpp>

/*
* This sample shows how to use the capture assistant service to suggest and set the capture
* settings parameter of the zivid node. Then, it shows how to subscribe to the points/xyzrgba and
* color/image_color topics, and finally invoke the capture service.
*/

void capture_assistant_suggest_settings(const std::shared_ptr<rclcpp::Node> & node)
{
using zivid_interfaces::srv::CaptureAssistantSuggestSettings;

auto client =
node->create_client<CaptureAssistantSuggestSettings>("capture_assistant/suggest_settings");
while (!client->wait_for_service(std::chrono::seconds(3))) {
if (!rclcpp::ok()) {
RCLCPP_ERROR(node->get_logger(), "Client interrupted while waiting for service to appear.");
std::terminate();
}
RCLCPP_INFO(node->get_logger(), "Waiting for the capture assistant service to appear...");
}

auto request = std::make_shared<CaptureAssistantSuggestSettings::Request>();
request->max_capture_time = rclcpp::Duration::from_seconds(5.0);
request->ambient_light_frequency =
CaptureAssistantSuggestSettings::Request::AMBIENT_LIGHT_FREQUENCY_NONE;

auto result = client->async_send_request(request);

if (rclcpp::spin_until_future_complete(node, result) != rclcpp::FutureReturnCode::SUCCESS) {
RCLCPP_ERROR(node->get_logger(), "Failed to call capture assistant service");
std::terminate();
}
}

void capture(const std::shared_ptr<rclcpp::Node> & node)
{
auto capture_client = node->create_client<std_srvs::srv::Trigger>("capture");
while (!capture_client->wait_for_service(std::chrono::seconds(3))) {
if (!rclcpp::ok()) {
RCLCPP_ERROR(
node->get_logger(),
"Client interrupted while waiting for service to appear.");
std::terminate();
}
RCLCPP_INFO(
node->get_logger(),
"Waiting for the capture service to appear...");
}

RCLCPP_INFO(node->get_logger(), "Triggering capture");
auto result =
capture_client->async_send_request(std::make_shared<std_srvs::srv::Trigger::Request>());

if (rclcpp::spin_until_future_complete(node, result) != rclcpp::FutureReturnCode::SUCCESS) {
RCLCPP_ERROR(node->get_logger(), "Failed to trigger capture");
std::terminate();
}
}

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto node = rclcpp::Node::make_shared("sample_capture_assistant");
RCLCPP_INFO(node->get_logger(), "Started the sample_capture_assistant node");

capture_assistant_suggest_settings(node);

auto points_xyzrgba_subscription =
node->create_subscription<sensor_msgs::msg::PointCloud2>(
"points/xyzrgba", 10,
[&](sensor_msgs::msg::PointCloud2::ConstSharedPtr msg) -> void {
RCLCPP_INFO(
node->get_logger(), "Received point cloud of width %d and height %d", msg->width,
msg->height);
});

auto color_image_color_subscription = node->create_subscription<sensor_msgs::msg::Image>(
"color/image_color", 10, [&](sensor_msgs::msg::Image::ConstSharedPtr msg) -> void {
RCLCPP_INFO(
node->get_logger(), "Received image of width %d and height %d", msg->width, msg->height);
});

capture(node);

RCLCPP_INFO(node->get_logger(), "Spinning node.. Press Ctrl+C to abort.");
rclcpp::spin(node);

rclcpp::shutdown();

return EXIT_SUCCESS;
}