Reinforcement Learning Approach to Optimizing Profilometric Sensor Trajectories for Surface Inspection
Sara Roos-Hoefgeest Mario Roos-Hoefgeest Ignacio Alvarez Rafael C. González










AUGUST 2024Roos-Hoefgeest et al.: RL Approach to Optimizing Trajectories Surface Inspection


Reinforcement Learning Approach to Optimizing Profilometric Sensor Trajectories for Surface Inspection
    Sara Roos-Hoefgeest, 
	    Mario Roos-Hoefgeest,
	    Ignacio Alvarez,
	    and Rafael C. GonzálezMember, IEEE
	Sara Roos-Hoefgeest, Ignacio Alvarez and Rafael C. González are with the Department of Electrical, Electronic, Communications and Systems Engineering, Oviedo University, Gijón, Spain
	Mario Roos-Hoefgeest is with CIN Systems, Gijón, Spain

    September 9, 2024
===================================================================================================================================================================================================================================================================================================================================================================




§ ABSTRACT 

High-precision surface defect detection in manufacturing is essential for ensuring quality control. Laser triangulation profilometric sensors are key to this process, providing detailed and accurate surface measurements over a line. To achieve a complete and precise surface scan, accurate relative motion between the sensor and the workpiece is required, typically facilitated by robotic systems. It is crucial to control the sensor's position to maintain optimal distance and orientation relative to the surface, ensuring uniform profile distribution throughout the scanning process. Reinforcement Learning (RL) offers promising solutions for robotic inspection and manufacturing tasks. This paper presents a novel approach using RL to optimize inspection trajectories for profilometric sensors. Building upon the Boustrophedon scanning method, our technique dynamically adjusts the sensor's position and tilt to maintain optimal orientation and distance from the surface,  while also ensuring a consistent profile distance for uniform and high-quality scanning. Utilizing a simulated environment based on the CAD model of the part, we replicate real-world scanning conditions, including sensor noise and surface irregularities. This simulation-based approach enables offline trajectory planning based on CAD models.
Key contributions include the modeling of the state space, action space, and reward function, specifically designed for inspection applications using profilometric sensors. We use Proximal Policy Optimization (PPO) algorithm to efficiently train the RL agent, demonstrating its capability to optimize inspection trajectories with profilometric sensors. 
To validate our approach, we conducted several experiments where a model trained on a specific training piece was tested on various parts in simulation. Also, we conducted a real-world experiment by executing the optimized trajectory, generated offline from a CAD model, to inspect a part using a UR3e robotic arm model.




Note to Practitioners
§ ABSTRACT 

	This paper addresses the problem of integrating robot manipulators and laser profilometric sensors in surface inspection tasks. Despite the relevance of both technologies in the manufacturing industry, little research has been done to integrate both technologies. In this work, we present a Reinforcement Learning-based trajectory generation algorithm that generates feasible sensor trajectories that optimizes the sensor’s position and orientation at each point along the scanning path. The input to the system is a CAD model of the object to be inspected. Therefore the system adapts dynamically to the geometrical characteristics of the objects. Experiments were conducted using simulations and real hardware. Objects with different geometric shapes and complexities were used to validate the approach, proving the effectiveness of this approach.



	Industrial robots, Trajectory Planning, Reinforcement Learning, Automatic optical inspection

	


§ INTRODUCTION


Surface inspection is a critical aspect of quality control in many industries, ensuring that manufactured components meet strict standards and function reliably. Accurate detection and characterization of surface defects are essential to maintaining product integrity and quality. 

Many industries still rely on manual inspection processes performed by human operators. However, manual inspection has already stopped being practical when it comes to the development of industrial demands for accuracy and efficiency. Manual inspection systems are not prone to detecting micron-scale defects. Therefore, advance sensor technologies are needed to serve for the need of small imperfections, which would be impossible to detect by human vision. A famous case study is car body pieces <cit.>. As the body components have a dimensional tolerance of several tenths of millimeter, the defects, whether it is functional stretching, or just a purely aesthetic protrusion, or imbalance, are sometimes one hundredth of this size. 

Furthermore, to meet the stringent requirements of modern industrial inspection, advanced sensor technologies have emerged as indispensable tools. Among them, laser triangulation is a widely adopted technique due to its superior precision and efficiency <cit.>, <cit.>. This method involves projecting a laser line onto the surface of an object and capturing the reflected light using a camera or sensor. Through the analysis of the distortion of the projected line, detailed information about the surface topography can be obtained with high accuracy.

To achieve an integral scan of the entire surface of the part to be inspected, relative motion between the part and the sensor is required. Robotic systems, including robotic arms, <cit.>, <cit.>, unmanned aerial vehicles (UAVs) <cit.>, drones or unmanned ground vehicles (UGVs) <cit.>, <cit.>, or autonomous underwater vehicles (AUVs) <cit.>, have been increasingly integrated into inspection procedures in different applications to address this requirement. These systems facilitate precise and controlled travel between the inspected part and the sensor, enabling complete surface coverage and efficient inspection processes. 

Effective and accurate inspection requires meticulous planning of the sensor paths over the part surface. While manual planning is sufficient for simpler scenarios, more complex geometries or stringent accuracy standards require the implementation of automated methods. The generation of inspection paths for robotic systems represents a significant challenge, requiring predefined paths that take into account surface geometry, defect characteristics and inspection requirements.

Although several studies related to automated inspection path planning can be found in the literature, highlighting the use of robotic arms, there is a significant gap in research specifically addressing the integration of robotics and profilometric sensors for surface inspection tasks.

