Browse Source

增加摇杆数据断开断开继电器+重启程序
Signed-off-by: caner <5658514@qq.com>

caner 3 years ago
parent
commit
976015dc0d
2 changed files with 29 additions and 14 deletions
  1. 5 2
      index.js
  2. 24 12
      lib/Pwm.js

+ 5 - 2
index.js

@@ -19,7 +19,9 @@ class CarServer {
         this.socket.on('msg', this.onMsg.bind(this))
         this.socket.on('msg', this.onMsg.bind(this))
         this.socket.on('leaved', this.onLeved.bind(this))
         this.socket.on('leaved', this.onLeved.bind(this))
         this.socket.on('joined', this.startVideo.bind(this))
         this.socket.on('joined', this.startVideo.bind(this))
+        this.socket.on('disconnect',this.onLeved.bind(this))
     }
     }
+    
     // socket 连接
     // socket 连接
     connected() {
     connected() {
         console.log('connected');
         console.log('connected');
@@ -53,6 +55,7 @@ class CarServer {
     onLeved() {
     onLeved() {
         if (this.child) this.child.kill()
         if (this.child) this.child.kill()
         if (this.udp) this.udp.close()
         if (this.udp) this.udp.close()
+        PWM.stop()
         this.child = null
         this.child = null
         this.udp = null
         this.udp = null
         process.exit(1)
         process.exit(1)
@@ -79,10 +82,10 @@ class CarServer {
             // video push
             // video push
             const args = [
             const args = [
                 "libcamerasrc",
                 "libcamerasrc",
-                "video/x-raw,width=640,height=480",
+                "video/x-raw,width=960,height=720",
                 "videoconvert",
                 "videoconvert",
                 "queue",
                 "queue",
-                "x264enc tune=zerolatency bitrate=1000 key-int-max=30",
+                "x264enc tune=zerolatency bitrate=1000 key-int-max=30 speed-preset=1",
                 "video/x-h264, profile=constrained-baseline",
                 "video/x-h264, profile=constrained-baseline",
                 "rtph264pay pt=96 mtu=1200 ssrc=42",
                 "rtph264pay pt=96 mtu=1200 ssrc=42",
                 `udpsink host=127.0.0.1 port=${port}`,
                 `udpsink host=127.0.0.1 port=${port}`,

+ 24 - 12
lib/Pwm.js

@@ -21,11 +21,9 @@ class ContrlService {
         }
         }
         // prio init
         // prio init
         RPIO.init({ mapping: "gpio" });
         RPIO.init({ mapping: "gpio" });
-        // 继电器解锁
+        // 关闭继电器
         RPIO.open(6, RPIO.OUTPUT, RPIO.LOW);
         RPIO.open(6, RPIO.OUTPUT, RPIO.LOW);
-        //关闭继电器
         RPIO.open(13, RPIO.OUTPUT, RPIO.LOW);
         RPIO.open(13, RPIO.OUTPUT, RPIO.LOW);
-        // 开灯
         RPIO.open(19, RPIO.OUTPUT, RPIO.LOW);
         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 => {
@@ -45,7 +43,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 (v2) == 'number') {
+        if (typeof (v0) == 'number' && typeof (v2) == 'number' && typeof (v1) == 'number' && typeof (v3) == 'number') {
             // 内八解锁
             // 内八解锁
             if (this.enable) {
             if (this.enable) {
                 //中位值
                 //中位值
@@ -90,7 +88,9 @@ class ContrlService {
                 this.unLOCK(v0, v1, v2, v3)
                 this.unLOCK(v0, v1, v2, v3)
             }
             }
         } else {
         } else {
-            console.log('参数格式错误,自动略过');
+            // 遥杆数据错误,断开继电器
+            this.stop()
+            process.exit(1)
             return
             return
         }
         }
     }
     }
@@ -111,7 +111,7 @@ 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(13, RPIO.OUTPUT, RPIO.HIGH);
@@ -127,13 +127,16 @@ class ContrlService {
      * @returns 
      * @returns 
      */
      */
     Motor2pwm(v) {
     Motor2pwm(v) {
-        if (v === 0) {
-            return parseInt(10 / (256 / 1000)) + 1000
-        } else if (v >= 200) {
-            return parseInt(200 / (256 / 1000)) + 1000
-        } else {
-            return parseInt((v - 5) / (256 / 1000)) + 1000
+        // 使用低速
+        let num = 128
+        if (v < 116) {
+            num = 116;
+        } else if (v >= 120 && v <= 131) {
+            num = 128;
+        } else if (v > 140) {
+            num = 140;
         }
         }
+        return parseInt(num / (255 / 2000)) + 500
     }
     }
     /**
     /**
      * 舵机换算比例:摇杆范围0-256,PWM范围1000~1500~2000
      * 舵机换算比例:摇杆范围0-256,PWM范围1000~1500~2000
@@ -143,5 +146,14 @@ class ContrlService {
     Servo2pwm(v) {
     Servo2pwm(v) {
         return parseInt(v / (256 / 1000)) + 1000
         return parseInt(v / (256 / 1000)) + 1000
     }
     }
+
+    // 关闭
+    stop() {
+        if (this.PWM) this.PWM.dispose()
+        RPIO.open(6, RPIO.OUTPUT, RPIO.LOW);
+        RPIO.open(13, RPIO.OUTPUT, RPIO.LOW);
+        RPIO.open(19, RPIO.OUTPUT, RPIO.LOW);
+        this.PWM = null
+    }
 }
 }
 module.exports = new ContrlService()
 module.exports = new ContrlService()