Vehicle state reading (location, attitude, battery)
📖 Scenario: You are programming a drone to monitor its current state. The drone provides data about its location, attitude (orientation), and battery level. You want to read and display this information clearly.
🎯 Goal: Build a simple program that stores the drone's state data, sets a battery warning level, extracts the important state details, and then prints the current location, attitude, and battery status.
📋 What You'll Learn
Create a dictionary called vehicle_state with keys location, attitude, and battery and their exact values
Create a variable called battery_warning_level set to 20
Create a new dictionary called important_state that includes only the location and battery from vehicle_state if the battery is below the warning level
Print the important_state dictionary
💡 Why This Matters
🌍 Real World
Drones and other vehicles often need to monitor their location, orientation, and battery to operate safely and efficiently.
💼 Career
Understanding how to read and filter vehicle state data is important for drone operators, robotics engineers, and embedded system developers.
Progress0 / 4 steps
1
Create the vehicle state dictionary
Create a dictionary called vehicle_state with these exact entries: 'location': {'latitude': 34.05, 'longitude': -118.25}, 'attitude': {'roll': 0.1, 'pitch': 0.0, 'yaw': 1.5}, and 'battery': 18.
Drone Programming
Hint
Use curly braces {} to create a dictionary. Include nested dictionaries for location and attitude.
2
Set the battery warning level
Create a variable called battery_warning_level and set it to 20.
Drone Programming
Hint
Just assign the number 20 to the variable battery_warning_level.
3
Create important state dictionary based on battery level
Create a dictionary called important_state that includes the location and battery from vehicle_state only if the battery is less than battery_warning_level. Otherwise, create an empty dictionary.
Drone Programming
Hint
Use an if statement to check battery level and create the dictionary accordingly.
4
Print the important state
Write a print statement to display the important_state dictionary.
Drone Programming
Hint
Use print(important_state) to show the dictionary.
Practice
(1/5)
1. What does the vehicle.location.global_frame property represent in drone programming?
easy
A. The drone's battery percentage
B. The drone's current GPS coordinates (latitude, longitude, altitude)
C. The drone's orientation angles (roll, pitch, yaw)
D. The drone's speed in meters per second
Solution
Step 1: Understand the property name
vehicle.location.global_frame refers to the drone's global position in the world.
Step 2: Identify what global_frame means
Global frame means GPS coordinates: latitude, longitude, and altitude.
Final Answer:
The drone's current GPS coordinates (latitude, longitude, altitude) -> Option B
Quick Check:
Location = GPS coordinates [OK]
Hint: Global frame means GPS location, not battery or angles [OK]
Common Mistakes:
Confusing location with battery status
Mixing attitude (orientation) with location
Thinking global_frame is speed
2. Which of the following is the correct way to read the drone's battery level in percentage using the vehicle object?
easy
A. battery_level = vehicle.battery.remaining
B. battery_level = vehicle.battery.level
C. battery_level = vehicle.battery.level_percent
D. battery_level = vehicle.battery.percent
Solution
Step 1: Recall the battery property
The vehicle object uses vehicle.battery.remaining to get battery percentage.
Step 2: Check other options
Other options like percent, level, or level_percent are not valid properties.
Final Answer:
battery_level = vehicle.battery.remaining -> Option A
A. The property global_frame does not have altitude
B. The print statement syntax is incorrect
C. Altitude should be accessed from vehicle.attitude
D. vehicle.location.global_frame is None before GPS lock
Solution
Step 1: Check property availability
vehicle.location.global_frame.altitude is valid but may be None if GPS is not locked.
Step 2: Understand None value issue
If GPS lock is missing, global_frame is None, so accessing altitude causes error.
Final Answer:
vehicle.location.global_frame is None before GPS lock -> Option D
Quick Check:
GPS lock needed before reading altitude [OK]
Hint: Check if global_frame is None before accessing altitude [OK]
Common Mistakes:
Assuming altitude is in attitude object
Ignoring NoneType errors when GPS not ready
Thinking print syntax is wrong
5. You want to create a function that returns a dictionary with the drone's current latitude, longitude, altitude, roll, pitch, yaw, and battery percentage. Which code snippet correctly does this?