The simple process is divided into three steps as shown below:

  • Connect to the plane and switch to virtualStickControl mode
  • Send the control data,
  • Close virtualStickControl mode when finished

VirtualStickMode

After connecting to the flying plane, obtain the DJIFlightController instance flightController from the DJIAircraft object. The code for switching to API control mode is simple, just one line:

  flightController.setVirtualStickModeEnabled(true, withCompletion: nil)
Copy the code

However, this line of code may not always be successful, and the switch can only happen if:

  1. The flight control program is not executing the flight control path. If the vehicle cannot switch when performing waypoint, Hotpoint, and follow missions.
  2. FlightOrientationMode needs to be set to aircraftHeading.
  flightController.setFlightOrientationMode(.aircraftHeading, withCompletion: nil)
Copy the code

FlightOrientationMode The default orientation is based on the current orientation of the UAV. This restriction should be added for safety reasons, if you want to move the vehicle, usually based on the image of the video to determine the position movement. FlightOrientationMode has two other modes: Course Lock and Home Lock. They lock on to a certain position to determine the direction of movement, which is useful for special missions, but it’s intuitive if you’re programmed to control the vehicle and still follow the current orientation of the vehicle.

After successfully switching to the program to control the movement of the aircraft, the control lever on the remote controller will be in an invalid state

This is a very, very important and overlooked point. The design should also be for safety reasons, hoping that the aircraft has only one clear control at a time, if the remote control is sent to the left at a time, the program controls to the right. Both instructions have the same priority, and it is not known which one takes precedence. So remember to set VirtualStickMode to false after the control motion is complete.

VirtualStickFlightControlData

After switching to program-controlled motion mode, you can send control instructions. As well as the two joysticks on the remote control, you can control the aircraft’s rise and fall, horizontal rotation, horizontal forward and backward, left and right. The parameters of the movement instruction is a DJIVirtualStickFlightControlData type structure, can control the movement of the four dimensions above mentioned, so there are four floating-point values can be set:

typedef struct
{
   float pitch;
   float roll;
   float yaw;
   float verticalThrottle;
} DJIVirtualStickFlightControlData
Copy the code

In order to allow users to control the movement of the aircraft more flexibly, the parameter values of these four dimensions represent different meanings in different modes.

RollPitchControlMode

The control modes for horizontal X and Y motions are consistent, so together, the enumeration is called RollPitchControlMode. When setting the value of horizontal motion, you need to determine the reference coordinate system selected at this time. DJI provides two Coordinate systems: Body Coordinate and Ground(World) Coordinate for more convenient attitude control.

Coordinate system

Set the coordinate system of the mode is very simple, set rollPitchCoordinateSystem can:

   flightController.rollPitchCoordinateSystem = .ground
Copy the code

Body Coordinate system: Body Coordinate

World Coordinate: Ground(World) Coordinate

RollPitchControlModeAngle

In fuselage coordinates, the Angle is the rotation Angle of the fuselage’s X and Y axes. The value ranges from -30 to 30 degrees.

RollPitchControlModeVelocity

The X and Y velocities range from -15 to 15 meters per second. The X and Y axes are going to be different depending on the coordinate system, as we’ve already explained.

YawControlMode

Yaw from the ground point of view is the aircraft rotating in place, that is, adjusting the nose orientation. There are two modes for the same parameter:

Angle

Relative to the rotation Angle of the nose, the value range is not 0-360, but -180 to 180. A positive Angle indicates clockwise rotation, and a negative Angle value indicates counterclockwise rotation.

AngularVelocity

The angular velocity of the rotation, which ranges from -100 to 100. A negative number indicates a counterclockwise rotation. Select this mode if you want the vehicle to rotate uniformly at a specified angular velocity.

VerticalControlMode

There are also two kinds of vertical height control.

Velocity

Height adjustment speed. The value ranges from -4 to 4. A positive value means you’re flying high, and a negative value means you’re falling.

Position

Directly set the desired height value, ranging from 0 to 500 meters. That means it can fly up to 500 meters.

Send instructions

The instruction code for sending control is simple, just one line:

   flightController.send(controlData, withCompletion: { error in  })
Copy the code

But things are not as simple as the code looks. The VirtualStick API is designed to communicate with the controller’s joystick. For example, if you push a joystick up, the aircraft will move up. So as you push up, the remote keeps sending the vehicle up at one frequency. As you release, the stick returns to center and commands stop. It wasn’t just one instruction sent. Similar to this scenario, if you want to control the aircraft’s movement, your commands should be continuously transmitted. The official transmission frequency is 10 to 25 Hertz. To translate this, the program should send control commands every 0.04 to 0.1 seconds. The program may be very clear about the end point of the aircraft movement and think that it only needs to call the interface once. However, for safety reasons, if the instruction is not continuously sent, the aircraft may not execute the instruction after receiving it. In order to avoid the aircraft not executing the instruction because of communication error, a timer can be used to send the instruction at a fixed interval. It is simpler to send 5 times at the correct frequency. If the frequency is 0.1 second, then it takes 0.5 seconds to ensure that a command is accepted by the aircraft. Another idea is to obtain the attitude information of the aircraft (DJISDKVector3D) from KeyManager after the instruction is sent. If the attitude data is changed during the instruction sending, then the control instruction must be executed.


Lofty ideal dynamic league MeshKit lofty ideal information technology co., LTD., a subsidiary of a iot based on unmanned aerial vehicle (uav) + artificial intelligence work platform, for the enterprise users and professional individual users with convenient and easy to use unmanned aerial vehicle (uav) software and cloud computing solutions, through the task automation, machine more collaborative, scene custom development function, such as the core of artificial intelligence technology, Greatly improve the efficiency of customers using intelligent iot devices such as drones to solve practical production problems. We are recruiting iOS and Android engineers in Hangzhou and Shenzhen. If you are interested, please send your resume to us: Qizhi Information Technology Recruitment information.