Browse Source

测试各传感器
Signed-off-by: Caner

Caner 4 years ago
parent
commit
95f0750819
8 changed files with 465 additions and 12 deletions
  1. 80 6
      .gitignore
  2. 35 0
      4GAT.js
  3. 59 0
      ADS1115.py
  4. 81 0
      GPS.js
  5. 84 0
      JY61P.js
  6. 17 4
      LICENSE
  7. 102 0
      PCF8591.py
  8. 7 2
      README.md

+ 80 - 6
.gitignore

@@ -1,30 +1,104 @@
-# ---> Node
 # Logs
 logs
 *.log
 npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+lerna-debug.log*
+
+# Diagnostic reports (https://nodejs.org/api/report.html)
+report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
 
 # Runtime data
 pids
 *.pid
 *.seed
+*.pid.lock
 
 # Directory for instrumented libs generated by jscoverage/JSCover
 lib-cov
 
 # Coverage directory used by tools like istanbul
 coverage
+*.lcov
+
+# nyc test coverage
+.nyc_output
 
-# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
 .grunt
 
+# Bower dependency directory (https://bower.io/)
+bower_components
+
 # node-waf configuration
 .lock-wscript
 
-# Compiled binary addons (http://nodejs.org/api/addons.html)
+# Compiled binary addons (https://nodejs.org/api/addons.html)
 build/Release
 
-# Dependency directory
-# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
-node_modules
+# Dependency directories
+node_modules/
+jspm_packages/
+
+# TypeScript v1 declaration files
+typings/
+
+# TypeScript cache
+*.tsbuildinfo
+
+# Optional npm cache directory
+.npm
+
+# Optional eslint cache
+.eslintcache
+
+# Microbundle cache
+.rpt2_cache/
+.rts2_cache_cjs/
+.rts2_cache_es/
+.rts2_cache_umd/
+
+# Optional REPL history
+.node_repl_history
+
+# Output of 'npm pack'
+*.tgz
+
+# Yarn Integrity file
+.yarn-integrity
+
+# dotenv environment variables file
+.env
+.env.test
+
+# parcel-bundler cache (https://parceljs.org/)
+.cache
+
+# Next.js build output
+.next
+
+# Nuxt.js build / generate output
+.nuxt
+dist
+
+# Gatsby files
+.cache/
+# Comment in the public line in if your project uses Gatsby and *not* Next.js
+# https://nextjs.org/blog/next-9-1#public-directory-support
+# public
+
+# vuepress build output
+.vuepress/dist
+
+# Serverless directories
+.serverless/
+
+# FuseBox cache
+.fusebox/
+
+# DynamoDB Local files
+.dynamodb/
 
+# TernJS port file
+.tern-port

+ 35 - 0
4GAT.js

@@ -0,0 +1,35 @@
+const SerialPort = require('serialport')
+const Readline = require('@serialport/parser-readline')
+const port = new SerialPort('/dev/ttyUSB2')
+const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
+const parser = port.pipe(new Readline({ delimiter: '\r\n' }))
+parser.on('data', (data) => {
+    if (data.includes('+CSQ:')) {
+        try {
+            const str = data.substring(data.indexOf(':') + 2)
+            const list = str.split(',')
+            // 分3等份正常信号值10~31
+            const s = +list[0]
+            if (s >= 10 && s <= 31) {
+                if (s <= 17) {
+                    console.log('信号差', 1, s);
+                } else if (s > 17 && s <= 24) {
+                    console.log('信号弱', 2, s);
+                } else {
+                    console.log('信号强', 3, s);
+                }
+            } else {
+                console.log('信号差', 1);
+            }
+        } catch (error) {
+            console.log(error);
+        }
+    }
+})
+port.write(Buffer.from('AT+CSQ\r\n'))
+test()
+async function test() {
+    await sleep(5000)
+    port.write(Buffer.from('AT+CSQ\r\n'))
+    await test()
+}

+ 59 - 0
ADS1115.py

