class: title, smokescreen, shelf, bottom, no-footer background-image: url(images/accel_cap.png) # 181U Spring 2020 ### Sensors --- layout: true .footer[ - Geoffrey Brown, 2020 - 181U ] <style> h1 { border-bottom: 8px solid rgb(32,67,143); border-radius: 2px; width: 90%; } .smokescreen h1 { border-bottom: none; } .small.remark-slide-content.compact {font-size:1.2rem} .smaller.remark-slide-content.compact {font-size:1.1rem} .small-code.remark-slide-content.compact code {font-size:1.0rem} .very-small-code.remark-slide-content.compact code {font-size:0.9rem} .line-numbers{ /* Set "line-numbers-counter" to 0 */ counter-reset: line-numbers-counter; } .line-numbers .remark-code-line::before { /* Increment "line-numbers-counter" by 1 */ counter-increment: line-numbers-counter; content: counter(line-numbers-counter); text-align: right; width: 20px; border-right: 1px solid #aaa; display: inline-block; margin-right: 10px; padding: 0 5px; } </style> --- class: compact # Agenda * Types of Sensors - examples from hobby vendors * Characteristics - communication interface, etc. * Digging Down * Software Model * Drivers --- class: compact,small,col-2 # Types of Sensors * Environmental - Temperature: **ambient**, infrared - **Humidity** - **Pressure** - Light - Proximity - Touch - Alcohol/Gas * Position - **Magnetometer** - GPS - Distance * Inertial - **Accelerometer** - **Gyroscope** * Audio/Image - Camera - Microphone * Mechanical - Strain - Weight - Compression - Flex - Vibration * Electrical - magnetic - current * Medical - heart (rate,ekg) - blood flow --- class: compact # Lab Board -- ST X-NUCLEO-IKS01A3 ![](images/space.png# w-10pct) ![](images/st-sensor-board.png# w-80pct) --- class: compact # Devices on ST X-NUCLEO-IKS01A3 This board is designed to allow evaluation of a collection of sensors. This is a typical offering of sensor manufacturers and makes system prototyping easy because there is no need to build hardware to start the software development effort. * [LSM6DSO:](https://www.st.com/en/mems-and-sensors/lsm6dso.html) MEMS 3D accelerometer (±2/±4/±8/±16 g) + 3D gyroscope (±125/±250/±500/±1000/±2000 dps) * [LIS2MDL:](https://www.st.com/en/mems-and-sensors/lis2mdl.html) MEMS 3D magnetometer (±50 gauss) * [LIS2DW12:](https://www.st.com/en/mems-and-sensors/lis2dw12.html) MEMS 3D accelerometer (±2/±4/±8/±16 g) * [LPS22HH:](https://www.st.com/en/mems-and-sensors/lps22hh.html) MEMS pressure sensor, 260-1260 hPa absolute digital output barometer * [HTS221:](https://www.st.com/en/mems-and-sensors/hts221.html) capacitive digital relative humidity and temperature * [STTS751:](https://www.st.com/en/mems-and-sensors/stts751.html) Temperature sensor (–40 °C to +125 °C) --- class: compact # LPS22HH Pressure sensor ![](images/space.png# w-10pct) ![](images/lps22h-pressure.png# w-40pct) ![](images/space.png# w-10pct) ![](images/steval-lps22hh.png# w-20pct) [datasheet](https://www.st.com/resource/en/datasheet/lps22hh.pdf) --- class: compact, small # Easily Available Development Boards ![https://www.sparkfun.com/categories/23?sort_by=name&per_page=200](images/sparkfunscreen.png# w-40pct)[link](https://www.sparkfun.com/categories/23?sort_by=name&per_page=200) ![](images/adafruit-sensors.png# w-40pct)[link](https://learn.adafruit.com/category/sensors) * There are a number of vendors selling small "breakout boards" for prototyping - Sparkfun - Adafruit * Also, manufacturers produce breakout boards available from - Digikey - Mouser --- class: compact,small,col-2 # While Devices are Diverse, Interface "Model" isn't * Small number of hardware *interfaces* -- subject of next lecture - Serial - I2C (used for lab device) - SPI - Analog (data delivered as voltage) * Programmer model - Device is configured/read through a set of "registers" -- typical byte-wide - Most devices have separate interrupt signal - Some devices have programmable autonomous behavior (e.g. monitor for interesting "events") * Low-level details are described in datasheet, often badly written * Low-level programming issues - Configuration (typically a lot of options) - Data/configuration spread across multiple registers at bit level - Calibration (many sensors have calibration procedure/values) * Low-level Drivers - Often provided by manufacturer of component - Abstract low-level details involving specific register r/w/bit-manipulation - Handle initialization, calibration - "Procedure" level interface (e.g. **float get_temperature()**) * High-level Drivers - Integrate device into RTOS/operating system - Handle sharing of interface among threads - Handle interrupts, DMA for data transfers, etc - May provide data *stream* for signals sampled at regular frequency --- class: compact # Hardware Interfaces of Lab Devices | Device | Type | UART | SPI | I2C | Interrupt | Stream | Event Monitor | |--------|------|:----:|:---:|:---:|:---------:|:--------------:|---------------| | LSM6DSO | Accelerometer/Gyrosope | | ✔ | ✔ | ✔ | ✔ | step, tilt, significant motion, tap | | LIS2MDL | Magnetometer | | ✔ | ✔ | ✔ | | significant change | | LIS2DW12 | Accelerometer | | ✔ | ✔ | ✔ | ✔ | stationary, motion, orientation, tap | | LPS22HH | Pressure | | ✔ | ✔ | ✔ | ✔ | threshold | | HTS221 | Temperature/Humidity | | ✔ | ✔ | | | | | STTS751 | Temperature | | | ✔ | ✔ | | high/low temperature limits | --- class: compact # STTS751 Temperature Sensor ![](images/space.png# w-3-12th) ![](images/stts751-block-diagram.png# w-50pct) --- class: compact # HTS221 Temperature/Humidity Sensor ![](images/space.png# w-20pct) ![](images/hts221-block-diagram.png# w-60pct) --- class: compact # LIS2DW12 Accelerometer ![](images/space.png# w-3-12th) ![](images/lis2dw12-block-diagram.png# w-50pct) --- class: compact # LPS22HH Pressure : Includes Signal Processing ![](images/space.png# w-3-12th) ![](images/lps22h-block-diagram.png# w-50pct) --- class: compact # LSM6DSO Accel/gyro : Complex Processing ![](images/lsm6dso-accel-filter.png# w-40pct fr) ![](images/lsm6dso-fsm.png# w-50pct) ![](images/lsm6dso-filters.png# w-50pct) --- class: compact # Digging Down -- [HTS221](https://www.st.com/resource/en/datasheet/hts221.pdf) ![](images/hts221-datasheet.png# w-40pct fr) Device Data Sheet Defines * Behavior * Electrical Properties * Sensor Specifications * Registers - Bit level definitions - Operation sequences - Configuration options --- class: compact # HTS221 Specifications ![](images/space.png# w-3-12th) ![](images/hts221-specifications.png# w-40pct) --- class: compact # HTS221 Register Map ![](images/hts221-registers.png# w-50pct fr) * WHO_AM_I : Device identifier * AV_CONF : Resolution Mode Configuration * CONTROL_REGISTERS : power down, reboot, * CTRL_REG2 : Control Register (e.g reboot) * STATUS_REG : Status (e.g. data available) * HUMIDITY_OUT : Humidity data * TEMP_OUT : Temperature data * Calibration registers : part specfic calibration constants for temperature and humidity --- class: compact # HTS221 Register Examples ![](images/hts221-status-reg.png# w-40pct) ![](images/space.png# w-2-12th) ![](images/hts221-temp-out.png# w-40pct) --- class: compact # HTS221 Calibration Example ![](images/space.png# w-3-12th) ![](images/hts221-calibration.png# w-40pct) --- class: class: very-small-code,compact,hljs-tomorrow-night-eighties # HTS221 Low-level Driver (Example) ![](images/hts221-status-reg.png# w-40pct fr) ```C #define HTS221_STATUS_REG 0x27U typedef struct { uint8_t t_da : 1; uint8_t h_da : 1; uint8_t not_used_01 : 6; } hts221_status_reg_t; ... int32_t hts221_status_get(stmdev_ctx_t *ctx, hts221_status_reg_t *val); int32_t hts221_temp_data_ready_get(stmdev_ctx_t *ctx, uint8_t *val); int32_t hts221_hum_data_ready_get(stmdev_ctx_t *ctx, uint8_t *val); int32_t hts221_humidity_raw_get(stmdev_ctx_t *ctx, uint8_t *buff); int32_t hts221_temperature_raw_get(stmdev_ctx_t *ctx, uint8_t *buff); ... ``` Low-level drivers simply provide a convenient interface to reading/writing registers by providing accessor functions to defined bit fields. --- class: class: very-small-code,compact,hljs-tomorrow-night-eighties # HTS221 Low-level Driver (Configuration) ```C /* Read humidity calibration coefficient */ axis1bit16_t coeff; lin_t lin_hum; hts221_hum_adc_point_0_get(&dev_ctx, &lin_hum.x0); hts221_hum_rh_point_0_get(&dev_ctx, &lin_hum.y0); hts221_hum_adc_point_1_get(&dev_ctx, &lin_hum.x1); hts221_hum_rh_point_1_get(&dev_ctx, &lin_hum.y1); /* Read temperature calibration coefficient */ lin_t lin_temp; hts221_temp_adc_point_0_get(&dev_ctx, &lin_temp.x0); hts221_temp_deg_point_0_get(&dev_ctx, &lin_temp.y0); hts221_temp_adc_point_1_get(&dev_ctx, &lin_temp.x1); hts221_temp_deg_point_1_get(&dev_ctx, &lin_temp.y1); /* Enable Block Data Update */ hts221_block_data_update_set(&dev_ctx, PROPERTY_ENABLE); /* Set Output Data Rate */ hts221_data_rate_set(&dev_ctx, HTS221_ODR_1Hz); /* Device power on */ hts221_power_on_set(&dev_ctx, PROPERTY_ENABLE); ``` --- class: very-small-code,compact,hljs-tomorrow-night-eighties # HTS221 Low-level Driver (Reading data) ```C while(1) { hts221_reg_t reg; hts221_status_get(&dev_ctx, ®.status_reg); if (reg.status_reg.h_da) { /* Read humidity data */ hts221_humidity_raw_get(&dev_ctx, data_raw_humidity.u8bit); humidity_perc = linear_interpolation(&lin_hum, data_raw_humidity.i16bit); } if (reg.status_reg.t_da) { /* Read temperature data */ hts221_temperature_raw_get(&dev_ctx, data_raw_temperature.u8bit); temperature_degC = linear_interpolation(&lin_temp, data_raw_temperature.i16bit); } ... ``` Data reading * Read status * Read raw data * Convert raw data to a measurement --- class: very-small-code,compact,hljs-tomorrow-night-eighties # HTS221 High(er)-level Driver ChibiOS driver provides an abstaction layer supporting multiple types of hydrometer,thermometer, etc. ![](images/chibios-hydrometer.png# w-40pct, fr) ```C /** * @brief Retrieves cooked data from the BaseHygrometer. ... * * @param[in] devp pointer to @p HTS221Driver. * @param[out] axes a buffer which would be filled with cooked data. * * @return The operation status. * @retval MSG_OK if the function succeeded. * @retval MSG_RESET if one or more I2C errors occurred, the errors can * be retrieved using @p i2cGetErrors(). * @retval MSG_TIMEOUT if a timeout occurred before operation end. * * @api */ #define hts221HygrometerReadCooked(devp, axes) \ hygrometerReadCooked(&((devp)->hygro_if), axes) ``` --- class: very-small-code,compact,hljs-tomorrow-night-eighties # HTS221 High(er)-level Driver ChibiOS Driver provides: * A portable interface * Encapsulation of low-level details * Intialization handles capturing calibration data and computing `m*x + b` ```C static msg_t hygro_read_cooked(void *ip, float axes[]) { HTS221Driver* devp; int32_t raw; msg_t msg; /* Getting parent instance pointer.*/ devp = objGetInstance(HTS221Driver*, (BaseHygrometer*)ip); msg = hygro_read_raw(ip, &raw); *axes = (raw * devp->hygrosensitivity) - devp->hygrobias; return msg; } ``` --- class: very-small-code,compact,hljs-tomorrow-night-eighties # HTS221 High(er)-level Driver ```C hts221ObjectInit(&HTS221D1); /* HTS221 Object Initialization.*/ hts221Start(&HTS221D1, &hts221cfg); /* Activates the HTS221 driver.*/ while (true) { hts221HygrometerReadCooked(&HTS221D1, &hygrocooked); hts221ThermometerReadCooked(&HTS221D1, &thermocooked); ... } } ``` --- class: compact # Summary * Sensors - types - software model - drivers * Cover image: By Tosaka - Self-made (Ref:èææåè 『éåデバイス』 ææåæåçç 2005å12æ20æåççè <a href="//commons.wikimedia.org/wiki/Special:BookSources/4534040083" title="Special:BookSources/4534040083">ISBN 4534040083</a>), <a href="https://creativecommons.org/licenses/by/3.0" title="Creative Commons Attribution 3.0">CC BY 3.0</a>, <a href="https://commons.wikimedia.org/w/index.php?curid=5204616">Link</a>