使用Galileo MRAA控制节点的伺服

时间:2014-11-01 12:44:28

标签: node.js hardware intel-galileo

我有一个英特尔伽利略我试图用Node.js控制,但我遇到了一些问题。我使用的库有更改引脚的二进制和/或模拟值的示例,但没有关于控制伺服电机的具体信息。我目前的代码如下。

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

问题是,我不知道如何控制伺服,MRAA的文档几乎不存在。这里有没有人做过类似的事情,能够提供帮助吗?

感谢。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

用于mraa的PWM示例(爱迪生但可以在Galileo上工作,但周期应为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);

答案 2 :(得分:0)

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

之前的任何内容是指向mraa node.js文档的链接: https://iotdk.intel.com/docs/master/mraa/node/

请注意:mraa是一个低级框架,所以如果这是你第一次使用伺服,我会建议你延迟使用mraa以后再使用CylonJS,这是一个使用CylonJS控制伺服的教程英特尔Edison与英特尔Galileo非常相似的是: http://slideplayer.com/slide/7959041/ 这是我之前在英特尔Edison套件上运行的一个非常好的例子。

那就是说,一旦你完成了这个教程并想在mraa node.js中尝试伺服,这里有一个旋转伺服的教程,直到你按Ctrl-C结束程序。它将占空比从0开始,将其递增到1,然后递减到0,然后再次循环。此代码是C代码的翻译 https://navinbhaskar.wordpress.com/2016/02/21/cc-on-intel-edisongalileo-part3-pwm/我没有测试翻译。

/*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);