Looking for maximal value of a published ROS message ( store data using table )
Hello everyone, I'm trying to subscribe to a ROS topic ( float) and looking to store the published data in a table and then to look for the maximal value inside the table. anyone could help me to do it. My objective is to determine the maximal value among the published data. If anyone has another idea rather than using table, i'm interested also.
#include <ros/ros.h>
#include <std_msgs/Float64.h>
#include <std_msgs/Int32.h>
#include <std_msgs/Bool.h>
#include <iostream>
#include <vector>
#include <ros/console.h>
#include <stdio.h>
using namespace std;
//Variable declaration
std_msgs::Float64 distanceFollowerLeader;
std_msgs::Bool startObserver;
std_msgs::Float64 maxDistance;
float ActualDistance;
vector<float> maxima(100);
vector<float> tableau (100);
//Callback function
void myCallbackObserver(const std_msgs::Bool& message_holder)
{
ROS_INFO("received Observer restart:%d",message_holder.data);
startObserver.data=message_holder.data;
}
void myCallbackdistanceLeaderFollower(const std_msgs::Float64& message_holder)
{
//check for data on topic "distanceFollowerLeader"
ROS_INFO("received distanceLeaderFollower value is:%f",message_holder.data);
distanceFollowerLeader.data=message_holder.data;
}
//main program
int main(int argc, char **argv)
{
ros::init(argc,argv,"ObserverNode");//name this node
ros::NodeHandle nh; // node Handle
//create subscribers
ros::Subscriber my_subscriber_object1=nh.subscribe("startObserver",1,myCallbackObserver);
ros::Subscriber my_subscriber_object2=nh.subscribe("distanceFollowerLeader",1,myCallbackdistanceLeaderFollower);
// create publishers
ros::Publisher my_publisher_object2=nh.advertise<std_msgs::Float64>("maxDistance",1);
ros::Rate loop_rate(1);
startObserver.data=true;
maxDistance.data = 10;
while(ros::ok()) {
if (startObserver.data) {
maxima.push_back(maxDistance.data);
ActualDistance=distanceFollowerLeader.data;
if (ActualDistance > maxDistance.data) {
maxDistance.data = ActualDistance;
ROS_INFO("max scenario :%f",maxDistance.data);
}
startObserver.data=false;
maxima.push_back(maxDistance.data);
}
my_publisher_object2.publish(maxDistance);
ROS_INFO("max distance =%f",maxDistance.data);
ros::spinOnce();
loop_rate.sleep();
}
return 0; //should never get here, unless roscore dies
}
I'm trying fo find the maximal distance. To do so this node subscribe to the topic
. the objective is to find the maximal distance value at the end of the observation.
Thank you in advance for your help.
If you're only interested in finding the maximum value you could simply store the previous maximum and compare new values to that. No need to store a table that potentially could get very large.
thank you very much for your reply. i tried to do it but the maximal value did not change and remain constant (initial value) although using the rostopic echo i clearly saw the content of the message changing along the simulation.
There must be a bug in your code in that case. Can you pose your source code for this and we'll get it fixed.
I updated the question to include my code. thank you very much for your help.