00001
00002
00003 import rospy
00004 import std_msgs.msg
00005 import dji_sdk.msg
00006 import nav_msgs.msg
00007 import dji_sdk.srv
00008 import math
00009 import time
00010 import actionlib
00011 import roslib
00012
00013 class DJIDrone:
00014 HORIZ_ATT = 0x00
00015 HORIZ_VEL = 0x40
00016 HORIZ_POS = 0x80
00017 VERT_VEL = 0x00
00018 VERT_POS = 0x10
00019 VERT_TRU = 0x20
00020 YAW_ANG = 0x00
00021 YAW_RATE = 0x08
00022 HORIZ_GND = 0x00
00023 HORIZ_BODY = 0x02
00024 STABLE_OFF = 0x00
00025 STABLE_ON = 0x01
00026
00027 def acceleration_subscriber_callback(self, acceleration):
00028 self.acceleration = acceleration
00029
00030 def attitude_quaternion_subscriber_callback(self, attitude_quaternion):
00031 self.attitude_quaternion = attitude_quaternion
00032
00033 def compass_subscriber_callback(self, compass):
00034 self.compass = compass
00035
00036 def flight_control_info_subscriber_callback(self, flight_control_info):
00037 self.flight_control_info = flight_control_info
00038
00039 def flight_status_subscriber_callback(self, flight_status):
00040 self.flight_status = flight_status
00041
00042 def gimbal_subscriber_callback(self, gimbal):
00043 self.gimbal = gimbal
00044
00045 def global_position_subscriber_callback(self, global_position):
00046 self.global_position = global_position
00047
00048 def local_position_subscriber_callback(self, local_position):
00049 self.local_position = local_position
00050
00051 def power_status_subscriber_callback(self, power_status):
00052 self.power_status = power_status
00053
00054 def rc_channels_subscriber_callback(self, rc_channels):
00055 self.rc_channels = rc_channels
00056
00057 def velocity_subscriber_callback(self, velocity):
00058 self.velocity = velocity
00059
00060 def activation_subscriber_callback(self, activation):
00061 self.activation = activation
00062
00063 def odometry_subscriber_callback(self, odometry):
00064 self.odometry = odometry
00065
00066 def time_stamp_subscriber_callback(self, time_stamp):
00067 self.time_stamp = time_stamp
00068
00069 def init_subscribers(self):
00070 self.acceleration_subscriber = rospy.Subscriber("dji_sdk/acceleration", dji_sdk.msg.Acceleration, self.acceleration_subscriber_callback)
00071 self.attitude_quaternion_subscriber = rospy.Subscriber("dji_sdk/attitude_quaternion", dji_sdk.msg.AttitudeQuaternion, self.attitude_quaternion_subscriber_callback)
00072 self.compass_subscriber = rospy.Subscriber("dji_sdk/compass", dji_sdk.msg.Compass, self.compass_subscriber_callback)
00073 self.flight_control_info_subscriber = rospy.Subscriber("dji_sdk/flight_control_info", dji_sdk.msg.FlightControlInfo, self.flight_control_info_subscriber_callback)
00074 self.flight_status_subscriber = rospy.Subscriber("dji_sdk/flight_status", std_msgs.msg.UInt8, self.flight_status_subscriber_callback)
00075 self.gimbal_subscriber = rospy.Subscriber("dji_sdk/gimbal", dji_sdk.msg.Gimbal, self.gimbal_subscriber_callback)
00076 self.global_position_subscriber = rospy.Subscriber("dji_sdk/global_position", dji_sdk.msg.GlobalPosition, self.global_position_subscriber_callback)
00077 self.local_position_subscriber = rospy.Subscriber("dji_sdk/local_position", dji_sdk.msg.LocalPosition, self.local_position_subscriber_callback)
00078 self.power_status_subscriber = rospy.Subscriber("dji_sdk/power_status", dji_sdk.msg.PowerStatus, self.power_status_subscriber_callback)
00079 self.rc_channels_subscriber = rospy.Subscriber("dji_sdk/rc_channels", dji_sdk.msg.RCChannels, self.rc_channels_subscriber_callback)
00080 self.velocity_subscriber = rospy.Subscriber("dji_sdk/velocity", dji_sdk.msg.Velocity, self.velocity_subscriber_callback)
00081 self.activation_subscriber = rospy.Subscriber("dji_sdk/activation", std_msgs.msg.UInt8, self.activation_subscriber_callback)
00082 self.odometry_subscriber = rospy.Subscriber("dji_sdk/odometry", nav_msgs.msg.Odometry, self.odometry_subscriber_callback)
00083 self.time_stamp_subscriber = rospy.Subscriber("dji_sdk/time_stamp", dji_sdk.msg.TimeStamp, self.time_stamp_subscriber_callback)
00084
00085 def init_services(self):
00086 rospy.wait_for_service("dji_sdk/attitude_control")
00087 rospy.wait_for_service("dji_sdk/camera_action_control")
00088 rospy.wait_for_service("dji_sdk/drone_task_control")
00089 rospy.wait_for_service("dji_sdk/gimbal_angle_control")
00090 rospy.wait_for_service("dji_sdk/gimbal_speed_control")
00091 rospy.wait_for_service("dji_sdk/global_position_control")
00092 rospy.wait_for_service("dji_sdk/local_position_control")
00093 rospy.wait_for_service("dji_sdk/sdk_permission_control")
00094 rospy.wait_for_service("dji_sdk/velocity_control")
00095 rospy.wait_for_service("dji_sdk/drone_arm_control")
00096 rospy.wait_for_service("dji_sdk/virtual_rc_enable_control")
00097 rospy.wait_for_service("dji_sdk/virtual_rc_data_control")
00098 rospy.wait_for_service("dji_sdk/sync_flag_control")
00099
00100 self.attitude_control_service = rospy.ServiceProxy("dji_sdk/attitude_control", dji_sdk.srv.AttitudeControl)
00101 self.camera_action_control_service = rospy.ServiceProxy("dji_sdk/camera_action_control", dji_sdk.srv.CameraActionControl)
00102 self.drone_task_control_service = rospy.ServiceProxy("dji_sdk/drone_task_control", dji_sdk.srv.DroneTaskControl)
00103 self.gimbal_angle_control_service = rospy.ServiceProxy("dji_sdk/gimbal_angle_control", dji_sdk.srv.GimbalAngleControl)
00104 self.gimbal_speed_control_service = rospy.ServiceProxy("dji_sdk/gimbal_speed_control", dji_sdk.srv.GimbalSpeedControl)
00105 self.global_position_control_service = rospy.ServiceProxy("dji_sdk/global_position_control", dji_sdk.srv.GlobalPositionControl)
00106 self.local_position_control_service = rospy.ServiceProxy("dji_sdk/local_position_control", dji_sdk.srv.LocalPositionControl)
00107 self.sdk_permission_control_service = rospy.ServiceProxy("dji_sdk/sdk_permission_control", dji_sdk.srv.SDKPermissionControl)
00108 self.velocity_control_service = rospy.ServiceProxy("dji_sdk/velocity_control", dji_sdk.srv.VelocityControl)
00109 self.drone_arm_service = rospy.ServiceProxy("dji_sdk/drone_arm_control", dji_sdk.srv.DroneArmControl)
00110 self.drone_vrc_enable_service = rospy.ServiceProxy("dji_sdk/virtual_rc_enable_control", dji_sdk.srv.VirtualRCEnableControl)
00111 self.drone_vrc_data_service = rospy.ServiceProxy("dji_sdk/virtual_rc_data_control", dji_sdk.srv.VirtualRCDataControl)
00112 self.sync_timestamp_service = rospy.ServiceProxy("dji_sdk/sync_flag_control", dji_sdk.srv.SyncFlagControl)
00113
00114 def init_actions(self):
00115 self.local_position_navigation_action_client = actionlib.SimpleActionClient("dji_sdk/local_position_navigation_action", dji_sdk.msg.LocalPositionNavigationAction)
00116 self.local_position_navigation_action_client.wait_for_server()
00117 self.global_position_navigation_action_client = actionlib.SimpleActionClient("dji_sdk/global_position_navigation_action", dji_sdk.msg.GlobalPositionNavigationAction)
00118 self.global_position_navigation_action_client.wait_for_server()
00119 self.waypoint_navigation_action_client = actionlib.SimpleActionClient("dji_sdk/waypoint_navigation_action", dji_sdk.msg.WaypointNavigationAction)
00120 self.waypoint_navigation_action_client.wait_for_server()
00121
00122 def local_position_navigation_send_request(self, x, y, z):
00123 goal = dji_sdk.msg.LocalPositionNavigationGoal(x = x, y = y, z = z)
00124 self.local_position_navigation_action_client.send_goal(goal)
00125
00126 def global_position_navigation_send_request(self, latitude, longitude, altitude):
00127 goal = dji_sdk.msg.GlobalPositionNavigationGoal(latitude = latitude, longitude = longitude, altitude = altitude)
00128 self.global_position_navigation_action_client.send_goal(goal)
00129
00130 def waypoint_navigation_send_request(self, waypoint_list):
00131 goal = dji_sdk.msg.WaypointNavigationGoal(waypoint_list=dji_sdk.msg.WaypointList(waypoint_list=waypoint_list))
00132 self.waypoint_navigation_action_client.send_goal(goal)
00133
00134 def arm_drone(self):
00135 self.drone_arm_service(arm=1)
00136
00137 def disarm_drone(self):
00138 self.drone_arm_service(arm=0)
00139
00140 def takeoff(self):
00141 self.drone_task_control_service(task=4)
00142
00143 def landing(self):
00144 self.drone_task_control_service(task=6)
00145
00146 def gohome(self):
00147 self.drone_task_control_service(task=1)
00148
00149 def take_picture(self):
00150 self.camera_action_control_service(camera_action=0)
00151
00152 def vrc_start(self):
00153 self.drone_vrc_enable_service(enable=1, if_back_to_real=1)
00154
00155 def vrc_stop(self):
00156 self.drone_vrc_enable_service(enable=0, if_back_to_real=1)
00157
00158 def vrc_control(self, roll=1024, pitch=1024, throttle=1024, yaw=1024, gear=1324, mode=496):
00159 self.drone_vrc_data_service(channel_data = [roll, pitch, throttle, yaw, gear, 0, mode, 0, 0, 0, 0, 0, 0, 0, 0, 0])
00160
00161 def sync_timestamp(self, frequency=0):
00162 self.sync_timestamp_service(frequency=frequency)
00163
00164 def start_video(self):
00165 self.camera_action_control_service(camera_action=1)
00166
00167 def stop_video(self):
00168 self.camera_action_control_service(camera_action=2)
00169
00170 def gimbal_angle_control(self, yaw = 0, roll = 0, pitch = 0, duration = 0, absolute_or_incremental = True, yaw_cmd_ignore = False, roll_cmd_ignore = False, pitch_cmd_ignore = False):
00171 self.gimbal_angle_control_service(yaw = yaw, roll = roll, pitch = pitch, duration = duration,
00172 absolute_or_incremental = absolute_or_incremental, yaw_cmd_ignore = yaw_cmd_ignore, roll_cmd_ignore = roll_cmd_ignore, pitch_cmd_ignore = pitch_cmd_ignore)
00173
00174 def gimbal_speed_control(self, yaw_rate = 0, roll_rate = 0, pitch_rate = 0):
00175 self.gimbal_speed_control_service(yaw_rate = yaw_rate, roll_rate = roll_rate, pitch_rate = pitch_rate)
00176
00177 def request_sdk_permission_control(self):
00178 self.sdk_permission_control_service(control_enable = 1)
00179
00180 def release_sdk_permission_control(self):
00181 self.sdk_permission_control_service(control_enable = 0)
00182
00183 def attitude_control(self, flag, x, y, z, yaw):
00184 self.attitude_control_service(flag = flag, x = x, y = y, z = z, yaw = yaw)
00185
00186 def velocity_control(self, frame, vx, vy, vz, yawRate):
00187 self.velocity_control_service(frame = frame, vx = vx, vy = vy, vz = vz, yawRate = yawRate)
00188
00189 def local_position_control(self, x, y, z, yaw):
00190 self.local_position_control_service(x = x, y = y, z = z, yaw = yaw)
00191
00192 def global_position_control(self, latitude, longitude, altitude, yaw):
00193 self.global_position_control_service(latitude = latitude, longitude = longitude, altitude = altitude, yaw = yaw)
00194
00195 def lookat(self, x, y, z, duration):
00196 x, y, z
00197 self.local_position
00198
00199 self.gimbal_angle_control_service(flag = flag, yaw = yaw, roll = 0, pitch = 0, duration = duration)
00200 self.local_position_control_service(x = x, y = y, z = z, yaw = yaw)
00201
00202 def __init__(self, namespace=''):
00203 rospy.init_node('dji_sdk_connector')
00204 self.namespace = namespace
00205
00206 self.acceleration = dji_sdk.msg.Acceleration()
00207 self.attitude_quaternion = dji_sdk.msg.AttitudeQuaternion()
00208 self.compass = dji_sdk.msg.Compass()
00209 self.flight_control_info = dji_sdk.msg.FlightControlInfo()
00210 self.flight_status = std_msgs.msg.UInt8()
00211 self.gimbal = dji_sdk.msg.Gimbal()
00212 self.global_position = dji_sdk.msg.GlobalPosition()
00213 self.global_position_ref = dji_sdk.msg.GlobalPosition()
00214 self.local_position = dji_sdk.msg.LocalPosition()
00215 self.local_position_ref = dji_sdk.msg.LocalPosition()
00216 self.power_status = dji_sdk.msg.PowerStatus()
00217 self.rc_channels = dji_sdk.msg.RCChannels()
00218 self.time_stamp = dji_sdk.msg.TimeStamp()
00219 self.velocity = dji_sdk.msg.Velocity()
00220 self.odometry = nav_msgs.msg.Odometry()
00221
00222 self.activation = False
00223 self.localposbase_use_height = True
00224
00225 self.init_actions()
00226 self.init_subscribers()
00227 self.init_services()
00228