Im quite new to ROS2 humble, so any advice would be appreciated. I am trying to build a socket inside the ROS2 node, so that I can receive some commands from other user. However, I get some build error when I try to build the package. The build error is mainly about:
AttributeError: module 'socket' has no attribute '_GLOBAL_DEFAULT_TIMEOUT'
The code I'm trying to execute is the following:
#!/usr/bin/env python3 # coding=utf-8 import socket import rclpy from std_msgs.msg import Char from std_msgs.msg import Float64 class MySocket: def __init__(self, sock=None): if sock is None: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: self.sock = sock self.host = "192.168.1.125" self.port = 8 self.MSGLEN = 1024 def connect(self): self.sock.connect((self.host, self.port)) def mysend(self, msg): if len(msg) != 1: raise ValueError("Accepting only the valid commands") totalsent = 0 while totalsent < 1: sent = self.sock.send(msg.encode()) if sent == 0: raise RuntimeError("Socket connection broken") totalsent += sent def distSend(self, msg): if not isinstance(msg, (int, float)): raise ValueError("Accepting only valid numeric values") msg_str = str(msg) totalsent = 0 while totalsent < len(msg_str): sent = self.sock.send(msg_str[totalsent:].encode()) if sent == 0: raise RuntimeError("Socket connection broken") totalsent += sent def myreceive(self): received_char = self.sock.recv(1).decode() if not received_char: raise RuntimeError("Socket connection broken") return received_char global toMaster # command global toMasterDist # current distance from the wall def callback(msg: Char): global toMaster toMaster = msg.data def dist_callback(msg: Float64): global toMasterDist toMasterDist = msg.data def main(): rclpy.init() node = rclpy.create_node('socket') pub = node.create_publisher(Char, 'from_master', 10) sub = node.create_subscription(Char, 'to_master', callback, 10) dist_sub = node.create_subscription(Float64, 'to_master_dist', dist_callback, 10) s = MySocket() s.connect() fm = Char() while 1: cmd = s.myreceive() if cmd == 'M': fm.data = 109 pub.publish(fm) while toMaster != 'A': pass if toMaster == 'A': s.mysend('A') toMaster = '0' elif cmd == 'D': fm.data = 100 pub.publish(fm) while toMaster != 'A': pass if toMaster == 'A': s.distSend(toMasterDist) toMaster = '0' elif cmd == 'F': fm.data = 102 pub.publish(fm) while toMaster != 'A': pass if toMaster == 'A': s.mysend('A') toMaster = '0' rclpy.shutdown()
Are there drivers for TCP/IP to communicate with external environment outside of ROS2? Thanks in advance