@@ -0,0 +1,59 @@
+import time
+import math
+# pip3 install Adafruit_ADS1x15
+import Adafruit_ADS1x15
+# 硬件地址 模块LADDR端接地
+ADDR = 0x48
+# 初始化ADS1115模块
+ADC = Adafruit_ADS1x15.ADS1115(address=ADDR)
+# 摇杆数据入口对应模块in0,in1
+P0 = 0
+P1 = 1
+# 最终方向
+content = 'Center'
+
+# 获取摇杆值
+def GetValue():
+    while True:
+        # 转换后的值
+        value0 = ADC.read_adc(P0, gain=1,data_rate=128)
+        value1 = ADC.read_adc(P1, gain=1,data_rate=128)
+        # print('初始值',value0,value1)
+        # 电压转换方向
+        Orientation(value0,value1)
+        time.sleep(0.05)
+
+
+# 区分方向
+def Orientation(value0,value1):
+    global content
+    # /1000得到电压值方便比较,实际电压值得/10000.
+    v0 = int(value0 / 1000)
+    v1 = int(value1 / 1000)
+    if Residual(v0) > 20 and Residual(v1) == 20:
+        content = 'Top'
+    elif Residual(v0) < 20 and Residual(v1) == 20:
+        content = 'Dwon'
+    elif Residual(v0) == 20 and Residual(v1) > 20:
+        content = 'Right'
+    elif Residual(v0) == 20 and Residual(v1) < 20:
+        content = 'Left'
+    else:
+        content = 'Center'
+    print('实际电压值',v0,v1,content)
+
+# 电压值误差根据实际情况调整
+def Residual(v):
+    # 中间值
+    vs = 20
+    # 误差值
+    vd = 3
+    if math.fabs(v - vs) > vd:
+        return v
+    elif math.fabs(v - vs) < vd:
+        return vs
+    else:
+        return vs
+
+if __name__ == '__main__':
+    GetValue()

+ 81 - 0
GPS.js

@@ -0,0 +1,81 @@
+const SerialPort = require('serialport')
+const Readline = require('@serialport/parser-readline')
+const port = new SerialPort('/dev/ttyUSB0', {
+    baudRate: 115200
+})
+
+const parser = port.pipe(new Readline({ delimiter: '\r\n' }))
+parser.on('data', (data) => {
+    console.log(data);
+    // if (data.includes('$GNRMC')) {
+    //     const gps = str_To_Gps84(data)
+    //     const gdGPS = gps84_To_Gcj02(gps.lat, gps.lon)
+    //     console.log(gdGPS);
+    // }
+})
+/**
+ * 北斗坐标系度分秒计算
+ * @param msg
+ * @return
+ */
+function str_To_Gps84 (msg) {
+    let lat = 0;
+    let lon = 0;
+    let split = msg.split(",");
+    if (split[4] == ("N") || split[4] == ("S")) {
+        const i = split[3].indexOf(".");
+        const latInt = split[3].substring(0, i - 2);
+        const latDec = split[3].substring(i - 2);
+        const i1 = split[5].indexOf(".");
+        const lonInt = split[5].substring(0, i1 - 2);
+        const lonDec = split[5].substring(i1 - 2);
+        lat = (+latInt) + (latDec) / 60;
+        lon = (+lonInt) + (lonDec) / 60;
+    }
+
+    return { lat, lon };
+}
+/** 高德采用GCJ编码 https://blog.csdn.net/weixin_45566249/article/details/118305913
+ * 84 to 火星坐标系 (GCJ-02)
+ * @param lat
+ * @param lon
+ */
+function gps84_To_Gcj02 (lat, lon) {
+    const pi = 3.1415926535897932384626;
+    const a = 6378245.0;
+    const ee = 0.00669342162296594323;
+
+    let dLat = transformLat(lon - 105.0, lat - 35.0);
+    let dLon = transformLon(lon - 105.0, lat - 35.0);
+    const radLat = lat / 180.0 * pi;
+    let magic = Math.sin(radLat);
+    magic = 1 - ee * magic * magic;
+    const sqrtMagic = Math.sqrt(magic);
+    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
+    dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
+    const Lat = +lat + dLat;
+    const Lon = +lon + dLon;
+    return { Lat, Lon }
+}
+
+function transformLat (x, y) {
+    const pi = 3.1415926535897932384626;
+    let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
+        + 0.2 * Math.sqrt(Math.abs(x));
+    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
+    ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
+    ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
+    return ret;
+}
+
+
+function transformLon (x, y) {
+    const pi = 3.1415926535897932384626;
+    let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
+        * Math.sqrt(Math.abs(x));
+    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
+    ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
+    ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
+        * pi)) * 2.0 / 3.0;
+    return ret;
+}

