As you may know, Space Shuttle Atlantis is about to return this weekend from man’s last visit to the Hubble Telescope. Those astronauts took some major risks to repair our favorite and most famous scientific instrument. One of the tools they used during space walks is the robotic manipulator from the Canadian Space Agency (the robotic arm).
Image Credit: NASA
I have never had a chance to operate a CSA robotic arm, but my colleague Guy Rouleau has! Guy told me about a Simulink S-function he created to help in controlling a robotic arm in Simulink.Changing the color of blocks during simulation
By Guy Rouleau
I have many good memories from my internship at the Canadian Space Agency when I was studying for my bachelor degree. During this internship, I was developing control logic for a robotic arm. Simulink was used to develop the controllers and as a user interface to command the robot.
When driving a robotic arm simulation using a joystick, it is not always easy to visualize if you are close to a joint limit or a singularity. It is possible to display the state values in the model, but hard to know if you are about to lock out control of the arm. To help the person driving the robot, we though it might be cool to make blocks in the Simulink diagram turn red when close to a limit. This is what it looks like in action with the SimMechanics mech_robot_vr.mdl model.
Here is a quick explanation of how to changing the color of a block with a simple Level 2 M-file S-Function.
As a template, I suggest starting with the demo M-file msfcn_times_two.m. In your MATLAB installation, you can open this file using the following command:
>> edit msfcn_times_two.m
Only a few modifications are required to make the file change the color of a block based on the value of the signal connected to the s-function.
To begin, save the file as ChangeColor.m for this example. At the minimum, you need to modify the code of the Output section. To start, I created a model named “Test” where we will change the color of the block named “Display”
The simplest implementation can look at the input value, and then use set_param to modify the block BackgroundColor:
In the above code, the color of the block “Test/Display” is set to green or red depending if the input is larger or smaller than zero.
In order to make the code reusable, I may want to determine automatically which block is connected to the S-Function. We can also add a parameter to the block to set the value at which the color switches. In that case, we add these lines to the setup function:
Notice that the handle to the destination block is stored in the “UserData” of the S-function. UserData is often useful for storing any data you need to persist with the S-function, but doesn’t fit the definition of a continuous or discrete state. The output function now looks like:
Finally, if you want the block to return to white at the end of the simulation, you can add these lines to the terminate function:
You can find this example in my File Exchange Submission: Changing the color of a block while the simulation runs.
Now it is your turn
Do you work on a robotic arm or on the Hubble? Have you used an S-function to customize what Simulink looks like? Share your experience and leave a comment here.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.