Browse Source

修正pwm
Signed-off-by: caner <5658514@qq.com>

caner 3 years ago
parent
commit
78d9a55e83
1 changed files with 24 additions and 18 deletions
  1. 24 18
      lib/Pwm.js

+ 24 - 18
lib/Pwm.js

@@ -14,18 +14,19 @@ class ContrlService {
         }
         }
         //PCA 板子位置电调+舵机
         //PCA 板子位置电调+舵机
         this.MoAndSero = {
         this.MoAndSero = {
-            m1: 0,
-            m2: 1,
+            m1: 1,
+            m2: 0,
             s1: 2,
             s1: 2,
             s2: 3
             s2: 3
         }
         }
         // prio init
         // prio init
-        //RPIO.init({ mapping: "gpio" });
+        RPIO.init({ mapping: "gpio" });
         // 继电器解锁
         // 继电器解锁
-        //RPIO.open(6, RPIO.OUTPUT, RPIO.HIGH);
-        //RPIO.open(13, RPIO.OUTPUT, RPIO.HIGH);
+        RPIO.open(6, RPIO.OUTPUT, RPIO.LOW);
+        //关闭继电器
+        RPIO.open(13, RPIO.OUTPUT, RPIO.LOW);
         // 开灯
         // 开灯
-        //RPIO.open(19, RPIO.OUTPUT, RPIO.HIGH);
+        RPIO.open(19, RPIO.OUTPUT, RPIO.LOW);
         // init PWM
         // init PWM
         this.PWM = new Pca9685Driver(this.pwmOption, er => {
         this.PWM = new Pca9685Driver(this.pwmOption, er => {
             if (er) {
             if (er) {
@@ -44,7 +45,7 @@ class ContrlService {
     // 设置pwm
     // 设置pwm
     async changPWM(params) {
     async changPWM(params) {
         const { v0, v1, v2, v3 } = params
         const { v0, v1, v2, v3 } = params
-        if (typeof (v0) == 'number' || typeof (v3) == 'number') {
+        if (typeof (v0) == 'number' || typeof (v2) == 'number') {
             // 内八解锁
             // 内八解锁
             if (this.enable) {
             if (this.enable) {
                 //中位值
                 //中位值
@@ -53,25 +54,25 @@ class ContrlService {
                     this.PWM.setPulseLength(this.MoAndSero.m2, this.Motor2pwm(Math.abs(this.centerNum)));
                     this.PWM.setPulseLength(this.MoAndSero.m2, this.Motor2pwm(Math.abs(this.centerNum)));
                 }
                 }
                 //上推解锁
                 //上推解锁
-                if (!this.contrlEnable && +v3 > 200) this.contrlEnable = true
+                if (!this.contrlEnable && +v3 < 200) this.contrlEnable = true
                 // PWM操作
                 // PWM操作
                 if (this.contrlEnable) {
                 if (this.contrlEnable) {
                     // 注意中位误差
                     // 注意中位误差
-                    if (Math.abs(+v2 - this.centerNum) < 20) {
-                        this.PWM.setPulseLength(this.MoAndSero.m1, this.Motor2pwm(Math.abs(+v3 - 255)));
-                        this.PWM.setPulseLength(this.MoAndSero.m2, this.Motor2pwm(Math.abs(+v3 - 255)));
+                    if (Math.abs(+v3 - this.centerNum) < 20) {
+                        this.PWM.setPulseLength(this.MoAndSero.m1, this.Motor2pwm(Math.abs(+v2 - 255)));
+                        this.PWM.setPulseLength(this.MoAndSero.m2, this.Motor2pwm(Math.abs(+v2 - 255)));
                     } else {
                     } else {
                         // 分左右 注意左右误差
                         // 分左右 注意左右误差
-                        if ((+v2 - this.centerNum) > 20) {
+                        if ((+v3 - this.centerNum) > 20) {
                             // 右
                             // 右
-                            const a = this.centerNum - (+v2 - this.centerNum)
-                            const b = this.centerNum + (+v2 - this.centerNum)
+                            const a = this.centerNum - (+v3 - this.centerNum)
+                            const b = this.centerNum + (+v3 - this.centerNum)
                             this.PWM.setPulseLength(this.MoAndSero.m1, this.Motor2pwm(+a));
                             this.PWM.setPulseLength(this.MoAndSero.m1, this.Motor2pwm(+a));
                             this.PWM.setPulseLength(this.MoAndSero.m2, this.Motor2pwm(+b));
                             this.PWM.setPulseLength(this.MoAndSero.m2, this.Motor2pwm(+b));
-                        } else if (+v2 - this.centerNum < -20) {
+                        } else if (+v3 - this.centerNum < -20) {
                             // 左
                             // 左
-                            const a = this.centerNum - (+v2 - this.centerNum)
-                            const b = this.centerNum + (+v2 - this.centerNum)
+                            const a = this.centerNum - (+v3 - this.centerNum)
+                            const b = this.centerNum + (+v3 - this.centerNum)
                             this.PWM.setPulseLength(this.MoAndSero.m1, this.Motor2pwm(+a));
                             this.PWM.setPulseLength(this.MoAndSero.m1, this.Motor2pwm(+a));
                             this.PWM.setPulseLength(this.MoAndSero.m2, this.Motor2pwm(+b));
                             this.PWM.setPulseLength(this.MoAndSero.m2, this.Motor2pwm(+b));
                         }
                         }
@@ -110,7 +111,12 @@ class ContrlService {
             this.Snum = 0
             this.Snum = 0
         }
         }
         if (this.Snum == 50) {
         if (this.Snum == 50) {
-            // RPIO.open(19, RPIO.OUTPUT, RPIO.HIGH);
+            //继电器
+            RPIO.open(19, RPIO.OUTPUT, RPIO.HIGH);
+            //空闲继电器
+            //RPIO.open(13, RPIO.OUTPUT, RPIO.HIGH);
+            //空闲继电器
+            //RPIO.open(6, RPIO.OUTPUT, RPIO.HIGH);
             this.enable = true
             this.enable = true
         }
         }
         console.log('解锁中', v0, v1, v2, v3)
         console.log('解锁中', v0, v1, v2, v3)