Master Thesis (2007)
Title: Humanoid robot: Development of a simulation environment of an entertainment humanoid robot

Abstract: This dissertation was developed in collaboration with Robosavvy Ltd and boosted the creation of the Humanoid Robotics Laboratory of IDMEC-Center of Intelligent Systems, at Instituto Superior Técnico. The developments presented include: i) the software development for interfacing the Matlab Real TimeWorkshop Toolbox with the Bioloid humanoid robot servos; ii) the identification of the internal and external dynamic parameter of the humanoid servos and structure, respectively; iii) the dynamics modeling and simulation of the humanoid robot using the SimMechanics and Virtual Reality Toolbox; iv) the deduction of the equations of motion for an underactuated n-link inverted pendulum. The main objective of the Humanoid Robotics Laboratory, for the time being, is to develop a humanoid robot able to make complex motions like walking, running and jumping through real-time feedback control techniques. This dissertation presents a LQR controller for the simulation and control of the humanoid robot doing the handstand on a high bar, by considering it as an underactuated 3-link inverted pendulum.


Application Example
  • Overview
  • Real-Time Windows Target set up
  • MCU code
  • C-MEX S-function
  • Simulink and Results

This is a tutorial showing how to build up this protocol with a simple application example consisting in sending a desired position to a servo (Matlab/Simulink-CM5-servo) and reading back at a sample time of 0.01 seconds the current position of that servo (servo-CM5-Matlab/Simulink).


In order to guarantee data samples at precise time inputs sent to the servos, a Simulink/RTWT implementation was used. The Real-Time Windows Target is a real-time kernel which permits C code, generated and compiled by the Real-Time Workshop from Simulink block diagram models, to run in real time, at ring zero, under the Windows
operative system.

Hereafter, it was necessary to establish a protocol for the serial communication between PC and the CM5. For doing this, we created an C-MEX S-function written in C to communicate with the CM5 throughout UART (universal asynchronous receiver / transmitter) [1], [2].

Finally, we wrote a little C program to Atmega128 [1], [2] for completing the serial communication bridge.


  1. RT Windows Target Section describes the steps to install Real-Time Windows Target in the PC.
  2. The C code required for the MCU Atmega128 will be presented in MCU code section along with its uploading to the MCU of CM5.
  3. Sfunction section presents the steps for creating an C-MEX S-function for our protocol.
  4. In Simulink section it is presented how to build a simulink model with C-MEX S-function embeded and the results of the running application

To ensure that an application runs in real time, Real-Time Windows Target uses a real time kernel. The real-time kernel use the internal clock of the CPU as its source of time (ring zero of CPU). Therefore installing the Real-Time Windows Target kernel is necessary.

Bellow is the MATLAB tutorial of how to install kernel.
After you install the Real-Time Windows Target, you can install the kernel. You need to install the kernel before you can run a Real-Time Windows Target executable:

1.In the MATLAB window, type

rtwintgt -install

MATLAB displays the message:
You are going to install the Reartwho
l-Time Windows Target kernel.
Do you want to proceed? [y] :

2.Continue installing the kernel. Type


MATLAB installs the kernel and displays the message
The Real-Time Windows Target kernel has been successfully installed.
If a message is displayed asking you to restart your computer, you need to restart your computer before the kernel runs correctly.
3.Check that the kernel was correctly installed. Type


MATLAB should display a message showing the kernel version number, followed by performance, timeslice, and other information.

Once the kernel is installed, you can leave it installed. After you have installed the kernel, it remains idle, which allows Windows to control the execution of any standard Windows application. Standard Windows applications include internet browsers, word processors, MATLAB, and so on.

It is only during real-time execution of your model that the kernel intervenes to ensure that your model is given priority to use the CPU to execute each model update at the prescribed sample intervals. Once the model update at a particular sample interval completes, the kernel releases the CPU to run any other Windows application that might need servicing.


To run Real-Time Windows Target it is necessary to have a PC with the following requirements:




Pentium or higher in a desktop, laptop, or compact PCI or PC104 industrial computer


Hard disk drive with 16 megabytes of free space
Data acquisition board For a list of supported boards, see Supported I/O Boards.
CD-ROM drive


128 megabytes minimum, 256 megabytes recommended





Microsoft Windows 2000 / XP


MATLAB provides the design and analysis tools that you use when creating Simulink block diagrams. After installing MATLAB, the following products should be installed by default.


Simulink provides an environment where you model your physical system and controller as a block diagram [9].

Real-Time Workshop

Real-Time Workshop provides the utilities to convert your Simulink models into C code, and then, with the Open Watcom C/C++ compiler, compile the code into a real-time executable [3].
The Real-Time Windows Target is designed for maximum flexibility during rapid prototyping. This flexibility allows parameter tuning and signal tracing during a real-time run, but increases the size of the generated code. However, Real-Time Workshop has other code formats that generate the more compact code needed for embedded applications.

Real-Time Windows Target

The Real-Time Windows Target is a PC solution for prototyping and testing real-time systems [2].

