MATLAB 控制baxter机器人相关信息(个人记录--学习用)

1、在MATLAB 官网answer中,找到如何发布/订阅机器人关节状态信息流。

     来源:https://ww2.mathworks.cn/matlabcentral/answers/270072-joint_states-publisher

A:Hello, I am using the Robotics Toolbox on Matlab to work on the Baxter robot. While I subscribe to a topic like /robot/joint_states and create a publisher for the same, instead of receiving the stream of joint angle sets I just receive one set of joint angles. The commands I am using are the following:

 joint_state_sub = rossubscriber('/robot/joint_states');
 state_pub = receive(joint_state_sub,10);
Also in the publisher I am unable to understand the value 10, as even if I change it to 100 or any other value  I still get the same result.

Q:The receive function waits up to N (10, 100, etc.) seconds to receive the NEXT incoming message in the stream -- it is not meant to receive the entire stream.

There are plenty of ways to receive the whole data set by repeatedly calling receive (Waits for the next message) or using the LatestMessage property (picks the last received message, can get the same message multiple times in a row if no new ones come in).

代码:

msg = receive(joint_state_sub,10);
msg = joint_state_sub.LatestMessage;

One way you could do it is in a for-loop. For example, suppose you want to receive the next 5000 messages that come in:

代码:

% Preallocate buffer of 5000 elements -- assuming there are 3 joints in this robot, hence 3 columns.
angles = zeros(5000,3);
% Loop through and receive
for idx = 1:5000
  state_msg = receive(joint_state_sub,10);
  angles(idx,:) = state_msg.Position;
end

By the way, it might be useful to also pick out the time stamps for each message if you want to plot these. To do this, you can use the Header portion of the message:

代码:

t_sec = state_msg.Header.Time.Stamp.Sec;
t_nsec = state_msg.Header.Time.Stamp.Nsec;
...
times(idx) = double(t_sec) + 1e-9*double(t_nsec)

2、从Simulink®生成独立的ROS节点

     来源:https://ww2.mathworks.cn/examples/robotics/mw/robotics-ex31008889-generate-a-standalone-ros-node-from-simulink

Generate a Standalone ROS Node from Simulink®

This example shows you how to generate and build a standalone ROS node from a Simulink model.

Introduction

In this example, you configure a model to generate C++ code for a standalone ROS node. You then build and run the ROS node on an Ubuntu® Linux® system.

在此示例中,您将配置模型以为独立ROS节点生成C ++代码。 然后,您可以在Ubuntu®Linux®系统上构建并运行ROS节点

Prerequisites先决条件

  • This example requires Simulink Coder™.
  • A Ubuntu Linux system with ROS and an SSH server installed is necessary for building and running the generated C++ code. You can use your own Ubuntu ROS system, or you can use the Linux virtual machine used for Robotics System Toolbox™ examples (see Get Started with Gazebo and a Simulated TurtleBot for instructions).
  • Review the Feedback Control of a ROS-enabled Robot example.

Task 1 - Configure a Model for Code Generation

In this task, you configure a model to generate C++ code for a standalone ROS node. The model is the proportional controller introduced in the Feedback Control of a ROS-enabled Robot example.

  • Open the robot feedback control model. Select Edit > Select All, then Edit > Copy.
  • Open a new Simulink model. Select Edit > Paste.
  • Delete the Simulation Rate Control block.
  • Click on Simulation > Model Configuration Parameters.
  • The model requires variable-sized arrays, so enable it by checking Code Generation > Interface > Software environment > Support > variable-size signals. Go to Hardwarde Implementation > Advanced Parameters and check Use Embedded Coder features to enable this option.
  • In the Hardware Implementation pane of the Configuration Parameters dialog, set Hardware board to Robot Operating System (ROS), and click Apply. The Hardware board settings section contains settings specific to the generated ROS package, such as information to be included in the package.xml file. Change Maintainer name to ROS Example User.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第1张图片

  • In the Solver pane of the Configuration Parameters dialog, ensure that Solver Type is set to Fixed-step, and set Fixed-step size to 0.05. In generated code, the Fixed-step size defines the actual time step, in seconds, that is used for the model update loop (see About Model Execution). It can be made smaller (e.g., 0.001 or 0.0001) but for current purposes 0.05 is sufficient.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第2张图片

  • Click OK to close the Configuration Parameters dialog. Save the model as RobotController.slx.

Task 2 - Configure the Connection to the ROS Device

A ROS device is any Linux system that has ROS installed and is capable of building and running a ROS node. If you have Simulink Coder, you can generate code for a standalone ROS node. If your system is connected to a ROS device, Simulink can also transfer the generated code to the ROS device, build an executable, and run the resulting ROS node (this is referred to as "deploying" the ROS node).

