[C#][Sensor]使用 Memsic 2125 雙軸加速度計,量測傾斜角

  • 12520
  • 0
  • 2010-08-02

使用 Basic Stamp 2 微控制器與 Memsic 2125 雙軸加速度計量測傾斜角,透過 COM Port 與電腦連接,透過C#開發使用者介面,即時顯示雙軸傾斜角

 

1. 簡介

延續上一篇文章 [C#][Sensor]使用 Hitachi HM55B 電子羅盤,因為發文後反應不錯,因此介紹另一個感測元件 Memsic 2125。Memsic 2125 是由 Parallax 所研發的產品,可量測雙軸加速度或傾斜角。

 

28017

圖1 Memsic 2125

 

 

2. 方法

2.1 Memsic 2125

Memsic 2125 可量測兩軸加速度或反推兩軸傾斜角;其原理可想像是一個裝有氣體的槽,如圖2所示,中間有一個小的加熱器,從加熱器向四周延伸並安裝小型溫度計,當氣槽往某一方傾斜時,加熱器因為地心引力的關係,仍然維持與地表水平面垂直向上蒸發,此時,四周的四個溫度檢測計,因為熱氣的接近,便會有不同的溫度變化,利用溫度變化的程度來換算物體傾斜的資料。

clip_image002

圖2 Memsic 2125 原理示意圖
 

2.2 硬體接線與程式碼

(1) 硬體接線

參考 Memsic 2125 Accelerometer Demo Kit (#28017) 內的接線

image

圖4 Memsic 2125 雙軸加速度計與 Basic Stamp 2 接線圖

 

(2) 程式碼

使用 Memsic 2125 Accelerometer Demo Kit (#28017) 內的範例程式碼,在此改了輸出部分,起始判斷字元 s 與結束判斷字元 e,X軸、Y軸傾斜角之間用 | 隔開;

例如X傾斜角為20度、Y軸傾斜角10度,則輸出為 s20|10e,讓後續透過 COM Port 讀入電腦時,容易做字串處理


' -----[ I/O Definitions ]-------------------------------------------------

Xin             PIN     8                       ' X input from Memsic 2125
Yin             PIN     9                       ' Y input from Memsic 2125


' -----[ Constants ]-------------------------------------------------------

' Set scale factor for PULSIN

#SELECT $STAMP
  #CASE BS2, BS2E
    Scale       CON     $200                    ' 2.0 us per unit
  #CASE BS2SX
    Scale       CON     $0CC                    ' 0.8 us per unit
  #CASE BS2P
    Scale       CON     $0C0                    ' 0.75 us per unit
  #CASE BS2PE
    Scale       CON     $1E1                    ' 1.88 us per unit
#ENDSELECT

HiPulse         CON     1                       ' measure high-going pulse
LoPulse         CON     0

DegSym          CON     176                     ' degrees symbol


' -----[ Variables ]-------------------------------------------------------

xRaw            VAR     Word                    ' pulse from Memsic 2125
xmG             VAR     Word                    ' g force (1000ths)
xTilt           VAR     Word                    ' tilt angle

yRaw            VAR     Word
ymG             VAR     Word
yTilt           VAR     Word

disp            VAR     Byte                    ' displacement (0.0 - 0.99)
angle           VAR     Byte                    ' tilt angle

' -----[ Program Code ]----------------------------------------------------

Main:
  DO
    GOSUB Read_Tilt                             ' reads G-force and Tilt

    ' display results

    DEBUG "s",(xTilt.BIT15 * 13 + " "),
          DEC ABS xTilt,"|"

    DEBUG (yTilt.BIT15 * 13 + " "),
          DEC ABS yTilt,"e"

    PAUSE 200                                   ' update about 5x/second
  LOOP
  END


' -----[ Subroutines ]-----------------------------------------------------

Read_G_Force:
  PULSIN Xin, HiPulse, xRaw                     ' read pulse output
  xRaw = xRaw */ Scale                          ' convert to uSecs
  xmG = ((xRaw / 10) - 500) * 8                 ' calc 1/1000 g
  PULSIN Yin, HiPulse, yRaw
  yRaw = yRaw */ Scale
  ymG = ((yRaw / 10) - 500) * 8
  RETURN


Read_Tilt:
  GOSUB Read_G_Force
  disp = ABS xmG / 10 MAX 99                    ' x displacement
  GOSUB Arcsine
  xTilt = angle * (-2 * xmG.BIT15 + 1)          ' fix sign
  disp = ABS ymG / 10 MAX 99                    ' y displacement
  GOSUB Arcsine
  yTilt = angle * (-2 * ymG.BIT15 + 1)          ' fix sign
  RETURN


' Trig routines courtesy Tracy Allen, PhD. (www.emesystems.com)

Arccosine:
  disp = disp */ 983 / 3                        ' normalize input to 127
  angle = 63 - (disp / 2)                       ' approximate angle
  DO                                            ' find angle
    IF (COS angle <= disp) THEN EXIT
    angle = angle + 1
  LOOP
  angle = angle */ 360                          ' convert brads to degrees
  RETURN


Arcsine:
  GOSUB Arccosine
  angle = 90 - angle
  RETURN

 

2.3 電腦端使用者介面

直接用上一篇文章 [C#][Sensor]使用 Hitachi HM55B 電子羅盤 的程式下去修改

(1) Form

image

(2) 程式碼


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        delegate void SetTextCallback(string text);

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string carray = serialPort1.ReadExisting();
            SetText(carray);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.DataBits = 8;
            serialPort1.StopBits = System.IO.Ports.StopBits.One;
            serialPort1.Parity = System.IO.Ports.Parity.None;
            serialPort1.ReadTimeout = 100;
            serialPort1.Open();
        }

        private void SetText(string text)
        {
            try
            {
                if (this.txtX.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(SetText);
                    this.Invoke(d, new object[] { text });
                }
                else
                {
                    txtTemp.Text += text;
                    if (txtTemp.Text.Length < 10) { return; }

                    string[] strTempS = txtTemp.Text.Split('s');
                    if (strTempS.Length <= 1) { return; }

                    string[] strTempE = strTempS[1].Split('e');
                    if (strTempE.Length > 0)
                    {
                        string[] strAngle = strTempE[0].Split('|');
                        this.txtX.Text = strAngle[0];
                        this.txtY.Text = strAngle[1];
                        txtTemp.Clear();
                    }
                }
            }
            catch 
            {
            }
        }
    }
}

3. 結果

影片為硬體 Basic Stamp 2 與 Memsic 2125 雙軸加速度計傾斜時,電腦即時接收到的X軸與Y軸傾斜角,並顯示於使用者介面上。

影片中先X軸方面傾斜,在往Y軸方向傾斜。