This paper proposed a modified bacterial foraging optimization (MBFO) algorithm to solve the problem in decentralized control of sensors-based swarm robots. The MFBO algorithm is used to solve the problems of target detecting and trapping for swarm robots. In the beginning, local coordinate system is established by initial position and the target area of the swarm robots. Then the target area is divided into Voronoi cells. After the initialization, swarm robots can complete the target detection and trapping missions automatically by the proposed MBFO algorithm with the leading of concentration gradient in the target area. Compared with other common used methods of swarm robots' distributed control, the main characteristics of the MBFO are that the robots' movements depend on the concentration in the area and the reactions between the robots based on the sensor range. Simulation results demonstrated the effectiveness and robustness of the of the MBFO algorithm.