要控制伺服,則發送給它的調製信號的脈衝寬度,這是這是一個信號定期的,即重複自己。每個週期,即信號點和其重複之間的時間,由兩部分組成:開和關。 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);