In this task, you decide if you want to generate code for the ROS node or if you want to build and run it on a ROS device. If you are connected to a ROS device, you can configure Simulink to use it as a deployment target for your ROS node.

  • Click on Simulation > Model Configuration Parameters.
  • In the Hardware Implementation pane of Configuration Parameters dialog, click on Build Options. The selected Build action affects the behavior of Simulink when building the model. None (the default setting) only generates the code for the ROS node, without building it on an external ROS device. Build and load generates the code, transfers it to an external device and builds a ROS node executable. If you select Build and run, the resulting node executable is started automatically at the end of the build.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第3张图片

  • Set the Build action to Build and run
  • Configure the connection to your external ROS device. Click Edit to open the device configuration dialog. In this dialog, you can enter all the information that Simulink needs to deploy the ROS node. This includes the IP address or host name of your ROS device, your login credentials, and the Catkin workspace. Change Catkin workspace to ~/catkin_ws_test.

ROS Folder is the location of the ROS installation on the ROS device. If you do not specify this folder, the settings test (see next step) tries to determine the correct folder for you.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第4张图片

  • If the ROS device is turned on and accessible from your computer, you can verify the connection settings by clicking Test. The test verifies every device setting and display warnings and errors in the Simulink Diagnostic Viewer if problems are found. If possible, the test also suggests how the problems can be fixed. Click Test now.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第5张图片

  • Most likely, the Catkin workspace ~/catkin_ws_test does not exist on the target device. The test detects this problem and suggests to create the folder and initialize the workspace. Click Fix to apply this action automatically. After a few seconds, you should see a green notice that the folder has been created successfully. To verify that the Catkin workspace is now available, click Test in the connection settings dialog again. The warning has disappeared and the Catkin workspace is ready to build your ROS node.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第6张图片

  • Change the device connection settings and test them until no other warnings or errors are shown. If an automatic fix to your settings is possible, Simulink suggests it by displaying the Fix button. Once you have a good set of settings, click OK in the connection settings dialog to save the settings.

The connection settings are not specific to a single model, but apply to all ROS models in Simulink.

Task 3 - Generate the C++ ROS Node

In this task, you generate code for a standalone ROS node, and automatically transfer, build, and run it on the ROS device. You exercise the generated ROS node using a ROS master running on the ROS device.

1. In MATLAB®, change the current folder to a temporary location where you have write permission.

2. The code generation process first prepares the model for simulation to ensure that all blocks are properly initialized. This preparation requires a valid connection to a ROS master.

In MATLAB, you can use the rosdevice object to start a ROS master on the ROS device. If you provide no arguments, rosdevice uses the device connection settings you entered in the Simulink dialog to connect to the ROS device.

d = rosdevice

runCore(d);

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第7张图片

3. Use rosinit to connect MATLAB to the ROS master running on the ROS device:

rosinit(d.DeviceAddress)

4. Tell Simulink to use the same ROS connection settings as MATLAB by selecting Tools > Robot Operating System > Configure Network Addresses, and setting the ROS Master and Node Host network addresses to Default.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第8张图片

You only have to execute steps 2 - 4 once per MATLAB session, not every time you generate a ROS node.

5. Click on Code > C/C++ Code > Deploy to Hardware. If you get any errors about bus type mismatch, close the model, clear all variables from the base MATLAB workspace, and re-open the model.

Click on the View Diagnostics link at the bottom of the model toolbar to see the output of the build process.

6. Once the code generation completes, the ROS node is transferred to the Catkin workspace on your ROS device. The node builds there and starts to run automatically.

The generated node connects to the ROS master running on the ROS device.

7. Use rosnode to list all running nodes is the ROS network. "robotcontroller" should be in the displayed list of nodes.

rosnode list

You can use rostopic to verify that the deployed node publishes data on the ROS topic to control the robot motion:

rostopic info /mobile_base/commands/velocity

Task 4 - Run and Verify the ROS Node

In this task, you run the newly-built ROS node and verify its behavior using a MATLAB-based robot simulator.

1. In MATLAB, type ExampleHelperSimulinkRobotROS to start the Robot Simulator. The simulator automatically connects to the ROS master running on the ROS device. If you want to connect to a Gazebo-based robot simulation, see Connect to a ROS-enabled Robot from Simulink.

sim = ExampleHelperSimulinkRobotROS

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第9张图片

2. Verify that the simulated robot moves toward the goal (the Desired Position constant specified in the model). The robot stops once it reaches the goal [-10, 10].

3. Click Reset Simulation to reset the robot's position to [0, 0]. The robot starts to move immediately towards the goal position.

4. In MATLAB, you can manage ROS nodes generated by Simulink with the rosdevice object. Once a Simulink model is deployed, you can use rosdevice to run and stop the node at any point, without having to rebuild it in Simulink.

