• stage simulator


    ---恢复内容开始---

     运行自带地图

    rosrun stage_ros stageros /opt/ros/indigo/share/stage_ros/world/willow-erratic.world 

    控制机器人运动
    sudo apt-get install ros-indigo-teleop-twist-keyboard
    rosrun teleop_twist_keyboard teleop_twist_keyboard.py
    简单的gmapping&保存map
    rosrun gmapping slam_gmapping scan:=base_scan
    rosrun map_server map_saver

    在home目录下查看地图

    利用激光雷达数据,在前方有障碍物0.5米内停止前行

    #include<ros/ros.h>
    #include<sensor_msgs/LaserScan.h>
    #include<geometry_msgs/Twist.h>
    
    class Stopper{
    public:
        const static double FORWARD_SPEED_MPS = 0.5;
        const static double MIN_SCAN_ANGLE_RAD = -30.0/180*M_PI;
        const static double MAX_SCAN_ANGLE_RAD = +30.0/180*M_PI;
        const static float MIN_PROXOMITY_RANGE_M = 0.5;
    
        Stopper();
        void startMoving();
    private:
        ros::NodeHandle node;
        ros::Publisher commandPub;
        ros::Subscriber laserSub;
        bool keepMoving;
        void moveForward();
        void scanCallback( const sensor_msgs::LaserScan::ConstPtr& scan);
    };
    Stopper::Stopper(){
        keepMoving = true;
        commandPub = node.advertise<geometry_msgs::Twist>("cmd_vel",10);
        laserSub = node.subscribe("base_scan",1, &Stopper::scanCallback,this);
    
    }
    
    void Stopper::moveForward(){
        geometry_msgs::Twist msg;
        msg.linear.x = FORWARD_SPEED_MPS;
        commandPub.publish(msg);
    }
    
    void Stopper::scanCallback(const sensor_msgs::LaserScan::ConstPtr& scan){
        int minIndex = ceil((MIN_SCAN_ANGLE_RAD - scan->angle_min) / scan->angle_increment);
        int maxIndex = floor((MAX_SCAN_ANGLE_RAD - scan->angle_min) / scan->angle_increment);
        float closestRange = scan->ranges[minIndex];
    
        for(int currIndex = minIndex + 1; currIndex <= maxIndex; currIndex++){
    
            if(scan->ranges[currIndex] < closestRange){
                closestRange = scan->ranges[currIndex];
            }
           ROS_INFO_STREAM("closest range: " <<closestRange);
           if(closestRange < MIN_PROXOMITY_RANGE_M){
               ROS_INFO("stop");
               keepMoving = false;
           }
        }
    
    }
    
    void Stopper::startMoving(){
    ros::Rate rate(10);
    ROS_INFO("start_moving");
    while(ros::ok()&&keepMoving){
        moveForward();
        ros::spinOnce();
        rate.sleep();
        }
    }
    
    int main(int argc,char **argv){
        ros::init(argc,argv,"stopper");
        Stopper stopper;
        stopper.startMoving();
        return 0;
    
    }
    
  • 相关阅读:
    「题解」300iq Contest 2 B Bitwise Xor
    「题解」agc031_e Snuke the Phantom Thief
    「题解」agc031_c Differ by 1 Bit
    「题解」NWRRC2017 Joker
    「题解」NWRRC2017 Grand Test
    「题解」USACO15FEB Fencing the Herd G
    zsh: command not found:xxx 解决方法
    curl
    redis搭建
    http 缓存分为客户端缓存/服务端缓存/数据库缓存
  • 原文地址:https://www.cnblogs.com/CZM-/p/6030692.html
Copyright © 2020-2023  润新知