Commit 918efe1a authored by Martin Isaksson's avatar Martin Isaksson
Browse files

posts

parent 67830713
---
layout: post
title: "Washing apparatus ready detection (WARD)"
date: 2015-08-30 21:53:48
categories: R PocketLab
---
## Abstract
Using a [PocketLab](http://thepocketlab.com/) put on the top of a washing machine, we measure the magnetic field generated by the motor. The magnitude of this vector is used to detect if the motor is on, and when the motor has been off for some time, we say that the cycle is finished.
## Checkpoint
{% highlight r %}
#library(checkpoint)
#checkpoint("2015-08-26")
{% endhighlight %}
## Libraries
{% highlight r %}
library(dplyr)
library(tidyr)
library(ggplot2)
library(TTR)
library(printr)
#library(mime)
{% endhighlight %}
## Helpers
## Read data
Read data from PocketLab CSV and calculate magnitude from `x`, `y` and `z` components. Then use an Exponential Moving Average filter to remove the trend (seems like the sensor does not return to zero when the washing machine motor turns off).
{% highlight r %}
wash <- read.csv("data/Magnetic field368141090.csv", col.names=c("time", "x", "y","z")) %>%
mutate(
magnitude = sqrt(x^2 + y^2 + z^2), # Calculate magnitude
magnitude_ema = EMA(magnitude,20), # Exponential Moving Average
magnitude_adj = magnitude - magnitude_ema, # Remove trend
magnitude_abs = magnitude_adj %>% abs %>% EMA(., 50),
magnitude_abs = scale(magnitude_abs) %>% as.vector, # Absolute value and more EMA
threshold = ifelse(magnitude_abs > 0, 1, 0)
) %>%
filter(time < 1190)
wash %>% head
{% endhighlight %}
| time| x| y| z| magnitude| magnitude_ema| magnitude_adj| magnitude_abs| threshold|
|----:|---------:|----------:|----------:|---------:|-------------:|-------------:|-------------:|---------:|
| 0.00| 0.0610352| 0.0610352| -0.3051758| 0.3171480| NA| NA| NA| NA|
| 0.02| 0.0610352| -0.0305176| -0.1220703| 0.1398491| NA| NA| NA| NA|
| 0.08| 0.1525879| 0.0915527| -0.3356934| 0.3799408| NA| NA| NA| NA|
| 0.16| 0.0305176| 0.1220703| 0.0610352| 0.1398491| NA| NA| NA| NA|
| 0.26| 0.0000000| 0.0305176| -0.0305176| 0.0431584| NA| NA| NA| NA|
| 0.36| 0.0000000| 0.0000000| -0.3967285| 0.3967285| NA| NA| NA| NA|
Rearrange and plot `x`,`y` and `z` components.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment