{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Temperature Control Lab Python Introduction\n", "\n", "The TCLab (Temperature Control Lab) has an LED, two heaters, and two temperature sensors that are controlled with an Arduino. The heater power output is adjusted to maintain a desired temperature. Thermal energy from the heater is transferred by conduction, convection, and radiation to the temperature sensor. Heat is also transferred away from the device to the surroundings. This lab is a resource to learn data analysis, visualization, regression, and model analysis in Python with real data. Temperature control is an important problem in many applications such as:\n", "\n", "* Maintain temperature of a home during the winter\n", "* Pre-heat an oven for baking\n", "* Regulate the temperature in a chemical reactor\n", "* Reduce temperature variations during semiconductor production\n", "* Adjust the infrared heater for a newborn baby in an incubator to maintain body temperature\n", "* Regulate natural gas to a water heater to provide consistent hot water\n", "* Adjust flow through a heat exchanger to maintain outlet temperature\n", "* Others?\n", "\n", "The heaters and LED can be adjusted between 0-100% output. The heater effect is shown in the plot below.\n", "\n", "![Temperature Control Lab](http://apmonitor.com/pdc/uploads/Main/pid_control.gif \"TCLab Test\")\n", "\n", "This Python lab covers the following topics:\n", "\n", "* Part 1: Loops, Files, and Plotting\n", "* Part 2: Linear, Quadratic, and Nonlinear Regression" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Install TCLab and Load Packages\n", "\n", "[Retrieve tclab.py from Github](https://github.com/APMonitor/arduino/blob/master/0_Test_Device/Python/tclab/tclab.py) if pip install is not successful such as for computers where the user does not have administrative privileges to install packages. Include tclab.py in the same directory as the Jupyter notebook." ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# download tclab.py from \n", "try:\n", " import tclab\n", "except:\n", " !pip install tclab\n", " import tclab\n", "\n", "# import additional packages \n", "import time\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "import pandas as pd\n", "from scipy.optimize import curve_fit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Connect Test TCLab to Computer with USB Cable\n", "\n", "Connect the temperature control lab with the USB cable to the computer (MacOS, Windows, or Linux).\n", "\n", "![Temperature Control Lab Setup](http://apmonitor.com/pdc/uploads/Main/tclab_usb.jpg \"USB Cable Connection\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test TCLab LED\n", "\n", "Run the following commands to make sure the board is communicating with the computer. The red LED light should turn on for 10 seconds. There is an [installation guide](http://apmonitor.com/pdc/index.php/Main/ArduinoSetup) if the connection is unsuccessful." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TCLab version 0.4.6\n", "Arduino Leonardo connected on port COM3 at 115200 baud.\n", "TCLab Firmware Version 1.01.\n", "TCLab disconnected successfully.\n" ] } ], "source": [ "lab = tclab.TCLab() # Connect to Arduino\n", "lab.LED(100) # Turn on LED (0-100%)\n", "time.sleep(10.0) # Pause for 10 seconds\n", "lab.LED(0) # Turn off LED\n", "lab.close() # Close connection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test TCLab Temperature Measurements\n", "\n", "Run the following commands to read temperatures from sensors T1 and T2." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TCLab version 0.4.6\n", "Arduino Leonardo connected on port COM3 at 115200 baud.\n", "TCLab Firmware Version 1.01.\n", "T1: 28.18 degC\n", "T2: 27.34 degC\n", "TCLab disconnected successfully.\n" ] } ], "source": [ "lab = tclab.TCLab() # Connect to Arduino\n", "print('T1: ' + str(lab.T1) + ' degC') # Print Temperature 1\n", "print('T2: ' + str(lab.T2) + ' degC') # Print Temperature 2\n", "lab.close() # Close connection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test TCLab Heaters\n", "\n", "![Temperature Control Lab Heaters](http://apmonitor.com/pdc/uploads/Main/tclab_cables.jpg \"USB Cable and Heater Connection\")\n", "\n", "Plug in the USB power cable to the power supply and the barrel jack connector to the top plug (not the one next to the USB connector). Run the following script to test the heaters to see if the temperature increases. Many of the TCLab kits have thermochromic dots on the back that turn pink once the temperature rises above 37 degC and turn back to black when the temperature drops below 37 degC." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TCLab version 0.4.6\n", "Arduino Leonardo connected on port COM3 at 115200 baud.\n", "TCLab Firmware Version 1.01.\n", "Turn on Heaters for 50 seconds\n", "T1: 29.86 degC, T2: 29.08 degC\n", "T1: 29.83 degC, T2: 29.02 degC\n", "T1: 30.24 degC, T2: 28.89 degC\n", "T1: 31.21 degC, T2: 29.37 degC\n", "T1: 32.5 degC, T2: 29.79 degC\n", "T1: 34.05 degC, T2: 30.15 degC\n", "T1: 35.08 degC, T2: 30.53 degC\n", "T1: 36.59 degC, T2: 30.73 degC\n", "T1: 38.14 degC, T2: 31.47 degC\n", "T1: 39.59 degC, T2: 31.79 degC\n", "TCLab disconnected successfully.\n" ] } ], "source": [ "# closes tclab connection when done\n", "with tclab.TCLab() as lab:\n", " print('Turn on Heaters for 50 seconds')\n", " lab.Q1(100) # Turn on Heater 1 to 100%\n", " lab.Q2(50) # Turn on Heater 2 to 50%\n", "\n", " for i in range(10):\n", " print('T1: ' + str(lab.T1) + ' degC, T2: ' + str(lab.T2) + ' degC')\n", " time.sleep(5) # Pause for 5 seconds" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 1: Loops, Files, and Plotting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A: Loop to Control LED\n", "\n", "Dim the LED from 100% to 0% over a period of 10 seconds. Decrease the LED intensity by 10% every second. Print the LED brightness every second." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### B: Record Temperatures\n", "\n", "Turn heater 1 to 100% and heater 2 to 40%. Record the temperatures (lab.T1 and lab.T2) every 0.5 seconds for 30 seconds. Store the values for time, temperature 1, and temperature 2 in a Numpy array. Use __time.time()__ to get the current time in seconds." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### C: Save Text File\n", "\n", "Save the temperatures to a text file \"HeaterTest.txt\" with three columns with the following headers:\n", "\n", "* Time (sec)\n", "* T1 (degC)\n", "* T2 (degC)\n", "\n", "The text file should be delimited with commas. Adjust the times so that they are relative to the start that is set to time=0. Load the text file with Pandas and print the first 5 rows to verify that the values are written correctly.\n", "\n", " data = pd.read_csv('HeaterTest.txt')\n", " print(data.head())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### D: Create Plot\n", "\n", "Create a plot of the temperature data (y-axis) versus the time (x-axis). Label the plot (x-label, y-label, legend), change the temperature 1 trend to be red circles, and change the temperature 2 trend to be blue circles." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 2: Regression" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A: Linear Regression\n", "\n", "Create a linear regression for Temperature 2. Report the sum of absolute difference between the measured values and the predicted values. Add the regression line as a black line to a plot with the measured temperature 2 as blue circles. Add appropriate labels to the plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### B: Quadratic Regression\n", "\n", "Create a linear and quadratic regression for Temperature 1. Report the sum of absolute difference between the measured values and the predicted values. Add a black line for the linear regression and a blue dashed regression curve for the quadratic regression. Plot the measured temperature 1 as red circles. Add appropriate labels to the plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### C: Nonlinear Regression\n", "\n", "Fit the $T_1$ data with a nonlinear regression of the form:\n", "\n", "$T_1 = a + b \\exp{(c \\, t)}$\n", "\n", "Report the sum of absolute difference between the measured values and the predicted values. Add a black line for the nonlinear regression. Plot the measured temperature 1 as red circles. Add appropriate labels to the plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 1 }