Browse Source

update code

corvin 5 years ago
parent
commit
3f3772919c

+ 1 - 1
mkr2018_mrc/robot_coordination/include/robot/obstacles.h

@@ -13,7 +13,7 @@ class obstacles
         obstacles() {}
         /** Default destructor */
         virtual ~obstacles() {}
-        vector< vector<geometry_msgs::Point> > getObstacleArray(int pose_x, int pose_y);
+        vector< vector<geometry_msgs::Point> > getObstacleArray(double pose_x, double pose_y);
     protected:
     private:
         vector< vector<geometry_msgs::Point> > obstacleArray;

+ 3 - 3
mkr2018_mrc/robot_coordination/launch/gen_robots_path_nav.launch

@@ -10,11 +10,11 @@ History:
     <arg name="namespace_0" default="robot0" />
     <arg name="namespace_1" default="robot1" />
 
-    <node pkg="robot_coordination" type="robot_control.py" name="robot0_path_gen" output="screen" required="true">
+    <node pkg="robot_coordination" type="robot_control_0.py" name="robot0_path_gen" output="screen" required="true">
         <param name="server_namespace" value="$(arg namespace_0)" />
     </node>
-    
-    <node pkg="robot_coordination" type="robot_control.py" name="robot1_path_gen" output="screen" required="true">
+
+    <node pkg="robot_coordination" type="robot_control_1.py" name="robot1_path_gen" output="screen" required="true">
         <param name="server_namespace" value="$(arg namespace_1)" />
     </node>
 </launch>

+ 3 - 3
mkr2018_mrc/robot_coordination/scripts/robot_control.py → mkr2018_mrc/robot_coordination/scripts/robot_control_0.py

@@ -15,10 +15,10 @@ import sys
 
 # Trajectory as a tuple of (x, y, time)
 g_trajectory = (
-    (5.6, 1.9, 10),
-    (4.8, 3.6, 10),
     (3.8, 2.6, 10),
-    (1.8, 1.6, 12)
+    (4.8, 3.6, 10),
+#    (3.8, 2.6, 10),
+#    (1.8, 1.6, 12)
 )
 
 

+ 139 - 0
mkr2018_mrc/robot_coordination/scripts/robot_control_1.py

@@ -0,0 +1,139 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+# Example of script to send a trajectory to a robot, start and stop motion.
+
+import time
+
+from geometry_msgs.msg import Pose
+from robot_coordination.msg import Waypoint
+from robot_coordination.srv import AddPath
+from robot_coordination.srv import StartMovement
+from robot_coordination.srv import StopMovement
+import rospy
+import random
+import sys
+
+# Trajectory as a tuple of (x, y, time)
+g_trajectory = (
+#    (1.8, 1.6, 12),
+#    (4.8, 3.6, 10),
+    (5.6, 1.9, 10),
+    (3.8, 2.6, 10)
+)
+
+
+def wait_for_service(server_ns, srv_name):
+    while not rospy.is_shutdown():
+        try:
+            rospy.wait_for_service(server_ns + '/' + srv_name, 5)
+            return True
+        except rospy.ROSException:
+            rospy.logwarn('Could not connect to service {}/{}, trying again'.format(
+                server_ns, srv_name))
+        except (rospy.ROSInterruptException, KeyboardInterrupt):
+            return False
+
+
+def start_movement(server_ns):
+    """Start the robot motion by calling the service 'start_movement'"""
+    srv_name = 'start_movement'
+    if not wait_for_service(server_ns, srv_name):
+        return False
+    try:
+        start_movement_srv = rospy.ServiceProxy(server_ns + '/' + srv_name, StartMovement)
+        reply = start_movement_srv()
+        return reply.ack
+    except rospy.ServiceException as e:
+        rospy.logerr('Service call failed: {}'.format(e))
+    return False
+
+
+def stop_movement(server_ns):
+    """Stop the robot motion by calling the service 'stop_movement'"""
+    srv_name = 'stop_movement'
+    if not wait_for_service(server_ns, srv_name):
+        return False
+    try:
+        stop_movement_srv = rospy.ServiceProxy(server_ns + '/' + srv_name, StopMovement)
+        reply = stop_movement_srv()
+        return reply.ack
+    except rospy.ServiceException as e:
+        rospy.logerr('Service call failed: {}'.format(e))
+    return False
+
+
+def waypoint_list_from_tuple(traj):
+    """Return a list of Waypoint instances from a list of (x, y, timepoint)"""
+    waypoint_list = []
+    for x, y, timepoint in traj:
+    #for i in range(1, 5):
+        waypoint = Waypoint()
+        waypoint.pose = Pose()
+        #waypoint.pose.position.x = random.uniform(0.8, 6.0)
+        #waypoint.pose.position.y = random.uniform(0.8, 3.0)
+        #waypoint.timepoint = rospy.Duration.from_sec(10)
+        waypoint.pose.position.x = x
+        waypoint.pose.position.y = y
+        waypoint.timepoint = rospy.Duration.from_sec(timepoint)
+        waypoint_list.append(waypoint)
+    return waypoint_list
+
+
+def add_path(server_ns, trajectory):
+    """Add a path with data from trajectory"""
+    waypoint_list = waypoint_list_from_tuple(trajectory)
+    srv_name = 'add_path'
+    if not wait_for_service(server_ns, srv_name):
+        return False
+    try:
+        add_path_srv = rospy.ServiceProxy(server_ns + '/' + srv_name, AddPath)
+        reply = add_path_srv(waypoint_list)
+        return reply.result
+    except rospy.ServiceException as e:
+        rospy.logerr('Service call failed: {}'.format(e))
+    return False
+
+
+def main():
+    rospy.init_node('example_robot_control')
+    server_ns_parameter_name = '~server_namespace'
+    try:
+        server_ns = rospy.get_param(server_ns_parameter_name)
+    except KeyError:
+        rospy.logerr('Parameter {} must be set'.format(
+            rospy.resolve_name(server_ns_parameter_name)))
+        return
+
+    if not add_path(server_ns, g_trajectory):
+        rospy.logerr('Could not add path, exiting')
+        return
+    rospy.loginfo('Path added')
+
+    if not start_movement(server_ns):
+        rospy.logerr('Could not start motion, exiting')
+        return
+    rospy.loginfo('Movement started')
+
+    while not rospy.is_shutdown():
+        time.sleep(10)
+        if not add_path(server_ns, g_trajectory):
+            rospy.logerr('Could not add path, exiting')
+            return
+        rospy.loginfo('Path added')
+
+    if not stop_movement(server_ns):
+        rospy.logerr('Could not stop motion, exiting')
+        return
+    rospy.loginfo('Movement stopped')
+
+    if not add_path(server_ns, []):
+        rospy.logerr('Could not clear path, exiting')
+        return
+    rospy.loginfo('Path cleared')
+
+
+if __name__ == '__main__':
+    try:
+        main()
+    except rospy.ROSInterruptException:
+        pass