The AvailableNodes property shows the deployed robotcontroller node. You can verify that the node is running by calling the isNodeRunning function.

d = rosdevice

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第10张图片

isNodeRunning(d, 'robotcontroller')

5. Stop the ROS node from running.

stopNode(d, 'robotcontroller')

isNodeRunning(d, 'robotcontroller')

6. Click the Reset Simulation button in the simulation window. The robot stays at location [0,0] and does not move.

  • Now restart the node.

runNode(d, 'robotcontroller')

  • The robot should start moving towards the goal position again.

7. Once you are done verifying, you can clean up the system state as follows.

  • Stop the node running on the target device

stopNode(d, 'robotcontroller')

  • On the host computer, close the Robot Simulator figure window and type rosshutdown at the MATLAB command line.

rosshutdown

Advanced Topics and Troubleshooting

Specify ROS network settings in Simulink: By default, Simulink uses the ROS connection settings from rosinit in MATLAB. To override these settings and to specify ROS connection settings in Simulink, select Tools > Robot Operating System > Configure Network Addresses, and set the ROS Master and Node Host network addresses:

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第11张图片

Generated C++ code archive: No matter what Build action you select (None, Build and load, Build and run), Simulink always generates two files in your current folder: an archive containing the C++ source code (RobotController.tgz in our example) and a shell script for extracting and building the C++ code manually (build_ros_model.sh). If your MATLAB computer is not connected to the ROS device, you can transfer the files manually and build them there.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第12张图片

Processor-specific generated code: If you use blocks from other products (such as Computer Vision System Toolbox™), the generated code may include processor-specific optimizations that lead to compilation problems when building the ROS node on Linux. In these cases, you need to let Simulink know the platform on which the generated code is compiled. You can do this through the Hardware Implementation pane of the Model Configuration Parameters dialog.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第13张图片

Install additional ROS packages: Robotics System Toolbox includes many ROS message types (e.g., for Baxter® or PR2®) that are not part of the standard ROS distribution. If your Simulink model includes non-standard or custom message types, you are able to simulate and generate code successfully. However, in order to build the generated ROS node, you need to first download and install all the required packages. Here is one way to do it (assume that the required package is baxter_core_msgs).

On the MATLAB prompt:

d = rosdevice;

openShell(d);

On the Linux shell:

# Refresh the list of available packages

sudo apt-get update

# Search the list for ROS Baxter packages (e.g., ros-indigo-baxter-core-msgs)

apt-cache search baxter

# Download and install the required package

sudo apt-get install ros-indigo-baxter-core-msgs

Note that this command may take several minutes to complete.

Running ROS Master in MATLAB: In the example above, you connected to a ROS master running on the ROS device. Alternatively, you can create a ROS master in MATLAB. Use rosinit at the MATLAB command line:

rosinit('NodeHost', )

For example, if the IP address of your host computer is 172.28.194.92, use the following command:

rosinit('NodeHost', '172.28.194.92')

The NodeHost setting is important to ensure that the generated ROS node is able to communicate to the master on MATLAB. Note: The generated ROS node will use the NodeHost IP address to communicate to the global ROS node in MATLAB, so ensure that the specified IP address is accessible from the ROS device (for example, using ping). See the Connect to a ROS Network example for more details on the significance of the NodeHost setting.

Tasking mode: Simulink can generate code for either multi-tasking or single-tasking modes (see Time-Based Scheduling and Code Generation). By default, generated ROS code uses single-tasking mode (a single thread for all the rates) without real-time scheduling. This allows the generated ROS code to execute without sudo privileges, but can lead to less predictable performance.

If you require more predictable performance, you can configure the model to use multi-tasking. In the Solver pane of the Configuration Parameters dialog enable Treat each discrete rate as a separate task to enable multi-tasking. In generated code, this creates a separate thread for each rate in the model and uses prioritized scheduling for the threads.

MATLAB 控制baxter机器人相关信息(个人记录--学习用)_第14张图片

To run the ROS node, you need to have administrative privileges on the ROS device. Simulink automatically detects if your privileges are insufficient when the model is deployed to the target device.

Summary

This example showed you how to configure a Simulink model to generate C++ code for a standalone ROS node. It also showed how to transfer, build, and verify the ROS node.

Copyright 2014-2017 The MathWorks, Inc.

3、ROS包上使用readMessages()会得到不完整的TF数据(版本原因2018a和之前版本。。。)

 https://ww2.mathworks.cn/matlabcentral/answers/394157-using-readmessages-on-ros-bag-gives-incomplete-tf-data

 

