Node.js 基于Galileo-MRAA的节点伺服控制

Node.js 基于Galileo-MRAA的节点伺服控制,node.js,hardware,intel-galileo,Node.js,Hardware,Intel Galileo,我有一个intel galileo,我正试图用Node.js控制它,但我遇到了一些问题。我正在使用的库中有更改管脚的二进制和/或模拟值的示例,但没有关于控制伺服电机的具体内容。我目前拥有的代码如下 var B = 3975; var mraa = require("mraa"); var servo = new mraa.Aio(1);//connects to the servo 问题是,我不知道如何控制伺服,MRAA的文档几乎不存在。这里有没有人曾经做过类似的事情,能够提供帮助 谢谢。伺服

我有一个intel galileo,我正试图用Node.js控制它,但我遇到了一些问题。我正在使用的库中有更改管脚的二进制和/或模拟值的示例,但没有关于控制伺服电机的具体内容。我目前拥有的代码如下

var B = 3975;
var mraa = require("mraa");
var servo = new mraa.Aio(1);//connects to the servo
问题是,我不知道如何控制伺服,MRAA的文档几乎不存在。这里有没有人曾经做过类似的事情,能够提供帮助

谢谢。

伺服系统通常使用PWM(脉宽调制)控制。您应该将伺服控制线连接到标有“~”符号的数字管脚之一。该符号表示引脚将生成PWM数据。然后谷歌你的伺服类型,以了解它接受的PWM参数。然后,您可以按照mraa PWM示例为PWM引脚设置合适的值。

mraa on的PWM示例(Edison但将在Galileo上工作,但周期应为62500)


为了控制伺服,你向它发送一个脉冲宽度调制信号,这是一个周期性的信号,即重复自身。每个周期,即信号点与其重复之间的时间,由两部分组成:开和关。接通是高电压(例如等于偏置电压),断开是低电压(例如等于0伏)。周期有一个时间,它就是周期时间,它的倒数就是频率。接通时间和断开时间之间的比率称为占空比,占空比范围为0.0到1.0。伺服电机仅旋转到与占空比相对应的角度并停止

在此之前是指向mraa node.js文档的链接:

需要说明的是:mraa是一个低级框架,因此,如果这是您第一次使用伺服,我建议您推迟使用mraa,以后再使用CylonJS,首先使用CylonJS,Intel Edison上使用CylonJS控制伺服的教程与Intel Galileo非常类似,它位于: 这是我以前在英特尔爱迪生套件上运行的一个非常好的示例

这就是说,一旦您完成本教程并想在mraa node.js中尝试伺服,这里有一个教程来旋转伺服,直到按下Ctrl-C结束程序。它从0开始占空比,递增到1,然后递减到0,然后再次循环。这段代码是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);
<代码> /* C++代码的翻译 https://navinbhaskar.wordpress.com/2016/02/21/cc-on-intel-edisongalileo-part3-pwm/ mraa node.js文档位于: https://iotdk.intel.com/docs/master/mraa/node/ */ “严格使用”; 常数mraa=要求(“mraa”); const spawnSync=require('child_process')。spawnSync; 常数PWM_引脚=5;/**<连接LED的引脚*/ var-keepRunning=false; ///**用于完全停止此应用程序的信号处理程序*/ /* *将ctrl+c与清除“keepRunning”的处理程序相关联 *允许我们在退出时停止PWM的标志 */ process.on('SIGINT',()=>{ 保持修剪=错误; }); //步骤1:初始化mraa系统 var result=mraa.init(); if(result==mraa.result.SUCCESS) log(“mraa初始化成功”); 其他的 log(“mraa初始化失败”) /*步骤2:为PWM操作初始化D5*/ var pwm_接口=mraa.pwm; var所有者=真; var-chipid=1; pwm_接口.pwm(pwm_引脚、所有者、芯片ID); /* *用“mraa\U pwm\U period\U us”控制周期 * * +----------------+ +----------------+ | * | | | | | * | | | | | * | | | | | * | | | | | * | | | | | * | | | | | * | | | | | * | | | | | * + +----------------+ +----------------+ * ^ ^ * | | * || * | ^ | * | | | * | *pwm_接口。周期_us(5000); */ /*步骤3:在PWM引脚上设置周期*/ 恒定脉宽调制周期(单位:微秒)=5000; pwm_接口。周期_us(pwm_周期_in_微秒);//将周期设置为5000 us或5ms /*步骤4:启用引脚上的PWM脉冲*/ var pwm_启用_结果=pwm_接口。启用(真); var delta=0.05;/*占空比的变化*/ var占空比=0.0;/*0%占空比*/ 保持修剪=正确; 常量睡眠持续时间(单位:微秒)=50000; 同时(继续修剪){ 如果(占空比>=1) { 占空比=1;//最高温度下LED的强度 delta=-0.05;//需要降低占空比 } 否则(责任)
/*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);