client.cpp
Go to the documentation of this file.
00001 
00014 #include <ros/ros.h>
00015 #include <stdio.h>
00016 #include <dji_sdk/dji_drone.h>
00017 #include <cstdlib>
00018 #include <stdlib.h>
00019 #include <actionlib/client/simple_action_client.h>
00020 #include <actionlib/client/terminal_state.h>
00021 
00022 using namespace DJI::onboardSDK;
00023 
00025 void ObtainControlMobileCallback(DJIDrone *drone);
00026 void ReleaseControlMobileCallback(DJIDrone *drone);
00027 void TakeOffMobileCallback(DJIDrone *drone);
00028 void LandingMobileCallback(DJIDrone *drone);
00029 void GetSDKVersionMobileCallback(DJIDrone *drone);
00030 void ArmMobileCallback(DJIDrone *drone);
00031 void DisarmMobileCallback(DJIDrone *drone);
00032 void GoHomeMobileCallback(DJIDrone *drone);
00033 void TakePhotoMobileCallback(DJIDrone *drone);
00034 void StartVideoMobileCallback(DJIDrone *drone);
00035 void StopVideoMobileCallback(DJIDrone *drone);
00037 void DrawCircleDemoMobileCallback(DJIDrone *drone);
00038 void DrawSquareDemoMobileCallback(DJIDrone *drone);
00039 void GimbalControlDemoMobileCallback(DJIDrone *drone);
00040 void AttitudeControlDemoMobileCallback(DJIDrone *drone);
00041 void LocalNavigationTestMobileCallback(DJIDrone *drone);
00042 void GlobalNavigationTestMobileCallback(DJIDrone *drone);
00043 void WaypointNavigationTestMobileCallback(DJIDrone *drone);
00044 void VirtuaRCTestMobileCallback(DJIDrone *drone);
00045 
00047 void StartMapLASLoggingMobileCallback(DJIDrone *drone);
00048 void StopMapLASLoggingMobileCallback(DJIDrone *drone);
00049 void StartCollisionAvoidanceCallback(DJIDrone *drone);
00050 void StopCollisionAvoidanceCallback(DJIDrone *drone);
00051 
00052 
00053 static void Display_Main_Menu(void)
00054 {
00055     printf("\r\n");
00056     printf("+-------------------------- < Main menu > ------------------------+\n");
00057         printf("| [1]  SDK Version Query        | [20] Set Sync Flag Test          |\n");
00058         printf("| [2]  Request Control          | [21] Set Msg Frequency Test      |\n");       
00059         printf("| [3]  Release Control          | [22] Waypoint Mission Upload     |\n");       
00060         printf("| [4]  Takeoff                  | [23] Hotpoint Mission Upload     |\n");       
00061         printf("| [5]  Landing                  | [24] Followme Mission Upload     |\n");       
00062         printf("| [6]  Go Home                  | [25] Mission Start               |\n");       
00063         printf("| [7]  Gimbal Control Sample    | [26] Mission Pause               |\n");       
00064         printf("| [8]  Attitude Control Sample  | [27] Mission Resume              |\n");       
00065         printf("| [9]  Draw Circle Sample       | [28] Mission Cancel              |\n");       
00066         printf("| [10] Draw Square Sample       | [29] Mission Waypoint Download   |\n");       
00067         printf("| [11] Take a Picture           | [30] Mission Waypoint Set Speed  |\n");       
00068         printf("| [12] Start Record Video       | [31] Mission Waypoint Get Speed  |\n");        
00069         printf("| [13] Stop Record Video        | [32] Mission Hotpoint Set Speed  |\n");       
00070         printf("| [14] Local Navigation Test    | [33] Mission Hotpoint Set Radius |\n");       
00071         printf("| [15] Global Navigation Test   | [34] Mission Hotpoint Reset Yaw  |\n");       
00072         printf("| [16] Waypoint Navigation Test | [35] Mission Followme Set Target |\n");       
00073         printf("| [17] Arm the Drone            | [36] Mission Hotpoint Download   |\n");       
00074         printf("| [18] Disarm the Drone         | [37] Enter Mobile commands mode  |\n");
00075     printf("| [19] Virtual RC Test           \n");
00076     printf("+-----------------------------------------------------------------+\n");
00077     printf("input 1/2/3 etc..then press enter key\r\n");
00078     printf("use `rostopic echo` to query drone status\r\n");
00079     printf("----------------------------------------\r\n");
00080 }
00081 
00082    
00083 int main(int argc, char *argv[])
00084 {
00085     int main_operate_code = 0;
00086     int temp32;
00087     int circleRadius;
00088     int circleHeight;
00089     float Phi, circleRadiusIncrements;
00090     int x_center, y_center, yaw_local;
00091     bool valid_flag = false;
00092     bool err_flag = false;
00093     ros::init(argc, argv, "sdk_client");
00094     ROS_INFO("sdk_service_client_test");
00095     ros::NodeHandle nh;
00096     DJIDrone* drone = new DJIDrone(nh);
00097 
00098         //virtual RC test data
00099         uint32_t virtual_rc_data[16];
00100 
00101         //set frequency test data
00102         uint8_t msg_frequency_data[16] = {1,2,3,4,3,2,1,2,3,4,3,2,1,2,3,4};
00103         //waypoint action test data
00104     dji_sdk::WaypointList newWaypointList;
00105     dji_sdk::Waypoint waypoint0;
00106     dji_sdk::Waypoint waypoint1;
00107     dji_sdk::Waypoint waypoint2;
00108     dji_sdk::Waypoint waypoint3;
00109     dji_sdk::Waypoint waypoint4;
00110 
00111         //groundstation test data
00112         dji_sdk::MissionWaypointTask waypoint_task;
00113         dji_sdk::MissionWaypoint         waypoint;
00114         dji_sdk::MissionHotpointTask hotpoint_task;
00115         dji_sdk::MissionFollowmeTask followme_task;
00116         dji_sdk::MissionFollowmeTarget followme_target;
00117     uint8_t userData = 0;
00118     ros::spinOnce();
00119     
00121     drone->setObtainControlMobileCallback(ObtainControlMobileCallback, &userData);
00122     drone->setReleaseControlMobileCallback(ReleaseControlMobileCallback, &userData);
00123     drone->setTakeOffMobileCallback(TakeOffMobileCallback, &userData);
00124     drone->setLandingMobileCallback(LandingMobileCallback, &userData);
00125     drone->setGetSDKVersionMobileCallback(GetSDKVersionMobileCallback, &userData);
00126     drone->setArmMobileCallback(ArmMobileCallback, &userData);
00127     drone->setDisarmMobileCallback(DisarmMobileCallback, &userData);
00128     drone->setGoHomeMobileCallback(GoHomeMobileCallback, &userData);
00129     drone->setTakePhotoMobileCallback(TakePhotoMobileCallback, &userData);
00130     drone->setStartVideoMobileCallback(StartVideoMobileCallback,&userData);
00131     drone->setStopVideoMobileCallback(StopVideoMobileCallback,&userData);
00132     drone->setDrawCircleDemoMobileCallback(DrawCircleDemoMobileCallback, &userData);
00133     drone->setDrawSquareDemoMobileCallback(DrawSquareDemoMobileCallback, &userData);
00134     drone->setGimbalControlDemoMobileCallback(GimbalControlDemoMobileCallback, &userData);
00135     drone->setAttitudeControlDemoMobileCallback(AttitudeControlDemoMobileCallback, &userData);
00136     drone->setLocalNavigationTestMobileCallback(LocalNavigationTestMobileCallback, &userData);
00137     drone->setGlobalNavigationTestMobileCallback(GlobalNavigationTestMobileCallback, &userData);
00138     drone->setWaypointNavigationTestMobileCallback(WaypointNavigationTestMobileCallback, &userData);
00139     drone->setVirtuaRCTestMobileCallback(VirtuaRCTestMobileCallback, &userData);
00140 
00141     drone->setStartMapLASLoggingMobileCallback(StartMapLASLoggingMobileCallback, &userData);
00142     drone->setStopMapLASLoggingMobileCallback(StopMapLASLoggingMobileCallback, &userData);
00143     drone->setStartCollisionAvoidanceCallback(StartCollisionAvoidanceCallback, &userData);
00144     drone->setStopCollisionAvoidanceCallback(StopCollisionAvoidanceCallback, &userData);
00145 
00146         
00147     Display_Main_Menu();
00148     while(1)
00149     {
00150         ros::spinOnce();
00151         std::cout << "Enter Input Val: ";
00152         while(!(std::cin >> temp32)){
00153         std::cin.clear();
00154         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
00155         std::cout << "Invalid input.  Try again: ";
00156         }
00157 
00158         if(temp32>0 && temp32<38)
00159         {
00160             main_operate_code = temp32;         
00161         }
00162         else
00163         {
00164             printf("ERROR - Out of range Input \n");
00165             Display_Main_Menu();
00166             continue;
00167         }
00168         switch(main_operate_code)
00169         {
00170                         case 1:
00171                                 /* SDK version query*/
00172                                 drone->check_version();
00173                                 break;
00174             case 2:
00175                 /* request control ability*/
00176                 drone->request_sdk_permission_control();
00177                 break;
00178             case 3:
00179                 /* release control ability*/
00180                 drone->release_sdk_permission_control();
00181                 break;
00182             case 4:
00183                 /* take off */
00184                 drone->takeoff();
00185                 break;
00186             case 5:
00187                 /* landing*/
00188                 drone->landing();
00189                 break;
00190             case 6:
00191                 /* go home*/
00192                 drone->gohome();
00193                 break;
00194             case 7:
00195                 /*gimbal test*/
00196 
00197                 drone->gimbal_angle_control(0, 0, 1800, 20);
00198                 sleep(2);
00199                 drone->gimbal_angle_control(0, 0, -1800, 20);
00200                 sleep(2);
00201                 drone->gimbal_angle_control(300, 0, 0, 20);
00202                 sleep(2);
00203                 drone->gimbal_angle_control(-300, 0, 0, 20);
00204                 sleep(2);
00205                 drone->gimbal_angle_control(0, 300, 0, 20);
00206                 sleep(2);
00207                 drone->gimbal_angle_control(0, -300, 0, 20);
00208                 sleep(2);
00209                 drone->gimbal_speed_control(100, 0, 0);
00210                 sleep(2);
00211                 drone->gimbal_speed_control(-100, 0, 0);
00212                 sleep(2);
00213                 drone->gimbal_speed_control(0, 0, 200);
00214                 sleep(2);
00215                 drone->gimbal_speed_control(0, 0, -200);
00216                 sleep(2);
00217                 drone->gimbal_speed_control(0, 200, 0);
00218                 sleep(2);
00219                 drone->gimbal_speed_control(0, -200, 0);
00220                 sleep(2);
00221                 drone->gimbal_angle_control(0, 0, 0, 20);
00222                 break;
00223 
00224             case 8:
00225                 /* attitude control sample*/
00226                 drone->takeoff();
00227                 sleep(8);
00228 
00229 
00230                 for(int i = 0; i < 100; i ++)
00231                 {
00232                     if(i < 90)
00233                         drone->attitude_control(0x40, 0, 2, 0, 0);
00234                     else
00235                         drone->attitude_control(0x40, 0, 0, 0, 0);
00236                     usleep(20000);
00237                 }
00238                 sleep(1);
00239 
00240                 for(int i = 0; i < 200; i ++)
00241                 {
00242                     if(i < 180)
00243                         drone->attitude_control(0x40, 2, 0, 0, 0);
00244                     else
00245                         drone->attitude_control(0x40, 0, 0, 0, 0);
00246                     usleep(20000);
00247                 }
00248                 sleep(1);
00249 
00250                 for(int i = 0; i < 200; i ++)
00251                 {
00252                     if(i < 180)
00253                         drone->attitude_control(0x40, -2, 0, 0, 0);
00254                     else
00255                         drone->attitude_control(0x40, 0, 0, 0, 0);
00256                     usleep(20000);
00257                 }
00258                 sleep(1);
00259 
00260                 for(int i = 0; i < 200; i ++)
00261                 {
00262                     if(i < 180)
00263                         drone->attitude_control(0x40, 0, 2, 0, 0);
00264                     else
00265                         drone->attitude_control(0x40, 0, 0, 0, 0);
00266                     usleep(20000);
00267                 }
00268                 sleep(1);
00269 
00270                 for(int i = 0; i < 200; i ++)
00271                 {
00272                     if(i < 180)
00273                         drone->attitude_control(0x40, 0, -2, 0, 0);
00274                     else
00275                         drone->attitude_control(0x40, 0, 0, 0, 0);
00276                     usleep(20000);
00277                 }
00278                 sleep(1);
00279 
00280                 for(int i = 0; i < 200; i ++)
00281                 {
00282                     if(i < 180)
00283                         drone->attitude_control(0x40, 0, 0, 0.5, 0);
00284                     else
00285                         drone->attitude_control(0x40, 0, 0, 0, 0);
00286                     usleep(20000);
00287                 }
00288                 sleep(1);
00289 
00290                 for(int i = 0; i < 200; i ++)
00291                 {
00292                     if(i < 180)
00293                         drone->attitude_control(0x40, 0, 0, -0.5, 0);
00294                     else
00295                         drone->attitude_control(0x40, 0, 0, 0, 0);
00296                     usleep(20000);
00297                 }
00298                 sleep(1);
00299 
00300                 for(int i = 0; i < 200; i ++)
00301                 {
00302                     if(i < 180)
00303                         drone->attitude_control(0xA, 0, 0, 0, 90);
00304                     else
00305                         drone->attitude_control(0xA, 0, 0, 0, 0);
00306                     usleep(20000);
00307                 }
00308                 sleep(1);
00309 
00310                 for(int i = 0; i < 200; i ++)
00311                 {
00312                     if(i < 180)
00313                         drone->attitude_control(0xA, 0, 0, 0, -90);
00314                     else
00315                         drone->attitude_control(0xA, 0, 0, 0, 0);
00316                     usleep(20000);
00317                 }
00318                 sleep(1);
00319 
00320                 drone->landing();
00321 
00322                 break;
00323 
00324             case 9:
00325                 /*draw circle sample*/
00326                 static float R = 2;
00327                 static float V = 2;
00328                 static float x;
00329                 static float y;
00330                 Phi = 0;
00331                 std::cout<<"Enter the radius of the circle in meteres (10m > x > 4m)\n";
00332                 std::cin>>circleRadius;   
00333 
00334                 std::cout<<"Enter height in meteres (Relative to take off point. 15m > x > 5m) \n";
00335                 std::cin>>circleHeight;  
00336 
00337                  if (circleHeight < 5)
00338                 {
00339                     circleHeight = 5;
00340                 }
00341                 else if (circleHeight > 15)
00342                 {
00343                     circleHeight = 15;
00344                 }           
00345                 if (circleRadius < 4)
00346                 {
00347                     circleRadius = 4;
00348                 }
00349                 else if (circleRadius > 10)
00350                 {
00351                     circleRadius = 10;
00352                 } 
00353                 
00354                 x_center = drone->local_position.x;
00355                 y_center = drone->local_position.y;
00356                 circleRadiusIncrements = 0.01;
00357                 
00358                     for(int j = 0; j < 1000; j ++)
00359                 {   
00360                   if (circleRadiusIncrements < circleRadius)
00361                            {
00362                         x =  x_center + circleRadiusIncrements;
00363                         y =  y_center;
00364                             circleRadiusIncrements = circleRadiusIncrements + 0.01;
00365                         drone->local_position_control(x ,y ,circleHeight, 0);
00366                         usleep(20000);
00367                           }
00368                    else
00369                            {
00370                     break;
00371                    }
00372                 }
00373 
00374                 /* start to draw circle */
00375                 for(int i = 0; i < 1890; i ++)
00376                 {   
00377                     x =  x_center + circleRadius*cos((Phi/300));
00378                     y =  y_center + circleRadius*sin((Phi/300));
00379                     Phi = Phi+1;
00380                     drone->local_position_control(x ,y ,circleHeight, 0);
00381                     usleep(20000);
00382                 }
00383                 break;
00384 
00385             case 10:
00386                 /*draw square sample*/
00387                 for(int i = 0;i < 60;i++)
00388                 {
00389                     drone->attitude_control( Flight::HorizontalLogic::HORIZONTAL_POSITION |
00390                             Flight::VerticalLogic::VERTICAL_VELOCITY |
00391                             Flight::YawLogic::YAW_ANGLE |
00392                             Flight::HorizontalCoordinate::HORIZONTAL_BODY |
00393                             Flight::SmoothMode::SMOOTH_ENABLE,
00394                             3, 3, 0, 0 );
00395                     usleep(20000);
00396                 }
00397                 for(int i = 0;i < 60;i++)
00398                 {
00399                     drone->attitude_control( Flight::HorizontalLogic::HORIZONTAL_POSITION |
00400                             Flight::VerticalLogic::VERTICAL_VELOCITY |
00401                             Flight::YawLogic::YAW_ANGLE |
00402                             Flight::HorizontalCoordinate::HORIZONTAL_BODY |
00403                             Flight::SmoothMode::SMOOTH_ENABLE,
00404                             -3, 3, 0, 0);
00405                     usleep(20000);
00406                 }
00407                 for(int i = 0;i < 60;i++)
00408                 {
00409                     drone->attitude_control( Flight::HorizontalLogic::HORIZONTAL_POSITION |
00410                             Flight::VerticalLogic::VERTICAL_VELOCITY |
00411                             Flight::YawLogic::YAW_ANGLE |
00412                             Flight::HorizontalCoordinate::HORIZONTAL_BODY |
00413                             Flight::SmoothMode::SMOOTH_ENABLE,
00414                             -3, -3, 0, 0);
00415                     usleep(20000);
00416                 }
00417                 for(int i = 0;i < 60;i++)
00418                 {
00419                     drone->attitude_control( Flight::HorizontalLogic::HORIZONTAL_POSITION |
00420                             Flight::VerticalLogic::VERTICAL_VELOCITY |
00421                             Flight::YawLogic::YAW_ANGLE |
00422                             Flight::HorizontalCoordinate::HORIZONTAL_BODY |
00423                             Flight::SmoothMode::SMOOTH_ENABLE,
00424                             3, -3, 0, 0);
00425                     usleep(20000);
00426                 }
00427                 break;
00428             case 11:
00429                 /*take a picture*/
00430                 drone->take_picture();
00431                 break;
00432             case 12:
00433                 /*start video*/
00434                 drone->start_video();
00435                 break;
00436             case 13:
00437                 /*stop video*/
00438                 drone->stop_video();
00439                 break;
00440             case 14:
00441                 /* Local Navi Test */
00442                 drone->local_position_navigation_send_request(-100, -100, 100);
00443                 break;
00444             case 15:
00445                 /* GPS Navi Test */
00446                 drone->global_position_navigation_send_request(22.5420, 113.9580, 10);
00447                 break;
00448             case 16:
00449                 /* Waypoint List Navi Test */
00450                 {
00451                     waypoint0.latitude = 22.535;
00452                     waypoint0.longitude = 113.95;
00453                     waypoint0.altitude = 100;
00454                     waypoint0.staytime = 5;
00455                     waypoint0.heading = 0;
00456                 }
00457                 newWaypointList.waypoint_list.push_back(waypoint0);
00458 
00459                 {
00460                     waypoint1.latitude = 22.535;
00461                     waypoint1.longitude = 113.96;
00462                     waypoint1.altitude = 100;
00463                     waypoint1.staytime = 0;
00464                     waypoint1.heading = 90;
00465                 }
00466                 newWaypointList.waypoint_list.push_back(waypoint1);
00467 
00468                 {
00469                     waypoint2.latitude = 22.545;
00470                     waypoint2.longitude = 113.96;
00471                     waypoint2.altitude = 100;
00472                     waypoint2.staytime = 4;
00473                     waypoint2.heading = -90;
00474                 }
00475                 newWaypointList.waypoint_list.push_back(waypoint2);
00476 
00477                 {
00478                     waypoint3.latitude = 22.545;
00479                     waypoint3.longitude = 113.96;
00480                     waypoint3.altitude = 10;
00481                     waypoint3.staytime = 2;
00482                     waypoint3.heading = 180;
00483                 }
00484                 newWaypointList.waypoint_list.push_back(waypoint3);
00485 
00486                 {
00487                     waypoint4.latitude = 22.525;
00488                     waypoint4.longitude = 113.93;
00489                     waypoint4.altitude = 50;
00490                     waypoint4.staytime = 0;
00491                     waypoint4.heading = -180;
00492                 }
00493                 newWaypointList.waypoint_list.push_back(waypoint4);
00494 
00495                 drone->waypoint_navigation_send_request(newWaypointList);
00496                 break;
00497                         case 17:
00498                                 //drone arm
00499                                 drone->drone_arm();
00500                 break;
00501                         case 18:
00502                                 //drone disarm
00503                                 drone->drone_disarm();
00504                 break;
00505                         case 19:
00506                                 //virtual rc test 1: arm & disarm
00507                                 drone->virtual_rc_enable();
00508                                 usleep(20000);
00509 
00510                                 virtual_rc_data[0] = 1024;      //0-> roll      [1024-660,1024+660] 
00511                                 virtual_rc_data[1] = 1024;      //1-> pitch     [1024-660,1024+660]
00512                                 virtual_rc_data[2] = 1024+660;  //2-> throttle  [1024-660,1024+660]
00513                                 virtual_rc_data[3] = 1024;      //3-> yaw       [1024-660,1024+660]
00514                                 virtual_rc_data[4] = 1684;              //4-> gear              {1684(UP), 1324(DOWN)}
00515                                 virtual_rc_data[6] = 1552;      //6-> mode      {1552(P), 1024(A), 496(F)}
00516 
00517                                 for (int i = 0; i < 100; i++){
00518                                         drone->virtual_rc_control(virtual_rc_data);
00519                                         usleep(20000);
00520                                 }
00521 
00522                                 //virtual rc test 2: yaw 
00523                                 drone->virtual_rc_enable();
00524                                 virtual_rc_data[0] = 1024;              //0-> roll      [1024-660,1024+660] 
00525                                 virtual_rc_data[1] = 1024;              //1-> pitch     [1024-660,1024+660]
00526                                 virtual_rc_data[2] = 1024-200;  //2-> throttle  [1024-660,1024+660]
00527                                 virtual_rc_data[3] = 1024;              //3-> yaw       [1024-660,1024+660]
00528                                 virtual_rc_data[4] = 1324;              //4-> gear              {1684(UP), 1324(DOWN)}
00529                                 virtual_rc_data[6] = 1552;      //6-> mode      {1552(P), 1024(A), 496(F)}
00530 
00531                                 for(int i = 0; i < 100; i++) {
00532                                         drone->virtual_rc_control(virtual_rc_data);
00533                                         usleep(20000);
00534                                 }
00535                                 drone->virtual_rc_disable();
00536                                 break;
00537                         case 20:
00538                                 //sync flag
00539                                 drone->sync_flag_control(1);
00540                                 break;
00541                         case 21:
00542                                 //set msg frequency
00543                                 drone->set_message_frequency(msg_frequency_data);
00544                                 break;
00545 
00546                         case 22:
00547 
00548                 // Clear the vector of previous waypoints 
00549                 waypoint_task.mission_waypoint.clear();
00550                 
00551                                 //mission waypoint upload
00552                                 waypoint_task.velocity_range = 10;
00553                                 waypoint_task.idle_velocity = 3;
00554                                 waypoint_task.action_on_finish = 0;
00555                                 waypoint_task.mission_exec_times = 1;
00556                                 waypoint_task.yaw_mode = 4;
00557                                 waypoint_task.trace_mode = 0;
00558                                 waypoint_task.action_on_rc_lost = 0;
00559                                 waypoint_task.gimbal_pitch_mode = 0;
00560 
00561                 static int num_waypoints = 4; 
00562                 static int altitude = 80;
00563                 // Currently hard coded, should be dynamic
00564                 static float orig_lat = 22.5401;
00565                 static float orig_long = 113.9468;
00566                 for(int i = 0; i < num_waypoints; i++)
00567                 {
00568                     
00569                     // Careens in zig-zag pattern
00570                                 waypoint.latitude = (orig_lat+=.0001);
00571                     if (i % 2 == 1){
00572                                     waypoint.longitude = orig_long+=.0001;
00573                     } else {
00574                                     waypoint.longitude = orig_long;
00575                     }
00576                                 waypoint.altitude = altitude-=10;
00577                                 waypoint.damping_distance = 0;
00578                                 waypoint.target_yaw = 0;
00579                                 waypoint.target_gimbal_pitch = 0;
00580                                 waypoint.turn_mode = 0;
00581                                 waypoint.has_action = 0;
00582                                 /*
00583                                 waypoint.action_time_limit = 10;
00584                                 waypoint.waypoint_action.action_repeat = 1;
00585                                 waypoint.waypoint_action.command_list[0] = 1;
00586                                 waypoint.waypoint_action.command_parameter[0] = 1;
00587                                 */
00588     
00589                                 waypoint_task.mission_waypoint.push_back(waypoint);
00590                 } 
00591                 
00592                 /* 
00593 
00594                                 waypoint.latitude = 22.540015;
00595                                 waypoint.longitude = 113.94659;
00596                                 waypoint.altitude = 120;
00597                                 waypoint.damping_distance = 2;
00598                                 waypoint.target_yaw = 180;
00599                                 waypoint.target_gimbal_pitch = 0;
00600                                 waypoint.turn_mode = 0;
00601                                 waypoint.has_action = 0;
00602                                 waypoint.action_time_limit = 10;
00603                                 waypoint.waypoint_action.action_repeat = 1;
00604                                 waypoint.waypoint_action.command_list[0] = 1;
00605                                 waypoint.waypoint_action.command_list[1] = 1;
00606                                 waypoint.waypoint_action.command_parameter[0] = 1;
00607                                 waypoint.waypoint_action.command_parameter[1] = 1;
00608 
00609 
00610                                 waypoint_task.mission_waypoint.push_back(waypoint);
00611 
00612                 */
00613 
00614                                 drone->mission_waypoint_upload(waypoint_task);
00615                                 break;
00616                         case 23:
00617                                 //mission hotpoint upload
00618                                 hotpoint_task.latitude = 22.542813;
00619                                 hotpoint_task.longitude = 113.958902;
00620                                 hotpoint_task.altitude = 20;
00621                                 hotpoint_task.radius = 10;
00622                                 hotpoint_task.angular_speed = 10;
00623                                 hotpoint_task.is_clockwise = 0;
00624                                 hotpoint_task.start_point = 0;
00625                                 hotpoint_task.yaw_mode = 0;
00626 
00627                                 drone->mission_hotpoint_upload(hotpoint_task);
00628                                 break;
00629                         case 24:
00630                                 //mission followme upload
00631                                 followme_task.mode = 0;
00632                                 followme_task.yaw_mode = 0;
00633                                 followme_task.initial_latitude = 23.540091;
00634                                 followme_task.initial_longitude = 113.946593;
00635                                 followme_task.initial_altitude = 10;
00636                                 followme_task.sensitivity = 1;
00637 
00638                                 drone->mission_followme_upload(followme_task);
00639                                 break;
00640                         case 25:
00641                                 //mission start
00642                                 drone->mission_start();
00643                                 break;
00644                         case 26:
00645                                 //mission pause
00646                                 drone->mission_pause();
00647                                 break;
00648                         case 27:
00649                                 //mission resume
00650                                 drone->mission_resume();
00651                                 break;
00652                         case 28:
00653                                 //mission cancel
00654                                 drone->mission_cancel();
00655                                 break;
00656                         case 29:
00657                                 //waypoint mission download
00658                                 waypoint_task = drone->mission_waypoint_download();
00659                                 break;
00660                         case 30:
00661                                 //mission waypoint set speed
00662                                 drone->mission_waypoint_set_speed((float)5);
00663                                 break;
00664                         case 31:
00665                                 //mission waypoint get speed
00666                                 printf("%f", drone->mission_waypoint_get_speed());
00667                                 break;
00668                         case 32:
00669                                 //mission hotpoint set speed
00670                                 drone->mission_hotpoint_set_speed((float)5,(uint8_t)1);
00671                                 break;
00672                         case 33:
00673                                 //mission hotpoint set radius
00674                                 drone->mission_hotpoint_set_radius((float)5);
00675                                 break;
00676                         case 34:
00677                                 //mission hotpoint reset yaw
00678                                 drone->mission_hotpoint_reset_yaw();
00679                                 break;
00680                         case 35:
00681                                 //mission followme update target
00682                                 for (int i = 0; i < 20; i++)
00683                                 {
00684                                         followme_target.latitude = 22.540091 + i*0.000001;
00685                                         followme_target.longitude = 113.946593 + i*0.000001;
00686                                         followme_target.altitude = 100;
00687                                         drone->mission_followme_update_target(followme_target);
00688                                         usleep(20000);
00689                                 }
00690                                 break;
00691             case 37:
00692                 printf("Mobile Data Commands mode entered. Use OSDK Mobile App to use this feature \n");
00693                 printf("End program to exit this mode \n");
00694                 while(1)
00695                 {             
00696                 ros::spinOnce();  
00697                 }
00698                         case 36:
00699                                 hotpoint_task = drone->mission_hotpoint_download();
00700 
00701             default:
00702                 break;
00703         }
00704         main_operate_code = -1;
00705         Display_Main_Menu();
00706     }
00707     return 0;
00708 }
00709 
00711     void ObtainControlMobileCallback(DJIDrone *drone)
00712     {
00713       drone->request_sdk_permission_control();
00714     }
00715 
00716     void ReleaseControlMobileCallback(DJIDrone *drone)
00717     {
00718       drone->release_sdk_permission_control();
00719     }
00720 
00721     void TakeOffMobileCallback(DJIDrone *drone)
00722     {
00723       drone->takeoff();
00724     }
00725 
00726     void LandingMobileCallback(DJIDrone *drone)
00727     {
00728       drone->landing();
00729     }
00730 
00731     void GetSDKVersionMobileCallback(DJIDrone *drone)
00732     {
00733       drone->check_version();
00734     }
00735 
00736     void ArmMobileCallback(DJIDrone *drone)
00737     {
00738       drone->drone_arm();
00739     }
00740 
00741     void DisarmMobileCallback(DJIDrone *drone)
00742     {
00743       drone->drone_disarm();
00744     }
00745 
00746     void GoHomeMobileCallback(DJIDrone *drone)
00747     {
00748       drone->gohome();
00749     }
00750 
00751     void TakePhotoMobileCallback(DJIDrone *drone)
00752     {
00753       drone->take_picture();
00754     }
00755 
00756     void StartVideoMobileCallback(DJIDrone *drone)
00757     {
00758       drone->start_video();
00759     }
00760 
00761     void StopVideoMobileCallback(DJIDrone *drone)
00762     {
00763       drone->stop_video();
00764     }
00765 
00766     void DrawCircleDemoMobileCallback(DJIDrone *drone)
00767     {
00768         static float R = 2;
00769         static float V = 2;
00770         static float x;
00771         static float y;
00772         int circleRadius;
00773         int circleHeight;
00774         float Phi =0, circleRadiusIncrements;
00775         int x_center, y_center, yaw_local; 
00776 
00777         circleHeight = 7;
00778         circleRadius = 7;
00779 
00780         x_center = drone->local_position.x;
00781         y_center = drone->local_position.y;
00782         circleRadiusIncrements = 0.01;
00783 
00784         for(int j = 0; j < 1000; j ++)
00785         {   
00786             if (circleRadiusIncrements < circleRadius)
00787             {
00788                 x =  x_center + circleRadiusIncrements;
00789                 y =  y_center;
00790                 circleRadiusIncrements = circleRadiusIncrements + 0.01;
00791                 drone->local_position_control(x ,y ,circleHeight, 0);
00792                 usleep(20000);
00793             }
00794                 else
00795             {
00796                 break;
00797             }
00798         }
00799         
00800 
00801         /* start to draw circle */
00802         for(int i = 0; i < 1890; i ++)
00803         {   
00804             x =  x_center + circleRadius*cos((Phi/300));
00805             y =  y_center + circleRadius*sin((Phi/300));
00806             Phi = Phi+1;
00807             drone->local_position_control(x ,y ,circleHeight, 0);
00808             usleep(20000);
00809         }
00810 
00811     }
00812     void DrawSquareDemoMobileCallback(DJIDrone *drone)
00813     {
00814     /*draw square sample*/
00815         for(int i = 0;i < 60;i++)
00816         {
00817             drone->attitude_control( Flight::HorizontalLogic::HORIZONTAL_POSITION |
00818             Flight::VerticalLogic::VERTICAL_VELOCITY |
00819             Flight::YawLogic::YAW_ANGLE |
00820             Flight::HorizontalCoordinate::HORIZONTAL_BODY |
00821             Flight::SmoothMode::SMOOTH_ENABLE,
00822             3, 3, 0, 0 );
00823             usleep(20000);
00824         }
00825         for(int i = 0;i < 60;i++)
00826         {
00827             drone->attitude_control( Flight::HorizontalLogic::HORIZONTAL_POSITION |
00828             Flight::VerticalLogic::VERTICAL_VELOCITY |
00829             Flight::YawLogic::YAW_ANGLE |
00830             Flight::HorizontalCoordinate::HORIZONTAL_BODY |
00831             Flight::SmoothMode::SMOOTH_ENABLE,
00832             -3, 3, 0, 0);
00833             usleep(20000);
00834         }
00835         for(int i = 0;i < 60;i++)
00836         {
00837             drone->attitude_control( Flight::HorizontalLogic::HORIZONTAL_POSITION |
00838             Flight::VerticalLogic::VERTICAL_VELOCITY |
00839             Flight::YawLogic::YAW_ANGLE |
00840             Flight::HorizontalCoordinate::HORIZONTAL_BODY |
00841             Flight::SmoothMode::SMOOTH_ENABLE,
00842             -3, -3, 0, 0);
00843             usleep(20000);
00844         }
00845         for(int i = 0;i < 60;i++)
00846         {
00847             drone->attitude_control( Flight::HorizontalLogic::HORIZONTAL_POSITION |
00848             Flight::VerticalLogic::VERTICAL_VELOCITY |
00849             Flight::YawLogic::YAW_ANGLE |
00850             Flight::HorizontalCoordinate::HORIZONTAL_BODY |
00851             Flight::SmoothMode::SMOOTH_ENABLE,
00852             3, -3, 0, 0);
00853             usleep(20000);
00854         }
00855     }
00856 
00857      void GimbalControlDemoMobileCallback(DJIDrone *drone)
00858         {
00859         drone->gimbal_angle_control(0, 0, 1800, 20);
00860         sleep(2);
00861         drone->gimbal_angle_control(0, 0, -1800, 20);
00862         sleep(2);
00863         drone->gimbal_angle_control(300, 0, 0, 20);
00864         sleep(2);
00865         drone->gimbal_angle_control(-300, 0, 0, 20);
00866         sleep(2);
00867         drone->gimbal_angle_control(0, 300, 0, 20);
00868         sleep(2);
00869         drone->gimbal_angle_control(0, -300, 0, 20);
00870         sleep(2);
00871         drone->gimbal_speed_control(100, 0, 0);
00872         sleep(2);
00873         drone->gimbal_speed_control(-100, 0, 0);
00874         sleep(2);
00875         drone->gimbal_speed_control(0, 0, 200);
00876         sleep(2);
00877         drone->gimbal_speed_control(0, 0, -200);
00878         sleep(2);
00879         drone->gimbal_speed_control(0, 200, 0);
00880         sleep(2);
00881         drone->gimbal_speed_control(0, -200, 0);
00882         sleep(2);
00883         drone->gimbal_angle_control(0, 0, 0, 20);
00884         }
00885 
00886     void AttitudeControlDemoMobileCallback(DJIDrone *drone)
00887     {
00888         /* attitude control sample*/
00889         drone->takeoff();
00890         sleep(8);
00891 
00892 
00893         for(int i = 0; i < 100; i ++)
00894         {
00895             if(i < 90)
00896                 drone->attitude_control(0x40, 0, 2, 0, 0);
00897             else
00898                 drone->attitude_control(0x40, 0, 0, 0, 0);
00899             usleep(20000);
00900         }
00901         sleep(1);
00902 
00903         for(int i = 0; i < 200; i ++)
00904         {
00905             if(i < 180)
00906                 drone->attitude_control(0x40, 2, 0, 0, 0);
00907             else
00908                 drone->attitude_control(0x40, 0, 0, 0, 0);
00909             usleep(20000);
00910         }
00911         sleep(1);
00912 
00913         for(int i = 0; i < 200; i ++)
00914         {
00915             if(i < 180)
00916                 drone->attitude_control(0x40, -2, 0, 0, 0);
00917             else
00918                 drone->attitude_control(0x40, 0, 0, 0, 0);
00919             usleep(20000);
00920         }
00921         sleep(1);
00922 
00923         for(int i = 0; i < 200; i ++)
00924         {
00925             if(i < 180)
00926                 drone->attitude_control(0x40, 0, 2, 0, 0);
00927             else
00928                 drone->attitude_control(0x40, 0, 0, 0, 0);
00929             usleep(20000);
00930         }
00931         sleep(1);
00932 
00933         for(int i = 0; i < 200; i ++)
00934         {
00935             if(i < 180)
00936                 drone->attitude_control(0x40, 0, -2, 0, 0);
00937             else
00938                 drone->attitude_control(0x40, 0, 0, 0, 0);
00939             usleep(20000);
00940         }
00941         sleep(1);
00942 
00943         for(int i = 0; i < 200; i ++)
00944         {
00945             if(i < 180)
00946                 drone->attitude_control(0x40, 0, 0, 0.5, 0);
00947             else
00948                 drone->attitude_control(0x40, 0, 0, 0, 0);
00949             usleep(20000);
00950         }
00951         sleep(1);
00952 
00953         for(int i = 0; i < 200; i ++)
00954         {
00955             if(i < 180)
00956                 drone->attitude_control(0x40, 0, 0, -0.5, 0);
00957             else
00958                 drone->attitude_control(0x40, 0, 0, 0, 0);
00959             usleep(20000);
00960         }
00961         sleep(1);
00962 
00963         for(int i = 0; i < 200; i ++)
00964         {
00965             if(i < 180)
00966                 drone->attitude_control(0xA, 0, 0, 0, 90);
00967             else
00968                 drone->attitude_control(0xA, 0, 0, 0, 0);
00969             usleep(20000);
00970         }
00971         sleep(1);
00972 
00973         for(int i = 0; i < 200; i ++)
00974         {
00975             if(i < 180)
00976                 drone->attitude_control(0xA, 0, 0, 0, -90);
00977             else
00978                 drone->attitude_control(0xA, 0, 0, 0, 0);
00979             usleep(20000);
00980         }
00981         sleep(1);
00982 
00983         drone->landing();
00984 
00985     }
00986     void LocalNavigationTestMobileCallback(DJIDrone *drone)
00987     {
00988 
00989     }
00990     void GlobalNavigationTestMobileCallback(DJIDrone *drone)
00991     {
00992 
00993     }
00994     void WaypointNavigationTestMobileCallback(DJIDrone *drone)
00995     {
00996         
00997     }
00998     void VirtuaRCTestMobileCallback(DJIDrone *drone)
00999     {
01000         //virtual RC test data
01001         uint32_t virtual_rc_data[16];
01002         //virtual rc test 1: arm & disarm
01003         drone->virtual_rc_enable();
01004         usleep(20000);
01005 
01006         virtual_rc_data[0] = 1024;  //0-> roll      [1024-660,1024+660] 
01007         virtual_rc_data[1] = 1024;  //1-> pitch     [1024-660,1024+660]
01008         virtual_rc_data[2] = 1024+660;  //2-> throttle  [1024-660,1024+660]
01009         virtual_rc_data[3] = 1024;  //3-> yaw       [1024-660,1024+660]
01010         virtual_rc_data[4] = 1684;      //4-> gear      {1684(UP), 1324(DOWN)}
01011         virtual_rc_data[6] = 1552;      //6-> mode      {1552(P), 1024(A), 496(F)}
01012 
01013         for (int i = 0; i < 100; i++){
01014             drone->virtual_rc_control(virtual_rc_data);
01015             usleep(20000);
01016         }
01017 
01018         //virtual rc test 2: yaw 
01019         drone->virtual_rc_enable();
01020         virtual_rc_data[0] = 1024;      //0-> roll      [1024-660,1024+660] 
01021         virtual_rc_data[1] = 1024;      //1-> pitch     [1024-660,1024+660]
01022         virtual_rc_data[2] = 1024-200;  //2-> throttle  [1024-660,1024+660]
01023         virtual_rc_data[3] = 1024;      //3-> yaw       [1024-660,1024+660]
01024         virtual_rc_data[4] = 1324;      //4-> gear      {1684(UP), 1324(DOWN)}
01025         virtual_rc_data[6] = 1552;      //6-> mode      {1552(P), 1024(A), 496(F)}
01026 
01027         for(int i = 0; i < 100; i++) {
01028             drone->virtual_rc_control(virtual_rc_data);
01029             usleep(20000);
01030         }
01031         drone->virtual_rc_disable();
01032     }
01033 
01034 void StartMapLASLoggingMobileCallback(DJIDrone *drone)
01035 {
01036   system("roslaunch point_cloud_las start_velodyne_and_loam.launch &");
01037   system("rosrun point_cloud_las write _topic:=/laser_cloud_surround _folder_path:=. &");
01038 }
01039 
01040 void StopMapLASLoggingMobileCallback(DJIDrone *drone)
01041 {
01042   system("rosnode kill /write_LAS /scanRegistration /laserMapping /transformMaintenance /laserOdometry  &");
01043 }
01044 
01045 void StartCollisionAvoidanceCallback(DJIDrone *drone)
01046 { 
01047   uint8_t freq[16];
01048   freq[0] = 1;    // 0 - Timestamp
01049   freq[1] = 4;    // 1 - Attitude Quaterniouns
01050   freq[2] = 1;    // 2 - Acceleration
01051   freq[3] = 4;    // 3 - Velocity (Ground Frame)
01052   freq[4] = 4;    // 4 - Angular Velocity (Body Frame)
01053   freq[5] = 3;    // 5 - Position
01054   freq[6] = 0;    // 6 - Magnetometer
01055   freq[7] = 3;    // 7 - M100:RC Channels Data, A3:RTK Detailed Information
01056   freq[8] = 0;    // 8 - M100:Gimbal Data, A3: Magnetometer
01057   freq[9] = 3;    // 9 - M100:Flight Status, A3: RC Channels
01058   freq[10] = 0;   // 10 - M100:Battery Level, A3: Gimble Data
01059   freq[11] = 2;   // 11 - M100:Control Information, A3: Flight Status
01060 
01061   drone->set_message_frequency(freq);
01062   usleep(1e4);
01063   system("roslaunch dji_collision_avoidance from_DJI_ros_demo.launch &");
01064 }
01065 
01066 void StopCollisionAvoidanceCallback(DJIDrone *drone)
01067 {
01068   drone->release_sdk_permission_control();
01069   system("rosnode kill /drone_tf_builder /dji_occupancy_grid_node /dji_collision_detection_node /collision_velodyne_nodelet_manager /manual_fly");
01070   usleep(1e4);
01071   drone->request_sdk_permission_control();
01072 }


dji_sdk_demo
Author(s):
autogenerated on Thu Jun 6 2019 17:55:32