+ 84 - 0
JY61P.js

@@ -0,0 +1,84 @@
+const i2c = require('i2c-bus');
+const i2c1 = i2c.openSync(1);
+
+// x,y,z角度=Angle/32768*180
+// const data1 = i2c1.readWordSync(0x50, 0x3d);
+// const data2 = i2c1.readWordSync(0x50, 0x3e);
+// const data3 = i2c1.readWordSync(0x50, 0x3f);
+
+// // 时间
+// const data4 = i2c1.readWordSync(0x50, 0x30);
+// const data5 = i2c1.readWordSync(0x50, 0x31);
+// const data6 = i2c1.readWordSync(0x50, 0x32);
+// const data7 = i2c1.readWordSync(0x50, 0x33);
+
+// 模块温度 =  */100 °c
+//  const data8 = i2c1.readWordSync(0x50, 0x40);
+
+// // 轴加速度xyz = Angle/32768*16
+// const data9 = i2c1.readWordSync(0x50, 0x34);
+// const data10 = i2c1.readWordSync(0x50, 0x35);
+// const data11 = i2c1.readWordSync(0x50, 0x36);
+
+// // 轴角速度xyz = Angle/32768*2000
+// const data12 = i2c1.readWordSync(0x50, 0x37);
+// const data13 = i2c1.readWordSync(0x50, 0x38);
+// const data14 = i2c1.readWordSync(0x50, 0x39);
+
+// // 高低气压pa
+// const data15 = i2c1.readWordSync(0x50, 0x45);
+// const data16 = i2c1.readWordSync(0x50, 0x46);
+
+// // 高度低|高cm
+// const data17 = i2c1.readWordSync(0x50, 0x47);
+// const data18 = i2c1.readWordSync(0x50, 0x48);
+
+// 高低经度 = %10000000/100000 lon
+// const data19 = i2c1.readWordSync(0x50, 0x49);
+// const data20 = i2c1.readWordSync(0x50, 0x4a);
+
+// // 高低纬度 = %10000000/100000 lat
+// const data21 = i2c1.readWordSync(0x50, 0x4b);
+// const data22 = i2c1.readWordSync(0x50, 0x4c);
+
+// // GPS高度
+// const data23 = i2c1.readWordSync(0x50, 0x4d);
+
+// // GPS航向角
+// const data24 = i2c1.readWordSync(0x50, 0x4e);
+
+// // GPS 地速低字
+// const data25 = i2c1.readWordSync(0x50, 0x4f);
+
+// // GPS 地速高字
+// const data26 = i2c1.readWordSync(0x50, 0x50);
+
+
+// const txt1 = `角度:x:${data1 / 32768 * 180},y:${data2 / 32768 * 180},z:${data3 / 32768 * 180}`
+
+// const txt2 = `GPS高度:${data23 / 100}`
+
+// const txt3 = `模块温度:${data8 / 100}`
+
+
+// const a = (data19.toString() + data20.toString()) % 10000000
+// const b = (data21.toString() + data22.toString()) % 10000000
+// console.log(data19, data20);
+// console.log(data21, data22);
+// console.log(txt1);
+
+const bf = Buffer.alloc(8)
+i2c1.writeByteSync(0x50, 0x49, 2)
+i2c1.i2cReadSync(0x50, bf.length, bf);
+const a = bf.readUIntBE(0, 6).toString();//切分字节流
+console.log(a);
+console.log(bf);
+const bd = Buffer.alloc(8)
+i2c1.writeByteSync(0x50, 0x4a, 2)
+i2c1.i2cReadSync(0x50, bd.length, bd);
+const b = bd.readUIntBE(0, 6).toString();//切分字节流
+console.log(b);
+console.log(bd);
+console.log(+a + +b);
+
+

