Documentation.tex (10930B)
1 \documentclass{scrartcl} 2 \usepackage{fontspec} 3 \setmainfont{STIX Two Text} 4 \usepackage{amsmath} 5 \usepackage{unicode-math} 6 \setmathfont{STIX Two Math} 7 8 \DeclareMathOperator*{\argmax}{argmax} 9 10 \usepackage{polyglossia} 11 \setdefaultlanguage{english} 12 13 \usepackage{microtype} 14 15 \usepackage[margin=2cm, bottom=2.5cm]{geometry} 16 17 \usepackage{float} 18 \usepackage[export]{adjustbox} 19 \usepackage{graphicx} 20 \usepackage{circuitikz} 21 22 \newcommand{\abs}[1]{\lvert{}#1\rvert{}} 23 24 \usepackage{hyperref} 25 \usepackage[all]{hypcap} 26 \hypersetup{pdfborder = {0 0 0}, colorlinks=true, allcolors=black, urlcolor=blue} 27 28 \parskip=.5ex 29 \parindent=1ex 30 31 \title{LaundrySorcery} 32 \subtitle{IP-Enabling a \~2011 Laundry Machine} 33 \author{Dominik Schmidt} 34 \date{\today} 35 36 \begin{document} 37 \maketitle 38 \begin{abstract} 39 This document describes the implementation of ON-state detection on a washing machine design from approximately 2011. 40 The project is implemented on a Raspberry Pi 3, and yields a correct on-off status with an additional (inaccurate) prediction of the washing time remaining. 41 It features an adaptive guesser on the ON-state via the washing machines ON-LED and a light sensor, 42 an AJAX/HTML interface for any Web browser, and clustering algorithms to determine how many washing programs of different lengths exist. 43 \end{abstract} 44 \section{Sensory} 45 To determine whether the washing machine is on or off we tape a light-sensor to the ON-LED. 46 This has a drawback: The machine registers as ON while in delayed start mode, which is good for displaying whether the 47 machine is free but bad for the machine learning section. 48 49 The light sensor we chose is a light dependant resistor (LDR), leaving us with the problem on how to read out the resistance 50 with the given hardware (a Raspberry Pi) 51 52 \subsection{Read-out Circuit} 53 Since the Raspberry Pi does not have an ADC, we need a different way to read out the resistance of the LDR. 54 \begin{figure}[H] 55 \centering 56 \begin{tikzpicture} 57 \input{Schematics_Core.tex} 58 \end{tikzpicture} 59 \caption{The circuit for our read-outs. We measure the resistance by timing-differences in the RC-circuit.} 60 \end{figure} 61 62 We drive the input of the RC-circuit to high (3.3V) when we measure low at the input (approximately at 1.8V). 63 Conversely, we drive the input to low (0V) when we measure high at the input (approximately 2.8V). 64 We can simulate this behaviour with a sinusoidal resistance for demonstration: 65 \begin{figure}[H] 66 \centering 67 \includegraphics[max width=0.75\linewidth]{Graph/simulation.pdf} 68 \caption{Simulation of the timing-differences with a sinusoidal resistance} 69 \end{figure} 70 71 To get our measurement data we use the time for a charge-cycle. 72 \subsection{Interpretation of Timings} 73 Our program now gets a series of charging times $\Delta t$. 74 How do we interpret these values to figure out whether the washing machine is on or off? 75 To figure that out, we make two assumptions: 76 \begin{enumerate} 77 \item When we begin measuring, the machine is off 78 \item The readings from our sensor have a gaussian distribution 79 \end{enumerate} 80 81 \begin{figure}[H] 82 \centering 83 \includegraphics[max width=0.75\linewidth]{Graph/distribution.pdf} 84 \caption{Two gaussian distributions of our $\Delta t$ timings, one centered around $300\mu s$ the other around $1300\mu s$} 85 \end{figure} 86 87 Decision theory tells us that the optimal way to decide whether the machine is on or off is to look 88 whether the measurement is above or below the threshold: 89 \begin{equation} 90 \footnotesize 91 \frac{-\mu_1\cdot \sigma_2^{2}+\mu_2\cdot \sigma_1^{2}\pm\sqrt{\mu_1^{2}-2\cdot \mu_1\cdot \mu_2+\mu_2^{2}+2\cdot \sigma_1^{2} \ln\left(\sigma_1\right)-2\cdot \sigma_1^{2} \ln\left(\sigma_2\right)-2\cdot \sigma_2^{2} \ln\left(\sigma_1\right)+2\cdot \sigma_2^{2} \ln\left(\sigma_2\right)} \abs{\sigma_1} \abs{\sigma_2}}{(\sigma_1^{2}-\sigma_2^{2})} 92 \end{equation} 93 This is rather complex, so if we assume equal variance we get that the optimal boundary lies at: 94 \begin{equation} 95 \frac{\mu_1 + \mu_2}{2} 96 \end{equation} 97 98 \subsubsection{Measurement of Gaussians} 99 We do not want hard-coded gaussians in our program. Therefore, the gaussians are guessed continuously from the measurements with the following method: 100 \begin{align} 101 \mu[k] &= \mu[k-1] \lambda + \Delta t[k] \cdot (1-\lambda)\\ 102 \tilde{\sigma}^2[k] &= \tilde{\sigma}^2[k-1] \cdot \lambda + (\Delta t[k])^2 (1-\lambda)\\ 103 \sigma^2[k] &= \tilde{\sigma}^2[k] - (\mu[k])^2 104 \end{align} 105 106 The paramter $\lambda$ defines how much of the old value we are going to retain. 107 Since we want the Gaussian to be quickly updated at the beginning, and only slowly update to small changes of lighting after it has stabilized, 108 we increase $\lambda$ as the number of measurements progresses. 109 110 \begin{equation} 111 \lambda[k] = \min\left\{0.99999, 0.75 + 0.25\frac{k}{1000}\right\} 112 \end{equation} 113 \subsubsection{Bootstrapping} 114 We make no a-priori assumption on these gaussians. 115 This results in a problem: While we know the decision boundary between two gaussians, 116 we have no idea on how the on-state is distributed at the beginning. 117 Hence we use a crutch: When the machine is initialized, it decides to switch to ON 118 if there is a value outside of $5\sigma_{OFF}$. 119 After that first decision we can train the $(\mu_{ON},\sigma_{ON}^2)$ and use the 120 proper decider. 121 122 \subsubsection{Outlier Resistancy} 123 If we redecided every time a single value across the decision boundary occurded, 124 we would switch between states all the time. 125 Hence, we only switch the guessed state once $100$ \emph{consecutive} values have been measured. 126 127 \subsubsection{Updates During Possible Transition} 128 What do we do when we detect an outlier? 129 Do we train our gaussian with it, or do we ignore it? 130 Both versions have drawbacks: Training the gaussian yields a large spurious variance during a transition. 131 Not using it for training could possibly block out the adaption to large variance changes. 132 133 Hence we do something in between: when we are detecting outliers, we make a copy of the gaussian before the outliers 134 started, update the gaussian with the outliers and when we decide to switch, we restore the gaussian to its copy before the 135 outliers. 136 \section{Machine Learning} 137 We now want to guess how long it will take until the washing is finished. 138 139 This is complicated by the fact that the washing machine has multiple programs, which all have different durations. 140 Additionally, program duration can vary by how much laundry is in the machine. 141 142 \subsection{Learning Program Lengths} 143 To address this issue, we use the K-Means clustering algorithm. 144 Since we do not know the number of clusters, we start with $k=1$ and increase k, 145 until the maximal variance of the cluster is below a threshold. 146 147 We check for the standard deviation to be lower than a threshold in two modes: absolute and relative. 148 \begin{itemize} 149 \item The absolute mode checks whether the standard deviation is below a certain value, e.g. below 30 minutes. 150 \item The relative mode checks whether the standard deviation from the mean is below a certain percentage, 151 e.g. we allow only standard deviations smaller than 10\% of the mean 152 \end{itemize} 153 154 Hence, we increase $k$ until the two following conditions hold 155 \begin{align} 156 \max_{i} \sigma_i &< \tau_{abs}\\ 157 \max_{i} \frac{\sigma_i}{\mu_i} &< \tau_{rel} 158 \end{align} 159 160 \subsection{Guessing the Remaining Time} 161 We now have clusters of gaussians, and want to know: How long is a washing going to take? 162 163 \begin{align} 164 \hat{t} &= \argmax_{t} P[T=t] \\ 165 & = \argmax_{t} \lim_{\Delta \to 0} \frac{P[T\leq t] - P[T \leq t+\Delta]}{\Delta} \\ 166 & = \argmax_{t} f(t) 167 \end{align} 168 169 We now have to know $P[T\leq t]$. This is easily calculated through marginalization with respect to the Cluster $C$ being active: 170 \begin{equation} 171 P[T\leq t] = \sum_{i} P[T\leq t | C=i] \cdot P[C=i] 172 \end{equation} 173 $P[C=i]$ is the a-priori distribution of cluster $i$ being active. 174 This we know since we know the number of points inside that cluster. 175 176 Therefore, it holds that: 177 \begin{align} 178 \hat{t} &= \argmax_{t} \sum_i P[C=i] \cdot f_{C=i}(t)\\ 179 &= \sum_i P[C=i] \cdot \mu_i 180 \end{align} 181 182 Which is exactly the expectation $E[T]$. 183 184 \subsubsection{Improving the Guess} 185 This can be improved, since we have some additional information: 186 We know, the program has been running for $t_0$ seconds. 187 Hence, we rewrite our guess to: 188 189 \begin{align} 190 \hat{t} &= \argmax_{t} P[T = t | T\geq t_0] \\ 191 &= \argmax_t \sum_i P[C=c_i | T \geq t_0] \cdot P[T = t | C=i \land T \geq t_0]\\ 192 &= \sum_i P[C=c_i | T \geq t_0] \cdot \argmax_t P[T = t | C=i \land T \geq t_0] 193 \end{align} 194 195 The term $P[T=t|C=i \land T\geq t_0]$ is easily calculated: it is just a cut and rescaled version of a normal gaussian. 196 197 \begin{figure}[H] 198 \centering 199 \includegraphics[max width=0.4\linewidth]{Graph/Cut_Gaussians/Cut_Gaussian.pdf}\hspace{2em} 200 \includegraphics[max width=0.4\linewidth]{Graph/Cut_Gaussians/Cut_Gaussian_-2.pdf}\\ 201 \includegraphics[max width=0.4\linewidth]{Graph/Cut_Gaussians/Cut_Gaussian_0.pdf}\hspace{2em} 202 \includegraphics[max width=0.4\linewidth]{Graph/Cut_Gaussians/Cut_Gaussian_2.pdf} 203 \caption{Gaussians given the knowledge that the variable is bigger than a certain value} 204 \end{figure} 205 206 It is easy to see that the maximum lies at $\argmax f_{N|T\geq >t_0}(t) = \left\{\begin{matrix} \mu & t\leq t_0 \\ t_0 & t > t_0\end{matrix}\right.$ 207 208 The a-priori distribution of $P[C=c | T \geq t_0]$ is a more complex matter, but can be calculated using Bayes Theorem: 209 \begin{align} 210 P[C=c | T \geq t_0] &= \frac{P[C=c]}{P[T \geq t_0]} \cdot P[T \geq t_0 | C=c]\\ 211 &= \frac{P[C=c]}{\sum_i P[C=c]\cdot P[T \geq t_0 | C=c]} \cdot P[T \geq t_0 | C=c] 212 \end{align} 213 This expression, while computationally difficult for large number of clusters, can be evaluated. 214 \subsection{Guessing the Cluster} 215 Now, since we are here anyways, we can as well guess the most probable cluster, too. 216 \begin{equation} 217 \hat{C} = \argmax_{i} P[C=i | T \geq t_0] 218 \end{equation} 219 This can, again, be addressed by the Bayes Theorem: 220 \begin{align} 221 \argmax_i P[C=i | T \geq t_0] &= \argmax_i P[T \geq t_0 | C=i] \cdot \frac{P[C=i]}{P[T \geq t_0]} 222 \intertext{Now, since $P[T \geq t_0]$ does not depend on $i$, we can ignore it for the maximization:} 223 &= \argmax_i P[T \geq t_0]\cdot P[C=i] 224 \end{align} 225 Since the number of clusters is quite small, we can evaluate this expression for all possible $i$ and choose the largest. 226 \end{document}