/ Tilt_Controlled_Servo_Driver / code.py
code.py
1 # SPDX-FileCopyrightText: 2019 Melissa LeBlanc-Williams for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 In this Demo we will drive two servos based on the Tilt along the Y and Z axis 7 of the BNO055 9-Degrees of Freedom IMU Sensor. This could easily be extended 8 to drive servos on all three axis as well as use a host of other information 9 including lateral acceleration. 10 """ 11 12 import board 13 import busio 14 from adafruit_servokit import ServoKit 15 import adafruit_bno055 16 17 # Set channels to the number of servo channels on your kit. 18 kit = ServoKit(channels=16) 19 20 # Setup the BNO055 to read data 21 i2c = busio.I2C(board.SCL, board.SDA) 22 sensor = adafruit_bno055.BNO055(i2c) 23 24 while True: 25 # Get the Euler angles from the BNO055 26 (x, y, z) = sensor.euler 27 28 # Euler angles are between -180 and 180 29 # We want to translate them to the Servo angles between 0-180 30 try: 31 kit.servo[0].angle = (y + 180) / 2 32 kit.servo[1].angle = (z + 180) / 2 33 except ValueError: 34 # Pass on any values that are out of range 35 pass