# Optimizing Assembly in Wiring Boxes Using API Technology for Digital Twin
## 4 Proof of Concept for Optimizing Assembly in Wiring Boxes
### Configuring and Integrating the Olfactory Lite 6 Robot
For controlling the robotic arm, we use the xArmAPI to interact with the xArm robotic arm. This API allows the initialization, control, and monitoring of the robotic arm's status. from xarm.wrapper import XArmAPI  
arm = XArmAPI('Robot ID', paud_checkset = False)  
arm.connect()  
arm.motion_enable(enable = True)  
arm.set_mode(0)  
arm.set_state(0)  
In this code, we initialize the connection with the robotic arm, enable motion, set the operating mode, and set the initial state. def sync_movements(self, joint_positions):  
for joint_position in joint_positions:  
if joint_position is not None and all(angle is not None for angle in joint_position):  
self.print(\"Moving to position: {joint_position}\")code = self_arm.set_servo_angle(angle=joint_position, is_radian = False) self_check_code(code,'set_servo_angle') time.sleep(1) else: self.pprint(\"Received invalid joint position: Contains None values\") This function synchronizes the movements of the robotic arm with the specified joint positions, ensuring that the values are valid before sending the command. Onshape is a cloud-based CAD platform that allows access to digital models through APIs. In the digital twin monitor program, we use the Onshape API to monitor the joint positions of the robotic arm and update the digital model. def monitor_robot(self): while self.is_alive: joint_positions = self_arm.get_servo_angle(is_radian = False) self.pprint(f\"Current joint positions: {joint_positions}\") joint_positions_rad = [angle * math.pi/180 for angle in joint_positions[1]] self.update_onshape(joint_positions_rad) time.sleep(0.1) This function continuously monitors the joint positions of the robotic arm and converts them to radians to be sent to Onshape. The next function updates the digital model in Onshape with the new joint positions, ensuring that all six joints are provided before sending the data. def update_onshape(self, joint_positions): try: if len(joint_positions) >= 6: set_mate_values(self.client, joint_positions[0], # Base joint_positions[1], # Shoulder joint_positions[2], # Elbow joint_positions[3], # Wrist joint_positions[4], # Hand joint_positions[5]) # Gripper else: self.pprint(\"Received invalid joint positions: Less than 6 joints\") except Exception as e: self.pprint(f\"Failed to update Onshape: {e}\") The continuous synchronization functionality ensures that changes in the physical environment are reflected in the digital model in real-time. In the digital twin control program, we have a continuous_sync function that handles this synchronization:  
def continuous_sync(client, arm, document_id, workspace_id, element_id, interval = 5): robot_main = RobotMain(arm) last_joint_positions = None while True: assembly_data = get_assembly(client, document_id, workspace_id, element_id) if assembly_data: joint_positions = extract_joint_positions(assembly_data) if joint_positions!= last_joint_positions: print(\"Detected changes in assembly, updating robot...\") robot_main.sync_movements(joint_positions) last_joint_positions = joint_positions else: print(\"No changes detected in assembly.\") else:print(\"Failed to retrieve assembly data\") time.sleep(interval)  
This function continuously monitors the assembly data from Onshape and synchronizes the movements of the robotic arm accordingly. By using the xArm and Onshape APIs, we have created a digital twin system capable of controlling and monitoring the assembly of fuses in wiring boxes. This system ensures precise synchronization between the physical and digital environments, allowing efficient control and real-time monitoring. During the development process, we considered several alternative solutions. One option was to explore different types of robotic arms and grippers with varying degrees of freedom to handle the fuse insertion process. After evaluating these alternatives, we concluded that the selected robotic system provided the best balance of precision, adaptability, and cost-effectiveness.