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 <audio controls> <source src="sensors_2.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> * Types of Sensors - examples from hobby vendors * Characteristics - communication interface, etc. * Digging Down * Software Model * Drivers --- class: compact,small,col-2 # Types of Sensors <audio controls> <source src="sensors_3.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> * 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 <audio controls> <source src="sensors_4.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>   <audio controls> <source src="test2.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> --- class: compact # Devices on ST X-NUCLEO-IKS01A3 <audio controls> <source src="sensors_5.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> 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 <audio controls> <source src="sensors_6.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>     [datasheet](https://www.st.com/resource/en/datasheet/lps22hh.pdf) --- class: compact, small # Easily Available Development Boards <audio controls> <source src="sensors_7.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> [link](https://www.sparkfun.com/categories/23?sort_by=name&per_page=200) [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 <audio controls> <source src="sensors_8.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> * 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 <audio controls> <source src="sensors_9.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> | 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 <audio controls> <source src="sensors_10.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>   --- class: compact # HTS221 Temperature/Humidity Sensor <audio controls> <source src="sensors_11.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>   --- class: compact # LIS2DW12 Accelerometer <audio controls> <source src="sensors_12.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>   --- class: compact # LPS22HH Pressure : Includes Signal Processing <audio controls> <source src="sensors_13.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>   --- class: compact # LSM6DSO Accel/gyro : Complex Processing <audio controls> <source src="sensors_14.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>    --- class: compact # Digging Down -- [HTS221](https://www.st.com/resource/en/datasheet/hts221.pdf) <audio controls> <source src="sensors_15.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>  Device Data Sheet Defines * Behavior * Electrical Properties * Sensor Specifications * Registers - Bit level definitions - Operation sequences - Configuration options --- class: compact # HTS221 Specifications <audio controls> <source src="sensors_16.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>   --- class: compact # HTS221 Register Map <audio controls> <source src="sensors_17.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>  * 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 <audio controls> <source src="sensors_18.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>    --- class: compact # HTS221 Calibration Example <audio controls> <source src="sensors_19.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>   --- class: class: very-small-code,compact,hljs-tomorrow-night-eighties # HTS221 Low-level Driver (Example) <audio controls> <source src="sensors_20.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>  ```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) <audio controls> <source src="sensors_21.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> ```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,col-2 # HTS221 Low-level Driver (Reading data) <audio controls> <source src="sensors_22.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> Data reading * Read status * Read raw data * Convert raw data to a measurement <br> <br> ```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); } ... ``` --- class: very-small-code,compact,hljs-tomorrow-night-eighties # HTS221 High(er)-level Driver <audio controls> <source src="sensors_23.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> ChibiOS driver provides an abstaction layer supporting multiple types of hydrometer,thermometer, etc.  ```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 <audio controls> <source src="sensors_24.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> 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 <audio controls> <source src="sensors_25.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> ```C hts221ObjectInit(&HTS221D1); /* HTS221 Object Initialization.*/ hts221Start(&HTS221D1, &hts221cfg); /* Activates the HTS221 driver.*/ while (true) { hts221HygrometerReadCooked(&HTS221D1, &hygrocooked); hts221ThermometerReadCooked(&HTS221D1, &thermocooked); ... } } ``` --- class: compact # Summary <audio controls> <source src="sensors_26.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio> * Sensors - types - software model - drivers * Cover image: By Tosaka - <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>