Skip to content

Commit

Permalink
examples: pycodestyle fixes
Browse files Browse the repository at this point in the history
This fixes what pycodestyle complains about which was formerly known as
the PEP8 checker.

Signed-off-by: Julian Oes <[email protected]>
  • Loading branch information
julianoes committed Mar 28, 2023
1 parent c0a0088 commit 08c093c
Show file tree
Hide file tree
Showing 19 changed files with 180 additions and 108 deletions.
1 change: 1 addition & 0 deletions examples/all_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
from mavsdk import System


async def run():
# Connect to the drone
drone = System()
Expand Down
51 changes: 40 additions & 11 deletions examples/camera_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
current_settings = []
possible_setting_options = []


async def run():
drone = System()
await drone.connect(system_address="udp://:14540")
Expand Down Expand Up @@ -61,7 +62,8 @@ async def run():
print_possible_settings(possible_setting_options)

try:
index_setting = await make_user_choose_setting(possible_setting_options)
index_setting = await \
make_user_choose_setting(possible_setting_options)
except ValueError:
print("Invalid index")
continue
Expand All @@ -75,20 +77,32 @@ async def run():
print(f"Options:")
try:
print_possible_options(possible_options)
index_option = await make_user_choose_option(possible_options)
index_option = await \
make_user_choose_option(possible_options)
selected_option = possible_options[index_option - 1]

print(f"Setting {selected_setting.setting_id} to {selected_option.option_description}!")
setting = Setting(selected_setting.setting_id, "", selected_option, selected_setting.is_range)
print(f"Setting {selected_setting.setting_id} "
f"to {selected_option.option_description}!")
setting = Setting(
selected_setting.setting_id,
"",
selected_option,
selected_setting.is_range)
except ValueError:
print("Invalid index")
continue
else:
try:
selected_value = await make_user_choose_option_range(possible_options)

print(f"Setting {selected_setting.setting_id} to {selected_value}!")
setting = Setting(selected_setting.setting_id, "", Option(selected_value, ""), selected_setting.is_range)
selected_value = await \
make_user_choose_option_range(possible_options)

print(f"Setting {selected_setting.setting_id}"
f" to {selected_value}!")
setting = Setting(
selected_setting.setting_id,
"",
Option(selected_value, ""),
selected_setting.is_range)
except ValueError:
print("Invalid value")
continue
Expand All @@ -102,21 +116,25 @@ async def run():
print("Invalid input!")
continue


async def observe_camera_mode(drone):
global camera_mode
async for mode in drone.camera.mode():
camera_mode = mode


async def observe_current_settings(drone):
global current_settings
async for settings in drone.camera.current_settings():
current_settings = settings


async def observe_possible_setting_options(drone):
global possible_setting_options
async for settings in drone.camera.possible_setting_options():
possible_setting_options = settings


def print_current_settings():
print(f"* CAM_MODE: {camera_mode}")
for setting in current_settings:
Expand All @@ -126,6 +144,7 @@ def print_current_settings():
else:
print(f" -> {setting.option.option_description}")


async def make_user_choose_camera_mode():
index_mode_str = await ainput(f"\nWhich mode do you want? [1..2] >>> ")
index_mode = int(index_mode_str)
Expand All @@ -134,38 +153,46 @@ async def make_user_choose_camera_mode():

return index_mode


def print_possible_settings(possible_settings):
i = 1
for setting in possible_settings:
print(f"{i}. {setting.setting_id}: {setting.setting_description}")
i += 1


async def make_user_choose_setting(possible_settings):
n_settings = len(possible_settings)
index_setting_str = await ainput(f"\nWhich setting do you want to change? [1..{n_settings}] >>> ")
index_setting_str = await \
ainput(f"\nWhich setting do you want to change?"
f" [1..{n_settings}] >>> ")

index_setting = int(index_setting_str)
if (index_setting < 1 or index_setting > n_settings):
raise ValueError()

return index_setting


def print_possible_options(possible_options):
i = 1
for possible_option in possible_options:
print(f"{i}. {possible_option.option_description}")
i += 1


async def make_user_choose_option(possible_options):
n_options = len(possible_options)
index_option_str = await ainput(f"\nWhich option do you want? [1..{n_options}] >>> ")
index_option_str = await \
ainput(f"\nWhich option do you want? [1..{n_options}] >>> ")

index_option = int(index_option_str)
if (index_option < 1 or index_option > n_options):
raise ValueError()

return index_option


async def make_user_choose_option_range(possible_options):
min_value = float(possible_options[0].option_id)
max_value = float(possible_options[1].option_id)
Expand All @@ -175,7 +202,9 @@ async def make_user_choose_option_range(possible_options):
interval_value = float(possible_options[2].option_id)
interval_text = f"interval: {interval_value}"

value_str = await ainput(f"\nWhat value do you want? [{min_value}, {max_value}] {interval_text} >>> ")
value_str = await \
ainput(f"\nWhat value do you want?"
f" [{min_value}, {max_value}] {interval_text} >>> ")

value = float(value_str)
if (value < min_value or value > max_value):
Expand Down
6 changes: 4 additions & 2 deletions examples/failure_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from mavsdk import System
from mavsdk.failure import FailureType, FailureUnit


async def run():
drone = System()
await drone.connect(system_address="udp://:14540")
Expand Down Expand Up @@ -44,13 +45,14 @@ async def run():
break

print("-- Flying up")
flying_alt = goto_alt + 20.0 # To fly drone 20m above the ground plane
flying_alt = goto_alt + 20.0 # To fly drone 20m above the ground plane
await drone.action.goto_location(goto_lat, goto_lon, flying_alt, 0)

await asyncio.sleep(5)

print("-- Injecting GPS failure")
await drone.failure.inject(FailureUnit.SENSOR_GPS, FailureType.OFF, instance=0)
await drone.failure.inject(
FailureUnit.SENSOR_GPS, FailureType.OFF, instance=0)

print("-- Waiting 20s before exiting script...")
await asyncio.sleep(20)
Expand Down
49 changes: 29 additions & 20 deletions examples/follow_me_example.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
#!/usr/bin/env python3

#This example shows how to use the follow me plugin
# This example shows how to use the follow me plugin

import asyncio
from mavsdk import System
from mavsdk.follow_me import (Config, FollowMeError, TargetLocation)

default_height = 8.0 #in Meters
follow_distance = 2.0 #in Meters, this is the distance that the drone will remain away from Target while following it
#Direction relative to the Target
#Options are NONE, FRONT, FRONT_LEFT, FRONT_RIGHT, BEHIND

default_height = 8.0 # in meters
# distance between drone and target
follow_distance = 2.0 # in meters

# Direction relative to the Target
# Options are NONE, FRONT, FRONT_LEFT, FRONT_RIGHT, BEHIND
direction = Config.FollowDirection.BEHIND
responsiveness = 0.02
responsiveness = 0.02

# This list contains fake location coordinates
# (These coordinates are obtained from mission.py example)
fake_location = [[47.398039859999997, 8.5455725400000002],
[47.398036222362471, 8.5450146439425509],
[47.397825620791885, 8.5450092830163271]]

#This list contains fake location coordinates (These coordinates are obtained from mission.py example)
fake_location = [[47.398039859999997,8.5455725400000002],[47.398036222362471,8.5450146439425509],[47.397825620791885,8.5450092830163271]]

async def fly_drone():
drone = System()
Expand All @@ -32,35 +39,37 @@ async def fly_drone():
print("-- Global position estimate OK")
break

#Arming the drone
print ("-- Arming")
# Arming the drone
print("-- Arming")
await drone.action.arm()

#Follow me Mode requires some configuration to be done before starting the mode
# Follow me Mode requires some configuration to be done before starting
# the mode
conf = Config(default_height, follow_distance, direction, responsiveness)
await drone.follow_me.set_config(conf)

print ("-- Taking Off")
print("-- Taking Off")
await drone.action.takeoff()
await asyncio.sleep(8)
print ("-- Starting Follow Me Mode")
print("-- Starting Follow Me Mode")
await drone.follow_me.start()
await asyncio.sleep(8)

#This for loop provides fake coordinates from the fake_location list for the follow me mode to work
#In a simulator it won't make much sense though
for latitude,longitude in fake_location:
# This for loop provides fake coordinates from the fake_location list for
# the follow me mode to work.
# In a simulator it won't make much sense though
for latitude, longitude in fake_location:
target = TargetLocation(latitude, longitude, 0, 0, 0, 0)
print ("-- Following Target")
print("-- Following Target")
await drone.follow_me.set_target_location(target)
await asyncio.sleep(2)