Chen et al. highlight this gap in their study<cit.>, where they propose a novel approach for automatically detecting surface defects on freeform 3D objects using a 6-degree-of-freedom manipulator equipped with a line scanner and a depth sensor. Their method involves defining local trajectories for precise defect inspection and optimizing global time for efficient scanning. 

Li et al. propose a method for planning scanning trajectories in automated surface inspection <cit.>. Their approach is based on a trajectory planning algorithm using a triangular mesh model. They divide the workpiece surface area into regions, determine scanning orientations and points in each region, and then generate scanning trajectories using the minimum enclosing rectangle. This method involves developing a section division algorithm to determine the scanning orientation, followed by generating trajectories that comply with system constraints.

Recently, a new trend has emerged for trajectory generation in robotics using Reinforcement Learning (RL) methods. While several papers have explored the potential of RL in various applications, few have focused specifically on its application in inspection tasks. This lack of research highlights the need for further exploration and research in this area.

For example, Lobbezoo et al. compile in <cit.> different strategies present in the literature that use RL algorithms for pick and place applications. On the other hand, Elguea-Aguinaco et al. provides in <cit.> a comprehensive review of the current research on the use of RL in tasks involving intensive physical interactions. These tasks refer to activities where object manipulation involves direct and meaningful contact between the robot and its environment. This study covers research related to a variety of areas, including rigid object manipulation tasks (e.g., assembly, disassembly, or polishing and grinding) and deformable object manipulation tasks (e.g., rope or garment and fabric folding, tensioning and cutting, or object manipulation). The approaches and methodologies employed in each of these areas are compiled and analyzed.

Han et al. present in <cit.> a comprehensive investigation of different applications of Deep RL in robotic manipulators, highlighting the main problems faced by RL in robotic applications. One of the most important problems is that the models used often do not perfectly replicate the real system. For example, in machine vision-guided robots, the simulation of RGB images can differ significantly from the actual images captured by cameras, a problem known as Sim-to-Real. This is because the simplified models do not fully capture the system dynamics. These discrepancies make it difficult to transfer simulation-trained models to real environments.

To address this issue, we use a realistic simulator presented in our previous work <cit.>, which allows us to accurately represent the measurements obtained by the profilometric laser triangulation sensor. 

Another problem they highlight is that trajectory generation in robotics is an inherently multidimensional problem, which further complicates the learning and optimization process. Also Ji et al. emphasize in <cit.> this problem, highlighting that in the field of robotics, most work using RL focuses on the field of mobile robot navigation due to its simple and well-developed theory <cit.>.

Surface inspection using profilometric sensors is typically performed in a straight line. If the workpiece is too large to be covered in a single scan, parallel passes are used, generally following Boustrophedon-like trajectories <cit.>. In our approach, we will start with these linear or Boustrophedon trajectories and enhance them using reinforcement learning techniques to optimize sensor movements and ensure comprehensive surface coverage. During each pass, the sensor advances in a predetermined direction while it can adjust its height and pitch angle over the piece, keeping the other orientations constant. Our approach effectively tackles the multidimensional challenge in surface inspection by concentrating on three critical parameters: the sensor's position along the scanning direction, its height adjustment, and pitch orientation. This focus simplifies the problem significantly, eliminating the need to individually manage each robot axis and streamlining sensor control and trajectory planning during inspections.

Reinforcement learning (RL) techniques offer effective solutions for problems with limited action spaces, making them well-suited for optimizing surface inspection tasks. RL's ability to learn from interactions and improve control policies based on rewards makes it a promising tool for addressing challenges in this field. Despite advancements in RL algorithms, its application in inspection tasks remains relatively underexplored compared to other robotics applications. This gap highlights the necessity for further exploration and research in this area.

In the realm of inspection applications, Xiangshuai Zeng's research presents PIRATE (Pipe Inspection Robot for Autonomous Exploration) <cit.>, a robot designed for internally inspecting pipes using reinforcement learning. Equipped with multiple bending joints and wheels, PIRATE offers adaptive and efficient mobility. By employing algorithms like PPO and deep neural networks, the robot learns to navigate sharp corners, adjusting its behavior in real-time and adapting to various pipe configurations. The system defines actions, rewards, and observations, with actions including wheel movements and adjustments in bending joints, and observations coming from a 2D laser scanner.

Another work focused on inspection applications is presented by Jing et al. in <cit.>, centering on the automatic generation of robotic trajectories for surface inspection in production lines. They use techniques such as Monte Carlo algorithms and greedy search for Coverage Path Planning (CPP), enabling the automatic generation of inspection trajectories adapted to objects of different sizes and geometries. Using a 3D structured light scanner, they generate a 3D point cloud representing the scanned object's geometry. The main objective is to minimize total cycle time, combining View Planning Problem (VPP) and trajectory planning to minimize the total sum of inspection and displacement costs after meeting surface coverage requirements. The trajectory generation process includes random viewpoint selection, robot movement calculation, collision-free path planning, evaluation of visibility for each viewpoint and covered surface portion, and application of an RL-based planning algorithm to select inspection actions until completion. Actions involve selecting a viewpoint and moving the robot to place the 3D scanner at the selected viewpoint. The proposed RL algorithm automatically generates the online inspection policy, taking as input the robot model, target object model, and sensor specifications.

