Real Time IMU Data Measurement using Alvik

Alvik: Detecting Falling and Crashes (IMU) Corrected Code

This entry is part 6 of 9 in the series Arduino Alvik

Important

The complete series on Arduino Alvik lovers is written by our expert faculties of robotics on this link. Please consider giving feedback after reading the series posts.

If you are Arduino Alvik lover, then you must have come up with the IMU part of programming with Alvik. As per the IMU code given on the official website of Arduino Alvik, I have spotted number of errors on their website.

IMU Code Problem

Particularly the errors associated with IMU (Inertial Measurement Unit) code given on their website are quite unexpected errors. On official website of Arduino, such errors for the beginners of Alvik learners will prove to be very problematic and will surely create lot of headache.

So after studying the official code of IMU for Arduino Alvik, I have tweaked it and now giving the lab tested code of the same, below.

The Arduino Alvik lovers are free to use this code for their study with Arduino Alvik.

Your comments and suggestions are highly appreciated.

The Corrected Code

# Lab Tested Code by Vidyasagar Academy
from arduino_alvik import ArduinoAlvik
import time

# Initialization
alvik = ArduinoAlvik()
alvik.begin() 

# Give some time for initialization
while True:
  #alvik.set_wheels_speed(30,30)
  orientation = alvik.get_orientation()
  acceleration = alvik.get_accelerations()
  angular_acceleration = alvik.get_gyros()
  print(f'Orientation: {orientation}')
  print(f'Acceleration: {acceleration}')
  print(f'Angular Acceleration: {angular_acceleration}')
  time.sleep(5)

The Faulty ‘Official’ Code (1) of Arduino Alvik for IMU

from arduino_alvik import ArduinoAlvik
import time

# Initialization
alvik = ArduinoAlvik()
alvik.begin()

# Give some time for initialization
time.sleep(2)

# Get orientation
orientation = alvik.get_orientation()
roll = orientation['r']
pitch = orientation['p']
yaw = orientation['y']
print(f"Orientation - Roll: {roll}, Pitch: {pitch}, Yaw: {yaw}")

# Get accelerations
accelerations = alvik.get_accelerations()
ax = accelerations['ax']
ay = accelerations['ay']
az = accelerations['az']
print(f"Acceleration - X: {ax}, Y: {ay}, Z: {az}")

# Get gyros
gyros = alvik.get_gyros()
gx = gyros['gx']
gy = gyros['gy']
gz = gyros['gz']
print(f"Gyro - X: {gx}, Y: {gy}, Z: {gz}")

# Combine all IMU readings
imu_readings = alvik.get_imu()
ax = imu_readings['ax']
ay = imu_readings['ay']
az = imu_readings['az']
gx = imu_readings['gx']
gy = imu_readings['gy']
gz = imu_readings['gz']

print(f"IMU Readings - Acceleration: X: {ax}, Y: {ay}, Z: {az}, Gyro: X: {gx}, Y: {gy}, Z: {gz}")

The Faulty ‘Official’ Code (2) of Arduino Alvik for IMU

Please read what they said on their official website, below –

Now that we’ve got a solid grasp on how the IMU works and how to interact with it, let’s put this knowledge to use in a practical scenario. Events like falls and crashes are critical for a robot, as they often result from sudden changes in linear acceleration (movement along the x, y, or z axes) or angular velocity (rotational movement around these axes). By monitoring these specific changes, we can effectively detect such incidents and respond accordingly. In the following example, we’ll create a sketch that uses the IMU to identify falls and crashes. The sketch will look for sudden spikes in linear acceleration or angular velocity, using set thresholds to accurately determine when these events occur. Here’s how you can implement this functionality:

The ‘Official’ Faulty Code (2)

from arduino_alvik import ArduinoAlvik
import time

# Initialize the Alvik robot
alvik = ArduinoAlvik()
alvik.begin()

# Function to detect falling and crashes
def detect_fall_or_crash():
    imu_readings = alvik.get_imu()
    
    ax = imu_readings['ax']
    ay = imu_readings['ay']
    az = imu_readings['az']
    gx = imu_readings['gx']
    gy = imu_readings['gy']
    gz = imu_readings['gz']
    
    # Thresholds for detecting a fall or crash (example values)
    acceleration_threshold = 2.5
    angular_acceleration_threshold = 1.0
    
    if abs(ax) > acceleration_threshold or abs(ay) > acceleration_threshold or abs(az) > acceleration_threshold:
        print("Fall or crash detected based on acceleration!")
    
    if abs(gx) > angular_acceleration_threshold or abs(gy) > angular_acceleration_threshold or abs(gz) > angular_acceleration_threshold:
        print("Fall or crash detected based on angular acceleration!")

while True:
    detect_fall_or_crash()
    time.sleep(0.1)  # Adjust the sleep time as needed
  1. Achieving control and orientation with Arduino Alvik on rotating platform
  2. Arduino Alvik: How to write the blinking LED code?
  3. How to write code for alternate Blinking LEDs with different colors in Arduino Alvik?
  4. How to move Arduino Alvik forward and backward using simple code?
  5. How to give turns to Arduino Alvik using simple code?
  6. Simple black line follower code for Arduino Alvik
Series Navigation<< Achieving control and orientation with Arduino Alvik on rotating platform<< Simple black line follower code for Arduino Alvik
Dr. Dattaraj Vidyasagar
Dr. Dattaraj Vidyasagar

M.S. Electronics & Telecomm. (Cleveland Institute of Electronics, Ohio), Associate Member (IETE, Kolkata), Panelist on Dr. Homi Bhabha Foundation, Google certified educator (Level-1), Mentor of Change (MoC-1619) Niti Ayog, Government of India, International Robotics Trainer, Veteran of Applied Electronics since 35+ years.

5 1 vote
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Tanaya
Tanaya
1 month ago

Very useful post. I recently purchased Alvik but on the official website of Arduino there is no good information about its coding. Thanks for the series you are publishing on Alvik.
Its very useful.

VSAcademy
Admin
1 month ago
Reply to  Tanaya

Thanks for the 5 star rating.
If you have any questions regarding Alvik, we would love to hear.
It will help us understand the actual requirement of the hobbyists working on Alvik.

2
0
Would love your thoughts, please comment.x
()
x