31 #include <boost/bind.hpp> 32 #include <boost/thread/locks.hpp> 33 #include <boost/make_shared.hpp> 37 #include "libARCommands/ARCommands.h" 38 #include "libARDiscovery/ARDiscovery.h" 39 #include <libARController/ARController.h> 43 #include "bebop_driver/BebopArdrone3Config.h" 91 Bebop* bebop_ptr_ =
static_cast<Bebop*
>(bebop_void_ptr);
95 case ARCONTROLLER_DEVICE_STATE_STOPPED:
98 case ARCONTROLLER_DEVICE_STATE_RUNNING:
105 ARCONTROLLER_DICTIONARY_ELEMENT_t *element_dict_ptr,
106 void *bebop_void_ptr)
109 static bool lwp_id_printed =
false;
113 ARSAL_PRINT(ARSAL_PRINT_INFO,
LOG_TAG,
"Command Received Callback LWP id is: %ld", lwp_id);
114 lwp_id_printed =
true;
116 Bebop* bebop_ptr =
static_cast<Bebop*
>(bebop_void_ptr);
119 ARCONTROLLER_DICTIONARY_ELEMENT_t *single_element_ptr = NULL;
121 if (element_dict_ptr)
124 HASH_FIND_STR(element_dict_ptr, ARCONTROLLER_DICTIONARY_SINGLE_KEY, single_element_ptr);
126 if (single_element_ptr)
128 callback_map_t::iterator it = bebop_ptr->
callback_map_.find(cmd_key);
132 it->second->Update(element_dict_ptr->arguments,
ros::Time::now());
141 Bebop* bebop_ptr =
static_cast<Bebop*
>(bebop_void_ptr);
142 if (codec.type == ARCONTROLLER_STREAM_CODEC_TYPE_H264)
144 ARSAL_PRINT(ARSAL_PRINT_INFO,
LOG_TAG,
"H264 configuration packet received: #SPS: %d #PPS: %d (MP4? %d)",
145 codec.parameters.h264parameters.spsSize,
146 codec.parameters.h264parameters.ppsSize,
147 codec.parameters.h264parameters.isMP4Compliant);
150 codec.parameters.h264parameters.spsBuffer,
151 codec.parameters.h264parameters.spsSize,
152 codec.parameters.h264parameters.ppsBuffer,
153 codec.parameters.h264parameters.ppsSize))
155 return ARCONTROLLER_ERROR;
160 ARSAL_PRINT(ARSAL_PRINT_WARNING,
LOG_TAG,
"Codec type is not H264");
161 return ARCONTROLLER_ERROR;
164 return ARCONTROLLER_OK;
171 static bool lwp_id_printed =
false;
174 ARSAL_PRINT(ARSAL_PRINT_INFO,
LOG_TAG,
"Frame Recv & Decode LWP id: %ld", lwp_id);
175 lwp_id_printed =
true;
180 ARSAL_PRINT(ARSAL_PRINT_WARNING,
LOG_TAG,
"Received frame is NULL");
181 return ARCONTROLLER_ERROR_NO_VIDEO;
184 Bebop* bebop_ptr =
static_cast<Bebop*
>(bebop_void_ptr);
185 if (!bebop_ptr->
IsConnected())
return ARCONTROLLER_ERROR;
191 ARSAL_PRINT(ARSAL_PRINT_WARNING,
LOG_TAG,
"Previous frame might have been missed.");
196 ARSAL_PRINT(ARSAL_PRINT_ERROR,
LOG_TAG,
"Video decode failed");
206 return ARCONTROLLER_OK;
222 if (custom_print_callback)
223 ARSAL_Print_SetCallback(custom_print_callback);
225 ARSAL_PRINT(ARSAL_PRINT_INFO,
LOG_TAG,
"Bebop Cnstr()");
240 if (
is_connected_)
throw std::runtime_error(
"Already inited");
245 eARDISCOVERY_ERROR error_discovery = ARDISCOVERY_OK;
246 device_ptr_ = ARDISCOVERY_Device_New(&error_discovery);
248 if (error_discovery != ARDISCOVERY_OK)
250 throw std::runtime_error(
"Discovery failed: " + std::string(ARDISCOVERY_Error_ToString(error_discovery)));
257 error_discovery = ARDISCOVERY_Device_InitWifi(
device_ptr_,
258 ARDISCOVERY_PRODUCT_ARDRONE,
"Bebop",
261 if (error_discovery != ARDISCOVERY_OK)
263 throw std::runtime_error(
"Discovery failed: " + std::string(ARDISCOVERY_Error_ToString(error_discovery)));
272 #define UPDTAE_CALLBACK_MAP 276 #undef UPDTAE_CALLBACK_MAP 281 ARCONTROLLER_Device_AddStateChangedCallback(
283 "Registering state callback failed");
285 ARCONTROLLER_Device_AddCommandReceivedCallback(
287 "Registering command callback failed");
296 ARCONTROLLER_Device_SetVideoStreamCallbacks(
298 NULL , reinterpret_cast<void*>(
this)),
299 "Registering video callback failed");
307 throw std::runtime_error(
"Waiting for device failed: " + std::string(ARCONTROLLER_Error_ToString(
error_)));
314 catch (
const std::runtime_error& e)
326 ARSAL_PRINT(ARSAL_PRINT_INFO,
LOG_TAG,
"Bebop Cleanup()");
332 ARSAL_PRINT(ARSAL_PRINT_INFO,
LOG_TAG,
"Disconnecting ...");
334 if (
error_ == ARCONTROLLER_OK)
349 ARSAL_PRINT(ARSAL_PRINT_WARNING,
LOG_TAG,
"Video streaming is already started ...");
359 ARSAL_PRINT(ARSAL_PRINT_WARNING,
LOG_TAG,
"Video streaming started ...");
361 catch (
const std::runtime_error& e)
363 ARSAL_PRINT(ARSAL_PRINT_INFO,
LOG_TAG,
"Failed to start video streaming ...");
380 catch (
const std::runtime_error& e)
382 ARSAL_PRINT(ARSAL_PRINT_ERROR,
LOG_TAG,
"Failed to stop video streaming ...");
391 ARSAL_PRINT(ARSAL_PRINT_INFO,
LOG_TAG,
"Disconnected from Bebop ...");
399 "Setting Date Failed");
407 "Request Settings Failed");
415 "Reset Settings Failed");
417 ARSAL_PRINT(ARSAL_PRINT_INFO,
LOG_TAG,
"All settings of the drone have been reset to default values.");
478 "Navigate home failed");
486 "Start autonomous flight failed");
494 "Pause autonomous flight failed");
502 "Stop autonomous flight failed");
508 if (anim_id >= ARCOMMANDS_ARDRONE3_ANIMATIONS_FLIP_DIRECTION_MAX)
510 throw std::runtime_error(
"Inavlid animation id");
515 anim_id % ARCOMMANDS_ARDRONE3_ANIMATIONS_FLIP_DIRECTION_MAX)),
516 "Navigate home failed");
519 void Bebop::Move(
const double &roll,
const double &pitch,
const double &gaz_speed,
const double &yaw_speed)
525 const bool do_rp = !((fabs(roll) < 0.001) && (fabs(pitch) < 0.001));
528 const bool do_hover = !do_rp && (fabs(yaw_speed) < 0.001) && (fabs(gaz_speed) < 0.001);
532 ARSAL_PRINT(ARSAL_PRINT_DEBUG,
LOG_TAG,
"STOP");
544 static_cast<int8_t>(roll * 100.0),
545 static_cast<int8_t>(pitch * 100.0),
546 static_cast<int8_t>(yaw_speed * 100.0),
547 static_cast<int8_t>(gaz_speed * 100.0),
558 static_cast<int8_t>(tilt),
559 static_cast<int8_t>(pan)));
576 ARSAL_PRINT(ARSAL_PRINT_DEBUG,
LOG_TAG,
"Waiting for frame to become available ...");
586 ARSAL_PRINT(ARSAL_PRINT_WARNING,
LOG_TAG,
"No picture size");
589 buffer.resize(num_bytes);
624 start ? ARCOMMANDS_ARDRONE3_MEDIARECORD_VIDEOV2_RECORD_START :
625 ARCOMMANDS_ARDRONE3_MEDIARECORD_VIDEOV2_RECORD_STOP));
632 throw std::runtime_error(message);
638 if (error != ARCONTROLLER_OK)
640 throw std::runtime_error(message + std::string(ARCONTROLLER_Error_ToString(error)));
static void StateChangedCallback(eARCONTROLLER_DEVICE_STATE new_state, eARCONTROLLER_ERROR error, void *bebop_void_ptr)
void MoveCamera(const double &tilt, const double &pan)
void UpdateSettings(const bebop_driver::BebopArdrone3Config &config)
void wait(unique_lock< mutex > &m)
callback_map_t callback_map_
boost::shared_ptr< VideoDecoder > video_decoder_ptr_
void ThrowOnCtrlError(const eARCONTROLLER_ERROR &error, const std::string &message=std::string())
uint32_t GetFrontCameraFrameWidth() const
static eARCONTROLLER_ERROR FrameReceivedCallback(ARCONTROLLER_Frame_t *frame, void *bebop_void_ptr)
boost::atomic< bool > is_connected_
void ThrowOnInternalError(const std::string &message=std::string())
void AnimationFlip(const uint8_t &anim_id)
static const char * LOG_TAG
void RequestAllSettings()
void NavigateHome(const bool &start_stop)
uint32_t GetFrontCameraFrameHeight() const
boost::condition_variable frame_avail_cond_
static void CommandReceivedCallback(eARCONTROLLER_DICTIONARY_KEY cmd_key, ARCONTROLLER_DICTIONARY_ELEMENT_t *element_dict_ptr, void *bebop_void_ptr)
bool GetFrontCameraFrame(std::vector< uint8_t > &buffer, uint32_t &width, uint32_t &height) const
static eARCONTROLLER_ERROR DecoderConfigCallback(ARCONTROLLER_Stream_Codec_t codec, void *bebop_void_ptr)
void StopAutonomousFlight()
ARCONTROLLER_Device_t * device_controller_ptr_
boost::mutex frame_avail_mutex_
void Move(const double &roll, const double &pitch, const double &gaz_speed, const double &yaw_speed)
void PauseAutonomousFlight()
void SetDate(const std::string &date)
eARCONTROLLER_DEVICE_STATE device_state_
void StartAutonomousFlight(const std::string &filepath)
void notify_one() BOOST_NOEXCEPT
ARDISCOVERY_Device_t * device_ptr_
void ToggleVideoRecording(const bool start)
void Connect(ros::NodeHandle &nh, ros::NodeHandle &priv_nh, const std::string &bebop_ip="192.168.42.1")
void SetExposure(const float &exposure)
eARCONTROLLER_ERROR error_
boost::atomic< bool > is_streaming_started_
Bebop(ARSAL_Print_Callback_t custom_print_callback=0)