From 73c6e10a11b3f325e630f05007be708d43999660 Mon Sep 17 00:00:00 2001 From: Felix Agner <felix.agner@control.lth.se> Date: Wed, 9 Nov 2022 11:12:03 +0100 Subject: [PATCH] Removed newline part of return strings from server side. Also added max-speed function --- src/omnibot/tcp.py | 18 ++++++++++- test/testserver.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/testserver.py diff --git a/src/omnibot/tcp.py b/src/omnibot/tcp.py index 986ebd0..e35a8c7 100644 --- a/src/omnibot/tcp.py +++ b/src/omnibot/tcp.py @@ -52,7 +52,8 @@ class Connection(object): # Receive confirmation ret=self.sock.recv(1024).decode('utf-8') - return ret + # Remove the end of the line to account for newline, which is required for julia socket implementation. + return ret[:-1] def set_speed(self,i,v): """Set the speed of servo i (int) to v (int). Return a string with status of the operation from the omnibot. @@ -147,3 +148,18 @@ class Connection(object): theta = self.get_theta() return [x,y,theta] + + def get_max_speed(self): + """"Get maximum speed-setpoint for servos.""" + + if self.verbose: + print("Requesting theta") + + ret = self._send_and_receive("rmaxspeed") + if ret[0] == "e": + raise Exception(ret) + + if self.verbose: + print("max speed: "+ret) + + return int(ret) diff --git a/test/testserver.py b/test/testserver.py new file mode 100644 index 0000000..ebe75b4 --- /dev/null +++ b/test/testserver.py @@ -0,0 +1,74 @@ +""" +A python script that can run some tests on the client which mimics the behavior of the real system. +""" + +from time import sleep +import logging + +# TCP connection +import socket + +from random import random + + +def run_server(HOST,PORT=9998): + + + # Create socket s + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((HOST, PORT)) # Bind the socket to specified port and ip + s.listen(1) # Start listening for connections + + # Loop "while true" to keep receiving new connections until the application closes. + while True: + logging.info("Waiting for connection") + conn, addr = s.accept() # Wait for connection + logging.info("Connection received from"+str(addr)) + # Connection received. Closes automatically once the session ends, and then waits for a new connection. + with conn: + # Log info about the connection + + + while True: + + # Receive a message + data = conn.recv(1024) + + # If the message is empty, close the connection. + if not data: + logging.debug("Empty input, stopping connection") + break + + try: + inp = data.decode('utf-8') + logging.debug(f"Received {inp}") + command_type = inp[0] + + if command_type == 'w': + # Handle writing speed to servos + message = "Operation " + inp + " successful.\n" + conn.sendall(message.encode('utf-8')) + elif command_type == 'r': + # Handle reading from the crazyflie + message = str(round(random(),5))+"\n" + conn.sendall(message.encode('utf-8')) + except Exception as e: + # If an exception is encountered, send it to the client. + logging.exception(f"Exception encountered: {e}") + message = "e:"+str(e) + conn.sendall(message.encode('utf-8')) + + + +if __name__ == "__main__": + + # Set IP (HOST) and port number (PORT) + # "" means listening to any available IP + HOST, PORT = "", 9998 + + # Define logging format + format = "%(asctime)s: %(message)s" + logging.basicConfig(format=format, level=logging.DEBUG,datefmt="%H:%M:%S") + + run_server(HOST,PORT=PORT) + \ No newline at end of file -- GitLab