Robotics

Chapter 12
Robot Programming, Middleware and Digital Engineering

Bachelors Program in Machine Technology
Robotics   •   Bachelor

Robot Programming, Middleware and Digital Engineering

Introduction

Industrial robots are no longer programmed only by teaching points on a pendant. Modern robot applications combine motion control, sensing, software architecture, industrial communication, simulation, data management and safety engineering. A robot cell may contain a manipulator, gripper, vision system, programmable logic controller (PLC), safety controller, manufacturing execution system and cloud or edge-computing services. Reliable operation depends on coordinating these elements in real time and verifying their behavior before physical deployment.

This chapter examines the software and digital-engineering foundations of robot applications. It introduces real-time programming, state machines, robot middleware, ROS 2, communication protocols, application programming interfaces (APIs), simulation, offline programming, virtual commissioning and digital twins. The emphasis is on design reasoning: selecting an appropriate architecture, managing timing and faults, validating behavior and transferring a verified application to production.

Learning Objectives

After studying this chapter, the reader should be able to:

  1. Distinguish hard real-time, soft real-time and non-real-time robot software.
  2. Design a robot application using states, transitions, events and fault recovery.
  3. Explain the roles of middleware, ROS 2 nodes, topics, services, actions and parameters.
  4. Select communication protocols and APIs for suitable industrial tasks.
  5. Develop and test robot programs using simulation and offline programming.
  6. Explain virtual commissioning and digital twins, including their limitations.
  7. Apply a disciplined workflow for deployment, verification, safety and maintenance.

1. Robot Applications as Cyber-Physical Systems

A robot application is a cyber-physical system: software makes decisions while mechanical and electrical equipment produces physical effects. The system typically contains five layers:

LayerMain responsibilityTypical examples
Physical layerProduces and measures motionRobot, gripper, conveyor, encoder
Control layerExecutes motion and I/O controlServo drive, robot controller, PLC
Middleware layerConnects software componentsROS 2, DDS, fieldbus interface
Application layerImplements the manufacturing taskPick-and-place, welding, inspection
Information layerStores, analyzes and reports dataDatabase, dashboard, MES, cloud service

The layers are conceptually distinct but interact continuously. A vision node may publish a detected pose, an application node may plan a motion, a robot controller may execute that motion, and a PLC may authorize the cycle only when guarding and tooling conditions are safe.

A useful design principle is separation of concerns. Motion generation should not be mixed unnecessarily with production reporting; safety authorization should not depend solely on a high-level application process; and a simulation model should be traceable to the physical configuration.

2. Real-Time Robot Programming

2.1 Meaning of real time

A real-time system is one in which correctness depends on both the computed result and the time at which it is produced. A late response can be as incorrect as a numerically wrong response. Real-time performance is therefore described by deadlines, latency, jitter and determinism.

  • Deadline: latest acceptable completion time for a task.
  • Latency: elapsed time between an event and the corresponding response.
  • Jitter: variation in latency or task execution time.
  • Determinism: predictability of timing behavior.
  • Cycle time: time required for one repeated control or production cycle.

Hard real-time tasks must meet deadlines reliably. Servo control and certain safety functions are examples. Soft real-time tasks can occasionally miss deadlines while remaining useful; production dashboards and some perception pipelines often have this characteristic. Non-real-time tasks, such as historical report generation, have no strict response deadline.

If a periodic task executes every T seconds and its worst-case execution time is C seconds, a basic feasibility requirement is:

C ≤ T

Here, T is the task period and C is the worst-case execution time. This condition is necessary but not sufficient when several tasks share a processor, because scheduling overhead, interrupts and competing tasks must also be considered. For a set of independent periodic tasks, a simple processor-utilization measure is:

U = ∑(Cᵢ/Tᵢ)

where Cᵢ is the execution time and Tᵢ is the period of task i. The value U represents the fraction of processor time requested by the tasks. It must be evaluated with the selected scheduling theory and priority policy; U ≤ 1 alone does not guarantee schedulability for every task set.

2.2 Control-loop timing

