Use matlab and virtual machine ros to connect and subscribe to message tutorial in win

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.
insert image description here

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.
insert image description here

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
insert image description here

3. Modify the ros configuration of the virtual machine

In the main directory, use Ctrl+HView hidden files, open .bashrcthe 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/
insert image description here

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
insert image description here

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()

insert image description here

3. View the ROS topic and subscribe to the topic

In the matlab command line, enter rostopic list, see the current topic
insert image description here
, 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)

insert image description here
Here is a reminder to use the showdetail() function to view the specific information of the message, and use it to see the effect
insert image description here

(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:
insert image description here

Supongo que te gusta

Origin blog.csdn.net/Er_Studying_Bai/article/details/129227579
Recomendado
Clasificación