SERINDA 9DOF

Osiyo. Dohiju? ᎣᏏᏲ. ᏙᎯᏧ? Hey, welcome back.

I chose this sensor from Adafruit because it included the different items I might want to use in the LARC telemetry package I was putting together. You wire the sensor to an Arduino or Raspberry Pi. Download some libraries. Install some libraries and you’re in business.

With the Raspberry Pi on this page, you can install with 2 pip3 commands and then access the library’s information.

I created a Flask route for saving the calls from the Raspberry Pi. For now, I’m running the DOF on a Raspberry Pi so I can test. Eventually, I’ll put the DOF with the LattePanda.

#message list
message = []

@app.route("/dofdata", methods=['GET', 'POST'])
def dofdata():
    gyrox = request.args.get("gyrox", "", type=float)
    gyroy = request.args.get("gyroy", "", type=float)
    gyroz = request.args.get("gyroz", "", type=float)
    accelx = request.args.get("accelx", "", type=float)
    accely = request.args.get("accely", "", type=float)
    accelz = request.args.get("accelz", "", type=float)
    magx = request.args.get("magx", "", type=float)
    magy = request.args.get("magy", "", type=float)
    magz = request.args.get("magz", "", type=float)

    # messages.append("gyrox={0:0.3f}&gyroy={0:0.3f}&gyroz={0:0.3f}&accelx={0:0.3f}&accely={0:0.3f}&accelz={0:0.3f}&magx={0:0.3f}&magy={0:0.3f}&magz={0:0.3f}".format(gyrox, gyroy, gyroz, accelx, accely, accelz, magx, magy, magz))
    msg = "{"
    msg += "\"gyrox\":\"{0:0.3f}\"".format(gyrox)
    msg += ",\"gyroy\":\"{0:0.3f}\"".format(gyroy)
    msg += ",\"gyroz\":\"{0:0.3f}\"".format(gyroz)
    msg += ",\"accelx\":\"{0:0.3f}\"".format(accelx)
    msg += ",\"accely\":\"{0:0.3f}\"".format(accely)
    msg += ",\"accelz\":\"{0:0.3f}\"".format(accelz)
    msg += ",\"magx\":\"{0:0.3f}\"".format(magx)
    msg += ",\"magy\":\"{0:0.3f}\"".format(magy)
    msg += ",\"magz\":\"{0:0.3f}\"".format(magz)
    msg += "}"
    messages.append(msg)

    return jsonify(result="success", status="success")

This code basically takes the request arguments and makes a string version of a json object then adds that object to the list.

I also create a message listener of sorts. It constantly runs in its own way and when there’s a message in the queue it pushes it to the page. Then, it removes the message from the queue.

@app.route('/listen', methods=['GET'])
def listen():
    def stream():
        # messages = listen()  # returns a queue.Queue
        while True:
            if len(messages):
                for message in messages:
                    print(message)
                    yield 'data:{}\n\n'.format("text= " + message)
                    messages.remove(message)

    return Response(stream(), mimetype='text/event-stream')

When the page loads I want to get the data from the message and parse it as JSON then extract data out. I don’t want the view to just wildly zip around when I move my head so I’m dividing the values I get by 100.

var messageContainer = document.getElementById("messageContainer_div");
    var listenSource = new EventSource("/listen")
    listenSource.onmessage = function(e) {
        var dataReturned = e.data;
        var text = dataReturned.substring(dataReturned.indexOf("text=") + 5).trim();
        var jsonText = JSON.parse(text);
        var gyrox = jsonText.gyrox;
        var gyroy = jsonText.gyroy;
        var gyroz = jsonText.gyroz;

        gyrox = gyrox/100;
        gyroy = gyroy/100;
        gyroz = gyroz/100;

        var accelx = jsonText.accelx;
        var accely = jsonText.accely;
        var accelz = jsonText.accelz;

        var magx = jsonText.magx;
        var magy = jsonText.magy;
        var magz = jsonText.magz;

        moveX(gyrox, gyroy, gyroz);

        messageContainer.innerHTML = text;
    };

The final piece to this is creating the javascript function moveX – which, for now, takes all of the gyro x, y, and z values and then updates the controls in my ThreeJs scene.

You’ll notice that I call rotateUp, rotateLeft, and dollyIn – but not their opposites. if rotateUp is passed a positive number then it will go up. If rotateUp is passed a negative number then it will go down. I don’t need to check and see whether it’s positive or negative and then call another method.

function moveX(gyroX, gyroy, gyroz) {
    controls.rotateUp(gyroX);
    controls.rotateLeft(gyroy);
    controls.dollyIn(gyroz);

    controls.update();
}

I have some cleanup to do and then I’ll have some testing to do with the scene adding elements off camera and then testing all of the directions.

As of this moment, the X and Y work great. The Z, however, doesn’t quite behave like it should. Some tweaks will be needed. NOTE: I’m testing some static values for Z. I pass in X and Y and then +/- for Z and don’t use the value from the DOF breakout board.

After I figure out what’s wrong with the Z I will move on to adjusting the view like I would in any other OpenGL scene.

Until next time. Dodadagohvi. ᏙᏓᏓᎪᎲᎢ.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.