问一个精准的问题借助CatGPT等AI助手得到需要的帮助-以ROS机器人为例

Posted zhangrelay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了问一个精准的问题借助CatGPT等AI助手得到需要的帮助-以ROS机器人为例相关的知识,希望对你有一定的参考价值。

精准搜索

精准提问

为什么写这一篇?

动机:之前学生或网上的朋友,问各类机器人问题,”遇到报错了,没调出来,帮我看看吧?“

我通常看不懂问题,也无法帮忙,毕竟加班太多了,实在抱歉。

问题其实具体一些,比如遇到xxx样的报错,具体xxx没有调出来这样更为合适。

之前,靠精准搜索来寻求自己遇到问题的答案,现在这种方式依然适用。

但是随着ChatGPT的成功,如果精准提问,其实此类AI应用也可以大幅提升学习效果或解决问题的效率。


AI指出:相比于搜索引擎,ChatGPT的优势在于它可以进行更加智能化的交互式对话,能够根据用户的提问进行更加精准的回答。同时,ChatGPT还可以进行自我学习和优化,不断提高回答的准确性和质量。不足之处在于,ChatGPT的回答可能会受到模型训练数据的限制,无法涵盖所有的知识领域和问题类型。此外,ChatGPT的回答也可能存在一定的主观性和误差,需要用户进行判断和筛选。


看如下案例:

回答特别敷衍,甩出官网完事,那如果具体一些呢?

ROS kinetic详细安装步骤:

AI答复:

我可以回答这个问题。首先,你需要在Ubuntu 16.04上安装ROS Kinetic。你可以按照以下步骤进行安装:
添加ROS源
在终端中输入以下命令:
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
添加ROS key
在终端中输入以下命令:
sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
更新软件包
在终端中输入以下命令:
sudo apt-get update
安装ROS Kinetic
在终端中输入以下命令:
sudo apt-get install ros-kinetic-desktop-full
初始化ROS
在终端中输入以下命令:
sudo rosdep init
rosdep update
设置环境变量
在终端中输入以下命令:
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
安装ROS工具
在终端中输入以下命令:
sudo apt-get install python-rosinstall python-rosinstall-generator python-wstool build-essential
安装完成后,你就可以开始使用ROS Kinetic了。

在linux系统安装ROS2 humble的详细步骤?

你可以在终端中输入以下命令来安装ROS2 Humble:

添加ROS2 Humble软件源:

sudo apt update && sudo apt install curl gnupg2 lsb-release

curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

sudo sh -c 'echo "deb [arch=amd64,arm64] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'

安装ROS2 Humble:

sudo apt update

sudo apt install ros-humble-desktop

初始化ROS2 Humble:

source /opt/ros/humble/setup.bash

创建ROS2工作空间:

mkdir -p ~/ros2_ws/src

cd ~/ros2_ws/src

构建ROS2工作空间:

cd ~/ros2_ws

colcon build

source ~/ros2_ws/install/setup.bash

现在,你已经成功安装了ROS2 Humble,并创建了一个ROS2工作空间。


复杂一些的问题呢?

turtlesim中键盘遥控机器人运动的详细步骤?

AI:

首先,打开终端,输入命令“roscore”启动ROS核心。然后,在新的终端中输入命令“rosrun turtlesim turtlesim_node”启动turtlesim节点。接下来,打开新的终端,输入命令“rosrun turtlesim turtle_teleop_key”启动键盘遥控节点。此时,你可以使用键盘上的方向键来控制机器人的运动。按下“Ctrl + C”可以停止节点的运行。

turtlesim实现贪吃蛇的详细步骤?

AI:

首先,需要安装turtlesim软件包。然后,打开终端并输入以下命令:

rosrun turtlesim turtlesim_node

这将启动turtlesim节点。接下来,创建一个新的ROS软件包并添加一个名为“snake”的节点。在该节点中,使用ROS消息来控制turtle的运动。使用ROS订阅器来接收键盘输入,并使用ROS发布器来控制turtle的运动。最后,将snake节点与turtlesim节点连接起来,以便在turtlesim中显示贪吃蛇游戏。

具体的步骤可以参考ROS官方文档或者相关教程。

https://blog.csdn.net/zhangrelay/article/details/124238177

import rospy
from tanksim.msg import Pose
from tanksim.srv import Spawn
from tanksim.srv import SetPen
from geometry_msgs.msg import Twist
from geometry_msgs.msg import TransformStamped
import random
import math
 
tank1_pose = Pose()
tanklist = []
lasttank = 1
nexttankIndex = 1
 
class mySpawner:
    def __init__(self, tname):
        self.tank_name = tname
        self.state = 1
        rospy.wait_for_service('/spawn')
        try:
            client = rospy.ServiceProxy('/spawn', Spawn)
            x = random.randint(1, 10)
            y = random.randint(1, 10)
            theta = random.uniform(1, 3.14)
            name = tname
            _nm = client(x, y, theta, name)
            rospy.loginfo("tank Created [%s] [%f] [%f]", name, x, y)
            rospy.Subscriber(self.tank_name + '/pose', Pose, self.tank_poseCallback)
            self.pub = rospy.Publisher(self.tank_name + '/cmd_vel', Twist, queue_size=10)
            self.tank_to_follow = 1
            self.tank_pose = Pose()
            rospy.wait_for_service("/" + tname + '/set_pen')
            try:
                client = rospy.ServiceProxy("/" + tname + '/set_pen', SetPen)
                client(0,0,0,0,1)
            except rospy.ServiceException as e:
                print("Service call failed: %s"%e)
        except rospy.ServiceException as e:
            print("Service tp spawn a tank failed. %s", e)
    
    def tank_poseCallback(self, data):
        self.tank_pose = data
    
    def tank_velocity(self, msg):
        self.pub.publish(msg)
 
 
