我在問你是否有辦法在兩個ROS節點之間有一個優先級。特別是,我有一個ROS節點,它使得輸出是一個包含60個數據的文本文件,並且每次都會重新創建它,因爲數據正在改變。然後我有一個節點必須分析該文本文件。基本上,我需要的是做一些更改,以便在編寫器節點運行時停止分析器節點,然後它必須向分析器節點發送信號,以使其能夠運行和分析文本文件。然後編寫器節點必須返回讓我們說「負責」才能重新寫入文本文件。所以,簡而言之,就是一個循環。有人告訴我,一個可能的解決方案可能就像寫信節點寫入的「信號量」主題,例如,在打開,寫入和關閉文本文件時,布爾值爲1,因此分析器節點知道無法進行詳細說明,因爲該文件尚未準備好。並且,當作者完成並關閉文本文件時,必須發佈值爲0的值,以允許分析器節點進行分析。我搜索了布爾值的發佈,我發現可以是這樣的代碼:有兩種ROS節點之間有優先權的方法嗎?
ros::Publisher pub = n.advertise<std_msgs::Bool>("semaphore", 1000);
std_msgs::Bool state;
state.data = 1;
我不知道如果我只用在作家節點發布者和用戶分析儀節點。也許我必須在兩個節點中使用它們,例如:編寫者在主題信號量中放置1,以便分析器知道無法訪問文本文件,生成文本文件,然後在主題中放入0並訂閱話題再等一個1;分析儀做了類似的事情,但是相反。我把這兩個代碼放在下面,因爲我不知道應該把發佈者和訂閱者放在哪裏,以及如何使它們運行良好。如果可能的話,我必須在我的代碼中保持這種工作流程結構。 注意:幾乎每10秒就會創建一個新的文本文件,因爲在文本文件中寫入的數據來自另一個ROS主題,並且編寫器中的代碼有一種機制來完成這種細化。 提前謝謝! 編輯:現在的代碼糾正與基於主題的解決方案,正如我在我最後的評論中解釋。
作家代碼:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "std_msgs/Bool.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <deque>
#include "heart_rate_monitor/analyse_heart_rate.h"
using namespace std;
static std::deque<std::string> queue_buffer;
static int entries_added_since_last_write = 0;
ros::Publisher pub;
void write_data_to_file()
{
// open file;
std::ofstream data_file("/home/marco/catkin_ws/src/heart_rate_monitor/my_data_file.txt");
if (data_file.is_open())
{
for (int i = 0; i < queue_buffer.size(); ++i)
{
data_file << queue_buffer[i] << std::endl;
}
}
else
{
std::cout << "Error - Cannot open file." << std::endl;
exit(1);
}
data_file.close();
std_msgs::Bool state;
state.data = 0;
pub.publish(state);
}
void process_message(const std_msgs::String::ConstPtr& string_msg)
{
std_msgs::Bool state;
state.data = 1;
pub.publish(state);
// 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);
pub = n.advertise<std_msgs::Bool>("/semaphore", 1000);
ros::spin();
return 0;
}
分析代碼:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "std_msgs/Bool.h"
#include "../include/heart_rate_monitor/wfdb.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <deque>
#include "heart_rate_monitor/analyse_heart_rate.h"
void orderCallback(const std_msgs::Bool::ConstPtr& msg)
{
if (msg->data == 0)
{
chdir("/home/marco/catkin_ws/src/heart_rate_monitor");
system("get_hrv -R my_data_file.txt >doc.txt");
}
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "analyzer");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/semaphore", 1000, orderCallback);
ros::spin();
return 0;
}
你好@Vtik,謝謝你的回覆!你介意我是否要求你詳細解釋你的想法?此外代碼片段將非常感激。只有當你可以,當然!我這樣說是因爲我對編碼非常陌生,對ROS本身也是超新的。再次感謝你! – Marcofon
非常感謝你!我會嘗試將它應用於我的案例!再次感謝你!我會讓你知道我是否可以做到。 – Marcofon
我很抱歉這個愚蠢的問題,但在.srv文件中,而不是文件名,我必須把我想分析的文本文件的名稱?編輯:還有一件事。有了你建議給我的結構,是每次都要調用的腳本,還是隻有當我有60個數據的文本文件中有10個新數據?因爲這是我的目標,也許從我的問題和我的代碼中不清楚,我試圖在註釋部分解釋這一點。我很抱歉,如果是這樣的話! – Marcofon