要控制伺服,您向它发送一个脉冲宽度调制信号,这是一个周期性的信号,即重复自身。每个周期,即信号点与其重复之间的时间,由两部分组成:开和关。on 是高电压(例如等于偏置电压),off 是低电压(例如等于 0 伏)。周期有一个时间,即周期时间,它的倒数就是频率。开启时间与关闭时间的比值称为占空比,占空比的范围为 0.0 到 1.0。伺服电机仅旋转到与占空比对应的角度并停止。
在此之前是 mraa node.js 文档的链接:
https ://iotdk.intel.com/docs/master/mraa/node/
并且要注意:mraa 是一个低级框架,所以如果这是您第一次使用伺服,我建议您推迟使用 mraa 并先使用 CylonJS,这是英特尔 Edison 上使用 CylonJS 控制伺服的教程与英特尔 Galileo 非常相似,位于:
http ://slideplayer.com/slide/7959041/
这是我之前在英特尔 Edison 套件上运行的一个非常好的示例。
也就是说,一旦你完成了本教程并想在 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);