4、与机器人工具箱兼容的ROS版本(应该都支持,只要Message type一致

  来源:https://ww2.mathworks.cn/matlabcentral/answers/345066-what-are-the-ros-versions-compatible-with-the-robotics-toolbox

回答:In general, different ROS distributions (Groovy, Hydro, Indigo) can communicate with each other, as long as they agree on the definition of the message type that they are interchanging. definitions do not change very often and a set of definitions is included for ROS Hydro in Robotics System Toolbox.

If you need support for message types that are not included in Robotics System Toolbox(or that changed between ROS distributions), you can use the custom message support package present at the following URL to add the new message type to MATLAB and Simulink:

http://www.mathworks.com/matlabcentral/fileexchange/49810-robotics-system-toolbox-interface-for-ros-custom-messages

If you want to re-define shipping message types to a different version, then you can follow the instructions described in the following MATLAB Answers post:

https://www.mathworks.com/matlabcentral/answers/283695-updating-existing-custom-message-types-with-rosgenmsg

Through this mechanism, MATLAB should be able to communicate to any version of ROS.

In terms of Baxter support, there are several demos of using Baxter with the toolbox, for example at the MATLAB Expo in 2015: http://www.matlabexpo.com/uk/2015/demo-stations.html

5、如何在Simulink中为Baxter机器人使用反向运动学求解器(回答模糊)

  来源:https://ww2.mathworks.cn/matlabcentral/answers/325012-how-to-use-inverse-kinematics-solver-for-the-baxter-robot-in-simulink

        解答说有文档:http://docs.ros.org/hydro/api/baxter_core_msgs/html/index-msg.html

6、使用Joint Trajectory Action Server移动Baxter的关节节点运行时错误(未解答)

https://ww2.mathworks.cn/matlabcentral/answers/356393-moving-baxter-s-joints-using-joint-trajectory-action-server

7、simulink中baxter_core_msgs / JointCommand的问题(阵列大小设置)

来源:https://ww2.mathworks.cn/matlabcentral/answers/226385-problems-of-baxter_core_msgs-jointcommand-in-simulink

问题:

I tried to control baxter robot using simulink, but I find there may be some problem of creating topic. 1, there is no names(data type, string[]) for baxter_core_msgs/JointCommand; 2, the Msg.command input port requires 128X1 elements of array, while actually we only need 7X1 array to publish the topic e.g. '/robot/limb/left/joint_command'. what's the 128X1 elements of array composed of? it works in matlab manuscripts,as follows, pub=rospublisher('/robot/limb/left/joint_command','baxter_core_msgs/JointCommand'); Msg=rosmessage('baxter_core_msgs/JointCommand');

command=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];% here we can see it's 7X1, not 128X1 declared in simulink. names=[{'left_w0'}, {'left_w1'}, {'left_w2'}, {'left_e0'}, {'left_e1'}, {'left_s0'}, {'left_s1'}]; mode=1; Msg.Command=int64(command);

Msg.Mode=int32(1); Msg.Names=(names); send(pub,Msg);

Looking forward to your answers! Thanks,

回答:128X1 is the default size. You can change this by going to Tools>>Robot Operating System and set the value in Manage Size Arrays. Please see this doc link for more information: http://www.mathworks.com/help/robotics/ug/manage-array-sizes-in-simulink-ros.html

8、BaxterComm WithSim()使用类时发生错误。()

     https://ww2.mathworks.cn/matlabcentral/answers/412178-baxtercommwithsim-class

回答:下载Baxter Communicator Class

           地址:https://jp.mathworks.com/matlabcentral/fileexchange/63564-baxter-communicator-class

9、打开或关闭baxter抓手

https://ww2.mathworks.cn/matlabcentral/answers/330259-open-and-close-baxter-s-gripper

代码但是错误:

rosinit('169.254.9.127') % ip address of baxter
rostopic info /robot/end_effector/right_gripper/command
pub = rospublisher('/robot/end_effector/right_gripper/command')
msg = rosmessage('baxter_core_msgs/EndEffectorCommand')
showdetails(msg)
msg.Id=65538
msg.Command= 'grip'
msg.Sender= 'foo'
msg.Sequence= 1
send(pub,msg)% nothing happens
msg.Command= 'release'
send(pub,msg) %nothing happens again
rosshutdown

回答:

I had the same problem. After some searching I managed to figure it out. The way to do this is through the script gripper_action_server.py

Use rosrun baxter_interface gripper_action_server.py

This will start an node for baxter gripper control

then in Matlab use rosaction list. You should now see 2 actions, one for the left gripper and one for the right.
然后在MATLAB中:

[actClient,goalMsg] = rosactionclient('/robot/end_effector/right_gripper/gripper_action');

waitForServer(actClient

goalMsg = 100       % fully open打开

sendGoalAndWait(actClient,goalMsg,10)

goalMsg = 0         % fully closed关闭

sendGoalAndWait(actClient,goalMsg,10)

 

你可能感兴趣的:(MATLAB与ROS中的机器人)