TRUETIME 2.0 beta—Reference
Manual
Anton Cervin
Dan Henriksson
Martin Ohlin
Department of Automatic Control
Lund University
June 2010
Contents
1.
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2. Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1 Software Requirements . . . . . . . . . . . . . . . . . . . . . .
2.2
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3. Using the Simulator . . . . . . . . . . . . . . . . . . . . . . . . . .
4. Writing Code Functions . . . . . . . . . . . . . . . . . . . . . . .
7
7
7
7
8
9
9
4.1 Writing a Matlab Code Function . . . . . . . . . . . . . . . . .
9
4.2 Writing a C++ Code Function . . . . . . . . . . . . . . . . . . 10
4.3 Calling Simulink Block Diagrams . . . . . . . . . . . . . . . . 10
5.
Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.1 Writing a Matlab Initialization Script . . . . . . . . . . . . . . 12
5.2 Writing a C++ Initialization Script . . . . . . . . . . . . . . . 13
6. Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
7. The TrueTime Kernel . . . . . . . . . . . . . . . . . . . . . . . . . 15
7.1 Dynamic Voltage Scaling . . . . . . . . . . . . . . . . . . . . . 15
8. The TrueTime Network . . . . . . . . . . . . . . . . . . . . . . . . 16
8.1 CSMA/CD (Ethernet) . . . . . . . . . . . . . . . . . . . . . . . 17
8.2 CSMA/AMP (CAN)
. . . . . . . . . . . . . . . . . . . . . . . . 17
8.3 Round Robin (Token Bus) . . . . . . . . . . . . . . . . . . . . . 17
8.4 FDMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
8.5 TDMA (TTP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
8.6 Switched Ethernet . . . . . . . . . . . . . . . . . . . . . . . . . 18
8.7 FlexRay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
8.8 PROFINET IO . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
9.1
9. The TrueTime Wireless Network . . . . . . . . . . . . . . . . . . 21
802.11b/g (WLAN) . . . . . . . . . . . . . . . . . . . . . . . . . 22
802.15.4 (ZigBee) . . . . . . . . . . . . . . . . . . . . . . . . . . 23
9.3 Calculation of Error Probabilities . . . . . . . . . . . . . . . . 24
9.2
9.4 User-Defined Path-Loss Function . . . . . . . . . . . . . . . . 25
10. The TrueTime Battery . . . . . . . . . . . . . . . . . . . . . . . . 27
11. The TrueTime Standalone Network Blocks . . . . . . . . . . . 27
3
12. Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
12.1 PID-control of a DC-servo . . . . . . . . . . . . . . . . . . . . . 28
12.2 Task Scheduling and Control . . . . . . . . . . . . . . . . . . . 31
12.3 Networked Control System . . . . . . . . . . . . . . . . . . . . 32
12.4 Wireless Control System with Automatic Gain Control . . . . 33
12.5 Wireless Ad-hoc Routing Using AODV . . . . . . . . . . . . . 34
12.6 Mote Soccer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
13. Kernel Implementation Details . . . . . . . . . . . . . . . . . . . 36
13.1 Kernel Data Structures . . . . . . . . . . . . . . . . . . . . . . 37
13.2 Task Model
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
13.3 The Kernel Function . . . . . . . . . . . . . . . . . . . . . . . . 40
13.4 Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
14. TrueTime Command Reference . . . . . . . . . . . . . . . . . . 42
ttAbortSimulation (TH) . . . . . . . . . . . . . . . . . . . . . . . . 47
ttAnalogIn (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
ttAnalogOut (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
ttAttachCBS (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
ttAttachDLHandler (I) . . . . . . . . . . . . . . . . . . . . . . . . 51
ttAttachHook (C++ only) (I) . . . . . . . . . . . . . . . . . . . . 52
ttAttachNetworkHandler (I) . . . . . . . . . . . . . . . . . . . . . 53
ttAttachTriggerHandler (I) . . . . . . . . . . . . . . . . . . . . . 54
ttAttachWCETHandler (I) . . . . . . . . . . . . . . . . . . . . . . 55
ttCallBlockSystem (TH)
. . . . . . . . . . . . . . . . . . . . . . . 56
ttCreateCBS (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
ttCreateEvent (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
ttCreateHandler (I) . . . . . . . . . . . . . . . . . . . . . . . . . . 59
ttCreateJob (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
ttCreateLog (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
ttCreateMailbox (I)
. . . . . . . . . . . . . . . . . . . . . . . . . . 63
ttCreateMonitor (I) . . . . . . . . . . . . . . . . . . . . . . . . . . 64
ttCreatePeriodicTask (I) . . . . . . . . . . . . . . . . . . . . . . . 65
ttCreatePeriodicTimer (ITH) . . . . . . . . . . . . . . . . . . . . 66
ttCreateSemaphore (I) . . . . . . . . . . . . . . . . . . . . . . . . 67
4
ttCreateTask (I)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
ttCreateTimer (ITH) . . . . . . . . . . . . . . . . . . . . . . . . . . 69
ttCurrentTime (ITH)
. . . . . . . . . . . . . . . . . . . . . . . . . 70
ttDiscardUnsentMessages (IT) . . . . . . . . . . . . . . . . . . . 71
ttEnterMonitor (T) . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
ttExitMonitor (T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
ttFetch (T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
ttGetData (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
ttGetMsg (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
ttGetInitArg (C++ only) (I) . . . . . . . . . . . . . . . . . . . . . 77
ttGetInvoker (H) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
ttGetX (ITH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
ttGive (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
ttInitKernel (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
ttKillJob (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
ttLogNow (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
ttLogStart (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
ttLogStop (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
ttLogValue (TH)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
ttNonPreemptible (I) . . . . . . . . . . . . . . . . . . . . . . . . . 88
ttNoSchedule (I) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
ttNotify (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
ttNotifyAll (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
ttPost (T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
ttRemoveTimer (TH)
. . . . . . . . . . . . . . . . . . . . . . . . . 93
ttRetrieve (T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
ttSendMsg (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
ttSetCBSParameters (ITH)
. . . . . . . . . . . . . . . . . . . . . 96
ttSetData (TH)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
ttSetKernelParameter (ITH)
. . . . . . . . . . . . . . . . . . . . 98
ttSetNetworkParameter (ITH) . . . . . . . . . . . . . . . . . . . 99
ttSetNextSegment (TH) . . . . . . . . . . . . . . . . . . . . . . . . 100
ttSetX (ITH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
5
ttSleep (T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
ttSleepUntil (T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
ttTake (T)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
ttTryFetch (TH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
ttTryPost (TH)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
ttWait (T) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
15. References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
6
1. Introduction
This manual describes the use of the Matlab/Simulink-based [The Mathworks,
2001] simulator TRUETIME, which facilitates co-simulation of controller task ex-
ecution in real-time kernels, network transmissions, and continuous plant dy-
namics. The simulator is presented in [Henriksson et al., 2003; Cervin et al.,
2003; Henriksson etal., 2002; Andersson etal., 2005], but be aware that several
differences from these papers exist.
The manual describes the fundamental steps in the creation of a TRUETIME sim-
ulation. This include how to write the code that is executed during simulation,
how to configure the kernel and network blocks, and what compilation that must
be performed to get an executable simulation. The code functions for the tasks
and the initialization commands may be written either as C++ functions or as
Matlab M-files, and both cases are described.
Several tutorial examples are provided, treating standard and networked PID-
control, scheduling, overrun handling, synchronization, control over wireless net-
works, mote coordination, wireless ad-hoc routing using AODV, mobile robot soc-
cer, and more.
The manual also describes some of the internal workings of TRUETIME, including
the task model, implementation details, and timing details. The network blocks
and the radio model used for the wireless simulations are also presented in
some detail. A TRUETIME command reference with detailed explanations of all
functionality provided by the simulator is given at the end of the manual.
For questions and bug reports, please direct these issues to
truetime@control.lth.se
2. Getting Started
2.1 Software Requirements
TRUETIME requires Matlab 6.1 (R12.1) or later with Simulink 4.1 or later.
A C++ compiler is required to run TRUETIME in the C++ version. For the Matlab
version, pre-compiled files are provided in the archive that is downloaded from
the TRUETIME web site. The following compilers are known to work:
• Visual Studio C++ for Windows
• gcc, g++ for Linux
2.2 Installation
Download and extract the compressed archive (truetime-2.0.zip), available at
the TRUETIME home page. Extracting the file creates the directory truetime-2.0,
which will be referred to as $DIR in the sequel.
In order to run TRUETIME, the environment variable TTKERNEL must be defined to
point the directory with the TRUETIME kernel files, $DIR/kernel. In later versions
of Matlab, this can be done using e.g. the command
7
Figure 1 The TRUETIME 2.0 beta block library.
setenv(’TTKERNEL’, ’C:\MyFiles\truetime-2.0\kernel’)
In older versions of Matlab, the setenv command is not available, so the envi-
ronment variable must be defined in the operating system:
• Unix/Linux: export TTKERNEL=$DIR/kernel
• Windows: use Control Panel / System / Advanced / Environment Variables
Then add the following lines to your Matlab startup script. This will set up all
necessary paths to the TRUETIME kernel files.
addpath([getenv(’TTKERNEL’)])
addpath([getenv(’TTKERNEL’) ’/matlab/help’])
addpath([getenv(’TTKERNEL’) ’/matlab’])
Starting Matlab and issuing the command
>> truetime
from the Matlab prompt will now open the TRUETIME block library, see Figure 1.
2.3 Compilation
Since the TRUETIME archive contains pre-compiled files, no compilation is required
to run TRUETIME with the M-file API.
However, TRUETIME also supports simulations written in C++ code, which then
must be compiled. In this case, you first need to configure your C++ compiler in
Matlab. This can be done by issuing the command
>> mex -setup
8