-
Notifications
You must be signed in to change notification settings - Fork 34
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
Software Training #16
base: master
Are you sure you want to change the base?
Changes from all commits
6937f7d
27d4731
163a3a3
5b2ef7f
c029bb8
0ff4154
a37862c
a2eeb5f
2677867
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dbus-1/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
cmake_minimum_required(VERSION 3.0.2) | ||
project(software_training_assignment) | ||
|
||
|
||
find_package(catkin REQUIRED COMPONENTS | ||
roscpp | ||
std_msgs | ||
message_generation | ||
turtlesim | ||
actionlib | ||
actionlib_msgs | ||
) | ||
|
||
find_package(Boost REQUIRED COMPONENTS system) | ||
|
||
## Generate messages in the 'msg' folder | ||
add_message_files( | ||
FILES | ||
Position.msg | ||
) | ||
|
||
## Generate actions in the 'action' folder | ||
add_action_files( | ||
FILES | ||
Goto.action | ||
) | ||
|
||
## Generate added messages and services with any dependencies listed here | ||
generate_messages( | ||
DEPENDENCIES | ||
std_msgs | ||
actionlib_msgs | ||
) | ||
|
||
catkin_package( | ||
CATKIN_DEPENDS message_runtime actionlib_msgs | ||
) | ||
|
||
include_directories( | ||
# include | ||
${catkin_INCLUDE_DIRS} | ||
${Boost_INCLUDE_DIRS} | ||
) | ||
|
||
install(DIRECTORY include/${PROJECT_NAME}/ | ||
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} | ||
FILES_MATCHING PATTERN "*.h" | ||
PATTERN ".svn" EXCLUDE | ||
) | ||
|
||
|
||
|
||
|
||
add_executable(spawnturtle src/spawnturtle.cpp) | ||
target_link_libraries(spawnturtle ${catkin_LIBRARIES}) | ||
# add_dependencies(spawnturtle software_training_assignment_gencpp) | ||
|
||
add_executable(testing src/testing.cpp) | ||
target_link_libraries(testing ${catkin_LIBRARIES}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
float32 x | ||
float32 y | ||
--- | ||
duration time_elapsed | ||
--- | ||
float32 distance |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<launch> | ||
<group ns="turtlesim1"> | ||
<node pkg="turtlesim" name="sim1" type="turtlesim_node"> | ||
<rosparam param="background_r">128</rosparam> | ||
<rosparam param="background_g">128</rosparam> | ||
<rosparam param="background_b">0</rosparam> | ||
</node> | ||
<node pkg="software_training_assignment" name="sim2" type="spawnturtle" output="screen"/> | ||
</group> | ||
</launch> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
float32 x | ||
float32 y | ||
float32 distance |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0"?> | ||
<package format="2"> | ||
<name>software_training_assignment</name> | ||
<version>0.0.0</version> | ||
<description>The software_training_assignment package</description> | ||
|
||
<maintainer email="[email protected]">htieu</maintainer> | ||
|
||
<license>MIT</license> | ||
|
||
|
||
<build_depend>message_generation</build_depend> | ||
|
||
<exec_depend>message_runtime</exec_depend> | ||
<test_depend>gtest</test_depend> | ||
<buildtool_depend>catkin</buildtool_depend> | ||
<build_depend>roscpp</build_depend> | ||
<build_depend>std_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> | ||
<build_depend>turtlesim</build_depend> | ||
<exec_depend>turtlesim</exec_depend> | ||
<build_depend>actionlib</build_depend> | ||
<exec_depend>actionlib</exec_depend> | ||
<build_depend>actionlib_msgs</build_depend> | ||
<exec_depend>actionlib_msgs</exec_depend> | ||
<export> | ||
|
||
</export> | ||
</package> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include <software_training_assignment/GotoAction.h> | ||
#include <actionlib/server/simple_action_server.h> | ||
#include <turtlesim/Pose.h> | ||
#include <geometry_msgs/Twist.h> | ||
|
||
namespace A | ||
{ | ||
class GotoAction | ||
{ | ||
public: | ||
ros::NodeHandle node; | ||
GotoAction(std::string name) : as_(node, name, boost::bind(&GotoAction::executeCB, this, _1), false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of having NodeHandle as public you can pass the NodeHandle into the constructor. Nobody should be modifying the NodeHandle so it should be private |
||
{ | ||
as_.start(); | ||
ROS_INFO("Action server started"); | ||
} | ||
|
||
~GotoAction(void) | ||
{ | ||
} | ||
|
||
ros::Publisher pub; | ||
ros::Subscriber sub; | ||
double x, y; | ||
|
||
void subCallback(const turtlesim::Pose::ConstPtr& msg) | ||
{ | ||
x = msg->x; | ||
y = msg->y; | ||
} | ||
|
||
void executeCB(const software_training_assignment::GotoGoal::ConstPtr &goal) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the easiest way to write this function would be to determine distance and angle from the point and send the twist msg until you've reached the point. the feedback would be given as distance from the goal and once you have reached the succeed flag could be set. this is something to think about |
||
{ | ||
|
||
ROS_INFO("executeCB called"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this formatting is all off, make sure to format properly so that it's easy to read the code |
||
double relativeX = goal->x - x, | ||
relativeY = goal->y - y, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. separate each double declaration, it's cleaner and easier to read |
||
relativeDist = sqrt(pow(relativeX, 2) + pow(relativeY, 2)), | ||
theta = acos(relativeX / relativeDist); | ||
|
||
if (relativeY < 0) | ||
theta *= -1; | ||
|
||
bool success = true; | ||
|
||
geometry_msgs::Twist rotation; | ||
|
||
feedback_.distance = relativeDist; | ||
as_.publishFeedback(feedback_); | ||
|
||
rotation.angular.z = theta; | ||
|
||
pub.publish(rotation); | ||
|
||
ros::Duration(1).sleep(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this sleep for? |
||
|
||
double gotoCount = (int)relativeDist / 2, gotoRe = relativeDist - (gotoCount * 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above for declaring variables |
||
ros::Duration gotoDuration(1); | ||
|
||
geometry_msgs::Twist forward; | ||
|
||
forward.linear.x = 2; | ||
for (int count = 0; count < gotoCount; count++) | ||
{ | ||
if(as_.isPreemptRequested() || !ros::ok()) | ||
{ | ||
as_.setPreempted(); | ||
success = false; | ||
break; | ||
} | ||
pub.publish(forward); | ||
ros::Duration(1).sleep(); | ||
gotoDuration += ros::Duration(1); | ||
|
||
} | ||
|
||
forward.linear.x = gotoRe; | ||
|
||
gotoDuration += ros::Duration(2); | ||
|
||
pub.publish(forward); | ||
ros::Duration(1).sleep(); | ||
|
||
if (success) | ||
{ | ||
result_.time_elapsed = gotoDuration; | ||
as_.setSucceeded(result_); | ||
} | ||
|
||
} | ||
|
||
protected: | ||
software_training_assignment::GotoFeedback feedback_; | ||
actionlib::SimpleActionServer<software_training_assignment::GotoAction> as_; | ||
software_training_assignment::GotoResult result_; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <turtlesim/Kill.h> | ||
#include "killTurtles.h" | ||
|
||
bool killTurtles(ros::NodeHandle n) | ||
{ | ||
ros::ServiceClient killClient = n.serviceClient<turtlesim::Kill>("kill"); | ||
|
||
ros::service::waitForService("kill"); | ||
|
||
turtlesim::Kill killSrv; | ||
|
||
killSrv.request.name = "turtle1"; | ||
|
||
|
||
if (killClient.call(killSrv)) | ||
{ | ||
ROS_INFO_STREAM (killSrv.request.name << " killed"); | ||
} | ||
else | ||
{ | ||
ROS_ERROR("Failed to call service kill"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef KILLTURTLES_H | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should just use |
||
#define KILLTURTLES_H | ||
|
||
#include <ros/ros.h> | ||
|
||
bool killTurtles(ros::NodeHandle n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should always pass NodeHandle by reference, we don't want create another copy |
||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <ros/ros.h> | ||
#include <software_training_assignment/Position.h> | ||
#include <turtlesim/Pose.h> | ||
#include <cmath> | ||
|
||
namespace P | ||
{ | ||
class Publisher | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't use Publisher as a name, it will be confusing. Instead be more descriptive - PositionPublisher |
||
{ | ||
public: | ||
void publisher() | ||
{ | ||
software_training_assignment::Position msg; | ||
msg.x = x - 5; | ||
msg.y = y - 5; | ||
msg.distance = sqrt(pow(msg.x, 2) + pow(msg.y, 2)); | ||
|
||
pub.publish(msg); | ||
} | ||
void subCallback(const turtlesim::Pose::ConstPtr& msg) | ||
{ | ||
x = msg->x; | ||
y = msg->y; | ||
} | ||
double x = 0, y = 0; | ||
ros::Publisher pub; | ||
ros::Subscriber sub; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <std_srvs/Empty.h> | ||
#include "resetPos.h" | ||
#include "killTurtles.h" | ||
#include "spawnTurtles.h" | ||
|
||
bool resetPos(std_srvs::Empty::Request &req, | ||
std_srvs::Empty::Response &res) | ||
{ | ||
ros::NodeHandle n; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should pass the NodeHandle by reference, you ideally should only need 1 NodeHandle per node you write |
||
|
||
ros::ServiceClient resetClient = n.serviceClient<std_srvs::Empty>("reset"); | ||
|
||
ros::service::waitForService("reset"); | ||
|
||
std_srvs::Empty resetSrv; | ||
|
||
if (resetClient.call(resetSrv)) | ||
{ | ||
killTurtles(n); | ||
spawnTurtles(n, "stationary_turtle", 5, 5); | ||
spawnTurtles(n, "moving_turtle", 25, 10); | ||
ROS_INFO_STREAM ("Resetted"); | ||
} | ||
else | ||
{ | ||
ROS_ERROR("Failed to call service reset"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef RESETPOS_H | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see comment above about header guards |
||
#define RESETPOS_H | ||
|
||
#include <ros/ros.h> | ||
|
||
bool resetPos(std_srvs::Empty::Request &req, | ||
std_srvs::Empty::Response &res); | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <turtlesim/Spawn.h> | ||
#include "spawnTurtles.h" | ||
|
||
|
||
bool spawnTurtles(ros::NodeHandle n, std::string name, int x, int y) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NodeHandle by reference |
||
{ | ||
ros::ServiceClient spawnClient = n.serviceClient<turtlesim::Spawn>("spawn"); | ||
|
||
|
||
ros::service::waitForService("spawn"); | ||
|
||
turtlesim::Spawn spawnSrv; | ||
|
||
spawnSrv.request.x = x; | ||
spawnSrv.request.y = y; | ||
spawnSrv.request.theta = 0; | ||
spawnSrv.request.name = name; | ||
|
||
|
||
if (spawnClient.call(spawnSrv)) | ||
{ | ||
ROS_INFO_STREAM ("Name: " << spawnSrv.response.name); | ||
} | ||
else | ||
{ | ||
ROS_ERROR("Failed to call service spawn"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef SPAWNTURTLES_H | ||
#define SPAWNTURTLES_H | ||
|
||
#include <ros/ros.h> | ||
|
||
bool spawnTurtles(ros::NodeHandle n, std::string name, int x, int y); | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if its build_depend and exec_depend you can just put the depend tag, its cleaner