From fd00e5f4819a9aa631ce2189fb51707967182b05 Mon Sep 17 00:00:00 2001 From: Sandeep Menon Date: Fri, 4 Mar 2022 22:30:54 +0530 Subject: [PATCH 1/5] added argparse interface --- README.org | 2 +- pyteapot.py | 50 +++++++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/README.org b/README.org index 45e2585..ff3766f 100644 --- a/README.org +++ b/README.org @@ -47,7 +47,7 @@ Each of these must be on separate lines (or should have a '\n' at the end of the * TODO Todos - [x] Receive data over WiFi instead of serial. - **Done!** -- [ ] Add a nice [[https://docs.python.org/3/library/argparse.html][ ~argparse~ ]] interface instead of requiring the user to change variables in the script. Include example usage in docstring (a la [[https://tldr.sh/][tldr]] or [[http://bropages.org][bro]]) +- [x] Add a nice [[https://docs.python.org/3/library/argparse.html][ ~argparse~ ]] interface instead of requiring the user to change variables in the script. Include example usage in docstring (a la [[https://tldr.sh/][tldr]] or [[http://bropages.org][bro]]) - [ ] Add some keyboard support, eg. pausing / resuming the visualization with spacebar etc - [ ] Add optional support for x, y, z too. Also, multiple simultaneous viewports (eg. to compare with ground truth from MoCap) - [ ] Read from text file instead of serial or UDP diff --git a/pyteapot.py b/pyteapot.py index d13ae5f..a866ea5 100644 --- a/pyteapot.py +++ b/pyteapot.py @@ -8,23 +8,10 @@ from OpenGL.GL import * from OpenGL.GLU import * from pygame.locals import * +import argparse -useSerial = False # set true for using serial for data transmission, false for wifi -useQuat = False # set true for using quaternions, false for using y,p,r angles -if(useSerial): - import serial - ser = serial.Serial('/dev/ttyUSB0', 38400) -else: - import socket - - UDP_IP = "0.0.0.0" - UDP_PORT = 5005 - sock = socket.socket(socket.AF_INET, # Internet - socket.SOCK_DGRAM) # UDP - sock.bind((UDP_IP, UDP_PORT)) - -def main(): +def main(useSerial, useQuat, ser, sock): video_flags = OPENGL | DOUBLEBUF pygame.init() screen = pygame.display.set_mode((640, 480), video_flags) @@ -38,9 +25,9 @@ def main(): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): break if(useQuat): - [w, nx, ny, nz] = read_data() + [w, nx, ny, nz] = read_data(ser, sock) else: - [yaw, pitch, roll] = read_data() + [yaw, pitch, roll] = read_data(ser, sock) if(useQuat): draw(w, nx, ny, nz) else: @@ -95,7 +82,7 @@ def cleanSerialBegin(): pass -def read_data(): +def read_data(ser, sock): if(useSerial): ser.reset_input_buffer() cleanSerialBegin() @@ -200,4 +187,29 @@ def quat_to_ypr(q): if __name__ == '__main__': - main() + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--useSerial', type=bool, default=False, help='set true for using serial for data transmission, false for wifi') + parser.add_argument('--useQuat', type=bool, default=False, help='set true for using quaternions, false for using y,p,r angles') + parser.add_argument('--port', type=str, default='/dev/ttyUSB0', help='serial port') + parser.add_argument('--udp_id', type=str, default="0.0.0.0") + parser.add_argument('--udp_port', type=int, default=5005) + args = parser.parse_args() + + useSerial = args.useSerial + useQuat = args.useQuat + + if(useSerial): + import serial + ser = serial.Serial(args.port, 38400) + else: + import socket + + UDP_IP = args.udp_id + UDP_PORT = args.udp_port + sock = socket.socket(socket.AF_INET, # Internet + socket.SOCK_DGRAM) # UDP + sock.bind((UDP_IP, UDP_PORT)) + + main(useSerial, useQuat, ser, sock) + From e6139cd69e04f5ced6a3acafbbaf5d8b509406b1 Mon Sep 17 00:00:00 2001 From: Sandeep Menon Date: Fri, 4 Mar 2022 22:44:32 +0530 Subject: [PATCH 2/5] example usage in docstring --- README.org | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.org b/README.org index ff3766f..29400e7 100644 --- a/README.org +++ b/README.org @@ -20,6 +20,15 @@ Most of the code is self-explanatory. However some modifications might be requir - Set =useQuat= to =True= if receiving *quaternions* over serial or WiFi and =False= if receiving *Euler angles*. - If receiving quaternions over serial or wifi, the declination at the particular location should to be updated in =quat_to_ypr(q)= function to get correct yaw angles printed *on screen*. (The cube rotation is not dependent on this and will still work fine otherwise) +Examples: +``` +# using serial stream and quaternion representation +python pyteapot.py --useSerial True --useQuat True --port /dev/ttyUSB0 + +# using udp stream and euler representation +python pyteapot.py --useSerial False --useQuat False --udp_id 0.0.0.0 --udp_port 5005 +``` + * String passed over Serial or Wifi To use this module, the data received over serial or udp port should be in the format specified below: - First quaternion value should be between two =w= s From b329137460ef8e24b87cb9f10bfc77fde4d19d8a Mon Sep 17 00:00:00 2001 From: Sandeep Menon Date: Fri, 4 Mar 2022 22:46:24 +0530 Subject: [PATCH 3/5] using #+END_EXAMPLE for highlights --- README.org | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.org b/README.org index 29400e7..9dd3b0d 100644 --- a/README.org +++ b/README.org @@ -21,13 +21,14 @@ Most of the code is self-explanatory. However some modifications might be requir - If receiving quaternions over serial or wifi, the declination at the particular location should to be updated in =quat_to_ypr(q)= function to get correct yaw angles printed *on screen*. (The cube rotation is not dependent on this and will still work fine otherwise) Examples: -``` +#+BEGIN_EXAMPLE # using serial stream and quaternion representation python pyteapot.py --useSerial True --useQuat True --port /dev/ttyUSB0 # using udp stream and euler representation python pyteapot.py --useSerial False --useQuat False --udp_id 0.0.0.0 --udp_port 5005 -``` +#+END_EXAMPLE + * String passed over Serial or Wifi To use this module, the data received over serial or udp port should be in the format specified below: From 76c674d4eb8977ccfe393eb0850867f35d3a6158 Mon Sep 17 00:00:00 2001 From: Sandeep Menon Date: Fri, 4 Mar 2022 23:01:23 +0530 Subject: [PATCH 4/5] passing use serial and quaternion to draw and read_data --- pyteapot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pyteapot.py b/pyteapot.py index a866ea5..d182e0a 100644 --- a/pyteapot.py +++ b/pyteapot.py @@ -25,13 +25,13 @@ def main(useSerial, useQuat, ser, sock): if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): break if(useQuat): - [w, nx, ny, nz] = read_data(ser, sock) + [w, nx, ny, nz] = read_data(ser, sock, useSerial, useQuat) else: - [yaw, pitch, roll] = read_data(ser, sock) + [yaw, pitch, roll] = read_data(ser, sock, useSerial, useQuat) if(useQuat): - draw(w, nx, ny, nz) + draw(w, nx, ny, nz, useQuat) else: - draw(1, yaw, pitch, roll) + draw(1, yaw, pitch, roll, useQuat) pygame.display.flip() frames += 1 print("fps: %d" % ((frames*1000)/(pygame.time.get_ticks()-ticks))) @@ -82,7 +82,7 @@ def cleanSerialBegin(): pass -def read_data(ser, sock): +def read_data(ser, sock, useSerial, useQuat): if(useSerial): ser.reset_input_buffer() cleanSerialBegin() @@ -107,7 +107,7 @@ def read_data(ser, sock): return [yaw, pitch, roll] -def draw(w, nx, ny, nz): +def draw(w, nx, ny, nz, useQuat): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glTranslatef(0, 0.0, -7.0) From 42a9b9bd392bb077bb6b4fb42d49832d75ff4360 Mon Sep 17 00:00:00 2001 From: Sandeep Menon Date: Fri, 4 Mar 2022 23:02:51 +0530 Subject: [PATCH 5/5] useQuat in cleanSerialBegin --- pyteapot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyteapot.py b/pyteapot.py index d182e0a..007df07 100644 --- a/pyteapot.py +++ b/pyteapot.py @@ -62,7 +62,7 @@ def init(): glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) -def cleanSerialBegin(): +def cleanSerialBegin(useQuat): if(useQuat): try: line = ser.readline().decode('UTF-8').replace('\n', '') @@ -85,7 +85,7 @@ def cleanSerialBegin(): def read_data(ser, sock, useSerial, useQuat): if(useSerial): ser.reset_input_buffer() - cleanSerialBegin() + cleanSerialBegin(useQuat) line = ser.readline().decode('UTF-8').replace('\n', '') print(line) else: