通信過程如果設置不當會出現,rosnode list下面可以看到matlab 的節點,但是從ros中發出的消息卻不能讀出data
如果rostopic echo topic名稱 在ros下能輸出,但是在matlab下面無法輸出,則可以定位爲通信問題,既然可以看到matlab的節點說明matlab 與ros是可以通信的,此時應該反向思考,ros與matlab通信應該存在問題。
https://www.mathworks.com/matlabcentral/answers/347809-matlab-ros-subscriber-basic-example
這裏說明了較爲詳細的解決辦法:
In my case, the IP adress are
PC with Matlab : 192.168.1.41
ROS computer : 192.168.1.28
In the .bashrc, I have
source /opt/ros/indigo/setup.bash
export ROS_MASTER_URI=http//192.168.1.28:11311
export ROS_HOSTNAME=192.168.1.28
export ROS_IP=192.168.1.28
.
In Matlab 2 solutions are possible:
solution 1:
setenv('ROS_IP','192.168.1.41') % adress PC with Matlab
setenv('ROS_HOSTNAME','192.168.1.41')
setenv('ROS_MASTER_URI','http://192.168.1.28:11311/')
讀數據ROS與matlab存在一些差異,其中ROS讀取數據是通過節點subscribe所訪問的topic,而在matlab中則不是如此。
rosshutdown
% setenv('ROS_MASTER_URI','http://192.168.1.21:11311')
% setenv('ROS_IP','192.168.1.18')
setenv('ROS_IP','192.168.1.18') % adress PC with Matlab
setenv('ROS_HOSTNAME','192.168.1.18')
setenv('ROS_MASTER_URI','http://192.168.1.21:11311/') % adress turtlebot
rosinit()
sub=rossubscriber('chatter')
while (1)
msg2 = receive(sub)
disp(msg2)
end
不需要創建節點。
https://www.mathworks.com/help/ros/ug/connect-to-a-ros-network.html
這個網站是配置matlab與ros的官方文檔,其中下面這段描述瞭如何編輯ROS環境變量,
ROS Environment Variables
In advanced use cases, you might want to specify the address of a ROS master and your advertised node address through standard ROS environment variables. The syntaxes that were explained in the previous sections should be sufficient for the majority of your use cases.
If no arguments are provided to rosinit
, the function will also check the values of standard ROS environment variables. These variables are ROS_MASTER_URI
, ROS_HOSTNAME
, and ROS_IP
. You can see their current values using the getenv
command:
getenv('ROS_MASTER_URI') getenv('ROS_HOSTNAME') getenv('ROS_IP')
You can set these variables using the setenv
command. After setting the environment variables, call rosinit
with no arguments. The address of the ROS master is specified by ROS_MASTER_URI,
and the global node's advertised address is given by ROS_IP
or ROS_HOSTNAME
. If you specify additional arguments to rosinit
, they override the values in the environment variables.
setenv('ROS_MASTER_URI','http://192.168.1.1:11311') setenv('ROS_IP','192.168.1.100') rosinit
You do not have to set both ROS_HOSTNAME
and ROS_IP
. If both are set, ROS_HOSTNAME
takes precedence.