Article Directory
1. Virtual machine part
After reading a lot of blogs, including Gu Yueju’s tutorials on the official website, the final method of success requires modifying the configuration of the virtual machine’s network connection.
First, you need to ensure that the first three digits of the host's IP address and the virtual machine's IP address are the same, that is, they are in the same LAN , as shown in the figure below.
1. Modify the network connection of the virtual machine to bridge connection mode
Open the settings interface of the virtual machine, and in the network adapter item, select bridged mode.
For the introduction of several modes of network connection of virtual machines, there is the following blogger’s blog reference: When the host is connected to wifi, how does the virtual machine VMware connect to the network
2. View your own virtual machine IP address
Open the terminal in the virtual machine and enter ifconfig
, as shown in the figure below, the IP address of my virtual machine is192.168.1.104
3. Modify the ros configuration of the virtual machine
In the main directory, use Ctrl+H
View hidden files, open .bashrc
the file, start roscore after adding at the end of the file export ROS_IP=192.168.1.104
, and see that the ROS kernel address of the virtual machine is displayed ashttp://192.168.1.104:11311/
2. Host part
1. Host IP address
Open cmd in your own host, enter ipconfig
, and see that your IP address is192.168.1.101
2. Use commands in matlab to establish a connection with ROS
Note that you must first enter roscore in the virtual machine to open the ros kernel before establishing a connection . Enter the following three sentences respectively,
setenv('ROS_MASTER_URI','http://192.168.1.104:11311/')
setenv('ROS_IP','192.168.1.101')
rosinit()
3. View the ROS topic and subscribe to the topic
In the matlab command line, enter rostopic list
, see the current topic
, and then subscribe to a topic to see what the data is sent. There are two ways, one is to use the receive() function, and the other is to use the callback function. The former Only one message can be obtained, and the latter can obtain data in real time, as long as the subscribed topic has publish data.
(1) Method 1: Use the receive() function
sub_name = rossubscriber(topic_name)
message = receive(sub_name, wait_time)
Here is a reminder to use the showdetail() function to view the specific information of the message, and use it to see the effect
(2) Method 2: Use a callback function
sub_name = rossubscriber(topic_name, @callbackfunction);
function callbackfunction(~, msg)
%your code
end
The realization effect is shown in the figure: