Using MATLAB Robotics System Toolbox and Baxter Simulator

In these next sections, we will explore using MATLAB to publish and subscribe to the Baxter Simulator and control some primary functions of his arms and grippers.

Installing Baxter messages in MATLAB

Baxter has a unique set of ROS messages that are used to communicate with a real Baxter and Baxter Simulator in Gazebo. MATLAB requires that these custom Baxter message and service definitions be processed into ROS custom messages understood by MATLAB.

Locate the ROS packages for Baxter on your computer. If you followed the installation instructions in Chapter 6, Wobbling Robot Arms Using Joint Control, these packages will be located in your Baxter workspace, ~/baxter_ws.

In the MATLAB command window, type the following command:

>> rosgenmsg('~/baxter_ws/src/baxter_common')

This is the location of Baxter's message files. If your Baxter workspace is in another location, use the absolute path name to the baxter_common package.

Note

For our computer, Baxter's packages were placed with the system files at /opt/baxter_ws/src/baxter_common. Be aware that the following screen text and screenshots reflect this location instead of ~/baxter_ws/src/baxter_common.

You may see a warning message like the one following, but the process should still be able to build the MATLAB message files.

Warning: The folder /opt/baxter_ws/src/baxter_common/.git does not contain a valid ROS package, because the 'package.xml' file is missing. Create the 'package.xml' file in this folder.

The following message should appear on the screen to show that all of Baxter's packages have been searched for message files:

Building custom message files for the following packages:
baxter_common
baxter_core_msgs
baxter_description
baxter_maintenance_msgs
rethink_ee_description

After additional screen output, the following instructions will appear:


To use the custom messages, follow these steps:
1. Edit javaclasspath.txt, add the following file locations as new lines, and save the file:
/opt/baxter_ws/src/baxter_common/matlab_gen/jar/baxter_common-1.2.0.jar
/opt/baxter_ws/src/baxter_common/matlab_gen/jar/baxter_core_msgs-1.2.0.jar
/opt/baxter_ws/src/baxter_common/matlab_gen/jar/baxter_description-1.2.0.jar
/opt/baxter_ws/src/baxter_common/matlab_gen/jar/baxter_maintenance_msgs-1.2.0.jar
/opt/baxter_ws/src/baxter_common/matlab_gen/jar/rethink_ee_description-1.2.0.jar
2. Add the custom message folder to the MATLAB path by executing:
addpath('/opt/baxter_ws/src/baxter_common/matlab_gen/msggen')
savepath
3. Restart MATLAB and verify that you can use the custom messages.
Type rosmsg list and ensure that the output contains the generated custom message types.

To follow these instructions, click on the link in the screen instructions preceding, on the word javaclasspath.txt. Cut and paste the five /opt/baxter_ws/src/baxter_common… lines from instruction 1 to the javaclasspath.txt file but add the <before> tag on the first line. The <before> token at the front of the JAR file will tell MATLAB to use this file, instead on any built-in messages. Our javaclasspath.txt is shown in the following screenshot. Be sure your file contains the absolute path names to your .jar files:

Installing Baxter messages in MATLAB

Contents of jarclasspath.txt file

As described in instruction 2, cut and paste the addpath and savepath commands into MATLAB's command window. After these commands have executed, restart MATLAB and these new ROS Baxter messages should be available in MATLAB. Type the command rosmsg list to verify that Baxter's messages have been added to the entire list of ROS messages in MATLAB.

Running Baxter Simulator and MATLAB

To start Baxter Simulator, open a terminal window and go to the baxter_ws workspace; then, run the Baxter shell script with the sim parameter specified:

$ cd ~/baxter_ws
$ ./baxter.sh sim

Note

Important:

Check the ROS environment variables with the following command:

$ env | grep ROS

Within the output screen text, look for the following result:

ROS_MASTER_URI=http://localhost:11311
ROS_IP= <your workstation's IP address>

Alternatively, it may show the following output:

ROS_HOSTNAME=<your workstation's hostname>or "localhost"

The ROS_HOSTNAME field need not be present.

If the ROS_IP or ROS_HOSTNAME environment variable does not match the IP address of your workstation, type exit to stop communication with the simulated Baxter. Then, edit the baxter.sh script to change the your_ip variable (near line 26) to the current IP address of your workstation or change the your_hostname variable (near line 28) to localhost. Save and exit the baxter.sh script.

To continue, repeat the preceding steps for a final check.

Next, call the roslaunch command to start the simulation:

$ roslaunch baxter_gazebo baxter_world.launch

These are the same commands used in Chapter 6, Wobbling Robot Arms Using Joint Control. You should see Baxter appear on the Gazebo screen with its BaxterIO window open to show the navigator buttons and cuff buttons.

Now, MATLAB should be started from the icon or command line. After it is up and running you should have screens similar to the following screenshot:

Running Baxter Simulator and MATLAB

MATLAB and Baxter Simulator running

All of the code for Chapter 10, Controlling Baxter with MATLAB©, is available online at the Packt Publishing website at http://www.PacktPub.com, or from GitHub at https://github.com/FairchildC/ROS-Robotics-By-Example-2nd-Edition. Download the .m files from the Chapter10_code to a location on your computer.

