zoukankan      html  css  js  c++  java
  • ros msg array

    #include <stdio.h>
    #include <stdlib.h>
    
    #include "ros/ros.h"
    
    #include "std_msgs/MultiArrayLayout.h"
    #include "std_msgs/MultiArrayDimension.h"
    
    #include "std_msgs/Int32MultiArray.h"
    
    int main(int argc, char **argv)
    {
        
    
    	ros::init(argc, argv, "arrayPublisher");
    
    	ros::NodeHandle n;
    
    	ros::Publisher pub = n.advertise<std_msgs::Int32MultiArray>("array", 100);
    
    	while (ros::ok())
    	{
    		std_msgs::Int32MultiArray array;
    		//Clear array
    		array.data.clear();
    		//for loop, pushing data in the size of the array
    		for (int i = 0; i < 90; i++)
    		{
    			//assign array a random number between 0 and 255.
    			array.data.push_back(rand() % 255);
    		}
    		//Publish array
    		pub.publish(array);
    		//Let the world know
    		ROS_INFO("I published something!");
    		//Do this.
    		ros::spinOnce();
    		//Added a delay so not to spam
    		sleep(2);
    	}
    
    }


    #include <stdio.h>
    #include <stdlib.h>
    #include <vector>
    #include <iostream>
    
    #include "ros/ros.h"
    
    #include "std_msgs/MultiArrayLayout.h"
    #include "std_msgs/MultiArrayDimension.h"
    #include "std_msgs/Int32MultiArray.h"
    
    int Arr[90];
    void arrayCallback(const std_msgs::Int32MultiArray::ConstPtr& array);
    
    int main(int argc, char **argv)
    {
    
    	ros::init(argc, argv, "arraySubscriber");
    
    	ros::NodeHandle n;	
    
    	ros::Subscriber sub3 = n.subscribe("array", 100, arrayCallback);
    
    	ros::spinOnce();
    
    	for(j = 1; j < 90; j++)
    	{
    		printf("%d, ", Arr[j]);
    	}
    
    	printf("
    ");
    	return 0;
    }
    
    void arrayCallback(const std_msgs::Int32MultiArray::ConstPtr& array)
    {
    
    	int i = 0;
    	// print all the remaining numbers
    	for(std::vector<int>::const_iterator it = array->data.begin(); it != array->data.end(); ++it)
    	{
    		Arr[i] = *it;
    		i++;
    	}
    
    	return;
    }
  • 相关阅读:
    vsftp搭建
    进程管理相关命令(15 个)
    系统管理与性能监视命令 (9 个)
    系统权限及用户授权相关命令(4 个)
    用户管理命令(10个命令)
    深入网络操作命令(9条命令)
    查看系统用户登陆信息的命令(7 个)
    查看文件及内容处理命令(21个命令)
    有关磁盘与文件系统的命令(16个命令)
    linux kernel bisops.h
  • 原文地址:https://www.cnblogs.com/hong2016/p/7550258.html
Copyright © 2011-2022 走看看