def tank1_poseCallback(data):
    global tank1_pose
    global lasttank
    global tanklist
    global nexttankIndex
    tank1_pose.x = round(data.x, 4)
    tank1_pose.y = round(data.y, 4)
    tank1_pose.theta = round(data.theta, 4)
 
    for i in range(len(tanklist)):
        twist_data = Twist()
        diff = math.sqrt(pow((tank1_pose.x - tanklist[i].tank_pose.x) , 2) + pow((tank1_pose.y - tanklist[i].tank_pose.y), 2))
        ang = math.atan2(tank1_pose.y - tanklist[i].tank_pose.y, tank1_pose.x - tanklist[i].tank_pose.x) - tanklist[i].tank_pose.theta
        
        if(ang <= -3.14) or (ang > 3.14):
            ang = ang / math.pi
 
        if (tanklist[i].state == 1):
            if diff < 1.0:
                tanklist[i].state = 2
                tanklist[i].tank_to_follow = lasttank
                lasttank = i + 2
                rospy.loginfo("tank Changed [%s] [%f] [%f]", tanklist[i].tank_name, diff, ang)
                nexttankIndex += 1
                tanklist.append(mySpawner("tank" + str(nexttankIndex)))
        else:
            parPose = tank1_pose
            if(tanklist[i].tank_to_follow != 1):
                parPose = tanklist[tanklist[i].tank_to_follow - 2].tank_pose
            
            diff = math.sqrt(pow((parPose.x - tanklist[i].tank_pose.x) , 2) + pow((parPose.y - tanklist[i].tank_pose.y), 2))
            goal = math.atan2(parPose.y - tanklist[i].tank_pose.y, parPose.x - tanklist[i].tank_pose.x)
            ang = math.atan2(math.sin(goal - tanklist[i].tank_pose.theta), math.cos(goal - tanklist[i].tank_pose.theta))
 
            if(ang <= -3.14) or (ang > 3.14):
                ang = ang / (2*math.pi)
            
            if(diff < 0.8):
                twist_data.linear.x = 0 
                twist_data.angular.z = 0
            else:
                twist_data.linear.x = 2.5 * diff                
                twist_data.angular.z = 20 * ang
                  
            tanklist[i].tank_velocity(twist_data)
            tanklist[i].oldAngle = ang    
 
 
 
def spawn_tank_fn():
    global nexttankIndex
    rospy.init_node('snake_tank', anonymous=True)
    rospy.Subscriber('/tank1/pose', Pose, tank1_poseCallback)
    rospy.wait_for_service("/tank1/set_pen")
    try:
        client = rospy.ServiceProxy('/tank1/set_pen', SetPen)
        client(0,0,0,0,1)
    except rospy.ServiceException as e:
        print("Service call failed: %s"%e)
    
    nexttankIndex += 1
    tanklist.append(mySpawner("tank" + str(nexttankIndex)))
    # for i in range(2,10):
    #     tanklist.append(mySpawner("tank" + str(i)))
        
    rospy.spin()
 
if __name__ == "__main__":
    spawn_tank_fn()

AI给出的案例不太可靠。

turtlesim实现贪吃蛇的Python代码

import rospy
from geometry_msgs.msg import Twist
from turtlesim.msg import Pose
from math import pow, atan2, sqrt

class TurtleBot:
    def __init__(self):
        rospy.init_node('turtlebot_controller', anonymous=True)
        self.velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
        self.pose_subscriber = rospy.Subscriber('/turtle1/pose', Pose, self.update_pose)
        self.pose = Pose()
        self.rate = rospy.Rate(10)

    def update_pose(self, data):
        self.pose = data
        self.pose.theta = round(self.pose.theta, 4)

    def euclidean_distance(self, goal_pose):
        return sqrt(pow((goal_pose.x - self.pose.x), 2) + pow((goal_pose.y - self.pose.y), 2))

    def linear_vel(self, goal_pose, constant=1.5):
        return constant * self.euclidean_distance(goal_pose)

    def steering_angle(self, goal_pose):
        return atan2(goal_pose.y - self.pose.y, goal_pose.x - self.pose.x)

    def angular_vel(self, goal_pose, constant=6):
        return constant * (self.steering_angle(goal_pose) - self.pose.theta)

    def move2goal(self):
        goal_pose = Pose()
        goal_pose.x = input("Set your x goal: ")
        goal_pose.y = input("Set your y goal: ")

        distance_tolerance = input("Set your tolerance: ")
        vel_msg = Twist()

        while self.euclidean_distance(goal_pose) >= distance_tolerance:
            vel_msg.linear.x = self.linear_vel(goal_pose)
            vel_msg.linear.y = 0
            vel_msg.linear.z = 0

            vel_msg.angular.x = 0
            vel_msg.angular.y = 0
            vel_msg.angular.z = self.angular_vel(goal_pose)

            self.velocity_publisher.publish(vel_msg)
            self.rate.sleep()

        vel_msg.linear.x = 0
        vel_msg.angular.z = 0
        self.velocity_publisher.publish(vel_msg)

if __name__ == '__main__':
    try:
        x = TurtleBot()
        x.move2goal()
    except rospy.ROSInterruptException:
        pass

以上是关于问一个精准的问题借助CatGPT等AI助手得到需要的帮助-以ROS机器人为例的主要内容,如果未能解决你的问题,请参考以下文章

借助CatGPT让turtlesim小乌龟画曲线

什么是CatGPT-使用效果如何-

AI 音辨世界:艺术小白的我,靠这个AI模型,速识音乐流派选择音乐 ⛵

用AI解密眼底世界

基于AI边缘智能网关的工业质检应用

基于AI边缘智能网关的工业质检应用