No Code AI(肉寇)AI自動化兩日精通|實體6小時+線上6小時
|

如何為ARM mbed加上三軸陀螺儀?

   

作者:Ben Shiue

 

陀螺旋進是日常生活中常見的現象,小時候玩過陀螺的就知道,在一定的旋轉速度下,它就能一直保持平衡。陀螺儀是一種用來感測與維持方向的裝置,其中三軸陀螺儀最大的作用就是測量“角速度”,以判別物體的運動狀態,它的應用很廣,例如用於飛行器上,就可讓飛行器知道自己目前要去哪,以及如何保持平衡穩定的飛行方向。

此篇文章為 ARM mbed 加上三軸陀螺儀(Grove – 3-Axis Digital Gyro) Block 的使用方法,首先讓大家看一下陀螺儀的外貌,如下圖:

三軸陀螺儀(Grove - 3-Axis Digital Gyro)

三軸陀螺儀(Grove – 3-Axis Digital Gyro)

準備工作

這次的 Block 整合是要將陀螺儀的 Sensor Data 推送至 WebSocket。 首先第一步,我們透過 ARM mbed LPC 1768 版子將三軸陀螺儀(Grove – 3-Axis Digital Gyro)與Wifly模組串接起來,如下圖:

將三軸陀螺儀與Wifly模組串接起來

將三軸陀螺儀與Wifly模組串接起來

開始實作

接下來技術實作的部分就分為三個部分:

  • Websocket channel server 服務,本文將使用 sockets.mbed.org
  • ARM mbed 的 Websocket client 實作
  • ARM mbed 與 Grove – 3-Axis Digital Gyro 實作


#include "mbed.h"
#include "WiflyInterface.h"
#include "Websocket.h"
#include "ITG3200.h"


/* wifly interface:
*     - p9 and p10 are for the serial communication
*     - p19 is for the reset pin
*     - p26 is for the connection status
*     - "mbed" is the ssid of the network
*     - "password" is the password
*     - WPA is the security
*/
WiflyInterface wifly(p13, p14, p19, p26, "WWNet", "mmmmmmmm", WPA);  
DigitalOut led1(LED1);  
ITG3200 gyro(p9, p10, 0x68);



int main() {  
    wifly.init(); //Use DHCP
    //wifly.init("192.168.21.33","255.255.255.0","192.168.21.2");
    while (!wifly.connect());
    led1=1;
    printf("IP Address is %s\n\r", wifly.getIPAddress());

    Websocket ws("ws://sockets.mbed.org/ws/mbedschool/viewer");
    //Websocket ws("ws://192.168.199.159:8888");
    while (!ws.connect());
    led1=2;


    int x = 0, y = 0, z = 0, temp = 0;
    //Set highest bandwidth.
    gyro.setLpBandwidth(LPFBW_42HZ);

    while (1) {
        char data[256];
        wait(0.1f);
        x = gyro.getGyroX();
        y = gyro.getGyroY();
        z = gyro.getGyroZ();
        temp = gyro.getTemperature();

        printf("Temp: %d, X: %d, Y: %d, Z: %d\n", temp, x, y, z);
        sprintf( data , "Temp: %d, X: %d, Y: %d, Z: %d\n", temp, x, y, z );
        ws.send(data);

        //wait(1.0);
    }
}
 

(本文轉載自合作單位Oranwind原文連結

Ben Shiue

訂閱MakerPRO知識充電報

與40000位開發者一同掌握科技創新的技術資訊!

Author: Ben Shiue

Ben是一位PRO Maker及全端工程師(full stack engineer),他動手做的興趣為迷你四驅車,開發領域則包括 Node.js、ARM mbed、IoT解決方案。 他是OranwindMakee.io共同創辦人。

Share This Post On
468 ad

Submit a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *