# Dronekit & SITL ###### tags: `Drone` `python` `Dronekit` ![Dronekit](https://img.shields.io/pypi/v/dronekit?label=dronekit) ![Dronekit-SITL](https://img.shields.io/pypi/v/dronekit-sitl?label=dronekit-sitl) >The SITL (software in the loop) simulator allows you to run Plane, Copter or Rover without any hardware. It is a build of the autopilot code using an ordinary C++ compiler, giving you a native executable that allows you to test the behaviour of the code(python) without hardware.[color=orange] ## Tools - [Dronekit-python](http://dronekit.io) - [Dronekit-SITL](https://ardupilot.org/dev/docs/sitl-simulator-software-in-the-loop.html) :::warning :warning: DroneKit-SITL currently only supplies x86 binaries for Mac, Linux and Windows. You can't run it on ARM platforms like RaPi. ::: ## Steps >Open your terminal type the command down below step by steps. ### Install Dronekit ``` sudo pip3 install dronekit ``` ### Install Dronekit-SITL ``` sudo pip3 install dronekit-sitl ``` ### Download GitHub example >Get the DroneKit-Python example source code onto your local machine. The easiest way to do this is to clone the dronekit-python repository from Github. ``` git clone http://github.com/dronekit/dronekit-python.git ``` ### Startup dronekit-sitl ``` dronekit-sitl copter ``` >The code is simulate the Copter. >It also can simulate the Plane and Rover. ### MAVProxy >Connect to the additional Groud station using MAVProxy. >Open the new terminal and type the command below. ``` mavproxy.py --master tcp:127.0.0.1:5760 --sitl 127.0.0.1:5501 --out 127.0.0.1:14550 --out 127.0.0.1:14551 ``` ## Run the mission >Running an example code. ``` cd dronekit-python/examples/ ``` >The file list in file. ``` ls ``` >Example for goto simple_goto file. ``` cd simple_goto ``` >Run the code and connect to the protocal. ``` python3 simple_goto.py --connect 127.0.0.1:14550 ``` That done you can see the simulator running the mission given by you. --- >Try the python code by yourself. >You can just copy the code below and create a file then run it. ### Qruick start ```python= print "Start simulator (SITL)" import dronekit_sitl sitl = dronekit_sitl.start_default() connection_string = sitl.connection_string() # Import DroneKit-Python from dronekit import connect, VehicleMode # Connect to the Vehicle. print("Connecting to vehicle on: %s" % (connection_string,)) vehicle = connect(connection_string, wait_ready=True) # Get some vehicle attributes (state) print "Get some vehicle attribute values:" print " GPS: %s" % vehicle.gps_0 print " Battery: %s" % vehicle.battery print " Last Heartbeat: %s" % vehicle.last_heartbeat print " Is Armable?: %s" % vehicle.is_armable print " System status: %s" % vehicle.system_status.state print " Mode: %s" % vehicle.mode.name # settable # Close vehicle object before exiting script vehicle.close() # Shut down simulator sitl.stop() print("Completed") ``` ### Takeoff and Land ```python= from dronekit import connect, VehicleMode, LocationGlobalRelative from pymavlink import mavutil import time import argparse parser = argparse.ArgumentParser() parser.add_argument('--connect', default='127.0.0.1:14550') args = parser.parse_args() # Connect to the Vehicle print 'Connecting to vehicle on: %s' % args.connect vehicle = connect(args.connect, baud=57600, wait_ready=True) # Function to arm and then takeoff to a user specified altitude def arm_and_takeoff(aTargetAltitude): print "Basic pre-arm checks" # Don't let the user try to arm until autopilot is ready while not vehicle.is_armable: print " Waiting for vehicle to initialise..." time.sleep(1) print "Arming motors" # Copter should arm in GUIDED mode vehicle.mode = VehicleMode("GUIDED") vehicle.armed = True while not vehicle.armed: print " Waiting for arming..." time.sleep(1) print "Taking off!" vehicle.simple_takeoff(aTargetAltitude) # Take off to target altitude # Check that vehicle has reached takeoff altitude while True: print " Altitude: ", vehicle.location.global_relative_frame.alt #Break and return from function just below target altitude. if vehicle.location.global_relative_frame.alt>=aTargetAltitude*0.95: print "Reached target altitude" break time.sleep(1) # Initialize the takeoff sequence to 20m arm_and_takeoff(20) print("Take off complete") # Hover for 10 seconds time.sleep(10) print("Now let's land") vehicle.mode = VehicleMode("LAND") # Close vehicle object vehicle.close() ``` ## Reference * [Dronekit official website](http://dronekit.io) * [Dronekit document](https://dronekit-python.readthedocs.io/en/latest/about/index.html) * [SITL Simulator](https://ardupilot.org/dev/docs/sitl-simulator-software-in-the-loop.html) * [Setting up a Simulated Vehicle (SITL)](https://dronekit-python.readthedocs.io/en/latest/develop/sitl_setup.html) * [Dronekit-python document](https://dronekit-python.readthedocs.io/en/latest/about/index.html) * [Using SITL](https://ardupilot.org/dev/docs/using-sitl-for-ardupilot-testing.html) * [Dronekit python github](https://github.com/dronekit/dronekit-python) <i class="fa fa-github" aria-hidden="true"></i> * [Dronekit SITL github](https://github.com/dronekit/dronekit-sitl#dronekit-sitl) <i class="fa fa-github" aria-hidden="true"></i>