matlab ros 通信

通信过程如果设置不当会出现,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_URIROS_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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章