Ros移动控制

        通过topic通讯机制来控制机器人的移动,主要依靠激光雷达、IMU、cmd/vel等。

#!/usr/bin/env python3
# coding=utf-8

import rospy
import math
from tf.transformations import euler_from_quaternion
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
from sensor_msgs.msg import Imu

count = 0


def ImuCallback(message):
    global velocity_publisher
    if message.orientation_covariance[0] < 0:
        return
    quaternion = [
        message.orientation.x,
        message.orientation.y,
        message.orientation.z,
        message.orientation.w
    ]
    (roll, pitch, yaw) = euler_from_quaternion(quaternion)
    roll = roll * 180 / math.pi
    pitch = pitch * 180 / math.pi
    yaw = yaw * 180 / math.pi
    rospy.loginfo("滚转=%.2f   俯仰=%.2f   朝向=%.2f" % (roll, pitch, yaw))
    # 航向锁定
    target_yaw = 90
    difference = target_yaw - yaw
    velocity_command = Twist()
    velocity_command.angular.z = difference * 0.05
    velocity_command.linear.x = 0.15
    velocity_publisher.publish(velocity_command)


def MoveCallback(message):
    global velocity_publisher
    global count
    distance = message.ranges[180]
    rospy.loginfo("距离前方障碍物还有%.2fm", distance)

    if count > 0:
        count = count - 1
        return
    velocity_command = Twist()
    if distance < 1.5:
        count = 35
        velocity_command.angular.z = 0.3
    else:
        velocity_command.linear.x = 0.15
    velocity_publisher.publish(velocity_command)


if __name__ == '__main__':
    rospy.init_node("move_control_node")
    rospy.logwarn("move_control_node start running")

    imu_subscriber = rospy.Subscriber("/imu/data", Imu, ImuCallback, queue_size=10)
    lidar_subscriber = rospy.Subscriber("/scan", LaserScan, MoveCallback, queue_size=10)
    velocity_publisher = rospy.Publisher("cmd_vel", Twist, queue_size=10)

    rospy.spin()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值