Aligned with prior research, Christian Landgraf, Bernd Meese et al. introduce in <cit.> an approach for automatic viewpoint planning in surface inspection. They propose a strategy to find optimal sets of viewpoints for 3D inspection of specific workpieces. Their goal is to automate this process for any industrial robotic arm available in ROS and any 3D sensor specification; in their application they employ a stereo camera. Using advanced reinforcement learning algorithms such as Q-learning, Proximal Policy Optimization (PPO) and Deep Q-Networks (DQN), each action involves selecting the viewpoint and planning and executing the robot's movement towards this pose. Upon reaching its goal, the sensor generates a 3D point cloud at this specific pose, with the state of the environment constructed using 3D measurement observations and the robot's current pose.

For case studies like the one in this article, which uses laser triangulation profilometric sensors for measurements along a line, traditional trajectory planning approaches, such as the mentioned View Planning Problem (VPP), are not suitable. The VPP is intended for finding optimal poses for capturing images or making measurements of objects or surfaces using 3D vision sensors that cover a wide area. This highlights the need for a trajectory planning approach adapted to the specific characteristics of profilometric sensors, focusing on optimizing surface exploration rather than capturing a complete three-dimensional view.

To date, no research has been found regarding the generation of inspection trajectories using Reinforcement Learning and profilometric sensors, highlighting a significant gap in the literature and a promising area for robotics and automation research. This study addresses this gap by presenting an RL-based approach designed for generating inspection trajectories using profilometric sensors. Key contributions involve the modeling of the state space, action space, and reward function. We use the PPO (Proximal Policy Optimization) algorithm to efficiently train the RL agent, demonstrating its ability to optimize inspection trajectories with profilometric sensors.

PPO is an algorithm proposed by OpenAI in <cit.>. The authors highlight its ability to balance three key aspects in reinforcement learning: ease of implementation, sampling efficiency, and simplicity in hyperparameter tuning. PPO not only offers competitive performance comparable to or better than more advanced reinforcement learning algorithms, but also stands out for its significantly simpler implementation and tuning.

The effectiveness of PPO has been demonstrated across a wide range of applications, including robot control such as the PIRATE robot <cit.> or the development of view planning systems for inspection, as presented in the work of Landgraf, Meese et al. <cit.>. Additionally, it has been successfully applied in pick-and-place tasks, such as training 7-degree-of-freedom robotic arms for precise object manipulation, as described in <cit.>, as well as in other pick-and-place applications, as discussed in <cit.>.

Furthermore, comparisons between PPO and other reinforcement learning algorithms, such as SAC and TD3, reveal interesting patterns in terms of training efficiency, performance, and convergence. For example, in <cit.>, it was found that PPO tends to perform better in smaller state spaces, while SAC shows advantages in larger state spaces. On the other hand, in <cit.>, PPO and SAC were compared, where SAC demonstrated greater efficiency in sampling, but PPO exhibited greater insensitivity to hyperparameters and more stable convergence in complex problems. These findings support the choice of PPO as the main algorithm for the proposed research.







§ MATERIALS AND METHODS


In this section, we present the proposed approach for generating inspection trajectories using profilometric sensors and Reinforcement Learning techniques. The goal is to optimize each pass of the Boustrophedon scanning method, seeking an optimal orientation and distance of the sensor relative to the part at each position. This involves dynamically adjusting the position and tilt (pitch) of the sensor to maintain a constant and appropriate pose between the sensor and the surface at all times. The other two sensor orientations will be fixed beforehand, allowing for precise and uniform data capture. Additionally, we aim to optimize the spacing between profiles. This approach ensures complete and detailed coverage of the part's surface, thereby maximizing the quality and accuracy of the captured data.

To train the RL algorithms, we use a simulated environment that replicates the conditions of the real system described developed in our previous work <cit.>. This simulator emulates the measurements of a laser triangulation profilometric sensor, including sensor noise and speckle noise generated by the object's surface. Thus, a realistic and controlled training environment is obtained.

The state space is constructed using the position and orientation of the robot's end-effector. This allows for generalization of the approach and facilitates the transfer of the method to different robotic configurations. Additionally, the state also includes other parameters, such as the mean profile distance, the direction angle, and the advance between consecutive scans.

The action space is defined by relative increments in the sensor's position and tilt angle, allowing for precise adjustments and smooth movements of the sensor. The reward function consists of three key components: the distance between the sensor and the surface, the alignment of the sensor with the surface normal, and the spacing between consecutive scans. This comprehensive reward function encourages optimal behaviors in terms of distance, orientation, and sensor advancement.

Next, each component of the proposed method is described in detail, providing a thorough understanding of its design and implementation.




 §.§ Reinforcement Learning (RL): Basic Concepts


Reinforcement Learning (RL) is a branch of machine learning inspired by behavioral psychology, which focuses on how agents make decisions within an environment to maximize some measure of accumulated reward over time <cit.>. 

In RL, the agent dynamically interacts with the environment, observing its current state and selecting actions in response. These actions affect the environment's state and generate a reward signal that guides the agent's behavior. The primary goal of the agent is to maximize the accumulation of these rewards over time, thereby optimizing its performance in the environment. Figure <ref> illustrates the basic interaction cycle between an agent and the environment.



The agent refers to the machine learning algorithm that interacts with the environment. The environment is the adaptive problem space with attributes such as variables, boundary values, rules, and valid actions. Each action represents a step taken by the RL agent to navigate through the environment. The set of all valid actions in a given environment is referred to as the action space 𝒜, defined mathematically as 𝒜 = {a_1, a_2, ..., a_n}, where a_i represents a specific action in the set, and n is the total number of actions. A state represents the environment at a given point in time. The set of all possible states in an environment is 𝒮 = {s_1, s_2, ..., s_m}. The reward is the positive, negative, or neutral value the agent receives as a consequence of an action, assessing its quality. The reward at each time step r_t depends on each state-action pair r_t = r(s_t, a_t). The accumulated reward is the sum of all rewards obtained over time.

