汽车人协会ros比赛sh脚本(水)

发布于 2024-05-16  71 次阅读


汽车人协会ros小车比赛一键启动脚本

以下全部基于汽车人协会小车比赛用映像的/home/hawkbot/stear/目录下运行

小龟算法

#!/bin/bash

# 检查roscore是否运行,如果没有,则在新终端中启动
if ! pgrep -x "roscore" >/dev/null; then
    gnome-terminal -- bash -c "roscore; exec bash"
    sleep 2 # 等待roscore完全启动
fi

# 获取用户输入
echo "workspace name:"
read workspace_name
echo "package name:"
read package_name
echo "source file name:"
read file_name

# 设置工作空间和源码目录的路径
workspace_dir=~/stear/${workspace_name}
src_dir=${workspace_dir}/src

# 创建新的工作空间
mkdir -p $src_dir
cd $workspace_dir
source /opt/ros/melodic/setup.bash
catkin_make
source devel/setup.bash

# 创建ROS包
cd $src_dir
catkin_create_pkg $package_name roscpp turtlesim
cd $package_name

# 创建C++源文件
mkdir -p src
cat <<EOF >src/$file_name.cpp
#include "ros/ros.h"
#include "geometry_msgs/Twist.h"

void move(double speed, double distance, bool is_forward, ros::Publisher &pub) {
    geometry_msgs::Twist twist;
    twist.linear.x = is_forward ? speed : -speed;

    ros::Time t0 = ros::Time::now();
    double current_distance = 0;

    ros::Rate loop_rate(1000); // 控制循环频率为 10Hz
    while (current_distance < distance) {
        pub.publish(twist);
        ros::Time t1 = ros::Time::now();
        current_distance = speed * (t1 - t0).toSec();
        loop_rate.sleep();
    }

    twist.linear.x = 0;
    pub.publish(twist);
}

void turn(double angle, bool is_clockwise, ros::Publisher &pub) {
    geometry_msgs::Twist twist;
    twist.angular.z = is_clockwise ? angle : -angle;

    ros::Time t0 = ros::Time::now();
    double current_angle = 0;

    ros::Rate loop_rate(1000); // 控制循环频率为 10Hz
    while (current_angle < angle) {
        pub.publish(twist);
        ros::Time t1 = ros::Time::now();
        current_angle = angle * (t1 - t0).toSec();
        loop_rate.sleep();
    }

    twist.angular.z = 0;
    pub.publish(twist);
}

void turtle_star() {
    ros::NodeHandle nh;
    ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);

    move(1, 2, true, pub);
    turn(1.0471975512, true, pub); // 72度转弧度
    move(1, 2, true, pub);
    turn(1.0471975512, true, pub); // 72度转弧度
    move(1, 2, true, pub);
    turn(1.0471975512, true, pub); // 72度转弧度
    move(1, 2, true, pub);
    turn(1.0471975512, true, pub); // 72度转弧度
    move(1, 2, true, pub);
    turn(1.0471975512, true, pub); // 72度转弧度
    move(1, 2, true, pub);
    turn(1.0471975512, true, pub); // 72度转弧度
    move(1, 1, true, pub);
    turn(4.7123889804, true, pub); // 72度转弧度
    move(1, 2, true, pub);
}

int main(int argc, char **argv) {
    ros::init(argc, argv, "turtle_star");
    try {
        turtle_star();
    } catch (ros::Exception &e) {
        ROS_ERROR("An exception occurred: %s", e.what());
        return 1;
    }

    return 0;
}

EOF

# 更新CMakeLists.txt以包括新的源文件
echo "find_package(catkin REQUIRED COMPONENTS
roscpp
geometry_msgs
)" >>CMakeLists.txt
echo "add_executable(${package_name}_node src/$file_name.cpp)" >>CMakeLists.txt
echo "target_link_libraries(${package_name}_node ${catkin_LIBRARIES})" >>CMakeLists.txt

# 返回工作空间根目录并编译
cd $workspace_dir
catkin_make

# 创建一个启动脚本
cat <<EOF >start_turtlesim_and_algorithm.sh
#!/bin/bash
# This script starts turtlesim and the algorithm node.
source /opt/ros/melodic/setup.bash
source $workspace_dir/devel/setup.bash
gnome-terminal -- bash -c "rosrun turtlesim turtlesim_node; exec bash"
gnome-terminal -- bash -c "rosrun $package_name ${package_name}_node; exec bash"
EOF

