Skip to content

Instantly share code, notes, and snippets.

@boulama
Created February 3, 2023 17:08
Show Gist options
  • Save boulama/5e35ae81d6a21ea18ecd50731d2914a6 to your computer and use it in GitHub Desktop.
Save boulama/5e35ae81d6a21ea18ecd50731d2914a6 to your computer and use it in GitHub Desktop.
transducer.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNOFQA97RWOykjx+t00QmhX",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/boulama/5e35ae81d6a21ea18ecd50731d2914a6/transducer.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"This notebook details how to convert the voltage received from the arduino in pressure both in PSI and kPA.\n",
"\n",
"We determine the m and c data for the linear relation between pressure and voltage."
],
"metadata": {
"id": "hJ9VS3e0E9BD"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "A5Gkq3qt-wy1"
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"source": [
"voltages = np.linspace(0, 5, 10)\n",
"pressures = np.linspace(0, 5000, 10)"
],
"metadata": {
"id": "8UwIwBPx-0K4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"A = np.vstack([voltages, np.ones(len(voltages))]).T\n",
"A"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_Kkbq-alAvEd",
"outputId": "ff59660c-5cf8-4f2e-bc33-e3441a83bd3f"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[0. , 1. ],\n",
" [0.55555556, 1. ],\n",
" [1.11111111, 1. ],\n",
" [1.66666667, 1. ],\n",
" [2.22222222, 1. ],\n",
" [2.77777778, 1. ],\n",
" [3.33333333, 1. ],\n",
" [3.88888889, 1. ],\n",
" [4.44444444, 1. ],\n",
" [5. , 1. ]])"
]
},
"metadata": {},
"execution_count": 3
}
]
},
{
"cell_type": "code",
"source": [
"m, c = np.linalg.lstsq(A, pressures, rcond=False)[0]"
],
"metadata": {
"id": "NW1bB8ANAwD8"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"m, c"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QPEh0lQ4BiSK",
"outputId": "43aff1f6-091b-427b-e906-7a16d51227bc"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(999.9999999999999, 5.913811846717936e-13)"
]
},
"metadata": {},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"source": [
"def find_pressure(v):\n",
" return (v - c)"
],
"metadata": {
"id": "I5Mc1AD8C_We"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"pressure_psi = find_pressure(2)\n",
"pressure_pa = pressure_psi * 6_895E-3\n",
"print('Pressure in PSI {}\\nPressure in KPa: {}'.format(pressure_psi, pressure_pa))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TjWIoUJ9DXWp",
"outputId": "2ef30f56-41ba-47a4-bba3-cd0039d2073e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Pressure in PSI 1.9999999999994087\n",
"Pressure in KPa: 13.789999999995922\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "xJWsaXlTDvKA"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment