|
@@ -1,37 +1,37 @@
|
|
|
-const i2cBus = require("i2c-bus")
|
|
|
|
|
-const { Pca9685Driver } = require("pca9685")
|
|
|
|
|
-
|
|
|
|
|
-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");
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- console.log('测试开始');
|
|
|
|
|
- // test
|
|
|
|
|
- let a = 128
|
|
|
|
|
- setInterval(() => {
|
|
|
|
|
- this.PWM.setPulseLength(3, this.Servo2pwm(a));
|
|
|
|
|
- console.log(a);
|
|
|
|
|
- }, 50);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 舵机换算比例:摇杆范围0-256,PWM范围1000~1500~2000
|
|
|
|
|
- * @param {number} v
|
|
|
|
|
- * @returns
|
|
|
|
|
- */
|
|
|
|
|
- Servo2pwm(v) {
|
|
|
|
|
- return parseInt(v / (256 / 1000)) + 1000
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
|
|
+const i2cBus = require("i2c-bus")
|
|
|
|
|
+const { Pca9685Driver } = require("pca9685")
|
|
|
|
|
+
|
|
|
|
|
+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");
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ console.log('测试开始');
|
|
|
|
|
+ // test
|
|
|
|
|
+ let a = 128
|
|
|
|
|
+ setInterval(() => {
|
|
|
|
|
+ this.PWM.setPulseLength(3, this.Servo2pwm(a));
|
|
|
|
|
+ console.log(a);
|
|
|
|
|
+ }, 50);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 舵机换算比例:摇杆范围0-256,PWM范围1000~1500~2000
|
|
|
|
|
+ * @param {number} v
|
|
|
|
|
+ * @returns
|
|
|
|
|
+ */
|
|
|
|
|
+ Servo2pwm(v) {
|
|
|
|
|
+ return parseInt(v / (256 / 1000)) + 1000
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
new ContrlService()
|
|
new ContrlService()
|