Publish nav_msgs::OccupancyGrid message in python

Publish nav_msgs::OccupancyGrid message in python

here is the demo.

#!/usr/bin/python3
'''
A Demo, to publish ros map message (nav_msgs::OccupancyGrid), is written 
in python.

Has been tested on ubuntu16.04 ros kinetic x86_64 platform

To use python3 as the interpreter, opencv for python3 will be needed, 
'''
import rospy
from nav_msgs.msg import OccupancyGrid
from std_msgs.msg import Int8MultiArray
import numpy as np
import sys
python3 = True if sys.hexversion > 0x03000000 else False
if python3:
    # remove ros python2 library
    ros_cv_path = '/opt/ros/kinetic/lib/python2.7/dist-packages'
    if ros_cv_path in sys.path:
        sys.path.remove(ros_cv_path)

import cv2 as cv

def RosInit():
    rospy.init_node('map_pub_node')
    pub = rospy.Publisher('map', OccupancyGrid, queue_size = 1000)
    return pub

def MakeMapMsg(img, resolution, position, orientation):
    map_msg = OccupancyGrid()
    map_msg.header.stamp = rospy.Time.now()
    map_msg.info.map_load_time = rospy.Time.now()
    map_msg.info.width = img.shape[0]
    map_msg.info.height = img.shape[1]
    map_msg.info.resolution = resolution
    map_msg.info.origin.position.x = position[0]
    map_msg.info.origin.position.y = position[1]
    map_msg.info.origin.position.z = position[2]
    map_msg.info.origin.orientation.w = orientation[0]
    map_msg.info.origin.orientation.x = orientation[1]
    map_msg.info.origin.orientation.y = orientation[2]
    map_msg.info.origin.orientation.z = orientation[3]
    img = cv.flip(img, 0, dst=None)
    img_data_int8array = [i for row in img.tolist() for i in row]
    map_msg.data = Int8MultiArray(data=img_data_int8array).data
    return map_msg

def DrawMapImg():
    img = np.zeros((600, 600, 1), np.uint8)
    cv.rectangle(img, (200,100), (400,150), 100, -1)
    cv.rectangle(img, (350,100), (400,400), 100, -1)
    return img

def ShowMapImg(img, tval=0):
    cv.imshow('map_msg', img)
    cv.waitKey(tval)

if __name__ == "__main__":
    pub = RosInit()
    img = DrawMapImg()
    ShowMapImg(img)
    msg = MakeMapMsg(img, 1, [0, 0, 0], [1, 0, 0, 0])
    while not rospy.is_shutdown():
        pub.publish(msg)
        rospy.loginfo('map_msg has been published, Ctrl+C to exit')
        rospy.sleep(1)

将C++#include <ros/ros.h> #include <ros/package.h> #include <quadrotor_msgs/PositionCommand.h> #include <nav_msgs/Odometry.h> #include <sensor_msgs/Joy.h> #include<mavros_msgs/AttitudeTarget.h> #include <tf/tf.h> #include <math.h> using namespace std; int main(int argc, char **argv) { ros::init(argc, argv, "traj_pub"); //##节点名traj_pub ros::NodeHandle nh; // ros::Publisher local_pos_pub3 = nh.advertise<quadrotor_msgs::PositionCommand> ("/position_cmd", 10); //##话题名字为/——position_cmd 10为缓存长度 quadrotor_msgs::PositionCommand poscom; //ros::Publisher local_pos_pub3 = nh.advertise<mavros_msgs::AttitudeTarget> // ("/mavros/setpoint_raw/attitude", 10); //mavros_msgs::AttitudeTarget msg; ros::Rate rate(20.0); //##循环频率20 int i = 0; while(ros::ok()) { poscom.position.x = 2.5*sin(M_PI*i/400); poscom.position.y = 5*sin(M_PI*i/800); poscom.position.z = 2; poscom.velocity.x = 0; poscom.velocity.y = 0; poscom.velocity.z = 0; poscom.acceleration.x = 0; poscom.acceleration.y = 0; poscom.acceleration.z = 0; poscom.yaw = 0; poscom.jerk.x=0; poscom.jerk.y=0; poscom.jerk.z=0; local_pos_pub3.publish(poscom); // tf::Quaternion q = tf::createQuaternionFromRPY(0, 0, 0.5); // msg.type_mask = msg.IGNORE_ROLL_RATE || msg.IGNORE_PITCH_RATE || msg.IGNORE_YAW_RATE; // msg.orientation.x = q.x(); // msg.orientation.y = q.y(); // msg.orientation.z = q.z(); // msg.orientation.w = q.w(); // msg.thrust = 0.75; // local_pos_pub3.publish(msg); ros::spinOnce(); rate.sleep(); i++; } } 转成pyrhon
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值