Mpu6050 +motion capture on processing ide

edited April 2017 in Arduino

I have got a code for processing ide where the code accepts data from mqtt cloud and inputs roll,yaw,pitch angles from it and designates on a mpu-tea pot(a famous processing example in arduino). Now I just want to represent the same on human head. The circuit is: A gyro/accelometer sensor +wifi module --->giving data to cloud----->getting data from mqtt cloud on processing software. Instead of mputea pot I need to visulise the same onto human head. How can I start? I am also new to this software. Any inputs? here is the processing code: `

     //-------------------------**********code will remain same till here irrespective of whether making a teapot or a human head*************************************************----------------------------------------------//
            import processing.serial.*;
            import processing.opengl.*;
            import toxi.geom.*;
            import toxi.processing.*;
            import org.eclipse.paho.client.mqttv3.*;
            import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;



        ToxiclibsSupport gfx;

        Serial port;                         // The serial port
        char[] teapotPacket = new char[14];  // InvenSense Teapot packet
        int serialCount = 0;                 // current packet byte position
        int synced = 0;
        int interval = 0;

        float[] q = new float[4];
        Quaternion quat = new Quaternion(1, 0, 0, 0);

        float[] gravity = new float[3];
        float[] euler = new float[3];
        float[] ypr = new float[3];
        String topic        = "check";
        String content      = "Hello CloudMQTT";
        int qos             = 1;
        String broker       = "tcp://m13.cloudmqtt.com:17673";
        String clientId     = "ClientId";
        MemoryPersistence persistence = new MemoryPersistence();
        MqttMessage message = new MqttMessage(content.getBytes());

        MqttClient  mqttClient;

        void setup() {
          try {
            println("Entered");
            mqttClient = new MqttClient(broker, clientId, persistence);
            mqttClient.setCallback(new MqttCallback() {
              public void messageArrived(String topic, MqttMessage msg)
                throws Exception {
                interval = millis();
                println("Received:" + new String(msg.getPayload()));
                String[] data = new String(msg.getPayload()).split(";");
                float[] qvalue = new float[data.length];
                for (int i = 0; i < data.length; ++i) {
                  qvalue[i] = Float.parseFloat(data[i]);
                }
                quat.set(qvalue[0], qvalue[1], qvalue[2], qvalue[3]);
              }

              public void deliveryComplete(IMqttDeliveryToken arg0) {
                println("Delivery complete");
              }

              public void connectionLost(Throwable arg0) {
                // TODO Auto-generated method stub
              }
            }
            );
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            connOpts.setUserName("crfqbqvi");
            connOpts.setPassword(new char[]{'_', '9', 'n', 'o', 'Z', 'Y', 'P', 'y', 'S', '7', 'D', '9'});
            mqttClient.connect(connOpts);
            message.setQos(qos);     
            mqttClient.subscribe(topic, qos);
            //mqttClient.publish(topic, message);
            // client.unsubscribe("/example");
          }

          catch(MqttException me) {
            System.out.println("reason "+me.getReasonCode());
            System.out.println("msg "+me.getMessage());
            System.out.println("loc "+me.getLocalizedMessage());
            System.out.println("cause "+me.getCause());
            System.out.println("excep "+me);
            me.printStackTrace();
          }
          // 300px square viewport using OpenGL rendering
          size(300, 300, OPENGL);
          gfx = new ToxiclibsSupport(this);

          // setup lights and antialiasing
          lights();
          smooth();
        }
        //-------------------------**********code will remain same till here irrespective of whether making a teapot or a human head*************************************************----------------------------------------------//
        void draw() 
        {
          //println("Entered");
          if (millis() - interval > 1000) {
            // resend single character to trigger DMP init/start
            // in case the MPU is halted/reset while applet is running
            //port.write('r');
            interval = millis();
          }

          // black background
          background(0);

          // translate everything to the middle of the viewport
          pushMatrix();
          translate(width / 2, height / 2);

          // 3-step rotation from yaw/pitch/roll angles (gimbal lock!)
          // ...and other weirdness I haven't figured out yet
          //rotateY(-ypr[0]);
          //rotateZ(-ypr[1]);
          //rotateX(-ypr[2]);

          // toxiclibs direct angle/axis rotation from quaternion (NO gimbal lock!)
          // (axis order [1, 3, 2] and inversion [-1, +1, +1] is a consequence of
          // different coordinate system orientation assumptions between Processing
          // and InvenSense DMP)
          float[] axis = quat.toAxisAngle();
          rotate(axis[0], -axis[1], axis[3], axis[2]);

          // draw main body in red
          fill(255, 0, 0, 200);
          box(10, 10, 200);

          // draw front-facing tip in blue
          fill(0, 0, 255, 200);
          pushMatrix();
          translate(0, 0, -120);
          rotateX(PI/2);
          drawCylinder(0, 20, 20, 8);
          popMatrix();

          // draw wings and tail fin in green
          fill(0, 255, 0, 200);
          beginShape(TRIANGLES);
          vertex(-100, 2, 30); 
          vertex(0, 2, -80); 
          vertex(100, 2, 30);  // wing top layer
          vertex(-100, -2, 30); 
          vertex(0, -2, -80); 
          vertex(100, -2, 30);  // wing bottom layer
          vertex(-2, 0, 98); 
          vertex(-2, -30, 98); 
          vertex(-2, 0, 70);  // tail left layer
          vertex( 2, 0, 98); 
          vertex( 2, -30, 98); 
          vertex( 2, 0, 70);  // tail right layer
          endShape();
          beginShape(QUADS);
          vertex(-100, 2, 30); 
          vertex(-100, -2, 30); 
          vertex(  0, -2, -80); 
          vertex(  0, 2, -80);
          vertex( 100, 2, 30); 
          vertex( 100, -2, 30); 
          vertex(  0, -2, -80); 
          vertex(  0, 2, -80);
          vertex(-100, 2, 30); 
          vertex(-100, -2, 30); 
          vertex(100, -2, 30); 
          vertex(100, 2, 30);
          vertex(-2, 0, 98); 
          vertex(2, 0, 98); 
          vertex(2, -30, 98); 
          vertex(-2, -30, 98);
          vertex(-2, 0, 98); 
          vertex(2, 0, 98); 
          vertex(2, 0, 70); 
          vertex(-2, 0, 70);
          vertex(-2, -30, 98); 
          vertex(2, -30, 98); 
          vertex(2, 0, 70); 
          vertex(-2, 0, 70);
          endShape();

          popMatrix();
        }

        void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
          float angle = 0;
          float angleIncrement = TWO_PI / sides;
          beginShape(QUAD_STRIP);
          for (int i = 0; i < sides + 1; ++i) {
            vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
            vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
            angle += angleIncrement;
          }
          endShape();

          // If it is not a cone, draw the circular top cap
          if (topRadius != 0) {
            angle = 0;
            beginShape(TRIANGLE_FAN);

            // Center point
            vertex(0, 0, 0);
            for (int i = 0; i < sides + 1; i++) {
              vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
              angle += angleIncrement;
            }
            endShape();
          }

          // If it is not a cone, draw the circular bottom cap
          if (bottomRadius != 0) {
            angle = 0;
            beginShape(TRIANGLE_FAN);

            // Center point
            vertex(0, tall, 0);
            for (int i = 0; i < sides + 1; i++) {
              vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
              angle += angleIncrement;
            }
            endShape();
          }
        }

` So I have put comments for a paragraph of code that is: it will remain same irrespective of teapot or head. After that code changes (which is presently for teapot) for human head. Can someone give me startup ideas to go ahead with this project..

Answers

  • Do you want the scene camera to rotate or the object to rotate? Not familiar with teapot here btw...

    Kf

  • edited April 2017

    @kfrajer I want object to rotate. The values for the object will get from MQTT cloud.

Sign In or Register to comment.