In order to program the ATmege128, the microcontroller (MCU) of CM5 board, it was used the Programmers notepad 2 of WinAVR (

1. Copy the example.c, given in bioloid software cd, into Programmers notepad 2 and insert the following piece of code right after the instruction sei(); inside the main.

Save the file as CM5_code.c inside the folder CM5

2. Copy the makefile and open it in the programmers notepad. In Target field write CM5_code and in SRC field put CM5_code.c

Safe as makefile inside the folder CM5

3. Go to Tools => [WinAVR] Make All
The foloowing files are generated.

4. Open the Robot Terminal
After verifying that the serial cable is well plugged to the PC and CM5, turn the CM5 on.
In Setup => Connect verify if the CM5 is connected at 57600bps and the used port is the COM1.
Now enter inside boot loader by pressing at the same time the red button of the CM5 and #. The following screen must appear.

Write now the command Load
Press enter
Then, go to Files => Transmit file and open the CM5_code.hex.
Finally go to Files => Disconnect.

The C-MEX S-Function code used for establishing the protocol communication between the PC
using Matlab/Simulink and the humanoid robot is described here. The code is
splited into 5 simple parts for full understanding.

Copy the S-Function code to a new file in Matlab and save it as PC_code.c
After doing this, type in the comand line of Matlab: mex PC_code.c
The next file is created: PC_code.mexw32 (note)

The first part of the code is for the declarations, just as setting the COM port.

The second part of the program initializes the Input and Output signal of the S-function and
RS232 protocol communication at the baudrate.

In the third part, the read function from CM5 is presented. Two bytes, corresponding
to the actual position of the servo, are read. A system of flags were used in order to
prevent possible receiving errors.

The fourth part is the principal block. It receives the desired final position for the
servo from Simulink and sends it as two bytes for the AX-12 throughout CM5. After sending the
bytes, it receives the actual position of the servos, sending to the CM5 first a confirmation that it
is ready to start reading the values.

The last part stands for the conclusion of the S-Function.


If it is the first time you're using the command mex, the following message will probably appear.

Select a compiler:
[1] Lcc C version 2.4 in C:\PROGRAMAS\MATLAB7\sys\lcc
[2] Microsoft Visual C/C++ version 7.1 in C:\Programas\Microsoft Visual Studio .NET 2003
[3] Microsoft Visual C/C++ version 6.0 in C:\Programas\Microsoft Visual Studio
[4] WATCOM C/C++ version 10.6 in C:\watcom-1.3

[0] None

Compiler: 4

Try to update options file: C:\Documents and Settings\Humanoid\Application Data\MathWorks\MATLAB\R14\mexopts.bat
From template: C:\PROGRAMAS\MATLAB7\BIN\WIN32\mexopts\watcopts.bat

Done . . .

Open Watcom C32 Optimizing Compiler Version 1.3
Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See for details.
Error! E1073: Invalid option '-7'
Error: Compiler returned a bad status compiling 'PC_code.c'
C:\PROGRAMAS\MATLAB7\BIN\WIN32\MEX.PL: Error: Compile of 'PC_code.c' failed.

We noticed a error of compiling. But if the code is right, maybe the problem is the chosen compiler. Therefore it is time to chose another one. To do so, type:

mex -setup

Please choose your compiler for building external interface (MEX) files:

Would you like mex to locate installed compilers [y]/n? y

Select a compiler:
[1] Lcc C version 2.4 in C:\PROGRAMAS\MATLAB7\sys\lcc
[2] Microsoft Visual C/C++ version 7.1 in C:\Programas\Microsoft Visual Studio .NET 2003
[3] Microsoft Visual C/C++ version 6.0 in C:\Programas\Microsoft Visual Studio
[4] WATCOM C/C++ version 10.6 in C:\watcom-1.3

[0] None

Compiler: 2

Please verify your choices:

Compiler: Microsoft Visual C/C++ 7.1
Location: C:\Programas\Microsoft Visual Studio .NET 2003

Are these correct?([y]/n): y

Try to update options file: C:\Documents and Settings\Humanoid\Application Data\MathWorks\MATLAB\R14\mexopts.bat
From template: C:\PROGRAMAS\MATLAB7\BIN\WIN32\mexopts\msvc71opts.bat

Done . . .

Type again now:

mex PC_code.c

If nothing appears, it means everything went alright.

1. Open Matlab and then simulink.
2. Build the following diagram blocks inside the root MATLAB.

3. Double click in Step block and then change its parameters as shown bellow.

4. In Simulation > configuration Parameters >> Solver, chose fixed-step and then 0.01 seconds as sample time

5. In Simulation select external mode. This will allow to run simulink in real time.
6. Save the diagram block as diagram.mdl in the same directory as PC_code.c

7. Double click in Sfuntion block to open Function Block Parameters: S-Function.
8. In S-function name put PC_code and press Apply and the Ok.

9. Save the model
10. Press Incremental build button to build the model ad wait until the play button becomes enable (>).

You should have now the following files and folders..

11. Before testing the application, verify if the CM5 is plugged to the PC and if it is OFF.
12. Attach the servo number one to the CM5 (you can chose other but you have to change it in the MCU code).
13. Finally, turn on the CM5 and then press the play button. If everything went alright you must see something similar to the next graph. The initial position of the servo was in 800 encoders (violet line) and it can be seen the response of the servo to a step (yellow line).


Before pressing the play button in order to run any application in real time, you should verify always the next conditions.

  1. The COM is disconnected after uploading any code to the MCU (Robot Terminal => Files => Disconnect).
  2. The CM5 should be turned OFF and then ON.



©2008-2011 Humanoid Robot Lab