The aim of this project is to demonstrate how to get readings from the accelerometer on the Pytrack/Pysense shields and visualise them. A LoPy (or other Pycom development board) will be programmed to send the accelerometer data over serial, this will then be read by a processing script that will tilt a 3D model.
What you will need:
Libraries:
- LIS2HH12 - To read values from the on-board accelerometer
Connect the Pycom development board to the Pysense expansion board, ensuring that the LED of the development board is facing on the same side as the micro-USB connector. Once connected you need to make sure the Pytrack/Pysense firmware is up to date. Instructions on how to update the firmware can be found here.
LoPy Code:Firstly, the required library files need to be copied onto the development board. This can be done using FTP or the "sync" functionality of Pymakr plug-in.
Once you have done this, a very simple script needs to be uploaded to the development board.
main.py
from LIS2HH12 import LIS2HH12
from pytrack import Pytrack
py = Pytrack()
acc = LIS2HH12()
while True:
pitch = acc.pitch()
roll = acc.roll()
print('{},{}'.format(pitch,roll))
time.sleep_ms(100)
The above code reads the accelerometers pitch and roll once every 100mS and outputs in over the serial port in comma separate value (CSV) format.
Processing Code:For the visualisation we will use a piece of software called Processing. In order to keep the code consistent, Python Mode for Processing will be used. Instructions on how to install it can be found here. Once you have this all setup up we can begin creating the visualisation.
The first thing we need to do is to add the serial
library to the Processing sketch like so:
add_library('serial')
Once this is done we can connect to the development board's serial port in the setup function:
SERIAL_DEVICE='/dev/tty.usbmodemPy343431'
def setup():
global my_port
#Connect to serial device
for dev in Serial.list():
if dev == SERIAL_DEVICE:
my_port = Serial(this, dev, 115200)
print("Connected to '{}'".format(SERIAL_DEVICE))
break
else:
msg = "Could not find serial port '{}'"
raise Exception(msg.format(SERIAL_DEVICE))
# Clear the serial buffer and consume first
# line incase we missed the start of it
my_port.clear()
my_port.readStringUntil(10)
#Setup window
size(400, 400, P3D)
This code loops through all available serial ports to find one matching SERIAL_DEVICE
. In this this example a typical MacOS/Linux serial port is used. If you are using Windows it should take the form of COM[number]
. If the serial port is found, we connect to it and then read until the next new line (ASCII value 10). This is to prevent errors occurring if we connect midway through a line of data being sent to us.
Next we need to read the data from the serial port and draw a 3D model appropriately:
lastRoll = 0
lastPitch = 0
def draw():
global lastRoll, lastPitch
#Get new reading from serial port
line = my_port.readStringUntil(10)
if line != None:
line = line.split(',')
if len(line) == 2:
pitch, roll = line
try:
pitch = float(pitch)
roll = float(roll)
lastPitch = pitch
lastRoll = roll
except Exception:
pass
background(0)
noStroke()
# Put view in the middle of the screen
# and far enough away to see properly
translate(width/2, height/2, -100)
# Default to last proper result
pitch = lastPitch
roll = lastRoll
# Rotate view
rotateX(-radians(pitch))
rotateZ(radians(roll))
# Zoom
scale(190)
# Draw the box
drawBox(0.6, 0.1, 1)
The first block of code above is responsible for reading the data from the serial port and parsing back into a pitch and roll angle. If there is an error with the data the previous good value is used. Once we have these value we can rotate our viewpoint by the angles:
# Rotate view
rotateX(-radians(pitch))
rotateZ(radians(roll))
All that is left to do now is drawing a 3D model. In this example a function called drawBox
was created that draws a flat cuboid with the Pycom logo, but you can replace this with anything. If you want to see the code that draws the pycom cuboid then you can go to the below link to the Pycom Libraries GitHub repository. The complete code for the visualiser can be found in: examples/accelerometer/visualiser/
If you would like to take this further and use more of the features on the Pytrack/Pysense shields then please visit the Pycom documentation.
Comments
Please log in or sign up to comment.