In most cases, the problem to be solved is formally modeled as a Markov Decision Process (MDP). An MDP can be defined as a tuple of 5 elements (𝒮, 𝒜, r, P, ρ_0), representing respectively the set of all valid states, the set of all valid actions, the reward function, the state-action transition probability function, and the initial state distribution. 

Another key parameter is the policy (π), which is the strategy guiding the agent's decision-making within the environment. The policy can be deterministic, where for each state in the environment, the agent selects a specific action predictably or stochastic, meaning that the agent chooses actions based on probabilities, introducing uncertainty and allowing for exploration.

The goal of any reinforcement learning algorithm is to select a policy that maximizes the expected return when the agent acts according to it. The expected return J(π) is represented by equation <ref>. The expected reward 𝔼_π[r(s_t, a_t)] is the average of the rewards the agent expects to receive by following the policy π in each state s. The objective is to adjust the policy parameters to maximize this reward, using optimization methods such as policy gradient to continuously improve the policy and the agent's performance in the specified task.


    J(π) = 𝔼_π[r(s_t,a_t)]


Therefore, the final optimization problem in an RL algorithm can be expressed as equation <ref>, where π^* is the optimal policy:


    π^* = max_π J(π)






 §.§ Scanning Characteristics for Surface Inspection with Profilometric Sensors


During the scanning process using laser triangulation profilometric sensors, the quality of the measured data is directly affected by various parameters associated with the relative position between the sensor and the inspected piece, as detailed in <cit.>. These parameters are critical to ensuring precise and thorough surface inspection. Therefore, it is essential to carefully consider these factors during the planning of scanning trajectories in order to achieve effective results in surface inspection. 

    


One of these crucial parameters is the Optimal Working Distance (W_d), which denotes the ideal distance between the sensor and the object's surface. This distance ensures optimal precision of the captured data by positioning the laser source at the scanning reference plane, typically located at the midpoint of the depth of field.

The Depth of Field (Z_r) refers to the range of distances within which the sensor can accurately capture surface data during a single scan (Figure <ref>(b)).  Assuming a point in the scanner's coordinate system is (x_s, 0, z_s), the equation <ref> must be satisfied. Operating within this range is critical as it minimizes noise levels associated with deviations from the optimal working distance. Studies <cit.> have demonstrated that maintaining proximity to the optimal working distance reduces noise, thereby enhancing measurement accuracy.
    

    W_d - Z_r/2≤ z_s ≤ W_d + Z_r/2


Another crucial parameter is the Direction Angle (α), which signifies the angle between the sensor's orientation vector l and the normal vector n of the workpiece surface (Figure <ref>(c). This angle is computed using Equation <ref>.  As the direction angle increases, there is a higher likelihood of introducing noise into the capture. This phenomenon occurs because the scanner may capture unwanted reflections of the laser light and variations in surface reflectivity, negatively impacting the data quality. Previous studies <cit.> have empirically shown how noise levels correlate with the direction angle, highlighting its significance in achieving precise surface capture.


    α = acos(-l·n)


Additionally, the Distance Between Profiles (Δ s) determines the density of points between consecutive scan profiles. Adequate point density ensures comprehensive coverage and accuracy of the inspected surface, particularly in areas with small features or irregular surfaces where a lower density might compromise inspection quality. See figure <ref>(d).










    

    
    























    





    

     

        







    



In addition to considering these parameters, it is crucial to choose the appropriate type of trajectory to achieve a complete scan of the surface of the piece. In laser profilometer inspection applications, one of the most common strategies is to use Boustrophedon paths <cit.>, <cit.>.

In a Boustrophedon scan, the sensor moves in a straight line along one axis until it reaches the edge of the surface to be inspected. Then, it shifts laterally a predetermined distance and changes direction to move in the opposite direction along the initial axis. This pattern of movements is repeated until the entire surface is covered. In the context of surface inspection, this method is highly efficient in ensuring that every area of the piece's surface is scanned without omissions, thereby maximizing coverage and inspection accuracy.



Considering these types of trajectories, the profilometric sensor collects data only during the parallel passes along the surface of the piece. In Figure <ref>, these trajectories are shown in red, from the initial point of a pass (P_ini_i) to its end point (P_fin_i), where i denotes the number of parallel passes. The movement of the robot between each pass is shown in black. The distance between passes, d, is carefully adjusted to ensure that the scans overlap adequately, thereby completely covering the piece.




 §.§ Simulated Environment


To train the reinforcement learning algorithms, it is essential to have an environment that simulates the conditions of the real system. Conducting tests directly on the real system can be costly, dangerous, or simply impractical in many situations. Therefore, simulators are used to virtually recreate the environment of interest.

We will use the simulator detailed in our previous work <cit.>, designed to replicate the conditions of the real system in a virtual environment. This simulator recreates the measurements of a laser triangulation profilometric sensor, emulating the parameters of any commercial sensor according to its specification sheet. It allows for precise reproduction of measurements on a CAD model of the part to be inspected, including the introduction of inherent sensor noise and speckle noise generated by the object's surface. See Figure <ref>.



In each iteration of the simulator, several critical parameters are obtained that will be used later by the RL algorithm. First, the distance profile is captured, a fundamental representation provided by any profilometric sensor, see Figure <ref>. Additionally, the 3D position of the scanned points of the CAD model is collected, providing detailed information about the surface geometry of the object, see Figure <ref>. Furthermore, the simulator also provides data on the normals at those points on the object's surface.







 §.§ State Space


