In order to make the railroad inspection robot better adapt to its complex working environment, it is especially important to study the robot object avoidance algorithm. The WOA algorithm has simple and understandable structure and strong optimization ability but is prone to local convergence. IWOA-PSO is used for railway inspection robots. The performance of IWOA-PSO in the experimental results is better than that of WOA and PSO, and the average accuracy and standard deviation of the IWOA-PSO can better reach the theoretical optimal value in the function tests, and it has performance close to the theoretical value. In the simple environment object avoidance route planning, the minimum path length of IWOA-PSO is 850 mm, which is 53.6% less than that of the PSO algorithm, and the search time is 13.12 seconds, which is 5.11 seconds less than that of PSO algorithm; in the ordinary environment object avoidance route planning, the minimum path length of IWOA-PSO is 830 mm, while the path length of PSO algorithm is 1339 mm, the former is 38% less than the latter, and the search time of IWOA-PSO is 14.05 seconds less than PSO algorithm, so the method has better effect on object avoidance.