index.js 684 B

123456789101112131415161718192021222324252627282930
  1. const PWM = require("rpio-pwm");
  2. let pwmCh, stepNum = 500, refresh = 50, pinBoardCode = 18
  3. console.log("初始化 PWM 通道");
  4. PWM.set_log_level(PWM.logLevel.debug);
  5. const cycleTimeUs = (1000 / refresh) * 1000,
  6. stepTimeUs = cycleTimeUs / stepNum,
  7. steeringPWMCfg = {
  8. cycle_time_us: cycleTimeUs,
  9. step_time_us: stepTimeUs,
  10. delay_hw: 1,
  11. };
  12. pwmCh = PWM.create_dma_channel(
  13. PWM.host_is_model_pi4() ? 5 : 12,
  14. steeringPWMCfg
  15. );
  16. const pin = pwmCh.create_pwm(pinBoardCode);
  17. pin.set_width((256 / 100) * stepNum);
  18. setTimeout(function () {
  19. // it's good idea to cleanup before exit.
  20. console.log('clear');
  21. pwmCh.shutdown();
  22. }, 2000);