2023年1月4日星期三

[Ameba] RTL8720DN 與 V7RC App 藍芽控制

Ameba 與手機連結

V7RC 是由 嵐奕科技 所開發的 App , 可以在 iOS 及 Android 系統運行。 V7RC App 可透過 藍芽及 WiFi 控制一般遙控車或是遠端控制應用。 在這裡做簡單直覺的範例做為基礎,望在爾後做更多的應用。

首先,開啟 範例
File / Examples / AmebaBLE / BLEUartService

ESP32,RTL8720DN,BW16

直接編譯並上傳至 RTL8720DN

V7RC App

設定連接藍芽

ESP32,RTL8720DN,BW16,V7RC

選定 車輛模式

ESP32,RTL8720DN,BW16,V7RC

開啟 Arduino IDE 的 Serial Monitor

移動 左右舵 及 上下舵
節錄一小段反饋

Received string: SRV1500150015001500#

SRV1500150015001500# 是由 V7RC 反饋的訊息

所以是
SRV (前綴碼) + 4 組 4 位數 + '#' 結束符
所組成的
前綴碼會依不同模式,而有所不同.
例如:
車輛: SRV
坦克: SRT

只要針對反饋的數值做處理就可以對我們的標的做控制。
到這裡,可以操作 左右舵及上下舵 看看有甚麼變化,這裡就不贅述

建立結構


typedef struct{
    bool reciveCMDFlag;
    int  ReciveValue;
    int  Pin;
    AmebaServo Servo;

}_rCMD;

uint8_t DefinePin[2] = {3,12};

定義 3,12 為PWM 輸出腳位

建立 - 解析反饋訊息


void ParseCMDString(String cmd)
{
    int comdLength = cmd.length();
    if(cmd.charAt(comdLength - 1) != '#')
        return;
    if(cmd.indexOf("SRV") > -1 ){
        int x = 3;
        int ValueIndex = 0;
        while(x < comdLength - 1){
            if(x + 3 < comdLength){
                String _NumString = cmd.substring(x,x + 4);
                
                if(ValueIndex < MaxNumValue){
                    if(bleReciveData[ValueIndex].ReciveValue != _NumString.toInt()){
                        bleReciveData[ValueIndex].ReciveValue = _NumString.toInt();
                        bleReciveData[ValueIndex].reciveCMDFlag = true;
                    }
                }
            }
            ValueIndex++;
            x += 4;
            Serial.println();
        }
        
    }
}

這個解析函式,將放在 writeCB call back function 裡
會自動填入 bleReciveDate 這個 Arrary.

當 reciveCMDFlag 為 true , 在 loop()裡就會個別對 sg90 Servo 控制。


void loop()
{

    while(Count < MaxNumValue) {
        
            if(bleReciveData[Count].reciveCMDFlag && bleReciveData[Count].Pin == 3){
                bleReciveData[Count].reciveCMDFlag = false;

                int Angle = map(bleReciveData[Count].ReciveValue,1000,2000,0,180);
                bleReciveData[Count].Servo.write(Angle);
            }
            

            if(bleReciveData[Count].reciveCMDFlag && bleReciveData[Count].Pin == 12){
                bleReciveData[Count].reciveCMDFlag = false;

                int Angle = map(bleReciveData[Count].ReciveValue,1000,2000,0,180);
                bleReciveData[Count].Servo.write(Angle);

            }

        
        Count++;
    }
    Count = 0;
    delay(1);
}

 

另外,在原範例的


Rx.setWriteProperty(true);

改為


Rx.setWriteNRProperty(true);

這個範例是控制 2個 SG90 Servo,若控制不同裝置時,例如: 驅動馬達,就要視情況再修改程式

 

原始程式碼

https://github.com/cold63/Arduino_Code/tree/main/V7RC_RTL8720DN

 

相關連結

iOS V7RC App
https://apps.apple.com/tw/app/v7rc/id1390983964
Android V7RC App
https://play.google.com/store/apps/details?id=com.v7idea.v7rcliteandroidsdkversion&hl=zh_TW&gl=US

 

0 comments:

發佈留言