# import time
# from machine import Pin
# import mpu6050
# # from wt901 import WT901
# from pid import PID
# # Define pins for your stepper motors
# motor1_step = Pin(25, Pin.OUT)
# motor1_dir = Pin(26, Pin.OUT)
# motor2_step = Pin(27, Pin.OUT)
# motor2_dir = Pin(14, Pin.OUT)
# # Initialize your stepper motor driver (configure as needed)
# # Initialize the MPU6050
# imu = MPU6050(sda=21, scl=22) # Adjust the I2C pins as per your connection
# # Initialize the WT901
# imu_serial = WT901(tx=17, rx=16) # Adjust the UART pins as per your connection
# # PID control parameters
# Kp = 1.0 # Proportional gain
# Ki = 0.0 # Integral gain
# Kd = 0.0 # Derivative gain
# pid = PID(Kp, Ki, Kd)
# # Setpoint angle (angle at which the robot should balance)
# setpoint_angle = 0.0
# # PID tuning parameters
# pid.sample_time = 0.01 # Update rate in seconds
# pid.output_limits = (-255, 255) # Adjust for your motor speed limits
# # Main loop
# while True:
# # Read IMU data
# imu_data = imu.get_accel_data()
# # Calculate the current angle
# current_angle = imu_data['x']
# # Read WT901 data
# imu_serial.send_command('W') # Request data
# imu_serial_data = imu_serial.get_data()
# # If you need to adjust the setpoint based on WT901 data, do it here
# # Calculate the PID output
# pid_output = pid(current_angle - setpoint_angle)
# # Adjust motor speeds based on PID output
# if pid_output > 0:
# # Rotate one motor forward and the other backward
# # Adjust this based on your motor configuration
# motor1_step.on()
# motor1_dir.on()
# motor2_step.on()
# motor2_dir.off()
# else:
# # Reverse motor rotation directions
# motor1_step.on()
# motor1_dir.off()
# motor2_step.on()
# motor2_dir.on()
# # Sleep for a short period (adjust as needed)
# time.sleep(pid.sample_time)
# # This is a basic example and may require significant tuning and additional logic for your specific robot and setup.
from machine import I2C
from machine import Pin
from machine import sleep
import mpu6050
i2c = I2C(scl=Pin(22), sda=Pin(21)) #initializing the I2C method for ESP32
mpu= mpu6050.accel(i2c)
while True:
mpu.get_values()
print(mpu.get_values())
sleep(500)