jperkin / node-rpio

Raspberry Pi GPIO library for node.js
859 stars 126 forks source link

About the pwm, servo don't work correctly #80

Open recallwolf opened 6 years ago

recallwolf commented 6 years ago

Sorry to interrupt you. This has been troubling for a few days.

servo(sg90) need 50hz(20ms) time base pulse. 0.5ms-------------0° 1.0ms------------45° 1.5ms------------90° 2.0ms-----------135° 2.5ms-----------180°

According to this formula:

pwmFrequency in Hz = 19.2e6 Hz / pwmClock / pwmRange

I tried pwmClock(64/128/256.....) and pwmRange(6000/3000/1500) but only 64/6000 128/3000 works. And work uncorrectly.

Here is the code var rpio = require('rpio'); rpio.init({gpiomem: false}); rpio.open(33, rpio.PWM); rpio.pwmSetClockDivider(128); rpio.pwmSetRange(33, 3000); rpio.pwmSetData(33, 150);

var rpio = require('rpio'); rpio.init({gpiomem: false}); rpio.open(33, rpio.PWM); rpio.pwmSetClockDivider(64); rpio.pwmSetRange(33, 6000); rpio.pwmSetData(33, 300); The first one whatever I change the data to 150/225/300/375, It only turn to 90° The second one change the data, It only turn to 0°

Is there any problem with the code? Hope someone can point out. thank you very much!

yutou527 commented 5 years ago

i use the RPi.GPIO succed drive the servo, and met the same problem. i think that you should calculate the interval (T) with 19.2e6 Hz / pwmClock / pwmRange and then , use range 2.5ms/T to get real data range. for example pwmClock = 128 pwmRange = 1000 19200000 / 128 / 2000 = 75Hz 1000ms/75= 13.33ms 2.5/13.33 = 0.1875 so your data range should between 0-187.5 (10000.1875) i guess so. but not very sure.

yutou527 commented 5 years ago

@recallwolf

GitMcClintock commented 3 years ago

I got this going using nodejs-rpio as my GPIO library, and the following settings:

Clock divider = 256 Range = 1500

These values give a 20 ms period (50 Hz rate) on the PWM output.

Then a value of 38 gives a pulse with of 0.5 ms, and:

My servo required this 0.5 to 2.5 ms range for a full 180 degrees of rotation. Yours may be different.

Tested on my Raspberry Pi 3A+, using pin 33.