Saturday, 16 November 2013

3D OpenGL visualisation of the data from an MPU-6050 connected to a Raspberry Pi

In this post I'll show how to serve the data over http and display a 3D representation in OpenGL extending on a previous blog post detailing how to read data from the MPU-6050 sensor and convert it into a something useful.

Using a simple web server to serve up the data

Let's start by setting up a simple server based on web.py, which is installed via apt-get
sudo apt-get install python-webpy
Now create a directory to put the code in and create a simple test program
mkdir webpy
cd webpy
vi server.py
Use the following as a test
Save the and then set it as executable with
chmod +x server.py 
and then run the code
./server.py
 you will see something like this showing the server is waiting for a request (pressing Ctrl+C will stop the server)
http://0.0.0.0:8080/
Now point your browser at http://ip-address-of-your-pi:8080 and it will show a web page with the content of Hello, world!.  We can make use of this to read data from a remote machine, in my case my Linux desktop.

Adding the sensor code to the server

Replace the contents of server.py with The server has to be run as sudo so you have permissions to read from the I2C
sudo ./server.py
Connecting via your browser will now return the X & Y rotation values e.g.
-28.7291281627 -39.4833542336

3D visualisation

I'm using a Linux desktop and that is all I have tested this simple code on, I've no idea if it works on Windows or Macs and it certainly won't run on the Pi itself.  I'm no OpenGL guru so this code is just hacked together to get something visible. 

Setting up OpenGL and pygame
sudo apt-get install python-opengl
sudo apt-get install python-pygame
Now save the following to a file (in my case level.py) and run it
Remember to change the URL line 038 to your specific value (the address you used earlier to test the server). When you run it a window will open showing the orientation of the sensor, rotating the sensor will update the display.



You'll notice that when the sensor isn't being physically moved the noisy data is causing it to wobble.  The next blog post shows how to reduce this.

15 comments:

  1. Once again, Thank you Andy! Counting the hours until the day job finishes so i can go play Pi! :)

    ReplyDelete
  2. Excellent Work! Up and running on OSX with only few changes! Thankyou!

    ReplyDelete
  3. Glad it worked ok. I'd be interested to see any changed / improvements you make to the code.

    ReplyDelete
  4. I have corrected a bug in the render code and the server code so the orientations are calculated correctly.

    ReplyDelete
  5. hello i tried the 3D visualisation thing in my raspi3 but i am getting the import errors of pygame and OpenGL but i have already did install them into my raspi and when i write "import pygame" into the python shell in raspi i get no error.Could you help with this problem i cant simulate my gyro in my raspberry pi3

    ReplyDelete
    Replies
    1. Unfortunately it won't run on the PI itself (as stated in the blog post), I ran the 3D code on my Linux machine.

      Delete
    2. Try this. https://github.com/Naohiro2g/MPU6050_teapot_demo

      Delete
    3. thanks for helping andrew birkett and nao2g, i think i got too excited to recognize it wont run on raspberry pi :D your blog is really helpful by the way have a nice day

      Delete
  6. dayday, you can make it run on your RasPi alone.

    $ git clone https://github.com/Naohiro2g/MPU6050_teapot_demo.git
    $ cd MPU6050_teapot_demo
    $ python server.py

    then in another terminal window:
    $ python client.py

    ReplyDelete
    Replies
    1. K, i will try this as soon as possible, thank you very much!

      Delete
    2. hi!
      server.py worked with no problem but when i try work the client.py i get an error which is:
      pygame error: couldnt fing matching GLX visual

      i wanna discuss about it if it is ok to you, do you have an mail adress, thanks again

      Delete
    3. I've updated the README.md.

      Please check if you have pyopengl installed.

      $ pip list |grep GL
      PyOpenGL (3.1.0)

      If not, install it;
      $ pip install pyopengl

      Delete
    4. sorry, I found sudo apt-get install python-opengl is better to install.

      Please check if it's available.
      apt list python-opengl

      Delete
    5. O did it and it is working now with no problem, really thank you very much nao2g! :)

      Delete