2014-11-01 62 views
2

我有一個英特爾伽利略我試圖用Node.js的控制,但我跨越了幾個問題來了。我正在使用的庫有更改引腳的二進制和/或模擬值的示例,但沒有關於控制伺服電機的具體內容。我目前的代碼如下。伽利略MRAA控制伺服的節點

var B = 3975; 
var mraa = require("mraa"); 
var servo = new mraa.Aio(1);//connects to the servo 

事情是,我不知道如何控制伺服,MRAA的文檔幾乎不存在。有沒有人在這裏做過類似的事情,並能夠提供幫助?

謝謝。

回答

0

伺服系統通常使用PWM(脈寬調製)控制。您應該將伺服控制線連接到標有'〜'符號的數字引腳之一。該符號表示該引腳將生成PWM數據。然後谷歌你的伺服類型,以瞭解它接受的PWM參數。然後可以按照mraa PWM示例爲PWM引腳設置合適的值。

0

PWM實施例對mraa(愛迪生但將工作於伽利略但期應當是62500)

mraa = require('mraa'); 
servo = new m.Pwm(3);//Pin 3 
servo.enable(true); 
servo.period_us(19000000) //PWM Period https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM 
servo.write(1); 
sleep(1000); 
servo.write(0); 
sleep(1000); 
servo.write(0.5); 
0

要控制伺服,則發送給它的調製信號的脈衝寬度,這是這是一個信號定期的,即重複自己。每個週期,即信號點和其重複之間的時間,由兩部分組成:開和關。 on是高電壓(例如等於偏壓),off是低電壓(例如等於0 Volt)。期間有一個時間段是時間段,它的倒數是頻率。開機時間與關機時間之間的比率稱爲佔空比,佔空比範圍爲0.0至1.0。伺服電機只旋轉到達到與佔空比對應的角度並停止。

什麼這裏之前是鏈接到mraa node.js的文檔: https://iotdk.intel.com/docs/master/mraa/node/

和一張紙條說:mraa是一種低層次的框架,因此,如果這是您第一次使用伺服,我會建議你延遲使用mraa以後以及第一次使用CylonJS,用於控制使用CylonJS英特爾愛迪生的伺服這是相當類似於英特爾伽利略教程是: http://slideplayer.com/slide/7959041/ 這是一個非常好的例子,我在英特爾愛迪生套件之前跑了。

這就是說,一旦你使用本教程結束,想嘗試在mraa node.js的伺服,這裏是直到你按下Ctrl-C結束程序旋轉伺服的教程。它在0開始佔空比,將其增加到1,然後遞減到0,然後再次循環。此代碼是在 https://navinbhaskar.wordpress.com/2016/02/21/cc-on-intel-edisongalileo-part3-pwm/ C代碼的翻譯和我沒有測試的翻譯。

/*translation of C++ code at 
https://navinbhaskar.wordpress.com/2016/02/21/cc-on-intel-edisongalileo-part3-pwm/ 
mraa node.js documentation at: 
https://iotdk.intel.com/docs/master/mraa/node/ 
*/ 
"use strict"; 


const mraa = require("mraa"); 
const spawnSync = require('child_process').spawnSync; 

const PWM_PIN = 5 ;  /**< The pin where the LED is connected */ 

var keepRunning= false; 

///** Signal handler used to stop this application cleanly */ 
/* 
    * Associate ctrl+c with our handler that clears the 'keepRunning' 
    * flag that allows us to stop the PWM when exiting 
    */ 
process.on('SIGINT',() => { 
    keepRunning = false; 
}); 

//Step 1: Initialize the mraa system 

var result =mraa.init(); 
if(result == mraa.Result.SUCCESS) 
    console.log("mraa initialization succeded."); 
else 
    console.log("mraa initializtion failed.") 

/* Step2: Initialize D5 for PWM operation */ 
var pwm_interface = mraa.PWM; 

var owner =true; 
var chipid= 1; 
pwm_interface.Pwm(PWM_PIN,owner,chipid); 
/* 
    * Control the period with "mraa_pwm_period_us" 
    * 
    *  +----------------+    +----------------+    | 
    *  |    |    |    |    | 
    *  |    |    |    |    | 
    *  |    |    |    |    | 
    *  |    |    |    |    | 
    *  |    |    |    |    | 
    *  |    |    |    |    | 
    *  |    |    |    |    | 
    *  |    |    |    |    | 
    *  +    +----------------+    +----------------+ 
    *  ^        ^
    *  |         | 
    *  |<---------- Period ------------->| 
    *  |    ^    | 
    *  |    |     | 
    *      | 
    *  pwm_interface.period_us(5000); 
    */ 

    /* Step3: Set the period on the PWM pin */ 
    const PWM_Period_in_microseconds=5000; 
    pwm_interface.period_us(PWM_Period_in_microseconds);  // Set the period as 5000 us or 5ms 

    /* Step4: Enable the PWM pulse on the pin */ 
    var pwm_enabling_result= pwm_interface.enable(true); 

    var delta = 0.05; /* Variation on the duty cycle */ 
    var duty = 0.0;  /* 0% duty cycle */ 
    keepRunning = true; 
    const sleep_duration_in_Microsecond=50000; 

while (keepRunning){ 
     if (duty >= 1) 
     { 
      duty = 1;   // Intensity of LED at highest 
      delta = -0.05;  // Need to decrease the duty cycle 
     } 
     else if (duty <= 0) 
     { 
      duty = 0;   // Intensity of LED at the lowest 
      delta = +0.05;  // Need to increase the duty cycle 
     } 
     /* 
     * Control the duty cycle with "write" 
     * +------+       +------+        
     * |  |       |  |       
     * |  |       |  |       
     * |  |       |  |       
     * |  |       |  |       
     * |  |       |  |       
     * |  |       |  |       
     * |  |       |  |       
     * |  |       |  |       
     * +  +----------------------------+  +---------------------------+ 
     * ^ ^
     * |  | 
     * |<---->| 
     *  ^
     *  |----------------- 
     *       | 
     * pwm_interface.write(0.2); 
     * 
     */ 

    /* Step5: Use the function 'mraa_pwm_write' to set the duty cycle */ 
    pwm_interface.write(duty); 
    /* Wait for some time */ 
    var sleep = spawnSync('usleep', [sleep_duration_in_Microsecond]); 

    duty = duty + delta; 
} 

    /* Step6: Stop the PWM when not required */ 
    pwm_interface.enable(false);