+ 6 - 1
mkr2018_mrc/robot_coordination/src/node.cpp

@@ -6,6 +6,7 @@ int main(int argc, char **argv)
 {
   /*initialize ROS node*/
   ros::init(argc, argv, "robot");
+  
   /*create node handle*/
   ros::NodeHandle nh;
   /*create an instance of a robot */
@@ -26,8 +27,10 @@ int main(int argc, char **argv)
   
   /*set the subscriber of the odometry to the robot*/
   ros::Subscriber odometry_subscriber = nh.subscribe("odom", 100, &Robot::odometryCallback, &robot);
+
   /*create a publiser of commands*/
   ros::Publisher cmd_pub = nh.advertise<geometry_msgs::Twist> ("cmd_vel", 10);
+  
   /*advertise the service to add a path for a robot*/
   ros::ServiceServer service_add_path = nh.advertiseService("add_path", &Robot::addPathService, &robot);
 
@@ -39,7 +42,9 @@ int main(int argc, char **argv)
 
   /*set the publisher to the robot*/
   robot.setCommandPublisher(&cmd_pub);
-//  robot.startExecutePath();
+
+  robot.startExecutePath();
+
   /*start processing messages*/
   ros::spin(); 
   return 0;

+ 1 - 1
mkr2018_mrc/robot_coordination/src/obstacles.cpp

@@ -2,7 +2,7 @@
 #include <geometry_msgs/Point.h>
 
 //param pose_x,pose_y other robot current pose
-vector< vector<geometry_msgs::Point> > obstacles::getObstacleArray(int pose_x, int pose_y)
+vector< vector<geometry_msgs::Point> > obstacles::getObstacleArray(double pose_x, double pose_y)
 {
     vector<geometry_msgs::Point> obstaclePoint;
     geometry_msgs::Point point;

+ 12 - 6
mkr2018_mrc/robot_coordination/src/robot.cpp

@@ -14,16 +14,17 @@
 #define success false
 #define running true
 
-Robot::Robot() :  threshold_(0.1) 
+
+Robot::Robot() : threshold_(0.1) 
 {
+
 }
 
 void Robot::odometryCallback (const nav_msgs::Odometry::ConstPtr& msg) 
 {
   //ROS_INFO("got odometry");
   robot_pose_ = msg->pose.pose;
-  ROS_INFO_STREAM("robot current pose_x:" << robot_pose_.position.x << " ,pose_y:" << robot_pose_.position.y);
-
+  
   if (execute_) 
   {
     followTheCarrot();
@@ -42,6 +43,7 @@ void Robot::stopExecutePath()
   command.angular.z = 0;
   command.linear.x = 0;
   command_publisher_->publish(command);
+
   execute_ = false;
 }
 
@@ -119,7 +121,8 @@ void Robot::generateTempPoint(RRT::rrtNode &tempNode)
 bool Robot::checkNodetoGoal(float X, float Y, RRT::rrtNode &tempNode)
 {
     double distance = sqrt(pow(X-tempNode.posX, 2)+pow(Y-tempNode.posY, 2));
-    ROS_INFO_STREAM("checkNodetoGoal: input x: " << X << " ,y:"<<Y<<",tempNode.posX:"<<tempNode.posX<<",y:"<<tempNode.posY);
+    ROS_INFO_STREAM("checkNodetoGoal: input x: " << X << " ,y:"<<Y<<",tempNode.posX:"
+                       <<tempNode.posX<<",y:"<<tempNode.posY);
     ROS_INFO_STREAM("distance:" << distance);
     if(distance < 1.5)
     {
@@ -187,11 +190,14 @@ bool Robot::followTheCarrot()
 
     //initializing rrtTree
     RRT myRRT(robot_pose_.position.x, robot_pose_.position.y);
-    int goalX = 0;
-    int goalY = 0;
+    double goalX = 0;
+    double goalY = 0;
     goalX = actual_goal.pose.position.x;
     goalY = actual_goal.pose.position.y;
 
+    ROS_INFO_STREAM("robot current pose_x:" << double(robot_pose_.position.x) << " ,pose_y:" << double(robot_pose_.position.y)
+                    << " ,goal_x:" << goalX << " ,goal_y:" << goalY);
+
     int rrtStepSize = 1;
 
     vector< vector<int> > rrtPaths;

+ 1 - 0
mkr2018_mrc/robot_coordination/src/rrt.cpp

@@ -12,6 +12,7 @@
 RRT::RRT()
 {
     RRT::rrtNode newNode;
+    
     newNode.posX = 0;
     newNode.posY = 0;
     newNode.parentID = 0;

+ 1 - 4
mkr2018_mrc/simulator_e130/launch/simulator.launch

@@ -1,11 +1,8 @@
 <?xml version="1.0"?>
 
 <launch>
-
 	<include file="$(find simulator_e130)/launch/server.launch" />
-
 	<include file="$(find simulator_e130)/launch/gui.launch" />
-
 	<include file="$(find simulator_e130)/launch/robots.launch" />
   
     <!-- startup multi robots control node -->
@@ -45,7 +42,7 @@
 
 
     <!-- startup move_base node -->
-    <include file="$(find stdr_move_base)/launch/stdr_move_base.launch" />
+    <!--<include file="$(find stdr_move_base)/launch/stdr_move_base.launch" />-->
     
     <!-- startup amcl node -->
     <!-- <include file="$(find stdr_amcl)/launch/stdr_amcl.launch" /> -->

+ 2 - 48
mkr2018_mrc/stdr_amcl/rviz/stdr_config.rviz

@@ -103,9 +103,9 @@ Visualization Manager:
       Enabled: true
       Invert Rainbow: true
       Max Color: 255; 255; 255
-      Max Intensity: 6.37994528
+      Max Intensity: 3.28363633
       Min Color: 0; 0; 0
-      Min Intensity: 6.37000704
+      Min Intensity: -0.0927267745
       Name: LaserScan
       Position Transformer: XYZ
       Queue Size: 10
@@ -181,52 +181,6 @@ Visualization Manager:
       Topic: /stdr_move_base/GlobalPlanner/plan
       Unreliable: false
       Value: true
-    - Alpha: 0.800000012
-      Buffer Length: 20
-      Class: rviz/Path
-      Color: 250; 232; 28
-      Enabled: true
-      Head Diameter: 0.0299999993
-      Head Length: 0.0299999993
-      Length: 0.300000012
-      Line Style: Billboards
-      Line Width: 0.00499999989
-      Name: Path
-      Offset:
-        X: 0
-        Y: 0
-        Z: 0
-      Pose Color: 250; 232; 28
-      Pose Style: None
-      Radius: 0.0299999993
-      Shaft Diameter: 0.00999999978
-      Shaft Length: 0.00999999978
-      Topic: /stdr_move_base/DWAPlannerROS/global_plan
-      Unreliable: false
-      Value: true
-    - Alpha: 0.5
-      Buffer Length: 1
-      Class: rviz/Path
-      Color: 255; 23; 112
-      Enabled: true
-      Head Diameter: 0.300000012
-      Head Length: 0.200000003
-      Length: 0.300000012
-      Line Style: Lines
-      Line Width: 0.0299999993
-      Name: Path
-      Offset:
-        X: 0
-        Y: 0
-        Z: 0
-      Pose Color: 255; 85; 255
-      Pose Style: None
-      Radius: 0.0299999993
-      Shaft Diameter: 0.100000001
-      Shaft Length: 0.100000001
-      Topic: /stdr_move_base/DWAPlannerROS/local_plan
-      Unreliable: false
-      Value: true
     - Angle Tolerance: 0.100000001
       Class: rviz/Odometry
       Covariance: