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

Corey Onboarding #95

Open
wants to merge 7 commits into
base: onboarding
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
34 changes: 34 additions & 0 deletions 10_Examples/corey-onboarding/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.8)
project(corey-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(std_msgs REQUIRED)
find_package(rclcpp REQUIRED)
find_package(ament_cmake_gtest REQUIRED)

ament_add_gtest(bankmaincode
test/banktest.cpp
)

add_executable(publisher_node src/publisher.cpp)
ament_target_dependencies(publisher_node rclcpp std_msgs)

add_executable(subscriber_node src/subscriber.cpp)
ament_target_dependencies(subscriber_node rclcpp std_msgs)

ament_add_gtest(test_person test_person.cpp)
target_link_libraries(test_person ${GTEST_INCLUDE_DIRS} pthread)

add_executable(hello_world src/helloworld.cpp)
install(TARGETS
hello_world
publisher_node
subscriber_node
DESTINATION lib/${PROJECT_NAME})

ament_package()
Binary file added 10_Examples/corey-onboarding/banktest
Binary file not shown.
21 changes: 21 additions & 0 deletions 10_Examples/corey-onboarding/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>corey-onboarding</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="[email protected]">corey</maintainer>
<license>TODO: License declaration</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<exec_depend>rclcpp</exec_depend>
<exec_depend>std_msgs</exec_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Binary file added 10_Examples/corey-onboarding/src/bank_executable
Binary file not shown.
23 changes: 23 additions & 0 deletions 10_Examples/corey-onboarding/src/bankheader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
#include <string>

using namespace std;

class BankAccount
{

public:

string name; //Variable

int balance; //Variable

void withdraw(int amount); //my method

void print(); //my method

BankAccount(string AccountName, int AccountBalance); // declare constructor

};


35 changes: 35 additions & 0 deletions 10_Examples/corey-onboarding/src/bankmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <string>
#include "bankheader.h"

using namespace std;

BankAccount::BankAccount(string AccountName, int AccountBalance)
{

name = AccountName;
balance = AccountBalance;

}

void BankAccount::withdraw(int amount)
{
balance = balance - amount;
}

void BankAccount::print()
{
cout << name << " has " << "a balance of " << balance << endl;
}


// int main ()
// {
// BankAccount account1("kelly", 50000);
// account1.print();
// account1.withdraw(2356);
// account1.print();

// return 0;
// }
//^ commented out to run the gtest
7 changes: 7 additions & 0 deletions 10_Examples/corey-onboarding/src/helloworld.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>

int main(int argc, char ** argv)
{
std::cout << "Hello World!" << std::endl;
return 0;
}
48 changes: 48 additions & 0 deletions 10_Examples/corey-onboarding/src/publisher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <chrono>
#include <string>
#include <memory>
#include <functional>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

class SimplePublisher : public rclcpp::Node
{
public:
SimplePublisher()
: Node("publisher_node")
{
CounterValue = 0;

publisherObject = this->create_publisher<std_msgs::msg::String>("communication_topic", 20);

timerObject = this->create_wall_timer(
1000ms, std::bind(
&SimplePublisher::callbackFunction,
this));
}

private:
void callbackFunction()
{
CounterValue++;
auto message = std_msgs::msg::String();
message.data = "Message Number: " + std::to_string(CounterValue);

RCLCPP_INFO(this->get_logger(), "Publishing message: '%s'", message.data.c_str());
publisherObject->publish(message);
}

rclcpp::TimerBase::SharedPtr timerObject;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisherObject;
int CounterValue;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<SimplePublisher>());
rclcpp::shutdown();
return 0;
}
35 changes: 35 additions & 0 deletions 10_Examples/corey-onboarding/src/subscriber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <chrono>
#include <string>
#include <memory>
#include <functional>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using std::placeholders::_1;

class SimpleSubscriber : public rclcpp::Node
{
public:
SimpleSubscriber()
: Node("subscriber_node")
{
subscriberObject = this->create_subscription<std_msgs::msg::String>(
"communication_topic", 10, std::bind(&SimpleSubscriber::callbackFunction, this, _1));
}

private:
void callbackFunction(const std_msgs::msg::String & msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
}

rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscriberObject;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<SimpleSubscriber>());
rclcpp::shutdown();
return 0;
}
44 changes: 44 additions & 0 deletions 10_Examples/corey-onboarding/test/banktest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "bankheader.h"
#include "gtest/gtest.h"

class BankAccountTest : public ::testing::Test

{
protected:
BankAccountTest()
{
bankmaincode = std::make_shared<BankAccount>("kelly", 50000);
}

void SetUp() override
{
}

void TearDown() override
{
}
std::shared_ptr<BankAccount> bankmaincode;

};

TEST_F(BankAccountTest, testWithdrawMoney)
{
bankmaincode->withdraw(20000);
EXPECT_EQ(bankmaincode->balance, 30000);
EXPECT_TRUE(bankmaincode->balance == 30000);
EXPECT_FALSE(bankmaincode->balance == 35000);
}

TEST_F(BankAccountTest, testPrint)
{
testing::internal::CaptureStdout();
bankmaincode->print();
std::string output = testing::internal::GetCapturedStdout();
EXPECT_STREQ(output.c_str(), "kelly has a balance of 50000\n");
}

int main(int argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Loading