Accelerometer / Datalogger Prototype

Goal: Track the flight of a rocket ship and graph its path in 3D space.

Please make sure you have the latest version of Python 3 installed on your computer.

Two Alternative Solutions


Solution 1. Two Microbit Solution

Microbit 1.  Inside Rocket (Sender)

This microbit, when started (either on Reset or when A-Button pressed)

BEGIN
        TURN ON RADIO
        CONFIG RADIO to Channel 19, Power 7
        start=False
        message = ""
        WHILE True THEN
                message = RADIO.Receive
                ##WHILE message = "start" DO
                IF message = "start" then
                        co-ords = Get Accelerometer Values (x,y,z)
                        RADIO send(co-ords)
                ELSE IF message = "stop" then
                        print("Stop")               
                ELSE
                    print ("Unknown message: <message>)
                END If 
        END WHIE
 END
SAMPLE CODE
## Sender - stored inside Rocket
## When RESET pressed will:
## 1. Sleep until receive a "Start" from Receiver
##    a. Gets x,y,z acceleration from accelerometer
##    b. Formats it as comma separated list
##    c. Uses Radio to broadcast this data
## 2. Receives Message from Receiving Microbit to Start/Stop
## NOTE: Change the Channel to a unique number with Receiver to prevent overlap
##
from microbit import *
import radio
radio.on()
radio.config(channel=19, power=7)

def sendData():
    x=accelerometer.get_x()
    y=accelerometer.get_y()
    z=accelerometer.get_z()
    coords="{},{},{}".format(x,y,z)
    radio.send(coords)
    display.show(Image.HEART_SMALL)
    sleep(100)
    display.show(Image.HEART)

start = False
while True:
    sleep(20)
    if start:
        sendData()
        
    message = radio.receive()
    if message=="start":
        start=True
        display.show(Image.SMILE)
    elif message=="stop":
        start=False
        display.show(Image.SAD)
        


Microbit 2.  Remote Control (Receiver)



SAMPLE CODE
##
## The Receiver's job is to:
## 1. Notify the Sender to Start/Stop
## 2. Receive Messages from the Sending Microbit.
## NOTE:  Change the Channel number in the Sender and Receiver
##        to prevent overlap from another Microbit
##
from microbit import *
import radio
radio.on()
radio.config(channel=19, power=7)
## File stored on Microbit for future download
filename = "datalog.txt"

def receiveData(f):
    message = radio.receive()
    print(message)
    msg=str(message).split(",")
    print(msg)
    f.write("{}\n".format(msg))
    #x=msg[0]
    #y=msg[1]
    #z=msg[2]
    #print(x,y,z)
    
with open(filename,"w") as f:
    start=False
    while True:
        if button_a.was_pressed():
            radio.send("start")
            display.show(Image.SMILE)
            start=True
        if button_b.was_pressed():
            radio.send("stop")
            display.show(Image.SAD)
            start=False
        if start:
            receiveData(f)

        sleep(200)












Solution 2. Single Microbit Solution
Step 1.  Track Flight.
We achieve this by using a device that has an Accelerometer and Data Logger.  The device we are using here is a BBC Micro:bit.  This device has these features built into it.

A. Coding Accelerometer and Data Logger.
Microbit can be coded using several languages, including MicroPython

The code may be programmed on a computer using the Mu IDE (A simple python programming tool). This IDE is specially designed for using with Microbit.

Note:  Microbit has 30KB onboard File System. This file system is used to store program files and small data files.
Programs written in Mu can be directly converted into a Binary File (Machine code) or in Microbit it is a HEX file, and 
downloaded directly (term: flash) to the Microbit.




Data Logging Algorithm
Sample code can be found on GitHub:  https://github.com/jlaispx/Microbit_Datalogger
Single Microbit Solution
  • Microbit has a built-in Accelerometer.
  • We want to start our program, and then when the A button is pressed, then start getting the current x,y,z co-ordinates of the Microbit from the Accelerometer and write that to a File.
  • We want to continue logging until the B Button is pressed, we then exit the program.




Running datalogger
Without Mu, running datalogger on microbit is quite complicated.  But because of the smooth integration to microbit, there is a button "Flash" to achieve this.

  1. Press the "Check" button to identify any potential syntax errors.
  2. If no errors, then Press the "Flash" which will
    1. Compiles the Python and creates a HEX file
    2. Downloads to the MicroBit
    3. Starts Running it straight away
Note: Microbit will also do a syntax check using MicroPython (embedded on device), and will display any errors on the LED display on microbit.


B. Retrieving Datalog File

To retrieve the datalog file from the Microbit file system, you need to be able to connect to the device using Microfs, as special Python package that, once installed on your computer, will allow you to PUT and GET files to /from Microbit to your Windows file system.

C. Using Datalog File

The Datalog file was created with "x,y,z" acceleration values recorded on a pre-determined time (rate).









We can then create some sort of 3D plotting program that draws the path based on the co-ordinates stored in the file.

Python has package called MatPlotLib that can be programmed to take a set of x, y and z co-ords and plot them on a 3D Graph, that visually represents the path of the microbit

Plotting Algorithm
  • Open the Datalog file
  • Read each line in the datalog file, which contains "x,y,z"
  • Get each x,y and z co-ord and add each into a separate array; an x array, y array, and z array.
  • Call the plotting subroutine, 
    • request a 3D Axes
    • pass the separate x, y, z arrays
    • label = "flight path"







APPENDIX

Mu IDE

  




Microfs
A simple command line tool and module for interacting with the limited file system provided by MicroPython on the BBC micro:bit.


To install:
  • Open CMD
  • Type:
    • pip install microfs
  • If you get a Config SSL error try the following on command line:
    • pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org pip setuptools
    • Then repeat the above
Briefly, How it works....
Copy a file onto the device:
$ ufs put path/to/local.txt
Get a file from the device:
$ ufs get remote.txt

Matplotlib
This is a multi-purpose graphing package for Python.

To install from CMD:
  • pip install matplotlib


../../_images/lines3d_demo1.png






Comments