C++でfloat、intなどのデータをstringタイプに簡単に変換し、ROSのstd_を利用する方法msg/Stringリリース

1020 ワード

ROSシステムではstd_のような標準的なメッセージタイプを利用する必要がある場合があります.msg/Stringなどは、いくつかのデータを公開します.これは、次のような異なるデータ型を相互に変換する必要があります.
float-->string int------>stringなど.ここではstringstreamを用いて変換し,stringstreamはC++が提供する別の文字列型のストリーム(stream)物である.
次に、具体的な操作方法と例を示します.
1.ヘッダファイルを含む.
#include
2.float(またはintなどの他のタイプ)型データを2段階に分けてstringデータに変換する.
すなわちfloatデータをstreamに出力する
streamからstringを得る
例:
float  WristRightR;
string Result; 
ostringstream convert;
convert << WristRightR;//put the float WristRightR into stream
Result = convert.str();	//get string from stream
3.変換したstringデータをROSメッセージシステムを用いて/chatterなどの話題に公開する.
std_msgs::String msg;//define standard string msg
msg.data = Result;//get the string need to be pblished
chatter_pub.publish(msg);//publish
のうち、chatter_pubは、予め定義されたパブリケーションイベントです.
ros::Publisher chatter_pub = n.advertise<:string>("chatter", 1000);
が終了しました.
参照先:クリックしてリンクを開く