From db3e02077c4d2318436551678ff029dde0321538 Mon Sep 17 00:00:00 2001
From: Felix Agner <felix.agner@control.lth.se>
Date: Tue, 1 Nov 2022 13:55:58 +0100
Subject: [PATCH] some polish...

---
 README.md          |  4 +--
 src/omnibot/tcp.py | 68 ++++++++++++++++++++--------------------------
 2 files changed, 31 insertions(+), 41 deletions(-)

diff --git a/README.md b/README.md
index e0b3866..eedb94e 100644
--- a/README.md
+++ b/README.md
@@ -40,8 +40,8 @@ with Connection(HOST) as bot:
             bot.set_speeds([vset,vset,vset])
             
             # print position
-            print('x:'+bot.get_x())
-            print('y:'+bot.get_y())
+            print('x:'+str(bot.get_x()))
+            print('y:'+str(bot.get_y()))
 
             sleep(0.1)
 ```
diff --git a/src/omnibot/tcp.py b/src/omnibot/tcp.py
index d5e4742..986ebd0 100644
--- a/src/omnibot/tcp.py
+++ b/src/omnibot/tcp.py
@@ -1,8 +1,7 @@
 import socket
         
 class Connection(object):
-    """
-    A class that implements a servo-client which is capable of sending servo speed-settings
+    """A class that implements a servo-client which is capable of sending servo speed-settings
     to an omnibot. 
 
     HOST: Host name of server (string)
@@ -16,8 +15,7 @@ class Connection(object):
         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
     def __enter__(self):
-        """
-        Boot up the socket and connect to the omnibot server
+        """Boot up the socket and connect to the omnibot server
         """
         if self.verbose:
             print("Connecting to HOST: " + str(self.HOST) + ", PORT: " + str(self.PORT))
@@ -30,15 +28,25 @@ class Connection(object):
         return self
     
     def __exit__(self, exc_type, exc_value, exc_tb):
-        """
-        Close down the socket after closing the client
+        """Close down the socket after closing the client
         """
         if self.verbose:
             print("Closing down socket")
         self.sock.close()
 
-    def send_and_receive(self,message):
-        """Send a message to the omnibot and return the answer that the omnibot gave."""
+    def _send_and_receive(self,message):
+        """Send a message (string) to the omnibot and return the answer (string) that the omnibot gave.
+
+        Message format (string):
+            "abc"
+            a: w or r, signifying write to servo or read from crazyflie
+            b: if a=w, b is the vector index of the servo to write to. If a=r, b is the type of information to read
+            c: if a=w, c is the speed to be written.
+
+        Examples of message format:
+            w110, "write speed 10 to servo of vector index 1"
+            rx, "read x coordinate"
+        """
         # Send the package                      
         self.sock.sendall(bytes(message,'utf-8'))
         # Receive confirmation
@@ -47,14 +55,8 @@ class Connection(object):
         return ret
 
     def set_speed(self,i,v):
-        """
-        Set the speed of servo i to v
-
-
-        Communicates with messages on the form "wivvvv"
-        where w stands for "write"self.
-        i stands for index of desired servo
-        vvvv stands for requested target speed for the servo
+        """Set the speed of servo i (int) to v (int). Return a string with status of the operation from the omnibot.
+        i can take values 0, 1 or 2, and it should be tested which of these values maps to which servo.
         """
 
         package = 'w'+str(i)+str(v)
@@ -62,30 +64,27 @@ class Connection(object):
             print("Sending " + package)
 
 
-        ret = self.send_and_receive(package)
+        ret = self._send_and_receive(package)
 
         if ret[0] == "e":
             raise Exception(ret)
         elif self.verbose:
             print(ret)
+        return ret
 
     def set_speeds(self,v):
-        """ 
-        Set the speed of the servos to the values in vector v
+        """Set the speed of the servos to the values in vector v (ints).
         """
         for (i,vi) in enumerate(v):
             self.set_speed(i,vi)
 
     def get_x(self):
-        """Get x coordinate
-        
-        Sends a message "rx"
-        where "r" stands for "read" and "x" stands for "x"
+        """Get x coordinate as float
         """
         if self.verbose:
             print("Requesting x")                 
 
-        ret = self.send_and_receive("rx")
+        ret = self._send_and_receive("rx")
         if ret[0] == "e":
             raise Exception(ret)
 
@@ -95,15 +94,12 @@ class Connection(object):
         return float(ret)
 
     def get_y(self):
-        """Get y coordinate
-        
-        Sends a message "ry"
-        where "r" stands for "read" and "y" stands for "y"
+        """Get y coordinate as float
         """
         if self.verbose:
             print("Requesting y")                 
 
-        ret = self.send_and_receive("ry")
+        ret = self._send_and_receive("ry")
         if ret[0] == "e":
             raise Exception(ret)
 
@@ -113,15 +109,12 @@ class Connection(object):
         return float(ret)
         
     def get_z(self):
-        """Get z coordinate
-        
-        Sends a message "rz"
-        where "r" stands for "read" and "z" stands for "z"
+        """Get z coordinate as float
         """
         if self.verbose:
             print("Requesting z")                 
 
-        ret = self.send_and_receive("rz")
+        ret = self._send_and_receive("rz")
         if ret[0] == "e":
             raise Exception(ret)
 
@@ -131,15 +124,12 @@ class Connection(object):
         return float(ret)
 
     def get_theta(self):
-        """Get x coordinate
-        
-        Sends a message "rtheta"
-        where "r" stands for "read" and "theta" stands for "theta"
+        """Get angle theta as float
         """
         if self.verbose:
             print("Requesting theta")                 
 
-        ret = self.send_and_receive("rtheta")
+        ret = self._send_and_receive("rtheta")
         if ret[0] == "e":
             raise Exception(ret)
 
-- 
GitLab