Getting input for predictive maintenance 2

In last week’s blog post, we went over our plan for collecting data for predictive maintenance. We also looked at the hardware configuration and began setting that up. This week, we will finalize getting our output data and do some final steps before we can train our model.

Handling x, y, and z output:

In an older blog post, we created a python program that will read the x, y, and z data from our BMA220 accelerometer and print it out. To use this data in our machine learning model, we would need it to be put into a .csv format. We will need to update our python code to do this. 

Our main function should look something like this: 

while (True):
        xdata = i2cbus.read_byte_data(
            i2caddress, 0x4)  # read the value of x data
        ydata = i2cbus.read_byte_data(
            i2caddress, 0x6)  #read the value of y data
        zdata = i2cbus.read_byte_data(
            i2caddress, 0x8)  #read the value of z data
        data = [xdata, ydata, zdata]
        print(data)  # print the value of x y and z data

We can add this code inside our while loop to create a .csv file named outputdata, and write each loop iteration’s data to the outputdata csv:

with open('outputdata.csv', 'a') as file:
            writer = csv.writer(file)

To keep track of how many rows of data are added, we can create a variable called datacount outside our while loop:

    datalog = 0

We can add the following code at the end of our while loop. This increases the datacount variable by one each time the loop adds an entry to the outputdata csv. It then prints out the number of entries that have been recorded in the outputdata csv:

datalog += 1
        print(datalog, "entries recorded")

After implementing all these changes to our python code and running it, a file called outputdata.csv should be created. Depending on how long you let the program run, the file should look like this with more or less entries.


The values may differ depending on how your accelerometer is positioned. 

Analyzing our data:

When I first looked at the x, y, z data in the outputdata file, I was surprised by the large changes I saw, when the accelerometer was seemingly not moving. I wasn’t sure if this data was normal and just due to noise, or if it was a defective accelerometer. To get a better understanding of it, I plotted the data to see if it was moving within a certain range, or if it was just all over the place.

Doing this is good practice in any case to make sure our training data isn’t full of strange outliers.

  • Plotting the data:

To plot our x, y, and z data, we will use the matplotlib library. Specifically mplot3d and pyplot from it. Include this code at the top to use it:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas

Then, we will use pandas to read our csv file and put it into a pandas dataframe:

points = pandas.read_csv('outputdata.csv')

Use the following piece of code to create a figure and separate our data into x, y, and z.

Note that to do this you will need to add x,y,z as the first entry in the outputdata csv.

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = points['x'].values
y = points['y'].values
z = points['z'].values

Finally, we can plot it as a 3D scatterplot. 

ax.scatter(x, y, z, c='r', marker='o')

We should get an output like this: 

This shows us that our data mainly is the same points over and over, which shows that it’s mainly due to noise and not a faulty accelerometer.

In the next blog post, we will make sure we properly collect test and training data, and begin to feed it to our model. 

Leave a Reply

Your email address will not be published.