Search Code

用手勢控制Arduino的伺服器位置

用手勢控制Arduino的伺服器位置

喺呢個教學入面,我哋會學點樣用手勢控制伺服馬達。透過將手向左或向右移動,你可以令伺服馬達作出相應嘅反應。呢個項目利用APDS-9960手勢傳感器來檢測手部動作,並將佢哋轉換成伺服位置。想要詳細嘅視覺解釋,記得去睇下視頻(喺視頻入面喺00:30)。

APDS-9960 gesture sensor module -1

我們將使用的核心組件包括伺服電機和 APDS-9960 手勢傳感器。伺服電機會根據檢測到的手勢旋轉到特定角度,而 APDS-9960 傳感器會解讀手部動作。這個設置讓人可以直觀地控制伺服電機,而無需實體接觸。

硬件解说

伺服馬達係一個細小嘅裝置,可以轉動到特定角度,適合用於需要精確定位嘅應用。佢通常有三條線:電源(紅色)、接地(黑色)同信號(橙色)。信號線接收命令,令伺服馬達移動到指定角度。

APDS-9960 係一個多功能嘅感應器,可以檢測手勢、環境光同顏色。佢透過 I2C 同 Arduino 通訊,需要幾個引腳來供電同數據傳輸。呢個感應器可以檢測到上、下、左、右等動作,我哋會用嚟控制伺服器。

數據表詳情

製造商博通
零件编号APDS-9960
邏輯/輸入輸出電壓2.4 - 3.6 V
供應電壓2.4 - 3.6 V
每個通道的輸出電流最大20毫安
PWM 頻率指導不適用
輸入邏輯閾值0.3 伏 (低), 0.7 伏 (高)
電壓降 / RDS(通)/ 飽和度不適用
熱限制-40至85°C
包裹6.0 x 3.0 毫米
備註 / 變體手勢感應,RGB燈光感應
  • 確保伺服器有足夠的電壓供應(通常是5V)。
  • 將 APDS-9960 透過 I2C 通訊連接到 Arduino。
  • 留意手勢感應器嘅中斷引腳。
  • 如有需要,使用上拉電阻以確保信號檢測穩定。
  • 確保喺唔同嘅光線條件下校準感應器,以提高準確性。
Arduino wiring of APDS9960 to control Servo motor using hand gesture

要接線部件,首先從伺服馬達開始。將紅線連接到Arduino的5V引腳,黑線連接到地(GND),橙色信號線連接到引腳。9呢個會令Arduino可以控制伺服器嘅位置。

接住,接線APDS-9960手勢感應器。連接個V_N釘到Arduino的3.3V,然後GND釘住地面。該INT引腳應該連接到引腳2在Arduino上,當...SDASCL針去針A4A5,分别。这个配置让传感器可以有效地同Arduino沟通。

代碼示例及步驟講解

喺設置函數入面,我哋初始化伺服器同手勢感應器。嗰行myservo.attach(9);將伺服器綁定到我們之前連接的9號針腳。這樣可以讓伺服器接收來自Arduino的指令。

APDS-9960 gesture sensor -3
void setup() {
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 // Set interrupt pin as input
 pinMode(APDS9960_INT, INPUT);
 // Initialize Serial port
 Serial.begin(9600);
}

接住,我哋定義系統點樣喺循環函數入面處理手勢。個方法handleGesture();檢查可用嘅手勢並執行相應嘅動作。例如,如果檢測到嘅手勢係左邊,伺服器就會移動到180度。

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_LEFT:
        myservo.write(180);  // added by RoboJax
        break;
      case DIR_RIGHT:
        myservo.write(0);   // added by RoboJax
        break;
    }
  }
}

最後,循環不斷檢查手勢,並根據檢測到的手勢更新伺服器位置。確保在不同的光照條件下測試設置,以獲得準確的手勢識別。

示範 / 期待什麼

一旦所有嘢都接好線同埋代碼上載完,你應該可以透過將手向左或向右移動來控制伺服器。伺服器會因為左邊手勢而轉動到180度,因為右邊手勢而返回0度。如果冇檢測到手勢,檢查下感應器嘅位置同埋周圍嘅光線條件(喺視頻05:30)。

章節

  • 介紹 - :00
  • 硬件概述 - 01:30
  • 接線指示 - 03:00
  • 代碼解釋 - 04:30
  • 示範 - 06:00

圖片

APDS-9960 gesture sensor module -2
APDS-9960 gesture sensor module -2
APDS-9960 gesture sensor module -1
APDS-9960 gesture sensor module -1
Arduino wiring of APDS9960 to control Servo motor using hand gesture
Arduino wiring of APDS9960 to control Servo motor using hand gesture
APDS-9960 gesture sensor -3
APDS-9960 gesture sensor -3
12-Source code for controlling a servo with your hand
語言: C++
++
/****************************************************************
Modified for RoboJax by A.B.S. on May 09, 2017
in Ajax, Ontario, Canada. www.RoboJax.com


Original Source:
APDS-9960 RGB and Gesture Sensor
Shawn Hymel @ SparkFun Electronics
May 30, 2014
https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor


Distributed as-is; no warranty is given.
****************************************************************/

#include <Wire.h>

// added by RoboJax
#include <Servo.h>

#include <SparkFun_APDS9960.h>


Servo myservo;  // create servo object to control a servo // added by RoboJax

// Pins
#define APDS9960_INT    2 // Needs to be an interrupt pin

// Constants

// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;

void setup() {
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);

  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  Serial.println(F("--------------------------------"));
  
  // Initialize interrupt service routine
  attachInterrupt(0, interruptRoutine, FALLING);

  // Initialize APDS-9960 (configure I2C and initial values)
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
  }
  
  // Start running the APDS-9960 gesture sensor engine
  if ( apds.enableGestureSensor(true) ) {
    Serial.println(F("Gesture sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
  }
}

void loop() {
  if( isr_flag == 1 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
}

void interruptRoutine() {
  isr_flag = 1;
}

void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      case DIR_LEFT:
        Serial.println("LEFT");
        myservo.write(180);  // added by RoboJax
        break;
      case DIR_RIGHT:
        Serial.println("RIGHT");
        myservo.write(0);   // added by RoboJax
        break;
      case DIR_NEAR:
        Serial.println("NEAR");
        break;
      case DIR_FAR:
        Serial.println("FAR");
        break;
      default:
        Serial.println("NONE");
    }
  }
}

資源與參考資料

暫時冇資源。

文件📁

冇文件可用。