{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "32ae0be7-5992-476f-ad74-e56705746f14", "metadata": {}, "outputs": [], "source": [ "! ../scripts/generate_cling_3rd_party.py roscpp" ] }, { "cell_type": "code", "execution_count": null, "id": "3ac2de29-2628-459a-9579-cdd8fd24f8c5", "metadata": {}, "outputs": [], "source": [ "! ../scripts/generate_cling_3rd_party.py roscpp_tutorials" ] }, { "cell_type": "code", "execution_count": null, "id": "599149ab-5807-422f-8ea9-7cbd9cbe86b1", "metadata": {}, "outputs": [], "source": [ "#include \"load_roscpp.h\"\n", "#include \"load_roscpp_tutorials.h\"" ] }, { "cell_type": "markdown", "id": "87826d04-9d67-41ef-84f0-f7b3dae2ce75", "metadata": {}, "source": [ "Code below are taken from http://wiki.ros.org/ROS/Tutorials/WritingServiceClient%28c%2B%2B%29" ] }, { "cell_type": "code", "execution_count": null, "id": "56bceb25-ebaf-4214-8166-ff9e0d73c501", "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \n", "#include " ] }, { "cell_type": "code", "execution_count": null, "id": "de4077bd-7651-4327-87ea-8ec7d046e7e5", "metadata": {}, "outputs": [], "source": [ "bool add(roscpp_tutorials::TwoInts::Request &req,\n", " roscpp_tutorials::TwoInts::Response &res)\n", "{\n", " res.sum = req.a + req.b;\n", " ROS_INFO(\"request: x=%ld, y=%ld\", (long int)req.a, (long int)req.b);\n", " ROS_INFO(\"sending back response: [%ld]\", (long int)res.sum);\n", " return true;\n", "}" ] }, { "cell_type": "code", "execution_count": null, "id": "b4db6c4e-b2e1-41ca-9d82-a34ff76bfb4e", "metadata": {}, "outputs": [], "source": [ "int argc = 1;\n", "char *argv[1];\n", "argv[0] = new char[10];\n", "strcpy(argv[0], \"srv_client\");\n", "ros::init(argc, argv, \"srv_client\");\n", "\n", "ros::NodeHandle n;\n", "ros::ServiceClient client = n.serviceClient(\"add_two_ints\");\n", "roscpp_tutorials::TwoInts srv;" ] }, { "cell_type": "code", "execution_count": null, "id": "99152bd9-981d-4977-ae50-85bde1e63173", "metadata": {}, "outputs": [], "source": [ "srv.request.a = 1;\n", "srv.request.b = 2;\n", "if (client.call(srv))\n", "{\n", " printf(\"Sum: %ld\\n\", (long int)srv.response.sum);\n", " ROS_INFO(\"Sum: %ld\\n\", (long int)srv.response.sum);\n", "}\n", "else\n", "{\n", " printf(\"Failed to call service add_two_ints\\n\");\n", " ROS_INFO(\"Failed to call service add_two_ints\\n\");\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "C++17", "language": "C++17", "name": "xcpp17" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "17" } }, "nbformat": 4, "nbformat_minor": 5 }