A robot controller commonly contains nested loops. A fast inner servo loop regulates motor torque or velocity, while slower loops manage trajectory generation, force control, perception or task sequencing. High-level software should not attempt to replace a certified servo controller unless it has appropriate real-time hardware, timing guarantees and safety validation.

Sampling time affects control quality. If a sensor is sampled at interval Δt, a control algorithm uses measurements that are discrete approximations of a continuous process. Excessive Δt can cause delayed corrective action, while unnecessarily small Δt increases computation and communication demands. The correct value depends on plant dynamics, controller design, network behavior and validation results.

2.3 Real-time programming practices

Important practices include:

  • avoid unbounded memory allocation in hard real-time loops;
  • avoid blocking calls, uncontrolled logging and network operations in critical cycles;
  • use bounded queues and explicit timeouts;
  • separate high-priority control from lower-priority visualization;
  • measure worst-case execution time rather than relying on average time;
  • use monotonic clocks for durations and deadlines;
  • timestamp sensor data and account for clock synchronization;
  • define behavior when data are missing, stale or inconsistent.

A real-time design is not established merely by using a fast processor. It requires evidence from timing analysis, stress testing and fault-injection tests.

3. State Machines for Robot Applications

A state machine represents an application as a finite set of operating states and permitted transitions. It is especially effective for manufacturing sequences because it makes normal operation, waiting conditions and recovery paths explicit.

A typical robot cell may contain these states:

  1. Initialization: load configuration and establish communications.
  2. Homing: move to a defined reference condition if permitted.
  3. Ready: await a production request.
  4. Acquire part: detect and secure a workpiece.
  5. Process: perform the robot operation.
  6. Release part: place or transfer the workpiece.
  7. Complete: report cycle completion.
  8. Fault: stop or safely inhibit operation and record the cause.
  9. Recovery: require specified operator or automatic actions.

A transition is enabled by a guard condition, such as part_present = true, gripper_closed = true and safety_ok = true. An event may be an operator command, sensor change, timeout or controller message. Each state should define entry actions, continuous actions, exit actions and permitted interruptions.

A robust state machine is preferable to a long sequence of unstructured commands. It prevents accidental continuation after a failed operation and supports diagnostic reporting. However, a state machine can become difficult to maintain if every minor detail is represented as a separate global state. Hierarchical state machines or behavior trees may then provide better structure.

Timeout and fault logic

Every wait condition should have a defined timeout. If a gripper-close command is issued and confirmation is not received within the allowable interval, the program should not wait indefinitely. It should stop motion as appropriate, preserve the diagnostic context and enter a controlled fault or recovery state.

Faults should be classified, for example:

  • transient communication fault;
  • recoverable process fault, such as an absent part;
  • operator-intervention fault;
  • safety fault;
  • unrecoverable hardware or configuration fault.

Recovery must not bypass safety functions. A reset command should clear a fault only after its cause has been removed and the required authorization has been obtained.

4. Middleware and ROS 2

4.1 Purpose of middleware

Middleware is software that provides common communication and service mechanisms between application components. It reduces the need for every node to implement its own transport, discovery, serialization and connection management.

Robot middleware commonly provides:

  • message definitions and serialization;
  • publish–subscribe communication;
  • request–response services;
  • long-running actions with feedback and cancellation;
  • parameter management;
  • time and coordinate-frame support;
  • logging, introspection and lifecycle management.

Middleware does not automatically make an application real time or safe. Communication quality depends on the operating system, transport, network, quality-of-service settings, computation load and system architecture.

4.2 ROS 2 concepts

ROS 2 applications are organized into nodes, each representing a logical computational component. Nodes communicate through several mechanisms:

  • Topics: asynchronous publish–subscribe streams, appropriate for sensor data and status.
  • Services: short request–response interactions, appropriate for commands that should complete quickly.
  • Actions: long-running goals with feedback, result and cancellation, appropriate for navigation or motion tasks.
  • Parameters: configurable values such as speed limits, frame names or planner settings.
  • Launch systems: tools for starting and configuring groups of nodes.
  • TF-style coordinate transforms: mechanisms for relating frames such as robot base, tool and camera.

