伽利略MRAA控制伺服的节点

问题描述:

我有一个英特尔伽利略我试图用Node.js的控制,但我跨越了几个问题来了。我正在使用的库有更改引脚的二进制和/或模拟值的示例,但没有关于控制伺服电机的具体内容。我目前的代码如下。伽利略MRAA控制伺服的节点

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

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

谢谢。

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

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

要控制伺服,则发送给它的调制信号的脉冲宽度,这是这是一个信号定期的,即重复自己。每个周期,即信号点和其重复之间的时间,由两部分组成:开和关。 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);