Go to the documentation of this file.00001
00025 #include <ardrone_autonomy/ardrone_sdk.h>
00026 #include <ardrone_autonomy/video.h>
00027 #include <ardrone_autonomy/teleop_twist.h>
00028 #include <stdint.h>
00029
00030 const navdata_unpacked_t* shared_raw_navdata_ptr;
00031 ros::Time shared_navdata_receive_time;
00032
00033 vp_os_mutex_t navdata_lock;
00034 vp_os_mutex_t video_lock;
00035 vp_os_mutex_t twist_lock;
00036
00037 int32_t current_navdata_id = 0;
00038
00039 ARDroneDriver* ros_driver;
00040
00041 int32_t looprate;
00042 bool realtime_navdata;
00043 bool realtime_video;
00044
00045 int32_t should_exit;
00046
00047 extern "C"
00048 {
00049 vp_stages_latency_estimation_config_t vlat;
00050
00051 DEFINE_THREAD_ROUTINE(update_ros, data)
00052 {
00053 PRINT("Thread `update_ros` started \n ");
00054 ARDroneDriver* driver = reinterpret_cast<ARDroneDriver*>(data);
00055 driver->run();
00056 return (THREAD_RET) 0;
00057 }
00058
00059 C_RESULT ardrone_tool_init_custom(void)
00060 {
00061 should_exit = 0;
00062 vp_os_mutex_init(&navdata_lock);
00063 vp_os_mutex_init(&video_lock);
00064 vp_os_mutex_init(&twist_lock);
00065
00066 ros_driver = new ARDroneDriver();
00067 int _w, _h;
00068
00069 if (IS_ARDRONE2)
00070 {
00071 ardrone_application_default_config.video_codec = H264_360P_CODEC;
00072 _w = D2_STREAM_WIDTH;
00073 _h = D2_STREAM_HEIGHT;
00074 }
00075 else if (IS_ARDRONE1)
00076 {
00077 ardrone_application_default_config.video_codec = UVLC_CODEC;
00078 _w = D1_STREAM_WIDTH;
00079 _h = D1_STREAM_HEIGHT;
00080 }
00081 else
00082 {
00083 printf("Something must be really wrong with the SDK!");
00084 }
00085
00086 ros::param::param("~looprate", looprate, 50);
00087 ros::param::param("~realtime_navdata", realtime_navdata, false);
00088 ros::param::param("~realtime_video", realtime_video, false);
00089 if (!realtime_navdata) ROS_WARN("realtime navdata is off, odometry may be imprecise");
00090
00091
00092
00093 ardrone_application_default_config.bitrate_ctrl_mode = VBC_MODE_DISABLED;
00094 if (IS_ARDRONE2)
00095 {
00096 ardrone_application_default_config.max_bitrate = 4000;
00097 }
00098
00099 ardrone_application_default_config.navdata_options = NAVDATA_OPTION_FULL_MASK;
00100 ardrone_application_default_config.video_channel = ZAP_CHANNEL_HORI;
00101 ardrone_application_default_config.control_level = (0 << CONTROL_LEVEL_COMBINED_YAW);
00102 ardrone_application_default_config.flying_mode = FLYING_MODE_FREE_FLIGHT;
00103
00104
00105 #include "ardrone_autonomy/snippet_ros_to_ardrone_config.h"
00106
00107
00108 ardrone_tool_input_add(&teleop);
00109 uint8_t post_stages_index = 0;
00110
00111
00112 specific_parameters_t* params = reinterpret_cast<specific_parameters_t*>(
00113 vp_os_calloc(1, sizeof(specific_parameters_t)));
00114 specific_stages_t* driver_pre_stages = reinterpret_cast<specific_stages_t*>(
00115 vp_os_calloc(1, sizeof(specific_stages_t)));
00116 specific_stages_t* driver_post_stages = reinterpret_cast<specific_stages_t*>(
00117 vp_os_calloc(1, sizeof(specific_stages_t)));
00118 vp_api_picture_t* in_picture = reinterpret_cast<vp_api_picture_t*>(
00119 vp_os_calloc(1, sizeof(vp_api_picture_t)));
00120 vp_api_picture_t* out_picture = reinterpret_cast<vp_api_picture_t*>(
00121 vp_os_calloc(1, sizeof(vp_api_picture_t)));
00122
00123 in_picture->width = _w;
00124 in_picture->height = _h;
00125
00126 out_picture->framerate = 20;
00127 out_picture->format = PIX_FMT_RGB24;
00128 out_picture->width = _w;
00129 out_picture->height = _h;
00130
00131 out_picture->y_buf = reinterpret_cast<uint8_t*>(vp_os_malloc(_w * _h * 3));
00132 out_picture->cr_buf = NULL;
00133 out_picture->cb_buf = NULL;
00134
00135 out_picture->y_line_size = _w * 3;
00136 out_picture->cb_line_size = 0;
00137 out_picture->cr_line_size = 0;
00138
00139
00140 driver_pre_stages->stages_list = NULL;
00141 driver_post_stages->stages_list = reinterpret_cast<vp_api_io_stage_t*>(
00142 vp_os_calloc(NB_DRIVER_POST_STAGES, sizeof(vp_api_io_stage_t)));
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 driver_post_stages->stages_list[post_stages_index].name = "ExtractData";
00154 driver_post_stages->stages_list[post_stages_index].type = VP_API_OUTPUT_SDL;
00155 driver_post_stages->stages_list[post_stages_index].cfg = NULL;
00156 driver_post_stages->stages_list[post_stages_index++].funcs = vp_stages_export_funcs;
00157
00158 driver_pre_stages->length = 0;
00159 driver_post_stages->length = post_stages_index;
00160
00161 params->in_pic = in_picture;
00162 params->out_pic = out_picture;
00163 params->pre_processing_stages_list = driver_pre_stages;
00164 params->post_processing_stages_list = driver_post_stages;
00165 params->needSetPriority = 1;
00166 params->priority = 31;
00167
00168 START_THREAD(video_stage, params);
00169 video_stage_init();
00170 if (ARDRONE_VERSION() >= 2)
00171 {
00172 START_THREAD(video_recorder, NULL);
00173 video_recorder_init();
00174 video_recorder_resume_thread();
00175 }
00176
00177 video_stage_resume_thread();
00178 ardrone_tool_set_refresh_time(25);
00179
00180 START_THREAD(update_ros, ros_driver);
00181 return C_OK;
00182 }
00183
00184 C_RESULT ardrone_tool_shutdown_custom()
00185 {
00186 PRINT("Shutting down ... \n ");
00187 JOIN_THREAD(update_ros);
00188 delete ros_driver;
00189 video_stage_resume_thread();
00190 ardrone_tool_input_remove(&teleop);
00191 return C_OK;
00192 }
00193
00194 C_RESULT navdata_custom_init(void* data)
00195 {
00196 (void) data;
00197 return C_OK;
00198 }
00199
00200 C_RESULT navdata_custom_process(const navdata_unpacked_t * const pnd)
00201 {
00202 vp_os_mutex_lock(&navdata_lock);
00203 shared_navdata_receive_time = ros::Time::now();
00204 shared_raw_navdata_ptr = reinterpret_cast<const navdata_unpacked_t*>(pnd);
00205
00206 if (realtime_navdata)
00207 {
00208
00209 const navdata_unpacked_t shared_raw_navdata = *shared_raw_navdata_ptr;
00210 ros_driver->PublishNavdataTypes(shared_raw_navdata, shared_navdata_receive_time);
00211 ros_driver->PublishNavdata(shared_raw_navdata, shared_navdata_receive_time);
00212 ros_driver->PublishOdometry(shared_raw_navdata, shared_navdata_receive_time);
00213 }
00214
00215 current_navdata_id++;
00216 vp_os_mutex_unlock(&navdata_lock);
00217 return C_OK;
00218 }
00219
00220 C_RESULT navdata_custom_release()
00221 {
00222 return C_OK;
00223 }
00224
00225 bool_t ardrone_tool_exit()
00226 {
00227 return (should_exit == 1);
00228 }
00229
00230 BEGIN_THREAD_TABLE
00231 THREAD_TABLE_ENTRY(video_stage, 31)
00232 THREAD_TABLE_ENTRY(update_ros, 43)
00233 THREAD_TABLE_ENTRY(video_recorder, 20)
00234 THREAD_TABLE_ENTRY(navdata_update, 31)
00235
00236 THREAD_TABLE_ENTRY(ardrone_control, 31)
00237 END_THREAD_TABLE
00238
00239 BEGIN_NAVDATA_HANDLER_TABLE
00240 NAVDATA_HANDLER_TABLE_ENTRY(
00241 navdata_custom_init,
00242 navdata_custom_process,
00243 navdata_custom_release,
00244 NULL)
00245 END_NAVDATA_HANDLER_TABLE
00246 }