Browse Source

上传文件至 ''

PWM寻找停止位
Caner 3 years ago
parent
commit
1762c14558
1 changed files with 73 additions and 0 deletions
  1. 73 0
      GetStopBit.js

+ 73 - 0
GetStopBit.js

@@ -0,0 +1,73 @@
+const i2cBus = require("i2c-bus")
+const { Pca9685Driver } = require("pca9685")
+const RPIO = require("rpio");
+const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
+class ContrlService {
+    constructor() {
+        // PWM 和 舵机配置
+        this.pwmOption = {
+            i2c: i2cBus.openSync(1), // 树莓派1:scl1
+            address: 0x40, //板子地址
+            frequency: 50, //频率
+            debug: false
+        }
+
+        // init PWM
+        this.PWM = new Pca9685Driver(this.pwmOption, er => {
+            if (er) {
+                console.error("Error initializing PCA9685");
+            }
+        })
+
+        // prio init
+        RPIO.init({ mapping: "gpio" });
+
+        // init PWM
+        this.ins = 0
+        this.timer = 0
+
+        // test Pwm
+        this.num = 0
+        this.startInt()
+    }
+
+    // 测试全程找中位
+    startInt() {
+        console.log('start');
+        RPIO.open(6, RPIO.OUTPUT, RPIO.HIGH);
+        RPIO.open(13, RPIO.OUTPUT, RPIO.HIGH);
+        RPIO.open(19, RPIO.OUTPUT, RPIO.HIGH);
+        this.timer = setInterval(() => {
+            this.num++
+            // if (this.num >= 255) {
+            //     // 继电器停止
+            //     RPIO.open(6, RPIO.OUTPUT, RPIO.LOW);
+            //     RPIO.open(13, RPIO.OUTPUT, RPIO.LOW);
+            //     RPIO.open(19, RPIO.OUTPUT, RPIO.LOW);
+            //     clearInterval(this.timer);
+            //     this.num = 0
+            //     console.log('end');
+            //     return
+            // }
+            // 寻找中位=>120-130
+            if(this.num >= 120 && this.num <= 130){
+                this.PWM.setPulseLength(this.ins, this.Servo2pwm(this.num));
+                clearInterval(this.timer);
+                console.log('center',this.num);
+                return
+            }
+            this.PWM.setPulseLength(this.ins, this.Servo2pwm(10));
+            console.log(this.num);
+        }, 50);
+    }
+
+    /**
+     * 舵机换算比例:摇杆范围0-256,PWM范围1000~1500~2000
+     * @param {number} v 
+     * @returns 
+     */
+    Servo2pwm(v) {
+        return parseInt(v / (256 / 1000)) + 1000
+    }
+}
+new ContrlService()