As previously mentioned the position and orientation of the end-effector are used instead of relying on the positions and velocities of the robot's joints. This choice simplifies the state space and facilitates the transfer of the method to different robotic configurations without the need for specific adjustments in the joints.

Mathematically, the state S is defined as a tuple as follows:


    S = {P(x, y, z), θ, D, α, Δ s}


Here, P(x,y,z) represents the position of the end-effector, while θ denotes its tilt. The parameters D, α, and Δ s correspond to the mean profile distance obtained from the scan, the angle between the sensor and the surface, and the advance between consecutive scans in the 3D space, respectively.



 §.§ Action Space


The action space is defined by the increments in the position and tilt angle of the inspection sensor. These increments are defined relative to the sensor's own coordinate system. Mathematically, the action space is represented by equation <ref>.


    A = {Δ y, Δ z, Δθ}


Where Δ y refers to the increment in position in the sensor's forward direction (Y), which will be previously defined by a unit vector indicating the scanning direction. Δ z refers to the increment in position in the sensor's vertical direction (Z), controlling the height of the end-effector relative to the part. Δθ denotes the change in the sensor's pitch orientation, which is the rotation around the X-axis. This is represented in Figure <ref>.





The action space is defined as continuous, meaning that actions span a continuous range of values rather than discrete ones. This approach ensures smooth and controlled sensor movements to avoid abrupt changes that could affect measurement accuracy or cause collisions with the workpiece. Equation <ref> establishes the limits for each type of action in the continuous space. Here, Δ y, Δ z, and Δθ are constrained to values between ±Δ y_max millimeters, ±Δ z_max millimeters, and ±Δθ_max degrees, respectively.


    Δ y ∈ [ - Δ y_max, Δ y_max] 
    Δ z ∈ [ - Δ z_max, Δ z_max] 
    Δθ∈ [ - Δθ_max, Δθ_max]






  §.§.§ Dynamic Action Limitation


To ensure smooth and safe movement of the inspection sensor, the selection of actions is dynamically adjusted based on the environment's observations. This action limitation accelerates the convergence of the reinforcement learning algorithm, enabling the system to learn more efficiently and effectively.

When the sensor is farther from the part surface than the optimal working distance W_d, limits are applied to the sensor's displacement in the Z direction Δ z to bring it closer to the surface in a controlled manner. Conversely, if the sensor is too close, displacements in the negative direction are limited, as per equation <ref>.


    Δ z = clip(Δ z, 0, Δ z_max)    if  (D - W_d) ≥ 0 
    clip(Δ z, -Δ z_max, 0)    if  (D - W_d) < 0