These MATLAB .m files were originally written by Carlos Santacruz-Rosero of MathWorks. Carlos developed the MATLAB classdef of BaxterCommWithSim to have Baxter to play checkers with a human opponent (2014). The code has been modified to work with Baxter's latest version of software v1.2.0 by this book's authors for use in this chapter.

After you have downloaded the code to your computer, locate the .m files for the Chapter10_code. These files include:

  • BaxterCommWithSim.m
  • ExampleScript.m
  • armUpdateTimerForRobot.m
  • armUpdateTimerForSim.m

Add the path to these files using the Set Path menu option on the MATLAB HOME menu bar. Now you are ready to start communicating with and moving Baxter.

Tip

Troubleshooting tip

When running the BaxterCommWithSim functions in MATLAB, infrequently errors may appear on the screen. At this point, it is best to shutdown both MALAB and Gazebo and restart from the instructions above in Running Baxter Simulator and MATLAB.

Making Baxter move

The ExampleScript.m file can be used as an example of the functions possible with the BaxterCommWithSim class as defined in the BaxterCommWithSim.m file. These functions are explained in the order they appear in ExampleScrip.m:

rosshutdown;

The rosshutdown command is issued as the first command in the script to assure that MATLAB is disconnected from any ROS network and that the global node and the ROS Master are not running.

rosinit;

The rosinit command starts the global ROS node and connects to the ROS Master running on localhost and port 11311. This communication will be with the ROS Master started by Baxter Simulator on Gazebo, as it should already be up and running.

bc = BaxterCommWithSim(true);

This command instantiates a class for communicating with the Baxter robot in the Gazebo simulation. The BaxterCommWithSim class provides data structures and methods to support enabling Baxter and moving Baxter's head, arms and grippers. The object bc is created as an instance of BaxterCommWithSim to allow the use of these data structures and methods.

enable(bc);

The enable command enables the Baxter robot to move. The robot must be put in an enabled state before any movement commands will move Baxter's joints.

handles.bc = bc;
timer1 = timer('TimerFcn',{@armUpdateTimerForSim,handles},'Period',0.1,'ExecutionMode','fixedSpacing');
start(timer1);

These three commands start a timer to periodically send Baxter's joint commands. The timer object timer1 is created and specifies a callback the function armUpdateTimerForSim with a handle to the bc object. This armUpdateTimeForSim function calls the function updateArms passing the handle to the bc object indicating that both arms should be updated. The updateArms function will publish ArmCmdLeftMsg and ArmCmdRightMsg messages to set the positions of each of the joints on both of Baxter's left and right arms. The publication of these messages is performed periodically at 10 Hz.

In the following commands, the commands to control the arms and grippers have a value that is passed as an argument that indicates whether the left, right, or both arm(s) or gripper(s) should be activated. These values correspond to the selections:

  • 1 = left
  • 2 = right
  • 3 = both
untuck(bc, 3);

This untuck command sets the fields in the ArmCmdLeftMsg and ArmCmdRightMsg messages specifying the arm joint positions for Baxter's untuck position.

calibrateGrip(bc, 3);

This calibrateGrip command calibrates both of Baxter's grippers. The grippers must be calibrated before use.

grip(bc, 2);

This grip command closes Baxter's right (2) gripper. The GripRightMsg message is set with the position 0.0 which is the gripper closed position.

release(bc, 2);

This release command opens Baxter's right (2) gripper. The GripRightMsg message is set with the position 100.0 which is the gripper open position.

panHead(bc, 1.0);
panHead(bc, 0.0, 0.2);

These panHead commands are used to move Baxter's head to a set angular position. The first command moves the head to a 1.0 radian (57 degrees) position to Baxter's left side. The second command moves the head to the 0.0 radian position which is straight ahead facing forward. The second argument in this command sets the speed to 0.2 which is slow. The speed setting is a percentage that ranges from 0.0 to 1.0.

pose_Y(bc);

The pose_Y command is an example of a function that moves Baxter's arms to a specific pose. This function sets each of the joint positions in both the right and left arms to create a pose for Baxter where his arms form the letter Y. The following screenshot shows Baxter in a Y pose:

Making Baxter move

Baxter's Y pose

enableEndpointDisplay(bc,3);

The enableEndpointDisplay command enables the display of endpoint information for both (3) of Baxter's arms.

currPose = bc.RightArmEndpoint;
disp(currPose.Pos);
disp(currPose.Orientation);

The variable currPose is set to the current endpoint_state information of Baxter's right arm. The disp function is called to print the information on the endpoint position and orientation to the screen as shown in the following screenshot:

Making Baxter move

Display of Baxter's endpoint position and orientation

untuck(bc, 3);

The untuck command resets both (3) of Baxter's arms into the untuck position.

tuck(bc, 3);

The tuck command sets both (3) of Baxter's arms to the tuck configuration that is used for storing Baxter.

disable(bc);

The disable command disables Baxter's movement. No movement commands will move Baxter's joints until an enable command is issued.

stop(timer1);
delete(timer1);

The stop command will stop the timer1 timer from incrementing and the delete command will delete timer1.

rosshutdown;

The rosshutdown command shuts down the global node and the ROS Master, if it is running. MATLAB will no longer be connected to the Baxter Simulator in Gazebo.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.237.136