用伽利略MRAA控制伺服节点

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

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

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

谢谢。

舵机通常使用PWM(脉宽调制)控制。 您应该将伺服控制线连接到标有“〜”符号的数字引脚之一。 该符号表示该引脚将生成PWM数据。 然后谷歌你的伺服types来学习它接受的PWM参数。 然后可以按照mraa PWM示例为PWM引脚设置适当的值。

PWM的例子(爱迪生,但将在伽利略工作,但期限应该是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。 伺服电机只旋转到与占空比对应的angular度停止。

在此之前,这里是链接到mraa node.js文档: https ://iotdk.intel.com/docs/master/mraa/node/

还有一个说明:mraa是一个低级别的框架,所以如果这是您第一次使用伺服器,我会build议您延迟使用mraa以后再使用CylonJS,一个使用CylonJS在Intel Edison上控制伺服的教程这与英特尔Galileo非常相似, url为: 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代码的翻译,我没有testing翻译。

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