+ 17 - 4
LICENSE

@@ -1,8 +1,21 @@
 MIT License
-Copyright (c) <year> <copyright holders>
 
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+Copyright (c) 2022 Canees
 
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
 
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 102 - 0
PCF8591.py

@@ -0,0 +1,102 @@
+import smbus
+import time
+# 初始化smbus  1表示scl.1  0表示scl.0
+BUS = smbus.SMBus(1)
+# 寄存器地址
+ADDR = 0x48
+# 电位器地址
+Pmeter1 = 0x42
+Pmeter2 = 0x43
+# 方向值
+LEFTORRIGHT = 0
+UPORDOWN = 0
+# 中位归零
+MESOL = 0
+MESOR = 0
+# 最终方向值
+POSTION = '中位'
+
+
+# 左+右
+def TLEFTRIGHT():
+    global LEFTORRIGHT,MESOL,UPORDOWN
+    BUS.write_byte(ADDR,Pmeter1)
+    value =  BUS.read_byte(ADDR)
+    # nowData = int(MESOL - value)
+    LEFTORRIGHT = value
+    # if nowData > 10:
+    #     a+=1
+    #     print('左',UPORDOWN)
+    # elif nowData < -10:
+    #     a-=1
+    #     print('右',UPORDOWN)
+    # else:
+    #     a =0
+    #     print('中',UPORDOWN)
+
+
+
+    # if value > MESOL:
+    #     LEFTORRIGHT += 1
+    #     if LEFTORRIGHT > 100:
+    #         LEFTORRIGHT = 100
+    # elif value < MESOL:
+    #     LEFTORRIGHT -= 1
+    #     if LEFTORRIGHT < -100:
+    #         LEFTORRIGHT = -100
+    # else:
+    #     LEFTORRIGHT = 0
+    #     MESOL = value
+    
+
+# 上+下
+def TUPDOWN():
+    global UPORDOWN,MESOR
+    BUS.write_byte(ADDR,Pmeter2)
+    value =  BUS.read_byte(ADDR)
+    UPORDOWN = value
+    # if  value > MESOR:
+    #     UPORDOWN += 1
+    #     if UPORDOWN > 100:
+    #         UPORDOWN = 100
+    # elif value < MESOR:
+    #     UPORDOWN -= 1 
+    #     if UPORDOWN < -100:
+    #         UPORDOWN = -100
+    # else:
+    #     UPORDOWN = 0
+    #     MESOR = value
+
+
+def Direction():
+    global LEFTORRIGHT,UPORDOWN,POSTION
+    if LEFTORRIGHT > 0 :
+        POSTION = '右'
+    elif LEFTORRIGHT < 0:
+        POSTION = '左'
+    else:
+        POSTION = '中'
+
+    
+
+def INIt():
+    # 初始化中位值
+    global MESOL,MESOR
+    BUS.write_byte(ADDR,Pmeter1)
+    MESOL = BUS.read_byte(ADDR)
+    BUS.write_byte(ADDR,Pmeter2)
+    MESOR = BUS.read_byte(ADDR)  
+
+
+if __name__ == "__main__":
+    # 初始化
+    # INIt()
+    # time.sleep(3)
+    print('开始查询数据')
+    # loop
+    while True:
+        TLEFTRIGHT()
+        TUPDOWN()
+        print(LEFTORRIGHT,UPORDOWN)
+        time.sleep(0.01)
+        pass

+ 7 - 2
README.md

@@ -1,3 +1,8 @@
-# sensor
+# DAC-ADC 测试串口+I2C
+# node 使用serialport | i2c-bus 两种库
 
-gps,ads,at,姿态等传感器
+# ADS1115 数模模块
+# JY61P I2C姿态模块
+# PCF8591 数模模块
+# 4GAT 4G模块信号强度
+# GPS 模块