Here, clip(x,a,b) limits the value of x between a and b, ensuring that the actions are within the permitted range, according to equation <ref>.


    clip(x,a,b) = {
        a if x ≤ a
     
        b if x ≥ b
     
        x else.


Similarly, if the sensor's direction angle (α) with respect to the surface normal is positive, indicating excessive tilt, limits are applied to the angular displacement Δθ to correct the sensor's orientation. Conversely, if the tilt angle is negative, limits are applied to the angular displacement in the opposite direction to keep the sensor properly aligned with the inspected surface. This is represented in equation <ref>.


    Δθ = clip(Δθ, 0, Δθ_max)    if α≥ 0 
    clip(Δθ, -Δθ_max, 0)    if α < 0




 §.§ Reward Function


In reinforcement learning, creating an effective reward model is crucial as it guides the agent toward desirable behaviors within the environment. This model assigns a value to each state-action pair, reflecting the immediate benefit or cost associated with the agent's decision. This section details the reward strategy designed in this research.

The proposed reward function R(s,a) consists of three distinct components, each capturing different aspects of the inspection process. Mathematically, this function is expressed as shown in equation <ref>.


    R(s,a)=w_d R_D+ w_α R_α+ w_Δ s R_Δ s


R_D represents the reward related to the distance between the sensor and the inspected surface, R_α denotes the reward related to the alignment of the sensor's orientation with the normal of the inspected object's surface, and R_Δ s captures the reward associated with the sensor's movement between consecutive scans in the 3D space corresponding to the point cloud of the inspected piece. w_d, w_α, w_Δ s represent the weights that each component contributes to the overall reward function.

The proposed rewards are in the range [0, -1], as the reward function aims to incentivize the agent to perform actions that improve the inspection process. The maximum value of 0 is assigned when the optimal goal is reached, while negative values indicate penalties for deviations from the desired behavior.




  §.§.§ Distance Reward (R_D)


To ensure that the sensor maintains an optimal distance from the inspected surface, a distance reward function R_D is defined as a continuous penalty function that decreases as the absolute difference between the observed distance and the optimal working distance W_d increases. The reward function is formulated as follows:


    R_D = - (W_d - D)^2/(Z_r/2)^2


Where W_d represents the optimal working distance, D the observed distance during scanning, and Z_r the specified working range of the sensor. This results in a parabolic function with values between [-1,0], corresponding to 0 when operating at the optimal working distance and -1 at the sensor's range limits, as shown in Figure <ref>. If the distance is outside this range, the penalty is maximum (-1).







  §.§.§ Orientation Reward (R_α)


To induce the agent to align its orientation with the surface normal, we introduce an orientation reward model (R_alpha). This model is designed to minimize the angular disparity between the sensor direction and the surface normal vector. The function is defined as a continuous penalty function that approaches 0 as the absolute orientation difference decreases, see Figure <ref>:


    R_α = max(-1, - α^2/α_max^2)


Where α is the angular difference between the sensor's orientation and the surface normal, and α_max is the maximum allowed angular disparity threshold. This model encourages the agent to maintain close alignment with the surface normal, optimizing the quality of the inspection.






  §.§.§ Movement Reward (R_Δ s)


In addition to optimizing the distance and orientation of the sensor, ensuring smooth forward movement is crucial for comprehensive surface coverage. Forward scanning movement ensures that each scanned 3D profile extends beyond the previous one, facilitating thorough inspection. The reward function R_Δ s is expressed as:


    R_Δ s = max(-1, - (Δ s - Δ s_opt)^2/Δ s_opt^2)


This function penalizes the agent when the scanning spacing Δ s is negative, indicating backward movement within the inspection area. Likewise, it behaves parabolically with respect to the scanning spacing Δ s. When the spacing is equal to twice the optimal value Δ s_opt, the reward reaches its minimum value of -1. This indicates a strong penalty for excessively large spacings. As the spacing decreases from this point, the reward gradually increases, reaching a maximum value of 0 when the spacing is exactly equal to the optimal value. Therefore, the reward function motivates the agent to maintain spacing close to the optimal, as both above and below-optimal values result in a decrease in reward, see Figure <ref>.






 §.§ RL Algorithm: PPO


The Proximal Policy Optimization (PPO) algorithm <cit.> is a policy gradient technique designed to provide faster and more efficient updates than previously developed reinforcement learning algorithms, such as Advantage Actor-Critic (A2C) or Deterministic Policy Gradient (DPG).

PPO was designed as an improvement over Trust Region Policy Optimization (TRPO). It simplifies and accelerates the training process by using first-order gradients and a clipped objective function that stabilizes policy updates.

PPO employs a clipped surrogate loss function, penalizing excessive changes in the policy, stabilizing training, and preventing significant divergence between new and old policies. The clipped objective function of PPO is defined by equation <ref>.


    J_clip(π) = 𝔼[ min( r(π) Â, clip( r(π), 1 - ϵ, 1 + ϵ) Â) ]


Here, π represents the policy, 𝔼 denotes the expectation over time, r is the ratio of probability under the new and old policies, respectively, Â is the estimated advantage, and ϵ is a hyperparameter controlling how much the new policies are allowed to differ from the old policies during the optimization process. It is used to compute a penalty function that limits the policy change at each optimization iteration. The probability ratio r(π) is calculated as the probability of selecting an action under the new policy divided by the probability of selecting the same action under the old policy, i.e.:


    r(π) = π_new(a_t|s_t)/π_old(a_t|s_t)



For the PPO algorithm, the hyperparameter configuration involves several key aspects. The neural network architecture defines the structure of the neural network, including the number of hidden layers and units per layer. An activation function, typically ReLU, is applied to the outputs of each hidden layer to introduce non-linearities into the model. The learning rate determines the size of the step taken during weight updates, influencing the speed and stability of learning. Additionally, the clip ratio limits policy changes between updates to ensure training stability. The epoch parameter denotes the number of complete passes through the training dataset during training. 



§ RESULTS


In this section, we present the experiments conducted to validate and evaluate the proposed methods in the context of generating inspection trajectories using reinforcement learning (RL) algorithms. These algorithms were implemented using the open-source library stable-baselines3 <cit.>, which provides enhanced implementations of reinforcement learning algorithms based on OpenAI. To analyze and process the obtained results, we used MATLAB 2023b.

First, we detail the training process of the RL model, including the architecture of the neural network used, the configured hyperparameters, and the training methodology employed. 

Subsequently, the trained RL model is employed to generate inspection trajectories for two different parts using their CAD models: a car door and the body of a Parrot drone, see figure <ref>. Due to its dimensions, the car door will be scanned using a Boustrophedon trajectory base, whereas the drone will be scanned with a single straight-line scan. 

The trained RL model takes the CAD model of the part as input and produces a sequence of movements that the sensor must follow to efficiently scan its surface. This trajectory is designed to minimize the error between the actual distance of the sensor to the part and the sensor's optimal working distance, as well as the direction angle. Additionally, it ensures a constant separation between scan profiles, guaranteeing uniform and precise coverage of the entire surface.



We perform different experiments, both in simulation and in a real environment. Simulation results obtained during the execution of the trajectories in our inspection simulator are presented and analyze. The results of the scans generated by RL optimized trajectories are compared with conventional methods such as straight line trajectories or Boustrophedon type trajectories. 

Finally, an experiment is conducted using a UR3e robot in a real-world environment to execute the inspection trajectory generated offline by the trained RL model for the Parrot drone. We analyze the results obtained to validate the transferability of the solution from the simulated environment to practical real-world applications.



 §.§ Training Process


The training process of the RL model for trajectory optimization in robotic inspection was developed using a detailed simulation environment, the characteristics of which are explained in <cit.>. In this context, a profilometric sensor was simulated with the same specifications as the Automation Technology model AT-C5-4090CS30-495, whose main parameters are detailed in Table <ref>. It is important to note that this setup is designed to generalize based on input parameters, allowing for adjustments to different working distance, for example.



The design of the training piece was aimed at representing a wide variety of conditions that could be encountered in real inspection applications. This piece, created in 3D modeling software, features changes in orientation, height variations, and flat surfaces. Its dimensions are 1050x150x50mm, as shown in Figure <ref>.



During training, each episode is defined so that it corresponds to a starting point and an ending point, determined by the scanning direction and the piece's dimensions, visually represented in the same figure showing the CAD model of the piece used for training, as illustrated in Figure <ref>.

In the experiments, the action space is continuous, meaning actions are expressed as values within a continuous range rather than discrete values. Specifically, these actions are limited within the interval of [-1, 1], where position increments are measured in millimeters and pitch angles in degrees.

Table <ref> summarizes the hyperparameters used for the PPO algorithm. These parameters were selected based on default values recommended by the authors of the stable-baselines3 library. The neural network architecture consists of two hidden layers with 64 units each and ReLU activation function. A learning rate of 0.0003 was employed, with updates performed every 2048 steps and a batch size of 64. The discount factor (γ) was set to 0.99, and a clip ratio of 0.2 was used to stabilize training by limiting policy updates. Training proceeded over 10 epochs to refine the policy effectively. These hyperparameters were chosen to balance exploration and exploitation, ensuring robust and efficient learning within the RL framework.



During the training process of the algorithm, various metrics are employed to assess their performance and convergence capability. These metrics include the reward per episode, the length of episodes, and the number of episodes required to reach a certain performance level. The reward per episode is a crucial metric indicating the total amount of reward accumulated by the agent in each training episode. Generally, a higher reward reflects better performance of the agent in the task.

However, in this specific training context, evaluating solely the accumulated reward per episode might not be the most appropriate approach. This is because the length of episodes can vary significantly depending on the step size, defined as the distance between profiles. Therefore, instead of focusing solely on the accumulated reward, it is preferred to evaluate the globally normalized reward by the length of the episode. This metric provides a more comprehensive assessment of the agent's performance, as it considers both the accuracy of measurements and the efficiency of the trajectory. By doing so, a more precise insight into the overall effectiveness of the model in trajectory optimization and inspection quality is obtained, regardless of the specific length of episodes.

Additionally, the total number of episodes required for the agent to reach a certain performance level or convergence is analyzed. This number gives us an idea of how quickly the RL algorithm can learn and improve its performance in the given task.



 



In this experiment, the weight of each partial reward has been set so that they have the same influence on the overall reward, that is, w_D = w_α = w_Δ s = 1/3.



 







 §.§ Car door


The first piece used to evaluate the model was a car door with dimensions 1440x1060x190mm. The initial trajectory, serving as a base for the optimization, is depicted in Figure <ref>. In this figure, the initial Boustrophedon-type trajectory is observed, with inspection passes marked in red and movements between passes in white.

The reinforcement learning model is applied throughout the different passes, dynamically adjusting the orientation and distance of the profilometric sensor. Each pass of the Boustrophedon scan is optimized to maintain an appropriate pose between the sensor and the door surface, ensuring precise and uniform data capture. The model is exclusively applied to the red passes, where inspection is performed, optimizing the sensor's orientation and distance in each of these passes. The white movements represent transitions between passes where the sensor is not scanning and, therefore, not optimized.






Here are the results obtained when applying the trained model to the trajectory defined in Figure <ref>. The definition of the start and end points of each pass is manually performed using the simulator interface.

Figure <ref> (a) displays the point cloud obtained during the scanning using the profilometric sensor. The point cloud is depicted with a color map indicating the error in the measured distance for each point. This error is defined as the difference between the optimal working distance of the sensor and the actual distance obtained during the measurement at each point.

The aim of the analysis is to evaluate the accuracy and consistency of the profilometric sensor under real scanning conditions. The use of the color map facilitates the visualization of areas with higher and lower errors, providing a clear insight into the spatial distribution of the error across the scanned surface.

To provide a meaningful comparison, a scan was also carried out using a more traditional method. In this approach, the scanning was performed following the boustrophedon trajectory with a static configuration of height and orientation, without dynamic adjustments. This method is commonly employed in industrial applications due to its simplicity and ease of implementation. Figure <ref> (b) shows the representation of the distance error map in a traditional scan.

In addition to the distance error map, an orientation error map was generated, which displays angular deviations from the optimal sensor orientation at each point. This deviation refers to the direction angle of the sensor on the surface. See Figure <ref>.













 §.§ Parrot drone




The results obtained from the optimization of the inspection trajectory of the Parrot drone are presented below. Figure <ref> shows an image of the drone's body and its CAD model. The dimensions of the part are 350x95x65mm.



A scan of one side of the drone body will be performed. Due to its dimensions, the base trajectory used will be a simple straight line from the beginning of the part to the end, see figure <ref>.



Using the trained reinforcement learning (RL) model, the optimized scanning trajectory for the drone is generated. 















 §.§ Pen Holder






  §.§.§ Real-world Validation Experiment


By leveraging the proposed simulation framework to generate and validate the scanning trajectories from the CAD model, the experiment aims to transition these optimized paths effectively to the real-world setup. This process ensures that the developed methods can be reliably applied to the actual drone body, achieving high precision and efficiency in industrial inspection scenarios.

The results obtained after executing the generated trajectory for scanning the Parrot drone in a real environment are presented below. The scanning system is composed of a 6 dof UR3e robotic arm equipped with a triangulation laser profilometer model AT-C5-4090CS30-495. The complete configuration of the inspection system, which includes the UR3e robotic arm, the profilometric sensor, and the drone body, can be seen in Figure 








We begin with a trajectory composed by sensor poses. Using the RoboDK framework [ref]
, we generate the motion program for the UR3e robot to follow the optimized path. This program is then transferred to the robot's controller, configured to execute the motion commands. As a result, the UR3e robotic arm tracks the planned trajectory to scan the Parrot drone, faithfully replicating the precise movements defined during simulation.







§ DISCUSSION




In the real-world validation tests of the scanning trajectories, we used a smaller piece that required only a single linear scanning path, eliminating the need for a boustrophedon pattern. This choice was dictated by the physical limitations of the UR3e robotic arm, which has a relatively restricted reach. Given the limited range of the UR3e, it was not feasible to scan larger surfaces that would necessitate multiple passes. Consequently, we opted for a smaller and more suitable piece that allowed for effective validation within the operational capabilities of the available equipment. This decision ensures that, although the UR3e cannot cover extensive surfaces, the methodology and trajectory optimization principles we developed are applicable and verifiable in a controlled and representative environment.

Despite its smaller size, the real piece chosen for the tests has sufficient geometric diversity to validate our proposed methodology. Its varied surface features ensured that the trajectory optimization and scanning techniques could be robustly tested. If we were to work with a larger piece, a boustrophedon scanning pattern would simply be employed. In such cases, our reinforcement learning model would be applied to each pass within the boustrophedon path, just as it was used in the simulated experiment with the door. This approach ensures that our methods are versatile and can be adapted to both small and large-scale scanning tasks.

The scanning trajectory generated by our approach is designed to be highly versatile and adaptable to any robotic system with sufficient reach. This versatility arises from the fact that the trajectory increments in both position and orientation are small and precise enough to be executed by any industrial robot. When these incremental commands are input into the robot’s control software, it calculates the necessary joint movements to achieve the specified end-effector positions.

This adaptability is a key feature of our method. By focusing on end-effector coordinates, our system avoids the need for specific kinematic adjustments for different robots, making the trajectory compatible with a wide range of robotic platforms. Whether the robot has a simple or complex joint configuration, the internal control software translates the end-effector trajectory into joint movements effectively.

This approach not only simplifies the integration process with various robots but also ensures that the trajectory optimization remains effective regardless of the robot used. This makes our system particularly beneficial in diverse industrial settings where different robots may be utilized for various tasks. Thus, our trajectory generation and optimization method provides a flexible solution, generalizing across multiple types of robotic systems and enhancing the applicability of automated inspection processes.





§ CONCLUSIONS


In this paper, we have presented a method for generating inspection trajectories using laser profilometric sensors using Reinforcement Learning (RL) techniques. Our goal was to optimize the scanning process by dynamically adjusting the sensor's position and tilt to maintain an optimal pose relative to the surface of the part being inspected. 

We employed a simulated environment that replicates the conditions of a real system, as developed in our previous work. In our method, the state space was defined using the position and orientation of the sensor, that normally is the robot's end effector, which allows for the approach to be generalized and easily transferred to different robotic configurations. We also included additional parameters in the state space, such as the mean profile distance, the direction angle, and the spacing between consecutive scans, providing a comprehensive understanding of the inspection process.

The action space was designed to include relative increments in the sensor’s position and tilt angle, allowing for precise adjustments and smooth sensor movements. Our reward function was design by incorporating three key components: the distance between the sensor and the surface, the alignment of the sensor with the surface normal, and the spacing between consecutive scans. This detailed reward function encourages optimal behavior in terms of maintaining appropriate distance, correct orientation, and efficient sensor progression across the surface.

Through our experiments in a simulated environment, we validated the capability of the RL-trained model to adapt to different parts and maintain optimal scanning trajectories not seen during the training phase. Furthermore, we tested the method in a real-world scenario using a UR3e robotic arm. The optimized trajectory, generated offline from a CAD model, was executed successfully, demonstrating that our method can produce high-quality, precise inspection trajectories that ensure effective surface coverage.

IEEEtran



[
    < g r a p h i c s >
]Sara Roos-Hoefgeest
	Sara Roos Hoefgeest is currently a PhD student at University of Oviedo, in Escuela Politécnica de Ingeniería of Gijón, Spain. She is working at the Department of Electrical, Electronics, Communications, and Systems Engineering. Her research work is focused on the area of Robotics, 3D Computer Vision and Visual Inspection of Manufactured Products. She holds a Bachelor's Degree in Electronic and Automation Engineering and a Master's Degree in Automation and Industrial Computing both from the University of Oviedo.


[
    < g r a p h i c s >
]Mario Roos-Hoefgeest
	Mario Roos Hoefgeest Toribio is currently a PhD student at University of Oviedo, in Escuela Politécnica de Ingeniería of Gijón, Spain. He is working at CIN Systems, applying the results of the research in production lines in industrial facilities. His research work is focused on the area of 3D Computer Vision and Visual Inspection for defects detection in production lines. He holds a Bachelor's Degree in Electronic and Automation Engineering and a Master's Degree in Automation and Industrial Computing both from the University of Oviedo.


[
    < g r a p h i c s >
]Ignacio Alvarez
	Ignacio Alvarez is PhD in Industrial Engineering at University of Oviedo (1997), and Associate Professor at the Electrical, Electronics and Automatic Control Department of the same University (1999). His research work is focused in 3D automatic inspection systems for dimensional control and defects detection in production lines, using several technologies (laser triangulation, stereo vision, conoscopic holography). He has participated in more than 20 public financed projects, more than 30 private financed projects, and published more than 15 papers in scientific journals; several prototypes developed or inspired by the author are in production in industrial facilities.


[
    < g r a p h i c s >
]Rafael C. González
	Rafael Corsino González de los Reyes was born in Gijón, Spain, in 1968. He received an Engineering degree and a Ph.D. degree in Electronics and Automation from the Escuela Superior de Ingenieros Industriales de Gijón, University of Oviedo, Spain, in 1993 and 1999, respectively. He is currently an Associate Professor in the Department of Electrical, Electronics, Communications, and Systems Engineering, Universidad de Oviedo. He is a member of IEEE and his research interests include 3D Computer Vision, Robotic Path Planning and Visual Inspection of Manufactured Products.




