Remote testing a Raspberry with HMC5883L

I wanted to get a taste of how precise and instantly the magnetometer worked, so my first program will read ‘the values’ and ‘show it’ on the screen.

Useful resources on the internet are:

    1. Using a HMC5883L magnetometer/compass with the #RaspberryPi
    2. Using the HMC5883L magnetometer/compass on the Raspberry Pi
    3. I2C Python Library – 3-Axis Digital Compass (HMC5883L) with the Raspberry Pi
    4. OpenGL with PyOpenGL introduction and creation of Rotating Cube
    5. Remote Programming of RaspberryPi using PyCharm – CodeProject
    6. PyCharm 2016.1 Help :: Remote Debugging – JetBrains

To get started, 1 and 2 show an example of how to use the library from 3, that I needed to access through archive.org, to get data from the compass. The compass readout code needs to run on the Pi, but my Pi is not backed up and I like the PyCharm IDE, so I also spend some time setting up remote development and debugging using PyCharm using 5 and 6. Finally, the raw data library 3 is presented as three coordinates x, y and z. Instead of printing values on the console, I wanted to visualize it, so I modified the example program from 4 to include an line (edge) from the origin to (x,y,z).

Pi compass programm

The Pi program sets up the i2c connection to the compass and then listens for HTTP requests. On a request of /, the current values are output as a json blob.

from i2clibraries import i2c_hmc5883l
from flask import Flask, Response
from json import dumps
import math

con = i2c_hmc5883l.i2c_hmc5883l(1)
con.setScale(0.88)
con.setContinuousMode()

app = Flask(__name__)

@app.route('/')
def index():
    (x, y, z) = con.getAxes()
    payload = dumps({'x': x, 'y': y, 'z': z})
    return Response(payload, mimetype="application/json")

if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0")

Host glplot program

The host program is in a game loop. In each loop, the program will read from the Pi with http the latest axes values, plot the cube and the compass edge. It also averages the latest MAX retrieved values to smooth the direction.

import pygame
import numpy as np
import requests
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

PIURL='http://pi:5000/'

verticies = [
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1),
    (0,0,0),
    (1,1,1)
    ]

edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7),
    (8,9)
    )

MAX = 30
xa = np.zeros(MAX,)
ya = np.zeros(MAX,)
za = np.zeros(MAX,)
array_index = 0

def arrayMean(a):
    return np.mean(a)

def arrayAdd(t):
    global array_index, MAX

    xa[array_index] = t[0]
    ya[array_index] = t[1]
    za[array_index] = t[2]
    array_index = array_index + 1
    if array_index >= MAX:
        array_index = 0

def getAxes():
    response = requests.get(PIURL)
    data = response.json()
    t = (data.get('x'), data.get('y'), data.get('z'))
    # return t
    arrayAdd(t)
    return (arrayMean(xa), arrayMean(ya), arrayMean(za))

def arrow():
    verticies[9] = getAxes()

    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glVertex3fv(verticies[vertex])

    glEnd()

def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 500.0)

    glTranslatef(0.0,0.0, -5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        # glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        arrow()
        pygame.display.flip()
        pygame.time.wait(20)

if __name__ == '__main__':
    main()

 

 

Advertenties

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s