ROS 2 generally uses DDS-based communication. Quality of service (QoS) policies include reliability, durability, history depth and deadline behavior. A sensor stream that can tolerate occasional loss may use best-effort delivery, whereas a critical command may require reliable delivery. QoS incompatibility can prevent apparently correct nodes from communicating, so it must be designed and tested rather than left to chance.

A typical architecture may include a camera node publishing images, a perception node publishing object poses, a planning node generating trajectories, a hardware interface sending commands to the robot controller, and a supervisory node implementing the production state machine.

4.3 Coordinate frames and data integrity

Robot software must distinguish coordinate frames and units. A pose consists of position and orientation relative to a named reference frame. Mixing millimetres with metres or confusing the camera frame with the robot base frame can produce dangerous motion even when the numerical software executes correctly.

Every message should, where applicable, include:

  • frame identifier;
  • timestamp;
  • unit convention;
  • sequence or validity information;
  • source and calibration status.

Transform chains should be tested with known reference points. Calibration error, timestamp delay and mechanical flexure can make a mathematically consistent transformation physically inaccurate.

5. Communication Protocols and APIs

Industrial robot systems use multiple communication technologies because no single protocol optimizes all requirements.

TechnologyTypical strengthTypical limitation
Discrete I/OSimple deterministic interlocksLimited data and scalability
Industrial EthernetFast controller and device exchangeConfiguration and vendor dependence
EtherCAT or similar fieldbusSynchronized motion and I/ORequires compatible hardware and engineering
OPC UAStructured interoperable informationUsually not the innermost servo loop
MQTTLightweight event and telemetry distributionNot a substitute for safety control
TCP/IP socketsFlexible custom communicationApplication must define reliability and timing
REST APIConvenient integration with information systemsRequest latency and overhead

A protocol should be selected according to timing, determinism, safety, data model, interoperability, distance, diagnostic requirements and cybersecurity. For example, a REST API may be appropriate for submitting production orders, while a fieldbus is more appropriate for synchronized I/O.

An API defines how software components request services or exchange information. A well-designed robot API specifies command syntax, units, valid ranges, response codes, timeout behavior, versioning and authentication. Idempotent commands are valuable: repeating a command such as “set operating mode to automatic” should not create an unintended second operation.

Safety-related communication requires special treatment. Ordinary status messages must not be assumed to provide a safety function. Safety systems use appropriate certified architectures, monitoring, redundancy and validation according to applicable machinery and functional-safety requirements.

6. Simulation and Offline Programming

Simulation reproduces selected aspects of a robot cell in software. Offline programming (OLP) creates or edits robot programs away from the physical robot, often using a CAD model and a virtual controller. These methods can reduce commissioning time, reveal reachability problems and allow production planning before equipment is installed.

A useful simulation model may contain:

  • robot kinematic model and joint limits;
  • tool center point and payload;
  • workpiece geometry and fixtures;
  • collision geometry;
  • conveyor or process timing;
  • sensors and communication interfaces;
  • controller logic and error behavior.

Simulation results are only as credible as the assumptions and model fidelity. A collision-free path in a simplified model may collide with cable bundles, guards or flexible tooling in the real cell. Similarly, a simulated cycle time may omit network delays, operator actions, robot acceleration limits or recovery operations.

6.1 Kinematic and collision considerations

Forward kinematics maps joint coordinates to the tool pose. In abstract form:

x = f(q)

where q is the vector of joint positions and x is the resulting pose. Inverse kinematics seeks q for a desired pose x. Multiple solutions may exist, and some may violate joint limits, create singular configurations or cause collisions.

A trajectory is not validated merely because its endpoints are reachable. Intermediate configurations must also satisfy joint limits, velocity and acceleration limits, workspace constraints and collision-clearance requirements. The programmed path should be checked under the actual tool, payload and approach orientation.

6.2 Offline-programming workflow

