This paper presents a real-time cooperative control algorithm for multi-robot systems to perform formation maintenance and navigate through the environment while avoiding collision with static obstacles and among different group formations of robots. The control algorithm is computed by solving a quadratic program and utilizing the control barrier function to efficiently incorporate multiple control objectives in a unified manner and provide a safety certificate. In addition, a novel method for collision avoidance among different formations of robots is presented which only requires a representative robot in each formation to communicate with each other and thus reduces the required communication between the robots. The cooperative control algorithm is verified using a robotic experimental testbed.