sign following robot with ros in matlab -凯发k8网页登录
this example shows how to control a simulated robot running on a ros-based simulator over a ros network. you generate a ros node for the control algorithm and deploy it to the remote device running ros. the example shown here uses ros and matlab for simulation, and matlab coder™ for code generation and deployment. for the other examples with ros 2 or simulink®, see:
in this example, you implement a sign-following algorithm and control the simulated robot to follow a path based on signs in the environment. the algorithm receives the location and camera information from the simulated robot. the algorithm detects the color of the sign and sends velocity commands to turn the robot based on the color. in this example, the robot turns left when it encounters a blue sign and right when it encounters a green sign. the robot stops when it encounters a red sign.
connect to robot simulator
start a ros-based simulator for a differential-drive robot and configure a matlab® connection with the robot simulator.
download a virtual machine using instructions in , then follow these steps.
start the ubuntu® virtual machine desktop.
click the gazebo sign follower ros icon to start the gazebo world for this example.
specify the ip address and port number of the ros master in gazebo so that matlab can communicate with the robot simulator. for this example, the ros master in gazebo has ip
172.18.228.122
and port number11311
,192.168.31.1
.start the ros 1 network using
rosinit
.
masterip = '172.18.228.122';
rosinit(masterip,11311);
initializing global node /matlab_global_node_12033 with nodeuri http://172.18.228.249:60144/ and masteruri http://172.18.228.122:11311.
configure ros communication
create publishers and subscribers to relay messages to and from the robot simulator over ros network. you need subscribers for the image and odometry data. to control the robot, set up a publisher to send velocity commands using /cmd_vel
. the
and functions support code generation for message structures only. to return a message structure, specify the name-value pair argument, "dataformat","struct"
, when creating subscribers and publishers.
imgsub = rossubscriber("/camera/rgb/image_raw","sensor_msgs/image",dataformat = "struct"); odomsub = rossubscriber("/odom","nav_msgs/odometry",dataformat = "struct"); [velpub, velmsg] = rospublisher("/cmd_vel", "geometry_msgs/twist",dataformat = "struct");
create a object to log the subscribed image and odometry data that the robot publishes.
bagwriter = rosbagwriter("sign_following_data.bag")
bagwriter = rosbagwriter with properties: filepath: 'z:\32\psahu.bdoc23a.j2115093\sign_following_data.bag' starttime: 0 endtime: 0 nummessages: 0 compression: 'uncompressed' chunksize: 786432 bytes filesize: 4117 bytes
define the image processing color threshold parameters. each row defines the threshold values for the corresponding color.
colorthresholds = [100 255 0 55 0 50; ... % red 0 50 50 255 0 50; ... % green 0 40 0 55 50 255]'; % blue
create sign-following controller using stateflow® chart
this example provides a helper matlab stateflow® chart that takes as inputs the image size, coordinates from a processed image, and robot odometry poses. the chart returns the linear and angular velocity to drive the robot.
controller = examplehelpersignfollowingcontrollerchart;
open("examplehelpersignfollowingcontrollerchart");
run control loop
run the controller to receive images and move the robot to follow the sign. the controller performs these steps:
get the latest image and odometry message from the ros network.
run the algorithm for detecting image features (
examplehelpersignfollowingprocessimg
) function.generate control commands from the stateflow® chart using .
publish the velocity control commands to the ros network.
to visualize the masked image that the robot sees, change the value of dovisualization
to true
.
examplehelpersignfollowingsetuppreferences; dovisualization = false; r = rosrate(10); receive(imgsub); receive(odomsub); while(~controller.done) % get the latest sensor messages and process them. imgmsg = imgsub.latestmessage; odommsg = odomsub.latestmessage; [img,pose] = examplehelpersignfollowingrosprocessmsg(imgmsg, odommsg); % log the subscribed image and odometry data into a bag file. currenttime = rostime("now"); write(bagwriter,imgsub.topicname,currenttime,imgmsg); write(bagwriter,odomsub.topicname,currenttime,odommsg); % run the vision and control functions. [mask,blobsize,blobx] = examplehelpersignfollowingprocessimg(img, colorthresholds); step(controller,"blobsize",blobsize,"blobx",blobx,"pose",pose); v = controller.v; w = controller.w; % publish the velocity commands. velmsg.linear.x = v; velmsg.angular.z = w; send(velpub,velmsg); % optionally visualize % note: visualizing data will slow down the execution loop. % if you have computer vision toolbox, we recommend using % vision.deployablevideoplayer instead of imshow. if dovisualization imshow(mask); title(['linear vel: ' num2str(v) ' angular vel: ' num2str(w)]); drawnow('limitrate'); end % pace the execution loop. waitfor(r); end
the robot follows the signs and stops at the final stop sign. this figure shows the robot in motion.
delete the rosbagwriter
object to close the bag file and reset the gazebo scene after simulation using the /gazebo/reset_simulation
service. create a object for the service. use the object function to call the service and reset the gazebo simulation scene.
delete(bagwriter); gazeboresetclient = rossvcclient('/gazebo/reset_simulation',dataformat = "struct"); call(gazeboresetclient);
visualize logged ros messages
open the ros bag viewer app by entering rosbagviewer
in the matlab® command window. you can also launch the app from the apps section from the matlab toolstrip.
click open and load the generated sign_following_data.bag
file. inspect the topic list and source details panel on the left side of the app to check the bag file information. launch image and odometry visualizers from the toolstrip and select data sources. use the playback panel controls to play the bag file and visualize the logged messages.
generate and deploy ros node
after you verify the controller, you can generate a ros node for the sign-following robot algorithm using matlab coder™ software. you can then deploy this node on the remote virtual machine running gazebo. using deployment, you can run ros nodes on remote machines directly, resulting in faster executions. create a matlab coder configuration object that uses "robot operating system (ros)" hardware.
cfg = coder.config("exe"); cfg.hardware = coder.hardware("robot operating system (ros)");
set configuration parameters. you can adjust these values so they are suitable for your remote device and verify them before deployment. the robot follows the signs and stops at the final stop sign.
cfg.hardware.deployto = "remote device"; cfg.hardware.remotedeviceaddress = '192.168.192.129'; cfg.hardware.remotedeviceusername = "user"; cfg.hardware.remotedevicepassword = "password";
set the build action to "build
and
run"
to ensure that the deployed ros node starts running after code generation.
cfg.hardware.buildaction = "build and run";
generate the ros node and deploy the controller. the deploysignfollowingrobotros
function contains the controller algorithm code.
codegen deploysignfollowingrobotros -config cfg
reset the gazebo scene after the node executes by calling the rossvcclient
object, gazeboresetclient
.
call(gazeboresetclient);
rerun deployed node using rosdevice
to rerun the deployed ros node, create a object. specify the deviceaddress,
username,
and password
inputs to establish an ssh connection between the ros device and matlab.
gazebovmdevice = rosdevice('192.168.192.129',"user","password");
check the available nodes on the connected remote device. verify that the deployed ros node, deploysignfollowingrobotros,
exists on the remote device .
gazebovmdevice.availablenodes
run the ros node on the remote device using
function. the robot follows the signs and stops at the final stop sign.
runnode(gazebovmdevice,'deploysignfollowingrobotros')