From b365e08c658eee8d8542cfbc4c1f67161f32fef5 Mon Sep 17 00:00:00 2001 From: Jett Vanbrocklin Date: Sun, 22 Sep 2024 15:37:25 -0500 Subject: [PATCH 1/3] This is my onboarding pull request. --- 10_Examples/jett-onboarding/CMakeLists.txt | 45 +++++++++++++++ 10_Examples/jett-onboarding/package.xml | 21 +++++++ .../jett-onboarding/src/hello_world.cpp | 13 +++++ .../src/publisher_member_function.cpp | 56 +++++++++++++++++++ .../src/subscriber_member_function.cpp | 45 +++++++++++++++ 10_Examples/jett-onboarding/src/talking.cpp | 35 ++++++++++++ 10_Examples/jett-onboarding/src/talking.h | 23 ++++++++ 10_Examples/jett-onboarding/src/test.cpp | 28 ++++++++++ 8 files changed, 266 insertions(+) create mode 100644 10_Examples/jett-onboarding/CMakeLists.txt create mode 100644 10_Examples/jett-onboarding/package.xml create mode 100644 10_Examples/jett-onboarding/src/hello_world.cpp create mode 100644 10_Examples/jett-onboarding/src/publisher_member_function.cpp create mode 100644 10_Examples/jett-onboarding/src/subscriber_member_function.cpp create mode 100644 10_Examples/jett-onboarding/src/talking.cpp create mode 100644 10_Examples/jett-onboarding/src/talking.h create mode 100644 10_Examples/jett-onboarding/src/test.cpp diff --git a/10_Examples/jett-onboarding/CMakeLists.txt b/10_Examples/jett-onboarding/CMakeLists.txt new file mode 100644 index 00000000..ab4f8840 --- /dev/null +++ b/10_Examples/jett-onboarding/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.8) +project(jett-onboarding) + +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-Wall -Wextra -Wpedantic) +endif() + +# find dependencies +find_package(ament_cmake REQUIRED) +find_package(rclcpp REQUIRED) +find_package(std_msgs REQUIRED) + +# uncomment the following section in order to fill in +# further dependencies manually. +# find_package( REQUIRED) +add_executable(listener src/subscriber_member_function.cpp) +ament_target_dependencies(listener rclcpp std_msgs) + +add_executable(talker src/publisher_member_function.cpp) +ament_target_dependencies(talker rclcpp std_msgs) +install(TARGETS + talker + listener + DESTINATION lib/${PROJECT_NAME}) + +add_executable(hello_world src/hello_world.cpp src/talking.cpp src/test.cpp) +install(TARGETS + hello_world + DESTINATION lib/${PROJECT_NAME}) + +if(BUILD_TESTING) + find_package(ament_lint_auto REQUIRED) + + # the following line skips the linter which checks for copyrights + # comment the line when a copyright and license is added to all source files + set(ament_cmake_copyright_FOUND TRUE) + + # the following line skips cpplint (only works in a git repo) + # comment the line when this package is in a git repo and when + # a copyright and license is added to all source files + set(ament_cmake_cpplint_FOUND TRUE) + ament_lint_auto_find_test_dependencies() +endif() + +ament_package() diff --git a/10_Examples/jett-onboarding/package.xml b/10_Examples/jett-onboarding/package.xml new file mode 100644 index 00000000..3af362a4 --- /dev/null +++ b/10_Examples/jett-onboarding/package.xml @@ -0,0 +1,21 @@ + + + + jett-onboarding + 0.0.0 + TODO: Package description + jettvanbrocklin + TODO: License declaration + + ament_cmake + + rclcpp + std_msgs + + ament_lint_auto + ament_lint_common + + + ament_cmake + + diff --git a/10_Examples/jett-onboarding/src/hello_world.cpp b/10_Examples/jett-onboarding/src/hello_world.cpp new file mode 100644 index 00000000..a9bcc870 --- /dev/null +++ b/10_Examples/jett-onboarding/src/hello_world.cpp @@ -0,0 +1,13 @@ +#include "talking.h" +#include +#include "string.h" + +using namespace std; + +int test(); + +int main(int argc, char **argv) +{ + test(); + return 0; +} diff --git a/10_Examples/jett-onboarding/src/publisher_member_function.cpp b/10_Examples/jett-onboarding/src/publisher_member_function.cpp new file mode 100644 index 00000000..2ce7ebed --- /dev/null +++ b/10_Examples/jett-onboarding/src/publisher_member_function.cpp @@ -0,0 +1,56 @@ +// Copyright 2016 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "rclcpp/rclcpp.hpp" +#include "std_msgs/msg/string.hpp" + +using namespace std::chrono_literals; + +/* This example creates a subclass of Node and uses std::bind() to register a + * member function as a callback from the timer. */ + +class MinimalPublisher : public rclcpp::Node +{ +public: + MinimalPublisher() + : Node("minimal_publisher"), count_(0) + { + publisher_ = this->create_publisher("topic", 10); + timer_ = this->create_wall_timer( + 500ms, std::bind(&MinimalPublisher::timer_callback, this)); + } + +private: + void timer_callback() + { + auto message = int[]; + message.data = "Hello, world! " + std::to_string(count_++); + RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str()); + publisher_->publish(message); + } + rclcpp::TimerBase::SharedPtr timer_; + rclcpp::Publisher::SharedPtr publisher_; + size_t count_; +}; + +int main(int argc, char *argv[]) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +} diff --git a/10_Examples/jett-onboarding/src/subscriber_member_function.cpp b/10_Examples/jett-onboarding/src/subscriber_member_function.cpp new file mode 100644 index 00000000..39d29f78 --- /dev/null +++ b/10_Examples/jett-onboarding/src/subscriber_member_function.cpp @@ -0,0 +1,45 @@ +// Copyright 2016 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +using std::placeholders::_1; + +class MinimalSubscriber : public rclcpp::Node +{ +public: + MinimalSubscriber() + : Node("minimal_subscriber") + { + subscription_ = this->create_subscription( + "topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1)); + } + +private: + void topic_callback(const std_msgs::msg::String::SharedPtr msg) const + { + RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str()); + } + rclcpp::Subscription::SharedPtr subscription_; +}; + +int main(int argc, char *argv[]) +{ + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + return 0; +} diff --git a/10_Examples/jett-onboarding/src/talking.cpp b/10_Examples/jett-onboarding/src/talking.cpp new file mode 100644 index 00000000..f3e4714e --- /dev/null +++ b/10_Examples/jett-onboarding/src/talking.cpp @@ -0,0 +1,35 @@ +#include "talking.h" +#include +#include "string.h" + +// using namespace std; + +talking::talking() +{ + this->num1 = 10; +} + +talking::~talking() +{ +} + +void talking::talk(string words) +{ + cout << words << endl; +} + +void talking::numberPrint() +{ + cout << num1 << endl; +} + +int talking::twoToPower(int i) +{ + int value = 1; + while (i > 0) + { + value *= 2; + i--; + } + return value; +} \ No newline at end of file diff --git a/10_Examples/jett-onboarding/src/talking.h b/10_Examples/jett-onboarding/src/talking.h new file mode 100644 index 00000000..415709cf --- /dev/null +++ b/10_Examples/jett-onboarding/src/talking.h @@ -0,0 +1,23 @@ +#ifndef TALKING_H_ +#define TALKING_H_ + +#include +#include "string.h" + +using namespace std; + +class talking +{ +public: + int num1; + + talking(); // constructor + ~talking(); // destructor + + void talk(string words); + void numberPrint(); + + int twoToPower(int i); +}; + +#endif \ No newline at end of file diff --git a/10_Examples/jett-onboarding/src/test.cpp b/10_Examples/jett-onboarding/src/test.cpp new file mode 100644 index 00000000..6efbcf0e --- /dev/null +++ b/10_Examples/jett-onboarding/src/test.cpp @@ -0,0 +1,28 @@ +#include +#include "talking.h" +#include "string.h" + +int test() +{ + + talking t1; + + // talk function + string words1 = "Hello World"; + t1.talk(words1); + t1.talk("Hello Again"); + + // number print + t1.num1 = 0; + t1.numberPrint(); + t1.num1 += 10; + t1.numberPrint(); + + // twoToPower + for (int i = 0; i < 10; i++) + { + cout << "2^" << i << " = " << t1.twoToPower(i) << endl; + } + + return 0; +} \ No newline at end of file From df258df8f75f4e4431b7fc5ad6e058270386f4a2 Mon Sep 17 00:00:00 2001 From: Jett Vanbrocklin Date: Mon, 30 Sep 2024 15:17:13 -0500 Subject: [PATCH 2/3] Jett's Onboarding Pull Request, Take 2 --- 10_Examples/jett-onboarding/src/publisher_member_function.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/10_Examples/jett-onboarding/src/publisher_member_function.cpp b/10_Examples/jett-onboarding/src/publisher_member_function.cpp index 2ce7ebed..5b4a81bb 100644 --- a/10_Examples/jett-onboarding/src/publisher_member_function.cpp +++ b/10_Examples/jett-onboarding/src/publisher_member_function.cpp @@ -31,13 +31,13 @@ class MinimalPublisher : public rclcpp::Node { publisher_ = this->create_publisher("topic", 10); timer_ = this->create_wall_timer( - 500ms, std::bind(&MinimalPublisher::timer_callback, this)); + 500ms, std::bind(&MinimalPublisher::timer_callback, this)); } private: void timer_callback() { - auto message = int[]; + auto message = std_msgs::msg::String(); message.data = "Hello, world! " + std::to_string(count_++); RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str()); publisher_->publish(message); From 88f8f2ee8fd6d861ce9d79703744052855dea7f4 Mon Sep 17 00:00:00 2001 From: Jett Vanbrocklin Date: Sun, 6 Oct 2024 15:50:23 -0500 Subject: [PATCH 3/3] I updated my Test file to follow gtest convention. --- 10_Examples/jett-onboarding/CMakeLists.txt | 51 ++++++++++++++----- .../jett-onboarding/src/hello_world.cpp | 7 +-- 10_Examples/jett-onboarding/src/talking.cpp | 29 ++++++----- 10_Examples/jett-onboarding/src/talking.h | 14 ++--- 10_Examples/jett-onboarding/src/test.cpp | 28 ---------- 10_Examples/jett-onboarding/src/testFile.cpp | 43 ++++++++++++++++ 6 files changed, 108 insertions(+), 64 deletions(-) delete mode 100644 10_Examples/jett-onboarding/src/test.cpp create mode 100644 10_Examples/jett-onboarding/src/testFile.cpp diff --git a/10_Examples/jett-onboarding/CMakeLists.txt b/10_Examples/jett-onboarding/CMakeLists.txt index ab4f8840..91be2267 100644 --- a/10_Examples/jett-onboarding/CMakeLists.txt +++ b/10_Examples/jett-onboarding/CMakeLists.txt @@ -10,6 +10,8 @@ find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) +find_package(ament_cmake_gtest REQUIRED) + # uncomment the following section in order to fill in # further dependencies manually. # find_package( REQUIRED) @@ -23,23 +25,48 @@ install(TARGETS listener DESTINATION lib/${PROJECT_NAME}) -add_executable(hello_world src/hello_world.cpp src/talking.cpp src/test.cpp) +add_executable(hello_world src/hello_world.cpp) install(TARGETS hello_world DESTINATION lib/${PROJECT_NAME}) +target_link_libraries(hello_world +talking +) -if(BUILD_TESTING) - find_package(ament_lint_auto REQUIRED) +# Add Class as Library to allow it to share across files easier probably +add_library(talking SHARED src/talking.cpp) +ament_target_dependencies(talking + ${DEPENDENCIES} +) +ament_export_targets(talking HAS_LIBRARY_TARGET) +install( + TARGETS talking + EXPORT talking + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin + INCLUDES DESTINATION include + ) - # the following line skips the linter which checks for copyrights - # comment the line when a copyright and license is added to all source files - set(ament_cmake_copyright_FOUND TRUE) - # the following line skips cpplint (only works in a git repo) - # comment the line when this package is in a git repo and when - # a copyright and license is added to all source files - set(ament_cmake_cpplint_FOUND TRUE) - ament_lint_auto_find_test_dependencies() -endif() +ament_add_gtest(testFile src/testFile.cpp) +target_link_libraries(testFile + talking + gtest_main +) + +# if(BUILD_TESTING) +# find_package(ament_lint_auto REQUIRED) + +# # the following line skips the linter which checks for copyrights +# # comment the line when a copyright and license is added to all source files +# set(ament_cmake_copyright_FOUND TRUE) + +# # the following line skips cpplint (only works in a git repo) +# # comment the line when this package is in a git repo and when +# # a copyright and license is added to all source files +# set(ament_cmake_cpplint_FOUND TRUE) +# ament_lint_auto_find_test_dependencies() +# endif() ament_package() diff --git a/10_Examples/jett-onboarding/src/hello_world.cpp b/10_Examples/jett-onboarding/src/hello_world.cpp index a9bcc870..8482cc12 100644 --- a/10_Examples/jett-onboarding/src/hello_world.cpp +++ b/10_Examples/jett-onboarding/src/hello_world.cpp @@ -6,8 +6,9 @@ using namespace std; int test(); -int main(int argc, char **argv) +int main(int argc, char ** argv) { - test(); - return 0; + talking t1; + t1.talk("Hello World"); + return 0; } diff --git a/10_Examples/jett-onboarding/src/talking.cpp b/10_Examples/jett-onboarding/src/talking.cpp index f3e4714e..91a2900a 100644 --- a/10_Examples/jett-onboarding/src/talking.cpp +++ b/10_Examples/jett-onboarding/src/talking.cpp @@ -2,34 +2,35 @@ #include #include "string.h" -// using namespace std; +using namespace std; talking::talking() { - this->num1 = 10; + this->num1 = 10; } talking::~talking() { } -void talking::talk(string words) +string talking::talk(string words) { - cout << words << endl; + cout << words << endl; + return words; } -void talking::numberPrint() +int talking::numberPrint() { - cout << num1 << endl; + cout << num1 << endl; + return num1; } int talking::twoToPower(int i) { - int value = 1; - while (i > 0) - { - value *= 2; - i--; - } - return value; -} \ No newline at end of file + int value = 1; + while (i > 0) { + value *= 2; + i--; + } + return value; +} diff --git a/10_Examples/jett-onboarding/src/talking.h b/10_Examples/jett-onboarding/src/talking.h index 415709cf..a4b00f2b 100644 --- a/10_Examples/jett-onboarding/src/talking.h +++ b/10_Examples/jett-onboarding/src/talking.h @@ -9,15 +9,15 @@ using namespace std; class talking { public: - int num1; + int num1; - talking(); // constructor - ~talking(); // destructor + talking(); // constructor + ~talking(); // destructor - void talk(string words); - void numberPrint(); + string talk(string words); + int numberPrint(); - int twoToPower(int i); + int twoToPower(int i); }; -#endif \ No newline at end of file +#endif diff --git a/10_Examples/jett-onboarding/src/test.cpp b/10_Examples/jett-onboarding/src/test.cpp deleted file mode 100644 index 6efbcf0e..00000000 --- a/10_Examples/jett-onboarding/src/test.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include "talking.h" -#include "string.h" - -int test() -{ - - talking t1; - - // talk function - string words1 = "Hello World"; - t1.talk(words1); - t1.talk("Hello Again"); - - // number print - t1.num1 = 0; - t1.numberPrint(); - t1.num1 += 10; - t1.numberPrint(); - - // twoToPower - for (int i = 0; i < 10; i++) - { - cout << "2^" << i << " = " << t1.twoToPower(i) << endl; - } - - return 0; -} \ No newline at end of file diff --git a/10_Examples/jett-onboarding/src/testFile.cpp b/10_Examples/jett-onboarding/src/testFile.cpp new file mode 100644 index 00000000..eb8a9cae --- /dev/null +++ b/10_Examples/jett-onboarding/src/testFile.cpp @@ -0,0 +1,43 @@ +#include "gtest/gtest.h" +#include "talking.h" +#include "string.h" + +class TestExample : public ::testing::Test +{ +protected: + TestExample() + { + talking_ = std::make_shared(); + } + + void SetUp() override + { + } + + void TearDown() override + { + } + + std::shared_ptr talking_; + +}; + +TEST_F(TestExample, testTalk) { + //testing the talk function + EXPECT_EQ("Test", talking_->talk("Test")); +} + +TEST_F(TestExample, testNumberPrint) { + EXPECT_TRUE(talking_->numberPrint() == talking_->num1); +} + +TEST_F(TestExample, testTwoToPower) { + EXPECT_EQ(talking_->twoToPower(5) == (2 ^ 5)); +} + + +// int main(int argc, char ** argv) +// { +// ::testing::InitGoogleTest(&argc, argv); +// return RUN_ALL_TESTS(); +// }