Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
37 changes: 37 additions & 0 deletions src/challenge/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.5)
project(challenge)

# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

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(geometry_msgs REQUIRED)
find_package(turtlesim REQUIRED)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

add_executable(solution src/solution.cpp)
ament_target_dependencies(solution rclcpp geometry_msgs turtlesim)

install(TARGETS
solution
DESTINATION lib/${PROJECT_NAME})

ament_package()

21 changes: 21 additions & 0 deletions src/challenge/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>challenge</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="warrenlukedmello@gmail.com">warren2005</maintainer>
<license>TODO: License declaration</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>rclcpp</depend>
<depend>geometry_msgs</depend>

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

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
59 changes: 59 additions & 0 deletions src/challenge/src/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <chrono>
#include <cmath>
#include "rclcpp/rclcpp.hpp"
#include "turtlesim/msg/pose.hpp"
#include "turtlesim/srv/teleport_absolute.hpp"
#include "geometry_msgs/msg/twist.hpp"

using namespace std::chrono_literals;

class SinePlotter : public rclcpp::Node
{
public:
SinePlotter() : Node("solution_node"), t_(0.0)
{
client_ = this->create_client<turtlesim::srv::TeleportAbsolute>("/turtle1/teleport_absolute");

// wait for teleport service
while(!client_->wait_for_service(1s)) {
RCLCPP_INFO(this->get_logger(), "Waiting for turtlesim...");
}

timer_ = this->create_wall_timer(20ms, std::bind(&SinePlotter::plot_callback, this));
}

private:
void plot_callback()
{
double x0 = 5.5; // center of screen (midpoint)
double y0 = 5.5; // vertical starting point
double amplitude = 3.0; // height of sine wave
double speed = 0.05; // how fast to move across x-axis
double frequency = 2.0; // sine wave frequency

double x = x0 + t_;
double y = y0 + amplitude * std::sin(frequency * t_);
double theta = 0.0; // orientation doesn't matter

auto request = std::make_shared<turtlesim::srv::TeleportAbsolute::Request>();
request->x = x;
request->y = y;
request->theta = theta;
client_->async_send_request(request);

t_ += speed;
}

rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Client<turtlesim::srv::TeleportAbsolute>::SharedPtr client_;
double t_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<SinePlotter>());
rclcpp::shutdown();
return 0;
}