簡體   English   中英

我想將來自ROS主題的消息存儲在數組中以進行進一步闡述

[英]I want to store in an array messages from a ROS topic for further elaboration

很抱歉,這個問題對您來說太簡單了,但是我沒有很好的編程技能和ROS知識。 我有一個ROS主題,其中發布了一些數字(以秒為單位的心跳間隔)。 我需要訂閱該主題並進行詳細說明:想法是要有十個數字組成的小數組,我可以在其中連續存儲十個心跳。 然后,我有一個更大的數組,其中包含60個數字,必須將這些數字上移十個位置,以使小數組的最新十個值位於底部,並且它必須“舍棄”十個最舊的值(我做了一些研究也許我必須使用向量而不是數組,因為據我所知,在C ++中數組是固定的。 然后,我每次必須在文本文件中打印這60個值(我的意思是循環,所以文本文件將被連續覆蓋)。 此外,我看到,ROS由一個主題輸出數據以這種方式: data: 0.987與每一個數據通過與其他划分---在一列。 我真正想要的是因為我需要一個以這種方式讀取文本文件的腳本,因此它是一個文本文件,其中值位於一列中,沒有空格和其他符號或單詞,如下所示:

0.404
0.952
0.956
0.940
0.960

我在下面為我的節點提供了代碼,由於我不知道如何處理以后要做的事情,因此到目前為止,我只執行了訂閱部分。 預先感謝您的幫助!!!

碼:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>

int main(int argc, char **argv)
{

ros::init(argc, argv, "writer");


ros::NodeHandle n;


ros::Subscriber sub = n.subscribe("/HeartRateInterval", 1000);


ros::spin();

return 0;
}

注意:我沒有包括Float32 / 64標頭,因為我將心跳發布為字符串。 我不知道這是否有意義。

編輯:我將在ROS主題上發布數據的發布者節點的代碼下面包括代碼。

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;


int main(int argc, char **argv)
{

ros::init(argc, argv, "heart_rate_monitor");

ros::NodeHandle n;

ros::Publisher pub = n.advertise<std_msgs::String>("/HeartRateInterval", 1000);

ros::Rate loop_rate(1);

while (ros::ok())

{ 

ifstream inputFile("/home/marco/Scrivania/marks.txt");

string line;

while (getline(inputFile, line)) {


istringstream ss(line);

string heart;

ss >> heart;

std_msgs::String msg;

msg.data = ss.str();

pub.publish(msg);

ros::spinOnce();

loop_rate.sleep();

}

}

return 0;

}

由於所公布的是“變量” msg ,我試圖給出答案的變量的代碼來替換string_msgmsg ,但一切都沒有改變。 謝謝!

我不確定我是否確切了解您想要什么,但是這里有一個簡短的示例,可以滿足您的需求。

我在這里使用的std::deque具有60個值的循環緩沖區。 您的代碼中缺少的是一個回調函數process_message ,該函數在每次process_message新消息時都會為訂戶調用。

我沒有編譯此代碼,因此它可能不會立即編譯,但基礎知識已經存在。

#include <ros/ros.h>
#include <std_msgs/String.h>
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <deque>


static std::deque<std::string> queue_buffer;
static int entries_added_since_last_write = 0;

void write_data_to_file()
{
  // open file
  std::ofstream data_file("my_data_file.txt");
  if (data_file.is_open())
  {
    for (int i = 0; i < queue_buffer.size(); ++i)
    {
      data_file << queue_buffer[i] << std::end;
    }
  }
  else
  {
    std::cout << "Error - Cannot open file." << std::endl;
    exit(1);
  }
  data_file.close();
}

void process_message(const std_msgs::String::ConstPtr& string_msg)
{
  // if buffer has already 60 entries, throw away the oldest one
  if (queue_buffer.size() == 60)
  {
    queue_buffer.pop_front(); 
  }

  // add the new data at the end
  queue_buffer.push_back(string_msg.data);

  // check if 10 elements have been added and write to file if so
  entries_added_since_last_write++;
  if (entries_added_since_last_write == 10
      && queue_buffer.size() == 60)
  {
    // write data to file and reset counter
    write_data_to_file();
    entries_added_since_last_write = 0;
  }
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "writer");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("/HeartRateInterval", 1000, process_message);
  ros::spin();
  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM