我是 python 的新手,但我已经能够编写一些有用的 python 代码(至少对我的工作有用)。我想结合两个 python 脚本,但我很难让它工作,我想我完全迷失了事情应该是什么样子。最终目标是让我的树莓在伺服器移动的同时读取传感器。
这是 IMU 脚本
from mpu6050 import mpu6050
import time
mpu = mpu6050(0x68)
while True:
print("Temp : "+str(mpu.get_temp()))
print()
accel_data = mpu.get_accel_data()
print("Acc X : "+str(accel_data['x']))
print("Acc Y : "+str(accel_data['y']))
print("Acc Z : "+str(accel_data['z']))
print()
gyro_data = mpu.get_gyro_data()
print("Gyro X : "+str(gyro_data['x']))
print("Gyro Y : "+str(gyro_data['y']))
print("Gyro Z : "+str(gyro_data['z']))
print()
print("-------------------------------")
time.sleep(1)
伺服电机脚本
# Servo1.py
import RPi.GPIO as GPIO
import time
P_SERVO = 22 # adapt to your wiring
fPWM = 50 # Hz (not higher with software PWM)
a = 10
b = 2
def setup():
global pwm
GPIO.setmode(GPIO.BOARD)
GPIO.setup(P_SERVO, GPIO.OUT)
pwm = GPIO.PWM(P_SERVO, fPWM)
pwm.start(0)
def setDirection(direction):
duty = a / 180 * direction + b
pwm.ChangeDutyCycle(duty)
print "direction =", direction, "-> duty =", duty
time.sleep(1) # allow to settle
print "starting"
setup()
for direction in range(0, 181, 10):
setDirection(direction)
direction = 0
setDirection(0)
GPIO.cleanup()
print "done"