Before using the Python API, execute FlightGoggles binary as described in Using the FlightGoggles Renderer
Users can provide input to the UAV in three different ways: directly command motor speeds, control the vehicle with angular rate controller, and control with waypoints controller.
- proceed_motor_speed(vehicle_id, motor_speed_command, duration) - proceed_angular_rate(vehicle_id, angular_rate_command, thrust_command, duration) - proceed_waypoint(vehicle_id, waypoint_command, duration)
Vehicle’s states are composed of timestamp, position, velocity, acceleration_raw, acceleration, attitude_euler_angle, attitude, gyro_raw, angular_velocity, angular_acceleration, motor_speed_raw, motor_speed, and motor_acceleration.
- set_state_vehicle(vehicle_id, **kwargs) - get_state(vehicle_id)
The camera’s position and attitude can be manually changed by set_state_camera and the latest camera image can be obtained by get_camera_image.
- set_state_camera(camera_id, position, attitude) - get_camera_image(camera_id)
This is the example code to run UAV simulation:
import numpy as np
from IPython.display import HTML, display
from flightgoggles.env import flightgoggles_env
if __name__ == "__main__":
env = flightgoggles_env()
env.set_state_vehicle(vehicle_id="uav1", attitude_euler_angle=np.array([0.,0.,-np.pi/2]))
current_pos = env.get_state("uav1")["position"]
current_att = env.get_state("uav1")["attitude_euler_angle"]
target_pose = np.zeros(4)
target_pose[:3] = current_pos + np.array([9.0,0.0,0.0])
target_pose[3] = current_att[2] + np.pi/2
for j in range(400):
env.proceed_waypoint("uav1", target_pose, 0.01)
ani_set = env.plot_state_video(flag_save=False, filename="uav")
display(HTML(ani_set["cam1"].to_html5_video()))
env.close()