অ্যাক্সেলেরোমিটার

অ্যাক্সেলেরোমিটার

একটি মাইক্রোপাইথন কোডের উদাহরণ রাস্পবেরি পাই পিকো ২ ওয়াট মাইক্রোকন্ট্রোলারে চালিত একটি ব্রেডবোর্ডে ৩-অক্ষ ডিজিটাল অ্যাক্সেলেরোমিটার মডিউল (ADXL345) এবং একটি OLED ডিসপ্লে মডিউল (SSD1306)-এর সাথে সংযুক্ত। অ্যাক্সেলেরোমিটারকে এর X এবং Y অক্ষ বরাবর ঘুরিয়ে OLED ডিসপ্লেতে একটি ত্রিমাত্রিক আকৃতি আঁকা এবং ঘোরানো।

একটি অ্যাক্সেলেরোমিটার x, y এবং z অক্ষ বরাবর ত্বরণ পরিমাপ করে।. রোবোটিক্সে, রোবটের অবস্থান এবং গতিবিধি নির্ধারণের জন্য এটি অপরিহার্য।. এই স্ক্রিপ্টে আমরা একটি ত্রিমাত্রিক আকৃতি আঁকি এবং অ্যাক্সেলেরোমিটার ব্যবহার করে এর ঘূর্ণন নিয়ন্ত্রণ করি।.

Thonny-তে accelerometer.py নামে একটি নতুন ফাইল তৈরি করুন।. আপনার তৈরি করা ফাইলে নিচের কোডটি কপি করে পেস্ট করুন।.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
print("\nAccelerometer")

# DESCRIPTION

# An accelerometer measures acceleration around the x, y and z axis. 
# In robotics, it is essential in determining the robot position and movement.
# In this script, we draw a 3D shape, and control its rotation using the accelerometer.

# CONNECTIONS REQUIRED

# Connect ADXL345 module using only the 4 pins on the right side
#   Pins: GND => GND, VCC => VCC, SDA => GP6, SCL => GP7

from machine import Pin, SoftI2C
from oled import OLED
from adxl345 import ADXL345
from cube import Cube
from math import atan2, sqrt, degrees

accel = ADXL345(SoftI2C(sda=Pin(6), scl=Pin(7)))
oled = OLED(scl_pin=15, sda_pin=14)
cube = Cube(scaleX=1, scaleY=1.4, scaleZ=0.2)

try:
    while True:
                
        # Calculate pitch (rotation around y-axis, 360 degrees)
        # and roll (rotaton around x-axis, 180 degrees)
        # using x, y and z from the accelerometer.
        # Yaw (rotation around vertical z-axis) needs
        # a different sensor, a magnetometer or a gyroscope.        
        x, y, z = accel.read()
        pitch = atan2(-x, sqrt(y*y + z*z))
        roll = atan2(y, z)
        
        cube.draw(display=oled.display, angleX=-degrees(roll), angleY=-degrees(pitch)) 

except KeyboardInterrupt:
    print("\nInterrupted by user!")

finally:
    oled.reset()

উপরে ফিরে যান

শিক্ষক ও শিক্ষার্থীদের জন্য হাতে-কলমে উদ্ভাবনকে আরও সহজলভ্য করে তোলা.
© ২০২৬ Inventor Pi, Inc.
Inventor Pi, Inc. একটি নিবন্ধিত 501(c)(3) সংস্থা