I am very new in python, but I have been able to make few useful python codes (at least useful for my work). I would like to combine two python scripts, but I have a hard time making it work, I think I am completely lost in how things should look like.The end goal is to have my Raspberry reading the sensor while also the servo moves.
Here is the the IMU script
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)
Servo motor scripts
# 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"