{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# QCoDeS Example with the Lakeshore Model 372 to Control the Temperature of the Bluefors Fridge\n", "\n", "The Lakeshore Temperature Controller Model 372 is used to control the temperature of the Bluefors fridges.\n", "\n", "To use it as such outside of the control software provided by Bluefors, one has to establish an addtional connection. Within the Bluefors system, the Lakeshore is connected via its usb port (through a USB hub along with the other devices) to the control Laptop (as part of the Bluefors setup). To control the temperature of the fridge via QCoDeS, it is the most convenient to connect the Lakeshore via its LAN port to the control computer (the one with QCoDeS, not the one from Bluefors). In order to reach the LAN port of the Lakeshore, the Bluefors rack has to be opened, and the PCB board that is fixed to the metal board has to be opened as well (it's a door as well with magnetic clips on one side). Do NOT disconnect the USB! Then switch the operation mode (usually there is an Interface button on the instrument) from USB to LAB. When using a router, remember to set the IP address setting to DHCP. Finally, use the following address format for VISA address: `\"TCPIP::::::SOCKET\"`, where \"port\" is a known value from the manual (most probably, \"7777\").\n", "\n", "As mentioned above, for using the Lakeshore with QCoDeS, the operation mode has to be switched from USB to LAN. When done with the measurements, please, switch back to USB, so that the logging of the Temperature provided by the Bluefors software continues. It is planned to implement a server that takes care of the logging, so that the switching to USB will no longer be necessary. For the time being, please, always remember to switch back!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Driver Setup\n", "\n", "This notebook is using a simulated version of the driver, so that it can be run and played with, without an actual instrument. When trying it out with a real Lakeshore, please set `simulation = False`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "simulation = True" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connected to: None lakeshore_372 (serial:None, firmware:None) in 0.19s\n" ] } ], "source": [ "if simulation:\n", " from qcodes.tests.drivers.test_lakeshore import Model_372_Mock as Model_372\n", " import qcodes.instrument.sims as sims\n", " visalib = sims.__file__.replace('__init__.py',\n", " 'lakeshore_model372.yaml@sim')\n", " ls = Model_372('lakeshore_372', 'GPIB::3::65535::INSTR',\n", " visalib=visalib, device_clear=False)\n", "else:\n", " from qcodes.instrument_drivers.Lakeshore.Model_372 import Model_372\n", " # put visa address here, see e.g. NI Max\n", " # or look up the IP address on \n", " # the instrument itself\n", " ls = Model_372('lakeshore_372', 'TCPIP::192.168.0.160::7777::SOCKET')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Readout Sensor Channels\n", "\n", "The lakeshore has two types of *channels*: *Readout channels* and *heaters*. For reading the temperature we use the readout channels. There are sixteen channels, each of which has the following parameters:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'temperature': ,\n", " 't_limit': ,\n", " 'sensor_raw': ,\n", " 'sensor_status': ,\n", " 'sensor_name': ,\n", " 'enabled': ,\n", " 'dwell': ,\n", " 'pause': ,\n", " 'curve_number': ,\n", " 'temperature_coefficient': ,\n", " 'excitation_mode': ,\n", " 'excitation_range_number': ,\n", " 'auto_range': ,\n", " 'range': ,\n", " 'current_source_shunted': ,\n", " 'units': }" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ls.ch01.parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All the parameters have docstrings, labels, and units, when applicable.\n", "\n", "Some of these parameters have been added just because other interesting parameters can only be set together with these (Lakeshore uses VISA commands with multiple inputs/outputs).\n", "\n", "Some parameters like `curve_number` should not be changed, unless the user knows what he's doing." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to read temperature values from all the sensors, we can do the following:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Temperature of ch01 (on): 4.0 kelvin\n", "Temperature of ch02 (on): 4.0 kelvin\n", "Temperature of ch03 (on): 4.0 kelvin\n", "Temperature of ch04 (on): 4.0 kelvin\n", "Temperature of ch05 (on): 4.0 kelvin\n", "Temperature of ch06 (on): 4.0 kelvin\n", "Temperature of ch07 (on): 4.0 kelvin\n", "Temperature of ch08 (on): 4.0 kelvin\n", "Temperature of ch09 (on): 4.0 kelvin\n", "Temperature of ch10 (on): 4.0 kelvin\n", "Temperature of ch11 (on): 4.0 kelvin\n", "Temperature of ch12 (on): 4.0 kelvin\n", "Temperature of ch13 (on): 4.0 kelvin\n", "Temperature of ch14 (on): 4.0 kelvin\n", "Temperature of ch15 (on): 4.0 kelvin\n", "Temperature of ch16 (on): 4.0 kelvin\n" ] } ], "source": [ "for ch in ls.channels:\n", " print(f'Temperature of {ch.short_name} ({\"on\" if ch.enabled() else \"off\"}): {ch.temperature()} {ch.units()}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `enabled` parameter of the sensor channel is very important because Lakeshore gets readings from the enabled channels in sequence. This means that if you have 3 channels enabled, while you are contantly requesting the temperature reading from only the first one, the array of readings will have parts when the value is constant. This is because within those parts Lakeshore was busy with reading temperature from the other two channels.\n", "\n", "The `units` parameter is also of big importance. As it will be explained below, it defines the units from the `setpoint` value of the heater that is used in a `closed_loop` mode." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Heating and feedback loop\n", "\n", "To set a certain temperature one needs to start a feedback loop that reads the temperature from a sensor channel, and feeds it back to the sample through a heater. The Lakeshore 372 has three heaters: `sample_heater`, `warmup_heater`, and `analog_heater`.\n", "\n", "Here the `sample_heater` will be used. It has the following parameters:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'mode': ,\n", " 'input_channel': ,\n", " 'powerup_enable': ,\n", " 'P': ,\n", " 'I': ,\n", " 'D': ,\n", " 'output_range': ,\n", " 'setpoint': ,\n", " 'range_limits': ,\n", " 'wait_cycle_time': ,\n", " 'wait_tolerance': ,\n", " 'wait_equilibration_time': ,\n", " 'blocking_t': ,\n", " 'polarity': ,\n", " 'use_filter': ,\n", " 'delay': }" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h = ls.sample_heater\n", "h.parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The allowed modes, polarities, and ranges are defined in:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'off': 0,\n", " 'monitor_out': 1,\n", " 'open_loop': 2,\n", " 'zone': 3,\n", " 'still': 4,\n", " 'closed_loop': 5,\n", " 'warm_up': 6}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h.MODES" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'off': 0,\n", " '31.6μA': 1,\n", " '100μA': 2,\n", " '316μA': 3,\n", " '1mA': 4,\n", " '3.16mA': 5,\n", " '10mA': 6,\n", " '31.6mA': 7,\n", " '100mA': 8}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h.RANGES" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'unipolar': 0, 'bipolar': 1}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h.POLARITIES" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Working with closed loop control" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To use a closed loop control, we need to set the `P`, `I`, `D` values, choose an `input_channel` that will be read within the closed loop, set the range of the heater (`output_range`), set the `setpoint` value (e.g. the target temperature), and start the operation by setting `mode` to `closed_loop`." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "h.P(10)\n", "h.I(10)\n", "h.D(0)\n", "h.output_range('31.6μA')\n", "h.input_channel(9)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "h.setpoint(0.01)\n", "h.mode('closed_loop')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Units of the setpoint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Be careful when setting the value of the `setpoint` - Lakeshore uses \"preferred units\" for it which are determined by the `units` parameter of the chosen `input_channel`. Thanks to that, Lakeshore 372 supports setting `setpoint` in `ohms` and `kelvins`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'kelvin'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ls.ch09.units()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The value of the setpoint in the preferred units of the control loop sensor (which is set via `input_channel` parameter)\r\n", "\r\n", "Parameter class:\r\n", "\r\n", "* `name` setpoint\r\n", "* `label` Setpoint value (in sensor units)\r\n", "* `unit` \r\n", "* `vals` \n" ] } ], "source": [ "print(h.setpoint.__doc__) # when working in Jupyter, just use `h.setpoint?` syntax" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Disable unrelated channels for continuos readings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that in order to have Lakeshore constantly reading from the `input_channel`, you need to disable other channels. Otherwise, Lakeshore will be reading all the enabled channels one by one, which will slow down the convergence of the control loop." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "ls.ch03.enabled(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Observe control loop working" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can observe how the temperature gets steered towards the setpoint (This is not implemented in the simulated instrument)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "T = 4.0\n", "T = 4.0\n", "T = 4.0\n", "T = 4.0\n", "T = 4.0\n" ] } ], "source": [ "import time\n", "for i in range(5):\n", " time.sleep(0.1)\n", " print(f'T = {ls.ch09.temperature()}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Textual representation is not very convenient, hence let's do the same but now with plotting (This is not implemented in the simulated instrument):" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "%matplotlib notebook\n", "\n", "import time\n", "import numpy\n", "from IPython.display import display\n", "from ipywidgets import interact, widgets\n", "from matplotlib import pyplot as plt\n", "\n", "def live_plot_temperature_reading(channel_to_read, read_period=0.2, n_reads=1000):\n", " \"\"\"\n", " Live plot the temperature reading from a Lakeshore sensor channel\n", " \n", " Args:\n", " channel_to_read\n", " Lakeshore channel object to read the temperature from\n", " read_period\n", " time in seconds between two reads of the temperature\n", " n_reads\n", " total number of reads to perform\n", " \"\"\"\n", "\n", " # Make a widget for a text display that is contantly being updated\n", " text = widgets.Text()\n", " display(text)\n", "\n", " fig, ax = plt.subplots(1)\n", " line, = ax.plot([], [], '*-')\n", " ax.set_xlabel('Time, s')\n", " ax.set_ylabel(f'Temperature, {channel_to_read.units()}')\n", " fig.show()\n", " plt.ion()\n", "\n", " for i in range(n_reads):\n", " time.sleep(read_period)\n", "\n", " # Update the text field\n", " text.value = f'T = {channel_to_read.temperature()}'\n", "\n", " # Add new point to the data that is being plotted\n", " line.set_ydata(numpy.append(line.get_ydata(), channel_to_read.temperature()))\n", " line.set_xdata(numpy.arange(0, len(line.get_ydata()), 1)*read_period)\n", "\n", " ax.relim() # Recalculate limits\n", " ax.autoscale_view(True, True, True) # Autoscale\n", " fig.canvas.draw() # Redraw" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3521c6b254ee4bb8b954a6e101a06b5c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Text(value='')" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " this.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('