PyTeapot-Quaternion-Euler-cube-rotation icon indicating copy to clipboard operation
PyTeapot-Quaternion-Euler-cube-rotation copied to clipboard

Visualization of IMU orientation from quaternion or Euler angles with a rotating cube

#+Author: Nishant Elkunchwar

  • Introduction Visualization of orientation of any IMU with the help of a rotating cube as per quaternions or Euler angles (strictly speaking, the [[https://eorg/wiki/Euler_angles#Tait%E2%80%93Bryan_angles][Tait Bryan Angles]] received over either the serial port or WiFi using OpenGL in Python. The MPU-9250 (has on-board accelerometer, magnetometer and gyroscope) has been used with Arduino for the demo below:

[[./resources/pyteapot-gif.gif]]

This is an easy to understand Python implementation of the often-used 'MPU Teapot' processing code (an example [[https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/examples/MPU6050_DMP6/Processing/MPUTeapot/MPUTeapot.pde][here]]) for visualizing an IMU, but not specific to any particular IMU (like the MPU6060 in the case of 'MPU Teapot'). Note that this project does not do the sensor fusion of each sensor in the IMU to obtain the orientation (for the demo in the above gif, a Madgwick or Mahony filter was used from [[https://github.com/kriswiner/MPU9250/blob/master/quaternionFilters.ino][here]] to obtain the orientation). It is assumed that the user already has obtained the orientation in the form of either the yaw-pitch-roll angles or quaternions and wishes to visualize this real-time on-screen for debugging etc.

** Requirements:

  • [[https://www.pygame.org/wiki/about][ ~pygame~ ]] and [[http://pyopengl.sourceforge.net/][ ~PyOpenGL~ ]] are used for graphics
  • [[https://github.com/pyserial/pyserial][ ~PySerial~ ]] is used to get data from serial port
  • simple python built-in [[https://wiki.python.org/moin/UdpCommunication][UDP ~sockets~ ]] are used to get data from WiFi.
  • Usage Most of the code is self-explanatory. However some modifications might be required as per different use cases:
  • Set =useSerial= to =True= if receiving data over serial and =False= if receiving data over wifi.
  • Set serial port by changing the variable =ser=, if using serial for data transmission.
  • Set udp port by changing the variable =UDP_PORT=, if using wifi for data transmission.
  • 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)
  • 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
  • Second quaternion value should be between two =a= s
  • Third quaternion value should be between two =b= s
  • Fourth quaternion value should be between two =c= s
  • Yaw angle should be betweem two =y= s
  • Pitch angle should be between two =p= s
  • Roll angles should be between two =r= s

Either quaternion or Euler angles or even both can be received over the serial or udp port. If both are received, the =useQuat= variable defines which one is used to rotate the cube. So for example, all of the following are valid formats for the data received over the serial or udp port:

#+BEGIN_EXAMPLE

Both quaternions and Euler angles

w0.09wa-0.12ab-0.09bc0.98cy168.8099yp12.7914pr-11.8401r

Quaternions only

w0.09wa-0.12ab-0.09bc0.98c

Euler angles only

y168.8099yp12.7914pr-11.8401r #+END_EXAMPLE

Each of these must be on separate lines (or should have a '\n' at the end of the string). Other data may also be passed over Serial or Wifi, provided that none of the characters =w=, =a=, =b=, =c=, =y=, =p=, =r= are passed (for example, =somethingw0.09wa-0.12ab-0.09bc0.98cy168.8099yp12.7914pr-11.8401rsomethingelse= is valid but =somedataw0.09wa-0.12ab-0.09bc0.98cy168.8099yp12.7914pr-11.8401ranotherstring= is not since it has the characters =a= and =r=)

  • 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]])
  • [ ] 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
  • [ ] Other data collection methods: bluetooth?
  • [ ] Write tests, docstrings etc