A disciplined OLP workflow is:

  1. Import and clean CAD geometry.
  2. Establish the world, robot base, tool and workpiece frames.
  3. Define tooling, payload and operating limits.
  4. Create target points and process paths.
  5. Generate trajectories and test reachability.
  6. Check collisions and singularities.
  7. Simulate I/O and production sequencing.
  8. Post-process for the target robot controller.
  9. Compare the generated program with controller syntax and safety constraints.
  10. Validate at reduced speed on the physical cell.

Robot calibration and frame registration are essential during transfer. A virtual model may use ideal geometry, whereas the physical system contains installation tolerances and calibration errors. Touch-up procedures should correct small deviations without hiding a systematic modeling problem.

7. Virtual Commissioning and Digital Twins

Virtual commissioning integrates a virtual model with real or emulated control hardware before physical production equipment is complete. It can test PLC logic, robot sequences, HMI screens, sensors, interlocks and production scenarios. Three common arrangements are:

  • Model-in-the-loop: the controller and plant are both simulated.
  • Software-in-the-loop: application software interacts with a simulated plant.
  • Hardware-in-the-loop: real controller hardware interacts with a simulated plant.

A digital twin is a digital representation connected to a physical asset or process through data and models. A static CAD model is not necessarily a digital twin. A useful twin has a defined purpose, a relationship to the physical asset, updated data and a model whose fidelity is appropriate to the intended decisions.

Digital twins support condition monitoring, process optimization, virtual training, what-if analysis and maintenance planning. They do not remove the need for physical validation. Sensor bias, unmodeled wear, changing payloads, network outages and human behavior can produce divergence between twin and plant.

Model fidelity and validation

Model fidelity should be selected according to the question being asked. A geometric model may be sufficient for reachability, whereas cycle-time prediction requires controller timing, acceleration limits and material-flow logic. Validation should compare observable outputs from the model and plant using representative test cases. Differences should be documented rather than silently tuned away.

8. Integrated Development and Deployment Workflow

A professional robot-software lifecycle includes:

  1. Requirements: define task performance, interfaces, safety constraints and acceptance criteria.
  2. Architecture: allocate functions among robot controller, PLC, middleware and information systems.
  3. Design: define states, messages, frames, APIs, timing and fault responses.
  4. Implementation: use version control, code review, configuration management and documented conventions.
  5. Simulation: test nominal, boundary and fault scenarios.
  6. Virtual commissioning: connect control logic to a realistic virtual plant.
  7. Hardware integration: verify communication, calibration, I/O and safety circuits.
  8. Controlled commissioning: begin with safe speed, restricted workspace and measured test cases.
  9. Acceptance testing: demonstrate requirements and record results.
  10. Operation and maintenance: monitor logs, manage updates and preserve traceability.

Testing should include unit tests for algorithms, integration tests for interfaces, system tests for complete cycles and regression tests after changes. Fault injection is particularly valuable: disconnect a sensor, delay a message, provide an invalid pose or remove a part, and verify that the system enters a defined safe response.

9. Illustrative Case: Vision-Guided Bin Picking

Background

A fictional manufacturer must remove randomly oriented components from a bin and place them into a machining fixture. The cell contains a six-axis robot, a vacuum gripper, a 3D camera, a PLC and a ROS 2 computer.

Problem

Early trials occasionally placed parts outside the fixture. Investigation showed inconsistent camera-to-robot calibration, stale image data and no timeout when vacuum confirmation failed.

Analysis

The application was decomposed into nodes and states. The camera node timestamped each point cloud. A perception node rejected data older than a specified age and published a pose in the camera frame. A calibration transform converted the pose to the robot base frame. The state machine required a valid pose, safe approach path and gripper readiness before motion.

Approach and solution

The team created a digital cell model containing the bin, robot, gripper and fixture. Thousands of simulated poses were tested for reachability and collision clearance. The ROS 2 communication design used a topic for camera data, an action for the pick operation and services for calibration and cell reset. The PLC retained authority over guard-door and emergency-stop conditions.