chmod +x start_turtlesim_and_algorithm.sh

# 自动执行启动脚本
./start_turtlesim_and_algorithm.sh

echo "Package $package_name created and built successfully in $workspace_dir."
echo "Turtlesim and algorithm node are running."

摄像头驱动

#!/bin/bash

# 检查roscore是否运行,如果没有,则在新终端中启动
if ! pgrep -x "roscore" >/dev/null; then
    gnome-terminal -- bash -c "roscore; exec bash"
    sleep 2 # 等待roscore完全启动
fi

# 获取用户输入
echo "workspace name:"
read workspace_name
echo "package name:"
read package_name
echo "source file name:"
read file_name

# 设置工作空间和源码目录的路径
workspace_dir=~/stear/${workspace_name}
src_dir=${workspace_dir}/src

# 创建新的工作空间
mkdir -p $src_dir
cd $workspace_dir
source /opt/ros/melodic/setup.bash
catkin_make
source devel/setup.bash

# 返回工作空间根目录并编译
cd $workspace_dir
catkin_make

sudo cp -r /home/hawkbot/摄像头算法包/usb_cam $workspace_dir/src/

cd $workspace_dir
catkin_make

source /opt/ros/melodic/setup.bash
source $workspace_dir/devel/setup.bash
roslaunch $workspace_dir/src/usb_cam/launch/usb_cam-test.launch

面部识别包

#!/bin/bash

# 检查roscore是否运行,如果没有,则在新终端中启动
if ! pgrep -x "roscore" >/dev/null; then
    gnome-terminal -- bash -c "roscore; exec bash"
    sleep 2 # 等待roscore完全启动
fi

# 获取用户输入
echo "workspace name:"
read workspace_name
echo "package name:"
read package_name
echo "source file name:"
read file_name

# 设置工作空间和源码目录的路径
workspace_dir=~/stear/${workspace_name}
src_dir=${workspace_dir}/src

# 创建新的工作空间
mkdir -p $src_dir
cd $workspace_dir
source /opt/ros/melodic/setup.bash
catkin_make
source devel/setup.bash

# 创建ROS包
cd $src_dir
catkin_create_pkg $package_name roscpp turtlesim
cd $package_name

sudo cp -r /home/hawkbot/ROS智能小车初赛/facedetect.cpp $workspace_dir/src/$package_name/src/

sudo cp -r /home/hawkbot/ROS智能小车初赛/cade_frontalface_default.xml $workspace_dir/src/

sed -i "s#haarcascade_frontalface_default.xml#$workspace_dir/src/cade_frontalface_default.xml#i" $workspace_dir/src/$package_name/src/facedetect.cpp

cat /dev/null > CMakeLists.txt

# 更新CMakeLists.txt以包括新的源文件
echo "cmake_minimum_required(VERSION 2.8.3)
project($package_name)

find_package(catkin REQUIRED COMPONENTS
roscpp
image_transport
cv_bridge
)

find_package(OpenCV REQUIRED)

catkin_package()

include_directories(
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)

add_executable($package_name src/facedetect.cpp)
target_link_libraries($package_name
${catkin_LIBRARIES}
${OpenCV_LIBRARIES}
)" >>CMakeLists.txt

# 返回工作空间根目录并编译
cd $workspace_dir
catkin_make

source /opt/ros/melodic/setup.bash
source $workspace_dir/devel/setup.bash
rosrun $package_name $package_name

emm录像

#!/bin/bash

# 检查roscore是否运行,如果没有,则在新终端中启动
if ! pgrep -x "roscore" >/dev/null; then
    gnome-terminal -- bash -c "roscore; exec bash"
    sleep 2 # 等待roscore完全启动
fi

sudo cp -r /home/hawkbot/ROS智能小车初赛/qcrros.bag /home/hawkbot/stear/

gnome-terminal -- bash -c "rosbag play -l qcrros.bag; exec bash"

gnome-terminal -- bash -c "rviz; exec bash"

rostopic list
QQ:2219349024
最后更新于 2024-05-21