Skip to content

added argparse interface #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ 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:
#+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:
- First quaternion value should be between two =w= s
Expand Down Expand Up @@ -47,7 +57,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
Expand Down
60 changes: 36 additions & 24 deletions pyteapot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -38,13 +25,13 @@ 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, useSerial, useQuat)
else:
[yaw, pitch, roll] = read_data()
[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)))
Expand Down Expand Up @@ -75,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', '')
Expand All @@ -95,10 +82,10 @@ def cleanSerialBegin():
pass


def read_data():
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:
Expand All @@ -120,7 +107,7 @@ def read_data():
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)
Expand Down Expand Up @@ -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)