The physical commissioning sequence began with low speed and a test artifact. Calibration was verified using several reference points rather than one point. A vacuum timeout caused the robot to retreat to a safe inspection position and enter a recoverable fault state. Logs recorded image timestamp, selected pose, robot target, vacuum status and cycle result.

Outcome

The revised design eliminated indefinite waits and made failed picks diagnosable. Simulation identified unreachable poses before production, while physical tests revealed a small fixture offset that was corrected through controlled frame registration. The cell met its acceptance criteria after both nominal and fault scenarios were demonstrated.

Learning points

The case illustrates that robot performance depends on the complete cyber-physical chain, not only on the manipulator. Time-stamped data, explicit frames, state-based recovery, simulation and measured commissioning are mutually reinforcing practices.

10. Challenges, Ethics and Governance

Robot software can create physical hazards, privacy risks and operational dependencies. Cameras may collect images of workers; logs may reveal individual performance; networked controllers may be exposed to unauthorized access. Governance should therefore address data minimization, access control, retention, secure updates and accountability for automated decisions.

Cybersecurity measures include network segmentation, authenticated interfaces, least-privilege accounts, signed or controlled software updates, backup configurations and incident procedures. Safety and cybersecurity are related but distinct: a secure system can still be mechanically unsafe, and a safe machine can be vulnerable to unauthorized commands.

Engineers should document assumptions, version dependencies, calibration status and known limitations. Artificial intelligence may improve perception, planning or predictive maintenance, but an AI component must have defined operating limits, monitoring and fallback behavior. A model that performs well on training images may fail under different lighting, occlusion or part presentation. Human oversight and safety-rated control must not be replaced by unverified confidence scores.

11. Best Practices and Contemporary Developments

Current professional practice is moving toward:

  • containerized and reproducible development environments;
  • standardized robot descriptions and interoperable data models;
  • edge computing for low-latency perception and analytics;
  • cloud services for fleet-level monitoring rather than direct servo control;
  • simulation-based testing in continuous integration pipelines;
  • data-driven maintenance using controller and sensor histories;
  • collaborative development between mechanical, electrical, controls and software engineers.

These developments increase flexibility but also increase integration complexity. The most effective organizations treat robot software as an engineered product with requirements, tests, release management and lifecycle support.

Chapter Summary

Robot applications are cyber-physical systems in which mechanical action, control, communication and software must operate coherently. Real-time programming requires attention to deadlines, latency, jitter, determinism and worst-case execution time. State machines provide a clear structure for normal operation, timeouts, faults and recovery.

Middleware separates application functions and provides communication services. ROS 2 uses nodes, topics, services, actions, parameters and DDS-based quality-of-service policies. Protocol and API selection must reflect timing, interoperability, safety, data structure and cybersecurity requirements.

Simulation and offline programming improve planning and reduce commissioning risk, but their results depend on model fidelity, calibration and validation. Virtual commissioning tests control logic against a simulated plant, while a digital twin connects a purposeful digital representation to a physical asset. Neither eliminates physical testing.

A robust development lifecycle progresses from requirements and architecture through simulation, virtual commissioning, controlled deployment, acceptance testing and maintenance. Safety, cybersecurity, privacy, traceability and responsible use of AI must be integrated throughout the lifecycle.

Key Terms

Real-time system: A system whose correctness depends on both result and response time.

Deadline: The latest acceptable completion time for a task.

Latency: Delay between an event and its response.

Jitter: Variation in timing or execution delay.

Middleware: Software that provides communication and common services between components.

Node: A logical computational component in a distributed robot application.

Topic: An asynchronous publish–subscribe communication channel.

Service: A short request–response interaction.

Action: A long-running operation with feedback, result and cancellation.

Quality of service: Communication policies governing reliability, durability, history and timing.

State machine: A model consisting of states, events, guards and transitions.

API: A defined interface through which software requests functions or exchanges data.

Offline programming: Creating robot programs away from the physical robot.

Simulation: Software-based representation of a robot, process or environment.

Virtual commissioning: Testing control systems against a virtual plant before physical completion.

Digital twin: A connected digital representation of a physical asset or process.

Calibration: Determining the relationship between measurements, frames or physical geometry.

Fault injection: Deliberately introducing failures to test system responses.

Virtual commissioning: Integration of real or emulated control hardware with a simulated plant.

Cyber-physical system: A system combining computational decision-making with physical processes.

Review Questions

Short Answer Questions

  1. What is the difference between hard real time and soft real time?
  2. Why are timestamps and coordinate-frame identifiers important in robot messages?
  3. Distinguish a ROS 2 topic, service and action.
  4. What is the purpose of a timeout in a robot state machine?
  5. Why can a collision-free simulation still fail to predict a physical collision?

Long Answer Questions

  1. Explain how a state machine can organize normal operation, faults and recovery in a robot cell.
  2. Discuss the factors that should guide selection of communication protocols for robot applications.
  3. Describe a complete offline-programming workflow from CAD preparation to physical commissioning.
  4. Compare model-in-the-loop, software-in-the-loop and hardware-in-the-loop testing.
  5. Explain the relationship between digital twins, model fidelity and validation.

Analytical / Application Questions

  1. Design a state sequence for a robot that loads parts into a machine, including at least three timeout or fault conditions.
  2. A perception node publishes poses, but the robot sometimes moves using old data. Analyze possible causes and propose middleware, timestamp and testing measures.
  3. A company wants to use a REST API to control servo motion directly from a cloud service. Evaluate this architecture and recommend a safer allocation of functions.

Multiple Choice Questions with Answers

Question 1

Which characteristic makes a task hard real-time?

A. It produces detailed historical reports.

B. It must meet its deadline reliably.

C. It uses a graphical interface.

D. It runs only when an operator requests it.

Correct Answer: B. It must meet its deadline reliably.

Question 2

If a periodic task has execution time C and period T, which basic feasibility condition is required?

A. C ≥ T

B. C = 0

C. C ≤ T

D. T = 0

Correct Answer: C. C ≤ T

Question 3

Which ROS 2 communication mechanism is most appropriate for a long-running operation requiring feedback and cancellation?

A. Parameter

B. Topic

C. Service

D. Action

Correct Answer: D. Action

Question 4

What is the principal purpose of a guard condition in a state machine?

A. To determine whether a transition may occur

B. To increase motor torque

C. To store CAD geometry

D. To encrypt every sensor value

Correct Answer: A. To determine whether a transition may occur

Question 5

Which technology is generally most suitable for structured information exchange between industrial systems rather than an innermost servo loop?

A. OPC UA

B. Emergency-stop circuit

C. Motor encoder

D. Mechanical limit stop

Correct Answer: A. OPC UA

Question 6

Why must robot messages identify coordinate frames?

A. To reduce the robot's payload

B. To show which reference system gives meaning to a pose

C. To guarantee collision-free motion

D. To replace calibration

Correct Answer: B. To show which reference system gives meaning to a pose

Question 7

What is a major purpose of offline programming?

A. To eliminate all physical commissioning

B. To create and test robot programs away from the physical robot

C. To disable robot safety functions

D. To replace the robot controller permanently

Correct Answer: B. To create and test robot programs away from the physical robot

Question 8

Which statement best describes virtual commissioning?

A. Painting a robot before installation

B. Testing control behavior against a virtual plant

C. Increasing motor voltage during production

D. Recording only operator attendance

Correct Answer: B. Testing control behavior against a virtual plant

Question 9

A digital twin differs from a static CAD model because it normally includes what feature?

A. A permanent mechanical redesign

B. A connection to data from a physical asset or process

C. No model assumptions

D. Guaranteed perfect prediction

Correct Answer: B. A connection to data from a physical asset or process

Question 10

Which practice is most appropriate for a hard real-time control loop?

A. Use unbounded blocking network calls

B. Allocate memory unpredictably on every cycle

C. Measure worst-case execution time and use bounded operations

D. Depend only on average execution time

Correct Answer: C. Measure worst-case execution time and use bounded operations

Robotics
Chapter 12 of 15