Introduction à la programmation temps-réel pour la...

337
Ludovic Saint-Bauzel 01/10/2012 Introduction à la programmation temps-réel pour la robotique

Transcript of Introduction à la programmation temps-réel pour la...

  • Ludovic Saint-Bauzel01/10/2012

    Introduction à la programmation temps-réel pour la robotique

  • Robotics Real-Time Programming

    Ludovic Saint-Bauzel

    Polytech’Paris UPMC

    2012-2013

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 1 / 334

  • Robotics Real-Time Programming

    Ludovic Saint-Bauzel

    Polytech’Paris UPMC

    2012-2013

  • Introduction

    Robotics Software Frameworks

    OS : Tasks And Threads

    OS Driver Programming

    Application

    Documentations

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 3 / 334

  • Introduction What is a robot ?

    Layout

    IntroductionWhat is a robot ?

    DefinitionHardwareSoftware

    First Idea : From ScratchSecond Idea : Operating System Approach

    Robotics Software Frameworks

    OS : Tasks And Threads

    OS Driver Programming

    Application

    DocumentationsLudovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 4 / 334

  • Introduction What is a robot ?

    Robot

    I Definition : An Autonomous Robot is acomplex system that has the ability todecide its actions on its environmentfrom its sensing, state in order to fill hisaims. Survive, Assist ...

    I SensorsI DecisionI Action

    I Physical ability : read information fromsensor, send orders to actuators, have aregulation (Time based computation)and a decision computation unit(policy).

    Effectors

    Control

    Sensors

    Robot Environment

    Supervision / Decision

    Effectors

    Decision

    Sensors

    Autonomous Robot Environment

    Figure: Classic (up) andAutonomous (down) Robot Models

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 5 / 334

  • Introduction What is a robot ?

    Robot : Computer science point of view

    A computer science way to see a robot is to say it is a computer withperipherals that bring sensing, actuation ability and because of real lifeinteraction need to put the stress on the time. So like a computer we willhave to deal with :

    I busI memoryI cpu

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 6 / 334

  • Introduction What is a robot ?

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 7 / 334

  • Introduction What is a robot ?

    Structure Choice criteria

    I Critical path?I the main loopI delayed sensors

    I MethodI TasksI Pipes (read only, read write, write)

    I ParametersI ComputationI Time constraintsI Memory needsI Precision of measure

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 8 / 334

  • Introduction What is a robot ?

    Monocomputer

    I PropertiesI Limited Computation AbilityI Delays are limited tooI Simple but efficient

    I Recent ImprovementsI GPUI Multicore CPU

    I ExampleFigure: mc2e

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 9 / 334

  • Introduction What is a robot ?

    Monocomputer

    I PropertiesI Limited Computation AbilityI Delays are limited tooI Simple but efficient

    I Recent ImprovementsI GPUI Multicore CPU

    I Example

    Effectors

    Control

    Sensors

    Robot

    Supervision / Decision

    -Acquisition Card (NI-PCI6034E)

    analog inputs (sensors),

    analog outputs (effectors)

    Figure: mc2e Architecture

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 10 / 334

  • Introduction What is a robot ?

    Multi computer

    I PropertiesI High scalability of

    computationI High time lost : Bus

    communicationI Recent Improvement

    I Distributed algorithmsdeveloppment

    I Example Staubli

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 11 / 334

  • Introduction What is a robot ?

    Multi computer

    I PropertiesI High scalability of

    computationI High time lost : Bus

    communicationI Recent Improvement

    I Distributed algorithmsdeveloppment

    I Example

    Effectors

    Control

    Sensors

    Robot

    Supervision / Decision

    Robot Models

    - Kinematics

    - Velocities

    Trajectory Control

    GUI To choose

    trajectories

    UDP

    Staubli

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 12 / 334

  • Introduction What is a robot ?

    Master Computer - Slave Computing Units

    I Master is connected to slave unitI Often a busI Image

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 13 / 334

  • Introduction What is a robot ?

    Bus

    I DefinitionI Physical connectionI Multiple devices

    I Common elements :I Base Address called portsI Local Address

    I Examples :I Serial

    I RS485I USBI CAN

    I ParallelI ISAI PCII ...

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 14 / 334

  • Introduction What is a robot ?

    How the robot can make environment interaction ?

    I CategoriesI PerceptionI DecisionI Actions

    I Computer Science Name : tasks

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 15 / 334

  • Introduction What is a robot ?

    Real-Time Programming

    I Low Level AccessI High Time constraints

    I Don’t mean quick!I Mean ability to respect those constraints

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 16 / 334

  • Introduction What is a robot ?

    Low Level Programming

    LINUX

    #include ...unsigned char inb (unsigned short port) ;void outb (unsigned char byte , unsigned short port) ;unsigned char insb (unsigned short port , void *addr ,

    unsigned long count) ;void outsb (unsigned short port , void *addr ,

    unsigned long count) ;

    DOS

    #include ...unsigned char inputb (unsigned short port) ;void outputb (unsigned short port ,unsigned char byte) ;

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 17 / 334

  • Introduction First Idea : From Scratch

    Layout

    IntroductionWhat is a robot ?First Idea : From Scratch

    Examplex86 exampleFrom scratch approach

    Second Idea : Operating System Approach

    Robotics Software Frameworks

    OS : Tasks And Threads

    OS Driver Programming

    Application

    DocumentationsLudovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 18 / 334

  • Introduction First Idea : From Scratch

    RobModex

    I ISA CardI 0x100 : Base AddressI 0x01 - 0x04 : Timer (IRQ 5)I 0x05 - 0x08 : DAC1I 0x09 - 0x12 : DAC2I 0x13 - 0x14 : 2 Wheel Encoder Registers

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 19 / 334

  • Introduction First Idea : From Scratch

    Closed Loop PID

    PID Controller Robot

    qdes

    qmes

    error

    variables qmes , qdeserror(k) =error_cumul(k) =error_diff(k)=

    qcons=olderror = error

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 20 / 334

  • Introduction First Idea : From Scratch

    Simple IRQ x86

    #0

    IRQ IRR

    #1

    #2

    #3

    IMR ISR

    0

    0

    0

    0

    0

    1

    0

    0

    0

    0

    0

    0

    IVT

    Addr Interrupt Routine

    PIC Registers Memory (0x0-0x3FF)

    ...

    ...

    ...

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 21 / 334

  • Introduction First Idea : From Scratch

    Simple IRQ x86

    #0

    IRQ IRR

    #1

    #2

    #3

    IMR ISR

    1

    0

    0

    0

    0

    1

    0

    0

    0

    0

    0

    0

    IVT

    Addr Interrupt Routine

    PIC Registers Memory (0x0-0x3FF)

    ...

    ...

    ...

    ?

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 21 / 334

  • Introduction First Idea : From Scratch

    Simple IRQ x86

    #0

    IRQ IRR

    #1

    #2

    #3

    IMR ISR

    1

    0

    0

    0

    0

    1

    0

    0

    1

    0

    0

    0

    IVT

    Addr Interrupt Routine

    PIC Registers Memory (0x0-0x3FF)

    ...

    ...

    ...

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 21 / 334

  • Introduction First Idea : From Scratch

    Simple IRQ x86

    #0

    IRQ IRR

    #1

    #2

    #3

    IMR ISR

    0

    0

    0

    0

    0

    1

    0

    0

    0

    0

    0

    0

    IVT

    Addr Interrupt Routine

    PIC Registers Memory (0x0-0x3FF)

    ...

    ...

    ...

    I 0x20I Command RegisterI Status Register

    I 0x21I Int Mask Register

    I Internal RegistersI Int. Req. Reg.I In Service Register

    I System MemoryI Interrupt Vector Table (4 bytes)I Byte 0 : Offset Low Address of the Interrupt Routine (Handler)I Byte 1 : Offset High Address of the IRI Byte 2 : Segment Low Address of the IRI Byte 3 : Segment High Address of the IR Offset

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 22 / 334

  • Introduction First Idea : From Scratch

    Piece of code :

    turboC DOS example

    #define IMR 0x21void _install_int_function(int IRQn ,

    void interrupt (* _new_int_function )()){int inter = IRQn + 8;_disable (); // disable interrupts//save the old interrupt vector_old_int_function=_dos_getvect(inter);// install the new interrupt vector_dos_setvect(inter ,_new_int_function );//save the state of the 8259A IMR register_old_mask=inportb(IMR);//Set new value for IMR registeroutportb(IMR ,_old_mask&_interrupt_mask(IRQn ));_enable (); // enable interrupts

    }

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 23 / 334

  • Introduction First Idea : From Scratch

    I/O APIC : Advanced Programmable Interrupt Controller

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 24 / 334

  • Introduction First Idea : From Scratch

    IRQ

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 25 / 334

  • Introduction First Idea : From Scratch

    Bus : ISA

    I Ax : AddressesI Dx : DataI IRQs : 3,4,5,6,7I AEN : DMA

    Figure: ISA Port

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 26 / 334

  • Introduction First Idea : From Scratch

    Bus : ISA

    #define ISAADDRshort data;void interrupt _new_int_function (){

    _disable (); // disable interruptsdata=inb(ISAADDR ); // getDataFromISA//that the ISA Component has requested//to tell the system the interrupt service function has finished_end_interrupt ();_enable (); // enable interrupts again}

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 27 / 334

  • Introduction First Idea : From Scratch

    Digital/Analog Converter

    Figure: Digital Analog Converter Schematic

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 28 / 334

  • Introduction First Idea : From Scratch

    RobModex Application

    #define ISAADDR .....short data;

    void interrupt _new_int_function (){_disable (); // disable interruptsdata=inb( );

    outb( , );_end_interrupt ();_enable (); // enable interrupts again}

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 29 / 334

  • Introduction First Idea : From Scratch

    Conclusion

    I More Complex = More codeI No- reusability of pieces of code

    Figure: RUPI

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 30 / 334

  • Introduction Second Idea : Operating System Approach

    Layout

    IntroductionWhat is a robot ?First Idea : From ScratchSecond Idea : Operating System Approach

    IntroductionPropertiesSimpleMultitasksReal-Time

    Robotics Software Frameworks

    OS : Tasks And Threads

    OS Driver Programming

    Application

    Documentations

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 31 / 334

  • Introduction Second Idea : Operating System Approach

    Operating System few words

    A software :or precisely the first software executed by your computer Consequences :Manager

    I Schedule task orderI Manage resources in order to fulfil every needsI Use of all the resources.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 32 / 334

  • Introduction Second Idea : Operating System Approach

    Resources

    Computer Science :I Processor : CPU, IRQI Communication : BUSI MemoryI ...

    Robotics :I MotorI Sensors

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 33 / 334

  • Introduction Second Idea : Operating System Approach

    What is RealTime

    Two examples :

    A navigation system computing the best path for a boat.

    A navigation system computing a boat position during its sailing.

    I First case: Computing time is not a constraint, the result is this onlything that is interesting

    I Second case, if computing time is too long, position is false.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 34 / 334

  • Introduction Second Idea : Operating System Approach

    An informal definition

    Real-Time system : A system where the behaviour relies on precision ofcomputation and also the time when it is produced.

    In other words, a delay is considered as an error that can lead to severeconsequences

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 35 / 334

  • Introduction Second Idea : Operating System Approach

    A Robotic definition

    A Real-Time System: a system that bring possibility to control a robotin a finite time and let us manage urgent informations in a high level ofpriority. That means a real time system for control and management of

    events with priority possibilities.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 36 / 334

  • Introduction Second Idea : Operating System Approach

    Deadline and realtime system

    Different kind of deadlineI Hard Deadline : a delay generate an exception (error event and

    computation)I Soft Deadline : a delay don’t lead to an exception

    Different possible real-time systemsI Hard Real-Time systems : exceeded deadlines must not arise.

    (VxWorks, RT-Linux, RTAI, Xenomai...)I Soft Real-Time : exceeded deadlines can sometimes happen. (DOS,

    Microsoft Robotic Studio?)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 37 / 334

  • Introduction Second Idea : Operating System Approach

    Environment and RealTime System

    System : set of activities linked to one or more computing tasks that aredone sequentially or concurrently and that can communicate each other.

    A Real-Time System interacts in its environmentI Sensors : signals and their measurement toolsI Computing UnitI Actuators : act on the environment at precise times

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 38 / 334

  • Introduction Second Idea : Operating System Approach

    Approaches

    Centralised

    I A CPU work on information coming from sensors and send the resultto actuators

    Multiple-systems

    I Controlled systems and a controlling system interacting with sensorsand actuators

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 39 / 334

  • Introduction Second Idea : Operating System Approach

    Time scheduling

    I time-driven system : measure of time leads to actionsI event-driven system : arising of an event leads to actionsI reactive system : constrained time to process

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 40 / 334

  • Introduction Second Idea : Operating System Approach

    Predictability determinism reliability

    Real time system : in worst case.

    Predictability : Ability to identify in advance if a system is able to respectits time constraints Knowledge of parameters related to computation.

    Determinism : remove any uncertainty in individual task behaviour andwhen they are together

    I Variation of task execution timeI IO duration, reaction timeI Interruption reaction

    Reliability : Behaviour and Fault tolerance

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 41 / 334

  • Introduction Second Idea : Operating System Approach

    Example GSM Mobile

    System must combine :I Hardware layer : emission, reception, voice coder activation, reception

    strength measure, etcI Software layer : DB communication for localisation update, look up

    calling messages, etc...Real-Time constraints : 577 µs of talking emitted and reception every 4,6ms

    Mobility constraints : emits with a earlier signal if distance increase andlater if it decreases.System must be enough reactive in order to the system comfortable for theuser.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 42 / 334

  • Introduction Second Idea : Operating System Approach

    Classical system limits

    Classical systems are based on multiple task not well adapted to real timeconstraints :

    I scheduling policy that aims to share time in a balance way for eachtask

    I access mechanisms that shares resources and synchronizes timeuncertainty

    I interruption management is not optimalI Virtual memory and cache memory management generate some delaysI timer management is not very accurate

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 43 / 334

  • Introduction Second Idea : Operating System Approach

    Services

    Let’s consider a simple system :

    I a single softwareI One CPUI One memoryI One IO controllerI One communication bus

    Software interacts with external world

    I Data acquisitionI ComputationI Returning the results

    How can a software react to an event that can arise at any time?

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 44 / 334

  • Introduction Second Idea : Operating System Approach

    Cyclic look up

    Look up regularly each peripheral devices : called interacting from acyclic look up

    dodocheck sensorswhile (no data are available)read sensorscompute datadoactivate actuatorswhile (action to do)while (system halts)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 45 / 334

  • Introduction Second Idea : Operating System Approach

    Cyclic look up

    ProsI predictable : one iteration time (easy to know)I easy to program : with a few peripheral devices while reaction time of

    each device are almost the sameCons

    I if there is many devices and reaction time are very different,sub-optimal (near the slower device), sub loop to treat each device

    I bad in a computer science point of view : software becomeunreadable, close to be impossible to maintain : a new device needs torewrite almost all the software.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 46 / 334

  • Introduction Second Idea : Operating System Approach

    Interruptions

    How to warn the software that something happens outside? mechanism tostop the regular computation is called interruption

    I Initially hardware event leads to some changing in softwarecomputation

    I Next, extended to internal events and events coming from softwareI In general point of view, an event modify the behaviour of your

    softwareDeal with interruptions

    I Save contextI Interruption is computedI Restore context

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 47 / 334

  • Introduction Second Idea : Operating System Approach

    Interruptions

    dodocheck sensorswhile (WA_DATA)read memorised datawhile (system halts)

    INTERRUPTION //from a sensorread datawrite WA_DATA is trueacknowledge interruption

    I Interruption deals with external eventsI Cyclic look up software is delayedI Good response time if interruption

    computation needs a few time

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 48 / 334

  • Introduction Second Idea : Operating System Approach

    Limits

    CPU can spend time to compute different things than the software

    Example : UART (Universal Asynchronous Receiver Transmitter)

    I Interruptions to emit and receive through two registers (parallelcomputation ability)

    I Emission of a ASCII Char every millisecond : a lot time to wait untilwe have to emit again

    I For a new emission : WAITI When UART is available : Interruption SIGNALI Software can restart

    Can do new things while waiting : multiple-task

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 49 / 334

  • Introduction Second Idea : Operating System Approach

    Synchronous or asynchronous approaches

    I need a system that manage the software : start and synchronizes tasksaccording to events and context evolution

    I 2 kind of approach :I synchronous one : direct link between a task and its activating

    device, without taking care of conflicts that can arise between this taskand the others during execution of the software.

    I asynchronous one : all events are considered, including duringcomputation of a task and reacts according to the context.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 50 / 334

  • Introduction Second Idea : Operating System Approach

    Synchronous approach

    I "simple" algorithmI does not work without behavioural and time error emission

    I No pre-emption neither concurrent access to share resourcesI "synchronous" hypothesis : task activating time is NULL

    (instantaneous reaction)

    I discrete time view, scheduled by events arising, in the opposite ofcontinuous time view (hard time)

    I simultaneity linked to observation of current events at the same timeI simple and powerful model

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 51 / 334

  • Introduction Second Idea : Operating System Approach

    Asynchronous approach

    I more complex algorithms to implementI hard to model for checking the behaviour, need to evaluate every

    possible pre-emptionI scheduling policy : event appearance, CPU load decision between :

    I activated task by eventI running task when event ariseI another task waiting for the CPU

    I sometimes it is possible to check off-line according to formal rulesI priority definition is based on some criteria like : deadline, period, level

    of importance

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 52 / 334

  • Introduction Second Idea : Operating System Approach

    Services of a multitasks system

    Conclusion : A single program rarely use all CPU load

    Used those idle times to compute some concurrent other tasks from othersoftware : a part of what an operating system must do

    I running concurrent tasksI exchanging with outside worldI managing memory resourcesI sharing hardware, synchronization, communicationI time management

    What are the limits for real-time system?

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 53 / 334

  • Introduction Second Idea : Operating System Approach

    CPU Management - Task, process

    Standard operating systems manage process that can be described asfollow :

    I separated memory roomI special channels for communicationI high time cost spent for creation of a new oneI protection mechanisms are also time costingI switch between two processes need a lot of memory switches

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 54 / 334

  • Introduction Second Idea : Operating System Approach

    CPU Management - Task, process

    One process works on many memory areasI computing area (instructions of the software)I a heap data areaI a stack area for temporary data (variable)

    Context of a process is composed of registers, a name, a state, etc.

    A thread only own individual context data , everything else is shared withfather process

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 55 / 334

  • Introduction Second Idea : Operating System Approach

    Management multiple task and scheduling

    A activated process can have manystates, here are the main one :

    I createdI terminatedI running : task is currently using

    the CPU. We say it is elected.I waiting : task is asking for

    CPU. It is electable.I blocked : task is blocked

    waiting for an event to arise.

    Created Terminated

    Running

    Waiting Blocked

    Swapped out and waiting Swapped out and blocked

    Main Memory

    Virtual Memory

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 56 / 334

  • Introduction Second Idea : Operating System Approach

    Management multiple task and scheduling

    State transitions are :I waking : blocked → waitingI allocating : waiting → runningI unallocating or pre-empted :

    running → waitingI blocking : running → blocked

    Created Terminated

    Running

    Waiting Blocked

    Swapped out and waiting Swapped out and blocked

    Main Memory

    Virtual Memory

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 57 / 334

  • Introduction Second Idea : Operating System Approach

    Scheduling

    Scheduler must manage how the task are allocated to the CPU.Schedule in the best way is very hard.A scheduler called with request or pre-emptive if running process can beunallocated by scheduler.Non pre-emptive systems can do context switch only when a task isterminated or when it is asking for context switchScheduler works most of the time on-line. That means that a policy mustexists and be applied

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 58 / 334

  • Introduction Second Idea : Operating System Approach

    Scheduling policy

    Classical scheduling rules :I First In First Out (FIFO)I Each one its turn (tourniquet, Round Robin : RR)I Priority based

    Example

    Task Length Priority Start timeT1 6 1 0T2 3 2 0T3 4 1 0

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 59 / 334

  • Introduction Second Idea : Operating System Approach

    Scheduling example 1

    I FIFOT1 T1 T1 T1 T1 T1 T2 T2 T2 T3 T3 T3 T3

    I RR

    T1 T2 T3 T1 T2 T3 T1 T2 T3 T1 T3 T1 T1

    I Priority

    T2 T2 T2 T1 T1 T1 T1 T1 T1 T3 T3 T3 T3

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 60 / 334

  • Introduction Second Idea : Operating System Approach

    Scheduling example 1

    Scheduler with task interactionsTask 1 (prio = 2) Task 2 (prio = 2) Task 3 (prio = 3)

    (not started)T[1.1] T[2.1] T[3.1]wait (event) Signal wait (1 sec.)T[1.2] (Task 1, event)

    T[2.2] T[3.2]Starting Task 3

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 61 / 334

  • Introduction Second Idea : Operating System Approach

    Scheduling example 2

    Pre-emption after OS signalTask 1 (prio = 2) Task 2 (prio = 2) Task 3 (prio = 3)T[1.1]Wait (evt)

    T[2.1] Signal T1T[1.2]

    T[2.2] Start T3T[3.1] Wait 1 sec.T[3.2]

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 62 / 334

  • Introduction Second Idea : Operating System Approach

    Scheduling example 2

    Without pre-emptionTask 1 (prio = 2) Task 2 (prio = 2) Task 3 (prio = 3)T[1.1]Wait (evt)

    T[2.1] Signal T1T[2.2] Start T3

    T[3.1] Wait 1 sec.T[1.2]

    T[3.2]

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 63 / 334

  • Introduction Second Idea : Operating System Approach

    Interruptions

    I 2 kindI asynchronous : emitted by external context (hardware) can arise at

    any timeI synchronous : emitted by a specific instruction can only arrive in

    known date for the developer

    I processed by the same mechanism

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 64 / 334

  • Introduction Second Idea : Operating System Approach

    Interruption management

    The way interruptions are processed have a large influence on efficiency ofthe OS

    I Control of interruptions : mostly hardwareI Context switchI Source identification

    I Processing interruptionI Short : same contextI Long : task are called : signal/wait

    I Back to original process

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 65 / 334

  • Introduction Second Idea : Operating System Approach

    Resources management

    Those mechanisms are used to give one access to one resource in one time.I Define some sections as atomic : critical sectionsI Mask interruptions (hardware)I Scheduler is disabled during this critical section (software)

    I MutexI Block other tasks whatever priority they areI Problem to choose the task when Mutex is released

    I Counting Semaphores to manage a set of devices

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 66 / 334

  • Introduction Second Idea : Operating System Approach

    Synchronization

    Force an order of processing instructions of a processI MutexI RendezvousI Product/Consume

    Uses the following mechanisms :I Signal an eventI Semaphores useI MailboxesI Rendezvous

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 67 / 334

  • Introduction Second Idea : Operating System Approach

    Synchronization Limits

    Maintain coherency of data : creation of waiting queues

    I "Balanced" Systems inefficientI Priority : Synchronization brings some priority issues where a low

    priority can block a high priority task

    Some solutions :I Priority based waiting management : unlock view the priority level of

    the taskI Flag like "availability test"I Time out on blocking flags

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 68 / 334

  • Introduction Second Idea : Operating System Approach

    Input/Output

    Different policies for CPU scheduling and for Input/Output (IO)I Access through a specialized layerI Synchronous processingI Asynchronous processingI Difficult to manage prioritiesI Bad time management

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 69 / 334

  • Introduction Second Idea : Operating System Approach

    Services

    I Definition : a software that schedules computing of tasks in one shotway,manage devices of the system and bring development libraries(Application Programming Interface or API)

    I It is organized with :I a kernel that schedules tasks and manage devicesI often it proposes services packaged as modules for example. These

    service can bring the ability of the system to manage File Systems,network devices or any services that are needed by software

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 70 / 334

  • Introduction Second Idea : Operating System Approach

    Components

    The main components of a RTOS are :I scheduler, inside every kernel, it is the component that apply different

    algorithms to manage access to the CPUI kernel interfaces that give developers the ability to create software

    I tasksI semaphoresI message queue...

    I services : actions that a kernel can do on a device or on the systemlike time measurement, interruption, bus controller,...

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 71 / 334

  • Robotics Software Frameworks RUPI

    Layout

    Introduction

    Robotics Software FrameworksRUPI

    RUPI Point Of ViewDesign PatternOROCOSROSFrameworks

    OS : Tasks And Threads

    OS Driver Programming

    Application

    DocumentationsLudovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 72 / 334

  • Robotics Software Frameworks RUPI

    Challenging Scenario

    Figure: RUPI

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 73 / 334

  • Robotics Software Frameworks Design Pattern

    Layout

    Introduction

    Robotics Software FrameworksRUPIDesign Pattern

    Software RecipesOROCOSROSFrameworks

    OS : Tasks And Threads

    OS Driver Programming

    Application

    DocumentationsLudovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 74 / 334

  • Robotics Software Frameworks Design Pattern

    Find the tasks!

    I No Fuzzy propertiesI Real-TimeI Good Refresh rate

    I Nothing is perfectI Properties

    I Time (Loop (Freq?), Oneshot)I Load (CPU)

    I BoundariesI DelaysI Memory

    I Task repeated every miliseconds with acceptable delay of 0.01 ms

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 75 / 334

  • Robotics Software Frameworks Design Pattern

    Classical Robotics Tasks

    I Control TaskI Loop, DelayI Variable ParametersI Static parameters

    I User InterfaceI SupervisionI Send Orders (Asynchronous, Parameters)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 76 / 334

  • Robotics Software Frameworks Design Pattern

    MVC

    I ViewI SupervisionI GUI

    I ControllerI Send OrdersI Control Task InformationI GUI

    I ModelI SynchronizationI Communication SystemI Database

    I RT Task

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 77 / 334

  • Robotics Software Frameworks OROCOS

    Layout

    Introduction

    Robotics Software FrameworksRUPIDesign PatternOROCOS

    Open RObotics COntrol SoftwareROSFrameworks

    OS : Tasks And Threads

    OS Driver Programming

    Application

    DocumentationsLudovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 78 / 334

  • Robotics Software Frameworks OROCOS

    OROCOS Project

    Figure: Orocos Project

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 79 / 334

  • Robotics Software Frameworks OROCOS

    History

    I December 2000, as an idea of Herman BruyninckxI European FundingI European Labs

    I K.U.Leuven in Belgium ( Orocos@KUL )I LAAS Toulouse in France ( Orocos@LAAS )I KTH Stockholm in Sweden ( Orocos@KTH )

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 80 / 334

  • Robotics Software Frameworks OROCOS

    Orocos Toolchain = RTT + OCL

    I Real-Time ToolkitI Orocos Component Library

    Figure: Orocos Component/PluginsArchitecture

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 81 / 334

  • Robotics Software Frameworks OROCOS

    Figure: RTT WorkflowLudovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 82 / 334

  • Robotics Software Frameworks OROCOS

    Task Context

    Figure: Component Description

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 83 / 334

  • Robotics Software Frameworks OROCOS

    OCL -> Logging Tools

    I 2 SolutionsI Custom Component

    I + Complete control of the generated fileI - Some additional code that is not the heart of the controller

    I Listen a component through logging toolsI In C++ code :I In deployment :I

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 84 / 334

  • Robotics Software Frameworks OROCOS

    Task State Diagram

    Figure: Component State DiagramLudovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 85 / 334

  • Robotics Software Frameworks OROCOS

    Activity vs Threads

    Figure: Component Activity Diagram

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 86 / 334

  • Robotics Software Frameworks OROCOS

    Data Flow

    Figure: Components Connections

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 87 / 334

  • Robotics Software Frameworks OROCOS

    Example : Definition

    I SupervisorI InitialisationI WaitingI Standing-UpI WalkingI Alert

    I SensorsI Force HandlesI Orientation of the armI Orientations of the

    wheels(Beta_L,Theta_L,Beta_R,Theta_R)

    I ControlsI Impedance ControlI Position ControlI Speed ControlI Models

    I XZ MGDI XYPhi MGDI Trajectory Generator Figure: Walky ISIR

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 88 / 334

  • Robotics Software Frameworks OROCOS

    Example : Modelling State Machine

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 89 / 334

  • Robotics Software Frameworks OROCOS

    Example : Modelling Data Flows

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 90 / 334

  • Robotics Software Frameworks OROCOS

    Example : State Machine (Excerpt)

    StateMachine OurStateMachineType{

    var bool detected;initial state INIT {

    entry {ConsoleOut.display ("[ INIT ]");/*...*/ Plant.configure ();

    }transition select Waiting;

    }state Waiting {

    entry {STSDetector.start ();

    }transition STSDetected(detected)

    if (detected) then select SitToStand;} /*...*/RootMachine OurStateMachineType statemachine

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 91 / 334

  • Robotics Software Frameworks OROCOS

    Example : Component Skeleton(Excerpt)

    class Supervisor : public TaskContext {protected:

    InputPort STSDetected; /*...*/string stateMachineName;

    public:Supervisor(string const& name) : TaskContext(name),

    stateMachineName (" statemachine "){

    this ->addEventPort (" STSDetected",STSDetected );this ->addProperty (" stateMachineName",stateMachineName );

    }

    bool configureHook () {return true;

    }void updateHook () { }void errorHook () { }void stopHook () { }void cleanupHook () { }};

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 92 / 334

  • Robotics Software Frameworks OROCOS

    Example : Deployment File(Excerpt)

    0.150ORO_SCHED_RT

    STSDetector

    STSDetected

    deployment/smSTSandWALK.osd

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 93 / 334

  • Robotics Software Frameworks ROS

    Layout

    Introduction

    Robotics Software FrameworksRUPIDesign PatternOROCOSROS

    Robotics Operating SystemFrameworks

    OS : Tasks And Threads

    OS Driver Programming

    Application

    DocumentationsLudovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 94 / 334

  • Robotics Software Frameworks ROS

    Definition

    I ROS (Robot Operating System) provides libraries and tools to helpsoftware developers create robot applications. It provides hardwareabstraction, device drivers, libraries, visualizers, message-passing,package management, and more.

    I Willow GarageI Created in 2006I 2008 first stable release of ROSI 2010 ROS 1.0

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 95 / 334

  • Robotics Software Frameworks ROS

    Connecting Middleware

    I NodesI Server (roscore)I Output (rosout)

    I TopicsI PublishersI Subscribers

    I Parameterized ConnectionsI TCPI SharedMemoryI Serial (Arduino RosSerial) Figure: rxgraph of /Example node with its

    topics

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 96 / 334

  • Robotics Software Frameworks ROS

    Meta-Operating System

    I Meta-Operating-SystemI rosdepI rosmakeI roscdI rosedI rosrun

    I Usefull toolsI rxgraphI rxplot

    Figure: rxplot of /Example node with itstopics

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 97 / 334

  • Robotics Software Frameworks ROS

    excerpt Manifest

    depth_reg

    bonjean BSD

    http ://ros.org/wiki/depth_reg

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 98 / 334

  • Robotics Software Frameworks Frameworks

    Layout

    Introduction

    Robotics Software FrameworksRUPIDesign PatternOROCOSROSFrameworks

    Conclusion

    OS : Tasks And Threads

    OS Driver Programming

    Application

    DocumentationsLudovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 99 / 334

  • Robotics Software Frameworks Frameworks

    Connectivity Question

    I Exercice :I Description

    I KinectI OpenCVI SLAMI Directions

    I AimI When someone do hello then go to its direction

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 100 / 334

  • Robotics Software Frameworks Frameworks

    Example

    I ExerciceI Description

    I KinectI OpenCVI SLAMI Directions

    I AimI When someone do hello then go to its direction

    I ResultI Kinect -> OpenCV -> Position -> SLAM -> Directions

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 101 / 334

  • Robotics Software Frameworks Frameworks

    Connectivity Solutions

    I In OROCOS :I Omni ORBI From Scratch Components Connection

    I RPCI CORBAI Sockets -> Server/Client Approach

    I ROS

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 102 / 334

  • Robotics Software Frameworks Frameworks

    Conclusion

    I Overview ImplementationI Controller on one Computer : OROCOSI Communication between computing units : ROS

    I MethodI Identify the states of your system (Tasks, loops)I Think of the time, delays and how to overlay some of themI Write the data flow for each stateI Look at the Computing Units At least one node per Unit (ROS)I Therefor the components are described (inside one node)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 103 / 334

  • OS : Tasks And Threads Scheduling

    Layout

    Introduction

    Robotics Software Frameworks

    OS : Tasks And ThreadsScheduling

    ModelAlgorithmsThread Scheduling

    ManagementTime ServicesSynchronizationMessages and CommunicationIO Control

    OS Driver Programming

    Application

    Documentations

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 104 / 334

  • OS : Tasks And Threads Scheduling

    Scheduler

    Aim : address any needs of a real-time software like emergency stop, highlevel action or reactivity needTaxonomy :

    I Off-line/On-line algorithm : static/dynamic algorithmI Static/Dynamic prioritiesI pre-emptive/non pre-emptive algorithms :

    I Ability to have mutual exclusion of a deviceI Scheduler cost lowI Small efficient

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 105 / 334

  • OS : Tasks And Threads Scheduling

    Scheduler

    Properties that we look for :I Feasibility : ability to decide "a priori" that all the constraints will be

    fulfilledI Predictability : response time of tasks is predictable.I Optimality : Optimal if able to find a schedule of every set of

    feasibility tasksI Complexity : feasibility test is long and difficult?I Easiness to implement

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 106 / 334

  • OS : Tasks And Threads Scheduling

    Scheduler

    Scheduler works only on active tasks :I Schedule Table : off-line schedulingI Off-line priorities definition, root of the schedulingI On-line analysis and scheduling

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 107 / 334

  • OS : Tasks And Threads Scheduling

    Analysis recipe

    Scheduling problem :I To Model tasks of the system and their constraintsI To Choose a scheduling algorithmI To Validate feasibility on a set of tasks

    I Theoretical feasibility : scheduling ability and complexityI Empirical feasibility : implement scheduler

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 108 / 334

  • OS : Tasks And Threads Scheduling

    Task model

    Task families :I Linked task or not (Precedence constraint).I Important task or notI Repetitive or periodic task : activated regularlyI Non periodic task

    I Sporadic : irregular activation but a minimum time between eachactivation

    I Aperiodic : deadline is less strict and no minimum time between eachactivation

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 109 / 334

  • OS : Tasks And Threads Scheduling

    Task model

    Parameters for task iI Si : start time when task arrives in the schedulerI Ci : Computation time needed by the task (Capacity).I Pi : PeriodI Di : Deadline of the taskI Ri : Earliest activation timeI Aperiodic task is defined by :

    (Si ,Ci ,Di ,Ri )I Periodic task is defined by :

    (Si ,Ci ,Di ,Pi )

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 110 / 334

  • OS : Tasks And Threads Scheduling

    Task model

    Let’s consider a simplified model :I Periodic taskI Independent (No precedence constraint)I Starting time (worst case) : Si = 0I Deadline equal to Period : Pi = Di

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 111 / 334

  • OS : Tasks And Threads Scheduling

    Scheduler Structure

    Computation of scheduling informationI On-line or Off-lineI Period, Deadline...

    Waiting queue managementI One queue for each priorityI FIFO policyI Round Robin policy

    Election phaseI Priority, deadline, ...

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 112 / 334

  • OS : Tasks And Threads Scheduling

    Rate Monotonic Algorithm

    Off-line analysis, fixed priority, periodic tasks : Static softwareRationale

    I Computation phase : priority = 1periodI Election phase : highest priority is chosen

    PropertiesI Low ComplexityI Optimal Algorithm in fixed priority algorithm set

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 113 / 334

  • OS : Tasks And Threads Scheduling

    Rate Monotonic Algorithm

    ExampleI Task 1 : C1 = 6, P1 = 10I Task 2 : C2 = 9, P1 = 30I Pre-emptive caseI Non pre-emptive case

    Can be scheduled if load rate of the CPU U agrees with the followingsufficient condition :

    U =n

    ∑i

    CiTi≤ n× (2 1n − 1)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 114 / 334

  • OS : Tasks And Threads Scheduling

    Critical time theorem

    If every tasks both arrive at the same time in a system if they respect theirfirst deadline,thenevery other following deadlines will be respected whatever time they arrivein the system

    I it’s a necessary and sufficient condition if every tasks arrive at thesame time

    I else, it is a sufficient condition

    If Di = Ti , finishing test is :

    ∀i , 1 ≤ i ≤ n min0≤t≤Dii

    ∑j=1

    Cjt

    ⌈tTj

    ⌉≤ 1

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 115 / 334

  • OS : Tasks And Threads Scheduling

    Response time computation

    Response time TR: Duration between time when a task begin and timewhen it is finished. Result can be exact relying on task model.

    TRi = Ci + ∑j∈hp(i)

    Ij

    TRi = Ci + ∑j∈hp(i)

    ⌈TRiPj

    ⌉Cj

    Where hp(i) represents a set of tasks with a higher priority than i .

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 116 / 334

  • OS : Tasks And Threads Scheduling

    Response time computation

    Computation method : iterative way of evaluation

    wn+1i = Ci + ∑j∈hp(i)

    ⌈wniPj

    ⌉Cj

    I Let start with w0i = CiI Fail if wni > PiI Achieved if wn+1i = w

    ni

    Example : with P1 = 7, C1 = 3, P2 = 12, C2 = 2, P3 = 20, C3 = 5. Weassume that every task have the same priority and are ran in this order

    TR1 = 3 TR2 = 5 TR3 = 18

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 117 / 334

  • OS : Tasks And Threads Scheduling

    Aperiodic tasks with high priorities

    Periodic task dedicated to these tasks.

    I bring feasibility in the worst caseI dedicated task is not activated if there is no aperiodic task to computeI easy solution, but sometime we loose some CPU load

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 118 / 334

  • OS : Tasks And Threads Scheduling

    Earliest Deadline First EDF algorithm

    I Periodic task and aperiodicI On-line Algorithm, more useful than Rate Monotonic

    Rationale :I Computation phase : Deadline computation

    With priorityi (t) current priority at t and i the task :I Aperiodic task : priorityi (t) = Si +Di − tI Periodic task : priorityi (t) = Si (t) [last start activation time when

    time is t] +Pi − tI Election phase: Earliest deadline is chosen.(Min)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 119 / 334

  • OS : Tasks And Threads Scheduling

    EDF Example

    Let consider 2 tasks :

    T1 : C1 = 6, P1 = 10

    T2 : C2 = 9, P2 = 30

    I Pre-emptive caseI Non pre-emptive case

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 120 / 334

  • OS : Tasks And Threads Scheduling

    EDF Properties

    Ability to schedule : Pre-emptive case, periodic and independent tasksI Necessary and sufficient condition if ∀i ,Di = Pi ; Only necessary if∃i ,Di ≤ Pi :

    U =n

    ∑j=1

    CjPj≤ 1

    I Sufficient condition if ∃i ,Di ≤ Pi :

    U =n

    ∑j=1

    CjDj≤ 1

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 121 / 334

  • OS : Tasks And Threads Scheduling

    Server for aperiodic and sporadic jobs

    I Periodic server :I Idle if waiting queue is emptyI Running in its time

    0 5 10 15 20

    PS

    e1

    e2

    e1 e2

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 122 / 334

  • OS : Tasks And Threads Scheduling

    Server for aperiodic and sporadic jobs

    I Deferred server :I Time BudgetI Waiting for taskI Used to be in highest priority

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 123 / 334

  • OS : Tasks And Threads Scheduling

    Server for aperiodic and sporadic jobs

    I Sporadic server :I Like Deferred serverI But extra computation is done in background (lowest priority)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 124 / 334

  • OS : Tasks And Threads Scheduling

    Services

    Properties :I Threads and process capabilityI Fixed priorities, pre-emptive ⇒ RM easy. A minimum of 32 levels is a

    mandatoryI One waiting queue for each priority and scheduling policy (SCHED_F

    IFO, SCHED_RR, SCHED_OTHERS).I Available Services to specific users (like root)

    Standard say that scheduling policies must be able to be applied each timethe choice threadprocess exists (ex. : choice of a threadprocess to release asemaphore).

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 125 / 334

  • OS : Tasks And Threads Scheduling

    Policies

    POSIX.4 policies :#define SCHED_OTHER 0#define SCHED_FIFO 1#define SCHED_RR 2

    I Parameter(s) : extensible to future policies

    struct sched_param{int sched_priority;...

    };

    I Parameters modification:I Thread : creation of a thread from an attribute or modification of a

    running thread.I Inherit from a fork() or modify of a running process.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 126 / 334

  • OS : Tasks And Threads Scheduling

    API

    sched_get_priority_max Get max priority value.sched_get_priority_min Get min priority value.sched_rr_get_interval Get duration of one time unit.sched_yield Free the CPU of this thread.sched_setscheduler Choose the scheduler policy.sched_getscheduler Get the scheduler policy value.sched_setparam Set parameters of the scheduler.sched_getparam Get parameters of the scheduler.pthread_setschedparam Set parameters of the scheduler for thread.pthread_getschedparam Get parameters of the scheduler for thread.

    The last 2 function are working only on threads, other functions can beapplied on process/thread.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 127 / 334

  • OS : Tasks And Threads Scheduling

    Example

    struct sched_param parm;int res=-1;.../* Task T1 ; P1=10 */parm.sched_priority =15;res=sched_setscheduler(pid_T1 ,SCHED_FIFO ,&parm)if(res

  • OS : Tasks And Threads Management

    Layout

    Introduction

    Robotics Software Frameworks

    OS : Tasks And ThreadsSchedulingManagement

    ProcessPosix Thread

    Time ServicesSynchronizationMessages and CommunicationIO Control

    OS Driver Programming

    Application

    Documentations

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 129 / 334

  • OS : Tasks And Threads Management

    Fork

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 130 / 334

  • OS : Tasks And Threads Management

    Kill - Signal

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 131 / 334

  • OS : Tasks And Threads Management

    POSIX Threads

    Defined in chapter POSIX.4a (Portable Operating System Interface). Thischapter describe both threads and synchronization tools that are close tothreads (ex : mutex).Properties :

    I A POSIX Thread is defined with a identifier that is local to theprocess.It owns its stack, its context and a set of attributes thatdescribe its behaviour.

    I A POSIX Thread can be implemented in user space or in kernel space⇒ Standard describe only the interface not the way it is coded.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 132 / 334

  • OS : Tasks And Threads Management

    POSIX Threads

    I Code must be re-entrant (or "thread safe") ⇒ code is able to becomputed in multiple instances in a safe way

    I re-entrant code means :I don’t manipulate shared variables.I or it manipulate shared variables in a critical section (that mean no

    other is able to access this variable when one is manipulating it) .

    I Everything must be re-entrant user code but also system libraries (ex.libc.so)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 133 / 334

  • OS : Tasks And Threads Management

    POSIX threads

    pthread_create Creation of a thread.Paramters : code, attributes, arg.

    pthread_exit End of a thread.Parameter : return error value.

    pthread_self returns id of the running threadpthread_cancel Destroy a thread.

    Parameter : id of the thread.pthread_join Suspend a thread until

    another is not finished.pthread_detach Suppress parent link

    between threads.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 134 / 334

  • OS : Tasks And Threads Management

    POSIX Threads

    pthread_kill Emit a signal to a thread.pthread_sigmask Modify signal mask of a thread.

    In a POSIX.4a system, some services have a different semantic.I fork() : create a new process containing a thread where the main()

    function is computedI exit() : finish a process and all threads it contains.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 135 / 334

  • OS : Tasks And Threads Management

    POSIX Threads

    Creation and join example of a thread

    #include void* th(void* arg){

    printf ("I am thread %d ;process %d\n",pthread_self (),getpid ());pthread_exit(NULL);

    }

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 136 / 334

  • OS : Tasks And Threads Management

    POSIX Threads

    Creation and join example of a thread

    #include void* th(void* arg){

    printf ("I am thread %d ;process %d\n",pthread_self (),getpid ());pthread_exit(NULL);

    }

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 137 / 334

  • OS : Tasks And Threads Management

    POSIX Threads

    int main(int argc , char* argv []){

    pthread_t id1 ,id2;pthread_create (&id1 ,NULL ,th,NULL);pthread_create (&id2 ,NULL ,th,NULL);pthread_join(id1 ,NULL);pthread_join(id2 ,NULL);printf ("End of main thread %d ;process %d\n",pthread_self (),getpid ());pthread_exit(NULL);

    }

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 138 / 334

  • OS : Tasks And Threads Management

    POSIX Threads

    I With Solaris :

    gcc -D_REENTRANT create.c -lpthread -lrt>a.out

    I am thread 4 ; process 5539I am thread 5 ; process 5539End of main thread 1 ; process 5539

    I With Linux :

    gcc -D_REENTRANT create.c -lpthread>a.out

    I am thread 1026 ; process 1253I am thread 2051 ; process 1254End of main thread 1024 ; process 1251

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 139 / 334

  • OS : Tasks And Threads Management

    Thread Attributes

    Thread Attributes : properties of a thread that are defined during itscreation. No heritage between father and son threads.

    Example of attributes :Attribute Name Meaningdetachstate pthread_join possible or notpolicy scheduler policypriority level of priority of the thread (≥ 0)stacksize Size of the allocated stack

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 140 / 334

  • OS : Tasks And Threads Management

    Thread Attributes

    During creation of a thread, an structure of type pthread_attr_t can befilled and given if we want to specify none default attributes to the newthread :

    pthread_attr_init Creation of a default attribute structure.pthread_attr_delete Destruction of attribute structure.pthread_attr_setATT set the value of "ATT" attribute.pthread_attr_getATT set the value of "ATT" attribute.

    where ATT is replace by the name of the attribute.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 141 / 334

  • OS : Tasks And Threads Management

    Thread Attributes

    #include void* th(void* arg){

    printf ("I am thread %d \n",pthread_self ());

    }

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 142 / 334

  • OS : Tasks And Threads Management

    Thread Attributes

    int main(int argc , char* argv []){

    int i;pthread_t id;pthread_attr_t attr;struct sched_param param;pthread_attr_init (&attr);pthread_attr_setdetachstate (&attr ,PTHREAD_CREATE_DETACHED );pthread_attr_setschedpolicy (&attr ,SCHED_FIFO );param.sched_priority =1;pthread_attr_setschedparam (&attr ,& param);for(i=1;i

  • OS : Tasks And Threads Management

    Thread Attributes

    The TSD (Thread Specific Data area) : is an area of memory where arestored specific information of each thread.

    Allow extension of regular attributes.pthread_key_create Creation of a key.pthread_key_delete Destruction of the key.pthread_getspecific get pointer value linked

    to the key of the running thread.pthread_setspecific set pointer value linked

    to the key of the running thread.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 144 / 334

  • OS : Tasks And Threads Management

    Thread Attributes

    Creation of a new attribute :

    pthread_key_t cd_key;

    int pthread_cd_init(void){return pthread_key_create (&cd_key ,NULL);}

    char* pthread_get_cd(void){return (char*) pthread_getspecific(cd_key );}

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 145 / 334

  • OS : Tasks And Threads Management

    Thread Attributes

    int pthread_set_cd(char* cd){

    char* mycd = (char*) malloc(sizeof(char )*100);strcpy(mycd ,cd);return pthread_setspecific(cd_key ,mycd);

    }int main(int argc , char* argv []){

    pthread_cd_init ();pthread_set_cd ("/ here/dir ");printf ("My local directory is %s\n",pthread_get_cd ());

    }

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 146 / 334

  • OS : Tasks And Threads Time Services

    Layout

    Introduction

    Robotics Software Frameworks

    OS : Tasks And ThreadsSchedulingManagementTime Services

    Posix TimeXenomai Time services

    SynchronizationMessages and CommunicationIO Control

    OS Driver Programming

    Application

    Documentations

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 147 / 334

  • OS : Tasks And Threads Time Services

    Time manipulation

    Time linked services :I What time is it ?I Block a thread/process during a defined time.I Wake up a thread/process regularly (timer) ⇒ periodic tasks.

    Precision of these services :I Linked to what hardware we have (clock circuit), its features

    (interruption period) and also to the software that use it (interruptionhandler).

    I Ex : Linux Intel : circuit activated periodically (10 ms) ⇒ wakingbetween 10 to 12 ms.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 148 / 334

  • OS : Tasks And Threads Time Services

    Time Manipulation : POSIX Extensions

    I Many timers are available ⇒ many physical clocks, profiling.I Need at least one real-time clock : CLOCK_REALTIME (less than 20

    ms precise).I timespec structure ⇒ "theoretical" precision close to micro-seconds

    ....I Available services :I Get and set clocks.I Put a task in sleep mode.I Link periodic timer to UNIX signals ; and maybe with real-time

    signals. With or without automatic restart.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 149 / 334

  • OS : Tasks And Threads Time Services

    Time Manipulation

    clock_gettime Get clock value.clock_settime Set clock value.clock_getres Get clock resolution.timer_create Create a timer.timer_delete Remove a timer.timer_getoverrrun Give the number of unprocessed signalstimer_settime Activate a timer.timer_gettime Get time to the end of the timernanosleep Block a process/thread during a

    defined duration.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 150 / 334

  • OS : Tasks And Threads Time Services

    Time Manipulation : Example

    int main(int argc , char * argv []){timer_t monTimer; struct sigaction sig;struct itimerspec ti;timer_create(CLOCK_REALTIME ,NULL ,& monTimer );sig.sa_flags=SA_RESTART;sig.sa_handler=trop_tard;sigemptyset (&sig.sa_mask );sigaction(SIGALRM ,&sig ,NULL);ti.it_value.tv_sec=capacite;ti.it_value.tv_nsec =0;ti.it_interval.tv_sec =0; // here timer is notti.it_interval.tv_nsec =0; // automatically restartedtimer_settime(monTimer ,0,&ti ,NULL);

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 151 / 334

  • OS : Tasks And Threads Time Services

    Time Manipulation : Example

    printf ("Begin\n");while(go >0)

    printf ("I am working .....\n");printf (" Unblocked by timer : deadline missed ..\n");

    }

    int go=1;void too_late(int sig){

    printf (" Signal %d received\n",sig);go=0;

    }

    Execution :

    BeginI am working .....I am working .....I am working .....

    I am working .....I am working .....Signal 14 receivedUnblocked by timer : deadline missed ..

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 152 / 334

  • OS : Tasks And Threads Time Services

    API Timer Management System

    API :I void rt_timer_spin (RTIME ns) : busy clockI int rt_timer_set_mode (RTIME nstick) : TM_ONESHOT or

    period timeHardware level :

    I oneshotI periodic

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 153 / 334

  • OS : Tasks And Threads Synchronization

    Layout

    Introduction

    Robotics Software Frameworks

    OS : Tasks And ThreadsSchedulingManagementTime ServicesSynchronization

    MutexVariable ConditionnalCounting SemaphoreXenomai Native API Synchronisation tools

    Messages and CommunicationIO Control

    OS Driver Programming

    Application

    Documentations

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 154 / 334

  • OS : Tasks And Threads Synchronization

    Mutex

    I Optimized Semaphores to implement critical section.I Waiting queue manager : depends of scheduling policy

    (SCHED_FIFO, SCHED_OTHER, ...). Default behaviour : threadsare waked in decreasing order of priority

    I Behaviour is defined by a set of attributes, main parameters are :Attribute name Meaningprotocol Inheriting Protocol usedpshared Inter-process or inter-thread mutexceiling Priority ceiling

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 155 / 334

  • OS : Tasks And Threads Synchronization

    Rationale

    Programming mechanism that can block a thread while a condition is nottrue.

    I Solution is based on a mutex and a Var CondI Waking signal has no memory : if nothing is waiting for the condition,

    waking signal is lost ( 6= counting semaphores).I Waiting queue management : depend on scheduling policy

    (SCHED_FIFO, SCHED_OTHER, etc ...). Default behaviour :threads are waked in decreasing order of priority.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 156 / 334

  • OS : Tasks And Threads Synchronization

    Mutex

    pthread_mutex_init Initialize a mutex.pthread_mutex_lock Lock the mutex in blocking manner eventually.pthread_mutex_trylock Non blocking try to lock the mutexpthread_mutex_unlock Release the mutex lock.pthread_mutex_destroy Destruct the mutex.pthread_mutexattr_init Initialize an attribute structurepthread_mutexattr_setATT Set attribute ATT.pthread_mutexattr_getATT Get attribute ATT value.

    o ATT is one of the attributes of the mutex.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 157 / 334

  • OS : Tasks And Threads Synchronization

    API

    pthread_cond_init Initialize a variable.pthread_cond_destroy Destruct a variable.pthread_cond_wait Wait a waking signal coming from a condition.pthread_cond_signal Signal that a condition is true to one threadpthread_cond_broadcast Signal that a condition is true to all threads

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 158 / 334

  • OS : Tasks And Threads Synchronization

    Mechanism

    A classical program where a thread modify a variable : a mutex is used(var_mutex) and a condition is used (var_cond).

    pthread_mutex_lock (& var_mutex );/* Modify the variable in criticalsection */var = .... ;

    /* If the condition is fulfilled , wealert the blocked thread */if(condition(var))

    pthread_cond_signal (& var_cond );

    pthread_mutex_unlock (& var_mutex );

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 159 / 334

  • OS : Tasks And Threads Synchronization

    Mechanism

    Classical program that wait for a variable to be modified :

    pthread_mutex_lock (& var_mutex );while(! condition(var)){

    /* If the condition is not true ,wait */

    pthread_cond_wait (&var_cond , &var_mutex );}/* use the variable in critical section */.....

    pthread_mutex_unlock (& var_mutex );

    Attention pthread_cond_wait is making some implicit lock and unlock ofthe mutex.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 160 / 334

  • OS : Tasks And Threads Synchronization

    Example

    int y=2, x=0;pthread_mutex_t mut;pthread_cond_t cond;void* th(void* arg){

    int cont =1;while(cont){

    pthread_mutex_lock (&mut);x++;printf ("x++\n");if (x > y){

    pthread_cond_signal (&cond);cont =0; }

    pthread_mutex_unlock (&mut );}}

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 161 / 334

  • OS : Tasks And Threads Synchronization

    Example

    int main(int argc , char* argv){pthread_t id;pthread_mutex_init (&mut ,NULL);pthread_cond_init (&cond ,NULL);pthread_mutex_lock (&mut);pthread_create (&id ,NULL ,th,NULL);while (x y is true

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 162 / 334

  • OS : Tasks And Threads Synchronization

    Counting semaphore

    I Semaphore: waiting queue + counter.I No management of priority inversion : case must be treated in

    developer level.I Used for synchronization and inter-process or inter-thread mutual

    exclusion.I Waiting queue management : depend on scheduling policy

    (SCHED_FIFO, SCHED_OTHER, etc ...). Default behaviour :threads are waked in decreasing order of priority.

    I Two kind of semaphore : named and unnamed semaphore.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 163 / 334

  • OS : Tasks And Threads Synchronization

    Counting Semaphore

    sem_open Connection to a named semaphore.sem_close Disconnection to a named semaphore.sem_unlink Destruction of a named semaphore.sem_init Initialisation of a unnamed semaphore..sem_destroy Destruction of a unnamed semaphore.sem_post Freeing a semaphore.sem_wait Acquiring a semaphore.sem_trywait Non blocking acquiring of a semaphore.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 164 / 334

  • OS : Tasks And Threads Synchronization

    Counting Semaphore Example

    #include #include sem_t sem;int main(int argc , char* argv []){

    pthread_t id;struct timespec delay;sem_init (&sem ,0,0);pthread_create (&id ,NULL ,th,NULL);deli.tv_sec =4; deli.tv_nsec =0;nanosleep (&delay ,NULL);printf ("main thread %d : free from

    the other thread \n",pthread_self ());sem_post (&sem);pthread_exit(NULL);

    }

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 165 / 334

  • OS : Tasks And Threads Synchronization

    Counting Semaphore Example

    void* th(void* arg){

    printf (" thread %d waiting\n",pthread_self ());sem_wait (&sem);printf (" thread %d unblocked \n",pthread_self ());

    }

    Execution

    thread 4 waitingmain thread 1 : free fromthe other threadthread 4 unblocked

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 166 / 334

  • OS : Tasks And Threads Synchronization

    API Mutexes

    int rt_mutex_create (RT_MUTEX *mutex, const char *name)int rt_mutex_acquire (RT_MUTEX *mutex, RTIME timeout)int rt_mutex_release (RT_MUTEX *mutex)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 167 / 334

  • OS : Tasks And Threads Synchronization

    API Condition variables

    int rt_cond_create (RT_COND *cond, const char *name)int rt_cond_signal (RT_COND *cond)int rt_cond_broadcast (RT_COND *cond)int rt_cond_wait (RT_COND *cond, RT_MUTEX *mutex, RTIME timeout)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 168 / 334

  • OS : Tasks And Threads Synchronization

    API Counting Semaphores

    int rt_sem_create (RT_SEM *sem, const char *name,unsigned long icount, int mode)

    int rt_sem_p (RT_SEM *sem, RTIME timeout)int rt_sem_v (RT_SEM *sem)int rt_sem_broadcast (RT_SEM *sem)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 169 / 334

  • OS : Tasks And Threads Messages and Communication

    Layout

    Introduction

    Robotics Software Frameworks

    OS : Tasks And ThreadsSchedulingManagementTime ServicesSynchronizationMessages and Communication

    SignalsMail QueueXenomai Native API

    IO Control

    OS Driver Programming

    Application

    Documentations

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 170 / 334

  • OS : Tasks And Threads Messages and Communication

    Real-Time Signals

    Signal : event delivered in asynchronous way to a process/thread (softwareinterruption).

    I Blocked signals (or masked) pending or delivered.I Default behaviour can be modified by user.I One signal register for each thread/process.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 171 / 334

  • OS : Tasks And Threads Messages and Communication

    Real-Time Signals

    Existing mechanism in POSIX.1 but with the following issues :I Implementation pending signal register = unsafe delivery (possible

    loss).I Emitting order not respected when they are delivered.I Does not contain many information and few of them are available for

    user : not adapted to IPC implementation.I Not efficient (slow : high latency).

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 172 / 334

  • OS : Tasks And Threads Messages and Communication

    Real-Time Signals : POSIX Interface

    kill Emission of a signal.sigaction Connection of a handler to a signal.sigemptyset Initialize with all signals disabled.(mask all signals)sigfillset Initialize with all signals enabled.(unmask all signals)sigaddset Add a signal in the set(unmask this signal).sigdelset Remove a signal from the set(mask this signal).sigismember Check if the signal is enabled in the set.sigsuspend Block a process until a signal is received.sigprocmask Install a mask.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 173 / 334

  • OS : Tasks And Threads Messages and Communication

    Signals : Non RT Example

    void handler(int sig){printf (" Signal %d received\n",sig);}

    int main(int argc , char * argv []){struct sigaction sig;sig.sa_flags=SA_RESTART;sig.sa_handler=handler;sigemptyset (&sig.sa_mask );sigaction(SIGUSR1 ,&sig ,NULL);while (1); // program computes !!

    }

    Execution :$sig &$kill -USR1 14090Signal 10 received

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 174 / 334

  • OS : Tasks And Threads Messages and Communication

    Real-Time signals : POSIX Extended

    Range of new signals numbered from SIGRTMIN to SIGRTMAX (thatrepresent a minimum of RTSIG_MAX signals)

    I Possibility to use a value linked to this signal.I No loss of signal : using of a queue for pending signals.I Ordered delivery : respecting scheduling policy chosen + priority

    linked to a signal ⇒ SIGRTMIN has the highest priority.I Emitted with a kill, sigqueue, with a timer or by an asynchronous IO.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 175 / 334

  • OS : Tasks And Threads Messages and Communication

    Real-Time Signals : POSIX Extended

    Complementary interfaces of POSIX.4 :sigqueue Emit a real-time signal.sigwaitinfo Wait for a signal without handler executionsigtimedwait Idem above + timeout on blocking time.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 176 / 334

  • OS : Tasks And Threads Messages and Communication

    Real-Time Signals : RT Example

    int main(int argc , char * argv []){struct sigaction sig;union sigval val;int cpt;sig.sa_flags=SA_SIGINFO;sig.sa_sigaction=handler;sigemptyset (&sig.sa_mask );if(sigaction(SIGRTMIN ,&sig ,NULL)

  • OS : Tasks And Threads Messages and Communication

    RealTime Signals : RT Example

    void handler (int sig , siginfo_t *sip , void *uap){

    printf (" Received signal %d, val = %d \n",sig ,sip ->si_value.sival_int );

    }

    Execution :

    \$rt -sigReceived signal 38, val = 0Received signal 38, val = 1Received signal 38, val = 2Received signal 38, val = 3Received signal 38, val = 4

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 178 / 334

  • OS : Tasks And Threads Messages and Communication

    Mechanism

    With UNIX, Communication mechanism is the pipeline (named or not) ⇒too abstract for real-time constraints.

    Mail queues in POSIX.4 have the same features :I Priority in emission/reception.I Can work in Non-blockingI Can preallocate resources.I Can communicate in Inter-Process and Inter-Thread mode.I Unfortunately, priority inversion issues are not addressed.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 179 / 334

  • OS : Tasks And Threads Messages and Communication

    API

    mq_open Creation or connection to a queue.mq_unlink Destruction of a queue.mq_receive Reception of the oldest and the most important

    mail.mq_send Emission of a mail with a given prioritymq_close Disconnection to a queue.mq_notify Notification that there is a new mail.mq_setattr Set attributes of the queue.mq_getattr Get attributes of the queue.

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 180 / 334

  • OS : Tasks And Threads Messages and Communication

    Example

    /* Emission with a priority 1 (from 0 toMQ_PRIO_MAX) */

    mq_send(id,buff ,100 ,1);pthread_create (&tid ,NULL ,consumer ,NULL);pthread_exit(NULL);

    }

    void* consumer(void* arg){mqd_t id;char buff [100];id=mq_open ("/ myqueue",O_RDONLY );mq_receive(id,buff ,100, NULL);printf ("msg = %s\n",buff);mq_unlink ("/ myqueue ");pthread_exit(NULL);

    }

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 181 / 334

  • OS : Tasks And Threads Messages and Communication

    Example

    #include #include int main(int argc , char* argv []){

    pthread_t tid;mqd_t id;char buff [100];struct mq_attr attr;attr.mq_maxmsg =100;attr.mq_flags =0;attr.mq_msgsize =100;id=mq_open ("/ mafile",O_CREAT|O_WRONLY ,

    444,& attr);strcpy(buff ,"Hi !!");...

    }

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 182 / 334

  • OS : Tasks And Threads Messages and Communication

    API Event flags groups

    I Synchronizing object based on a long word structure.I Any bit in the word can be used as a user flagI Task oriented signal mechanismI Conjunctive or Disjunctive

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 183 / 334

  • OS : Tasks And Threads Messages and Communication

    API Event flags groups

    int rt_event_create (RT_EVENT *event, const char *name,unsigned long ivalue, int mode)

    int rt_event_signal (RT_EVENT *event, unsigned long mask)int rt_event_wait (RT_EVENT *event, unsigned long mask,

    unsigned long *mask_r, int mode, RTIME timeout)int rt_event_clear (RT_EVENT *event, unsigned long mask,

    unsigned long *mask_r)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 184 / 334

  • OS : Tasks And Threads Messages and Communication

    API Messages queue

    int rt_queue_create (RT_QUEUE *q, const char *name,size_t poolsize, size_t qlimit, int mode)

    void * rt_queue_alloc (RT_QUEUE *q, size_t size)int rt_queue_free (RT_QUEUE *q, void *buf)int rt_queue_send (RT_QUEUE *q, void *mbuf, size_t size, int mode)int rt_queue_write (RT_QUEUE *q, const void *buf, size_t size, int mode)ssize_t rt_queue_receive (RT_QUEUE *q, void **bufp, RTIME timeout)ssize_t rt_queue_read (RT_QUEUE *q, void *buf, size_t size, RTIME timeout)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 185 / 334

  • OS : Tasks And Threads Messages and Communication

    API Messages pipe

    int rt_pipe_create (RT_PIPE *pipe, const char *name,int minor, size_t poolsize)

    ssize_t rt_pipe_receive (RT_PIPE *pipe,RT_PIPE_MSG **msgp, RTIME timeout)

    ssize_t rt_pipe_read (RT_PIPE *pipe, void *buf, size_t size, RTIME timeout)ssize_t rt_pipe_send (RT_PIPE *pipe, RT_PIPE_MSG *msg,

    size_t size, int mode)ssize_t rt_pipe_write (RT_PIPE *pipe, const void *buf, size_t size,

    int mode)ssize_t rt_pipe_stream (RT_PIPE *pipe, const void *buf, size_t size)RT_PIPE_MSG * rt_pipe_alloc (RT_PIPE *pipe, size_t size)int rt_pipe_free (RT_PIPE *pipe, RT_PIPE_MSG *msg)int rt_pipe_flush (RT_PIPE *pipe, int mode)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 186 / 334

  • OS : Tasks And Threads Messages and Communication

    Example : Pipe Kernel side

    #include #include #include #include #include #define TASK_PRIO 0 /* Highest RT priority */#define TASK_MODE T_FPU|T_CPU (0) /* Uses FPU , bound to CPU #0 */#define TASK_STKSZ 4096 /* Stack size (in bytes) */

    RT_TASK task_desc;RT_PIPE pipe_desc;

    void task_body(void){RT_PIPE_MSG *msgout , *msgin;int err , len , n;for (;;) {/* ... */len = sizeof (" Hello ");/* Get a message block of the right size in order toinitiate the message -oriented dialog with theuser -space process. */msgout = rt_pipe_alloc(len);if (! msgout)fail ();

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 187 / 334

  • OS : Tasks And Threads Messages and Communication

    Example : Pipe Kernel side

    #include RT_PIPE pipe_desc;int init_module (void){int err;/* Connect the kernel -side of the message pipe to thespecial device file /dev/rtp7. */err = rt_pipe_create (&pipe_desc ," MyPipe",7,NULL);...}

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 188 / 334

  • OS : Tasks And Threads Messages and Communication

    Example : Pipe User side

    From a regular Linux process:#include int pipe_fd;int main (int argc , char *argv []){/* Open the Linux side of the pipe. */pipe_fd = open ("/ dev/rtp7",O_RDWR );/* ou grace au service de registres */pipe_fd = open ("/ proc/xenomai/registry/native/pipes/MyPipe",O_RDWR );.../* Write a message to the pipe. */write(pipe_fd ,"hello world ",11);...}

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 189 / 334

  • OS : Tasks And Threads IO Control

    Layout

    Introduction

    Robotics Software Frameworks

    OS : Tasks And ThreadsSchedulingManagementTime ServicesSynchronizationMessages and CommunicationIO Control

    MemoryIO

    OS Driver Programming

    Application

    Documentations

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 190 / 334

  • OS : Tasks And Threads IO Control

    Memory Management

    A shared time system leads to possible time indeterminism because :I Dynamic memory allocation.I Page swap : swapin=swapout.

    Solution : limit dynamic memory allocation and lock pages in centralmemory (POSIX.4 Interface) :

    I mlockall()=munlockall() : lock/unlock swap with all memory pages ofthis process..

    I mlock()=munlock() : lock/unlock a range of addresses.

    ! Warning : mlock() is not portable (because there is no memory model inPOSIX standard).

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 191 / 334

  • OS : Tasks And Threads IO Control

    API Memory heap

    int rt_heap_create (RT_HEAP *heap, const char *name, size_t heapsize, int mode)int rt_heap_alloc (RT_HEAP *heap, size_t size, RTIME timeout, void **blockp)int rt_heap_free (RT_HEAP *heap, void *block)

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 192 / 334

  • OS : Tasks And Threads IO Control

    Posix AIO

    I aio_readI aio_writeI aio_return, aio_errorI aio_cancelI aio_fsyncI aio_suspend

    Ludovic Saint-Bauzel (Polytech’Paris UPMC) Robotics Real-Time Programming 2012-2013 193 / 334

  • OS : Tasks And Threads IO Control

    Example :

    sa.sa_sigaction = aioSigHandler;if (sigaction(