#Stopping the follow me mode
print ("-- Stopping Follow Me Mode")
# Stopping the follow me mode
print("-- Stopping Follow Me Mode")
await drone.follow_me.stop()
await asyncio.sleep(5)

print ("-- Landing")
print("-- Landing")
await drone.action.land()

if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions examples/geofence.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"""
This example shows how to use the geofence plugin.
Note: The behavior when your vehicle hits the geofence is NOT configured in this example.
Note: The behavior when your vehicle hits the geofence is NOT configured
in this example.
"""

Expand All @@ -25,7 +26,8 @@ async def run():
print(f"-- Connected to drone!")
break

# Fetch the home location coordinates, in order to set a boundary around the home location
# Fetch the home location coordinates, in order to set a boundary around
# the home location.
print("Fetching home location coordinates...")
async for terrain_info in drone.telemetry.home():
latitude = terrain_info.latitude_deg
Expand Down
4 changes: 3 additions & 1 deletion examples/gimbal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from mavsdk import System
from mavsdk.gimbal import GimbalMode, ControlMode


async def run():
# Init the drone
drone = System()
await drone.connect(system_address="udp://:14540")

# Start printing gimbal position updates
print_gimbal_position_task = asyncio.ensure_future(print_gimbal_position(drone))
print_gimbal_position_task = \
asyncio.ensure_future(print_gimbal_position(drone))

print("Taking control of gimbal")
await drone.gimbal.take_control(ControlMode.PRIMARY)
Expand Down
16 changes: 10 additions & 6 deletions examples/manual_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"""
This example shows how to use the manual controls plugin.
Note: Manual inputs are taken from a test set in this example to decrease complexity. Manual inputs
can be received from devices such as a joystick using third-party python extensions
Note: Manual inputs are taken from a test set in this example to decrease
complexity. Manual inputs can be received from devices such as a joystick
using third-party python extensions.
Note: Taking off the drone is not necessary before enabling manual inputs. It is acceptable to send
positive throttle input to leave the ground. Takeoff is used in this example to decrease complexity
Note: Taking off the drone is not necessary before enabling manual inputs.
It is acceptable to send positive throttle input to leave the ground.
Takeoff is used in this example to decrease complexity
"""

import asyncio
Expand Down Expand Up @@ -80,12 +82,14 @@ async def manual_controls():
roll = float(input_list[0])
# get current state of pitch axis (between -1 and 1)
pitch = float(input_list[1])
# get current state of throttle axis (between -1 and 1, but between 0 and 1 is expected)
# get current state of throttle axis
# (between -1 and 1, but between 0 and 1 is expected)
throttle = float(input_list[2])
# get current state of yaw axis (between -1 and 1)
yaw = float(input_list[3])

await drone.manual_control.set_manual_control_input(roll, pitch, throttle, yaw)
await drone.manual_control.set_manual_control_input(
roll, pitch, throttle, yaw)

await asyncio.sleep(0.1)

Expand Down
5 changes: 3 additions & 2 deletions examples/mission_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ async def run():
print(f"-- Connected to drone!")
break

mission_import_data = await drone.mission_raw.import_qgroundcontrol_mission("example-mission.plan")
mission_import_data = await \
drone.mission_raw.import_qgroundcontrol_mission(
"example-mission.plan")
print(f"{len(mission_import_data.mission_items)} mission items imported")
await drone.mission_raw.upload_mission(mission_import_data.mission_items)
print("Mission uploaded")



if __name__ == "__main__":
# Run the asyncio loop
asyncio.run(run())
7 changes: 5 additions & 2 deletions examples/mission_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mavsdk import System
from mavsdk import mission_raw


async def px4_connect_drone():
drone = System()
await drone.connect(system_address="udp://:14540")
Expand All @@ -14,19 +15,21 @@ async def px4_connect_drone():
print("-- Connected to drone!")
return drone


async def run():
drone = await px4_connect_drone()
await run_drone(drone)


async def run_drone(drone):

mission_items = []

mission_items.append(mission_raw.MissionItem(
0, # start seq at 0
0, # start seq at 0
6,
16,
1, # first one is current
1, # first one is current
1,
0, 10, 0, float('nan'),
int(47.40271757 * 10**7),
Expand Down
Loading

0 comments on commit 08c093c

Please sign in to comment.