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

I hope this works pt5 #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nothing here
76 changes: 76 additions & 0 deletions software_training/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
cmake_minimum_required(VERSION 3.0.2)
project(software_training)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
message_generation
actionlib_msgs
actionlib
)

include_directories(
# include
${catkin_INCLUDE_DIRS}
)


## Generate messages in the 'msg' folder
add_message_files(
FILES
software_training_message.msg

)

## Generate services in the 'srv' folder
add_service_files(
FILES
reset_service.srv

)

## Generate actions in the 'action' folder
add_action_files(
FILES
software_training_action.action

)

## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
std_msgs
actionlib_msgs
#reset_service
)




catkin_package(
CATKIN_DEPENDS
roscpp
std_msgs
message_runtime
actionlib_msgs

)


add_executable(software_training_node src/software_training_node.cpp)
target_link_libraries(software_training_node ${catkin_LIBRARIES})
add_dependencies(software_training_node software_training_generate_messages_cpp)

add_executable(moving_turtle_service src/moving_turtle_service.cpp)
target_link_libraries(moving_turtle_service ${catkin_LIBRARIES})
add_dependencies(moving_turtle_service software_training_generate_messages_cpp)

add_executable(moving_turtle_action_node src/moving_turtle_action_node.cpp)
target_link_libraries(moving_turtle_action_node ${catkin_LIBRARIES})
add_dependencies(moving_turtle_action_node software_training_generate_messages_cpp)
10 changes: 10 additions & 0 deletions software_training/action/software_training_action.action
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#goal
float32 x_position_to_waypoint
float32 y_position_to_waypoint
---
#result
duration total_time
---
#feedback
float32 x_distance_to_goal
float32 y_distance_to_goal
9 changes: 9 additions & 0 deletions software_training/launch/software_training.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<launch>
<node name="turtlesim_node" pkg="turtlesim" type="turtlesim_node">
<param name="background_b" value="0" type="int"/>
<param name="background_g" value="128" type="int"/>
<param name="background_r" value="128" type="int"/>
</node>

<node name="software_training_node" pkg="software_training" type="software_training_node"/>
</launch>
3 changes: 3 additions & 0 deletions software_training/msg/software_training_message.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
float64 x_stationary_turtle_to_moving_turtle
float64 y_stationary_turtle_to_moving_turtle
float64 distance
68 changes: 68 additions & 0 deletions software_training/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0"?>
<package format="2">
<name>software_training</name>
<version>0.0.0</version>
<description>The software_training package</description>

<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="[email protected]">Jane Doe</maintainer> -->
<maintainer email="[email protected]">niiquaye</maintainer>


<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>


<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/software_training</url> -->


<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="[email protected]">Jane Doe</author> -->


<!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
<!-- <depend>roscpp</depend> -->
<!-- Note that this is equivalent to the following: -->
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_depend>actionlib_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>
<exec_depend>actionlib_msgs</exec_depend>

<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->

</export>
</package>
103 changes: 103 additions & 0 deletions software_training/src/moving_turtle_action_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "ros/ros.h"
#include "actionlib/server/simple_action_server.h"
#include "software_training/software_training_actionAction.h"
#include "geometry_msgs/Twist.h"
#include <string>

class moving_turtle_action_server{

protected:
//create node handle
ros::NodeHandle nh;
//create action server
actionlib::SimpleActionServer
<software_training::software_training_actionAction> action_server;
//action server name
std::string name;
//create feedback for action server
software_training::software_training_actionFeedback feedback;
//create result for action server
software_training::software_training_actionResult result;
//create publisher to cmd_vel topic for moving turtle
ros::Publisher pub_moving_turtle;
public:
//constructor
moving_turtle_action_server(std::string _name_of_server);
//callback function
void executeCB(const software_training::software_training_actionGoalConstPtr& goal);

};

moving_turtle_action_server::moving_turtle_action_server(std::string _name_of_server):
action_server(nh,_name_of_server, boost::bind(&moving_turtle_action_server::executeCB, this, _1), false),
name(_name_of_server)
{
//start action server
action_server.start();
// instantiate publisher
pub_moving_turtle = nh.advertise<geometry_msgs::Twist>("/moving_turtle/cmd_vel",1000);
}

void moving_turtle_action_server::executeCB(
const software_training::software_training_actionGoalConstPtr& goal)
{
//set a variable to check if finished
//set ros rate
ros::Rate loop_rate{1};
//get components of the goal
double x_goal = goal->x_position_to_waypoint;
double y_goal = goal->y_position_to_waypoint;
//create a message to publish
geometry_msgs::Twist msg;
//initialize message to be published
msg.linear.x = 0.0;
msg.linear.y = 0.0;
msg.linear.z = 0.0;
msg.angular.x = 0.0;
msg.angular.y = 0.0;
msg.angular.z = 0.0;

//start of timer
ros::Time start = ros::Time::now();

while(ros::ok() && action_server.isActive() && !action_server.isNewGoalAvailable()){
//publish to cmd_vel topic one interval at a time
if(msg.linear.x < x_goal || msg.linear.y < y_goal){
if(msg.linear.x < x_goal){
msg.linear.x++;
feedback.x_distance_to_goal = x_goal - msg.linear.x;
}
if(msg.linear.y < y_goal){
msg.linear.y++;
feedback.y_distance_to_goal = y_goal - msg.linear.y;
}
//publish message
pub_moving_turtle.publish(msg);
//publish feedback
action_server.publishFeedback(feedback);
}
else if(msg.linear.x >= x_goal && msg.linear.y >= y_goal) break;
loop_rate.sleep();
}
//check to see if action is still up and running since we broke the loop
if(!ros::ok() || action_server.isPreemptRequested() || !action_server.isActive()){
ROS_ERROR_STREAM("moving_turtle_action_server NOT AVAILABLE/SHUTDOWN/NEW GOAL");
}
else{
//end of timer
ros::Time end = ros::Time::now();
//get total time
ros::Duration res_time = end - start;
result.total_time = res_time;
//set result
action_server.setSucceeded(result);
}

}

int main(int argc, char **argv){
ros::init(argc, argv, "moving_turtle_action_node");
moving_turtle_action_server as("mover");
ros::spin();
return 0;
}
49 changes: 49 additions & 0 deletions software_training/src/moving_turtle_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "ros/ros.h"
#include "software_training/reset_service.h"
#include "turtlesim/TeleportAbsolute.h"

class server{
public:
//callback function for server
bool callback(software_training::reset_service::Request& request,
software_training::reset_service::Response& response);
// helper function to call moving_turtle_client - client call
bool moving_turtle_client();

};

bool server::callback(software_training::reset_service::Request& request,
software_training::reset_service::Response& response){
response.success = (moving_turtle_client())? true : false;
return true;
}

bool server::moving_turtle_client(){
turtlesim::TeleportAbsolute srv;
srv.request.x = 5.544444561004639;
srv.request.y = 5.544444561004639;
srv.request.theta = 0;
ros::NodeHandle nh;
ros::ServiceClient client = nh.serviceClient<turtlesim::TeleportAbsolute>
("/moving_turtle/teleport_absolute");
if(client.call(srv)){
srv.response;
ROS_INFO_STREAM("'moving_turtle' reseted");
return true;
}
else{
ROS_ERROR_STREAM("the moving turtle service failed to call");
return false;
}

}


int main(int argc, char ** argv){
ros::init(argc, argv, "moving_turtle_reset_server_node");
ros::NodeHandle nh;
server srv_object;
ros::ServiceServer srv = nh.advertiseService("moving_turtle_service", &server::callback, &srv_object);
ros::spin();
return 0;
}
Loading