teleop_twist.cpp
Go to the documentation of this file.
1 
27 #include "ardrone_autonomy/LedAnim.h"
28 #include "utils/ardrone_date.h"
29 
30 #include <algorithm>
31 
32 bool needs_takeoff = false;
33 bool needs_land = false;
34 bool needs_reset = false;
35 geometry_msgs::Twist cmd_vel;
36 float old_left_right = -10.0;
37 float old_front_back = -10.0;
38 float old_up_down = -10.0;
39 float old_turn = -10.0;
40 
41 int cam_state = DEFAULT_CAM_STATE; // 0 for forward and 1 for vertical, change to enum later
43 int32_t detect_enemy_color = ARDRONE_DETECTION_COLOR_ORANGE_YELLOW;
44 int32_t detect_dtype = CAD_TYPE_MULTIPLE_DETECTION_MODE;
45 int32_t detect_hori_type = TAG_TYPE_MASK(TAG_TYPE_SHELL_TAG_V2);
46 int32_t detect_vert_type = TAG_TYPE_MASK(TAG_TYPE_BLACK_ROUNDEL);
47 int32_t detect_indoor_hull = 0;
50 
51 const LED_ANIMATION_IDS ledAnimMap[14] =
52 {
53  BLINK_GREEN_RED, BLINK_GREEN, BLINK_RED, BLINK_ORANGE,
54  SNAKE_GREEN_RED, FIRE, STANDARD, RED, GREEN, RED_SNAKE, BLANK,
55  LEFT_GREEN_RIGHT_RED, LEFT_RED_RIGHT_GREEN, BLINK_STANDARD
56 };
57 
58 // ros service callback to set the camera channel
59 bool SetCamChannelCallback(ardrone_autonomy::CamSelect::Request& request,
60  ardrone_autonomy::CamSelect::Response& response)
61 {
62  const int _modes = (IS_ARDRONE1) ? 4 : 2;
63  cam_state = request.channel % _modes;
64  ARDRONE_TOOL_CONFIGURATION_ADDEVENT(video_channel, &cam_state, NULL);
65  fprintf(stderr, "\nSetting camera channel to : %d.\n", cam_state);
66  response.result = true;
67  return true;
68 }
69 // ros service callback function for toggling Cam
70 bool ToggleCamCallback(std_srvs::Empty::Request& request,
71  std_srvs::Empty::Response& response)
72 {
73  const int _modes = (IS_ARDRONE1) ? 4 : 2;
74  cam_state = (cam_state + 1) % _modes;
75  ARDRONE_TOOL_CONFIGURATION_ADDEVENT(video_channel, &cam_state, NULL);
76  fprintf(stderr, "\nSetting camera channel to : %d.\n", cam_state);
77  return true;
78 }
79 
80 // ros service callback to turn on and off camera recording
81 bool SetRecordCallback(ardrone_autonomy::RecordEnable::Request &request,
82  ardrone_autonomy::RecordEnable::Response& response)
83 {
84  char record_command[ARDRONE_DATE_MAXSIZE + 64];
85  int32_t new_codec;
86 
87  if (request.enable == true)
88  {
89  char date[ARDRONE_DATE_MAXSIZE];
90  time_t t = time(NULL);
91  // For some reason the linker can't find this, so we'll just do it manually, cutting and pasting
92  // ardrone_time2date(t, ARDRONE_FILE_DATE_FORMAT, date);
93  strftime(date, ARDRONE_DATE_MAXSIZE, ARDRONE_FILE_DATE_FORMAT, localtime(&t));
94  snprintf(record_command, sizeof(record_command), "%d,%s", USERBOX_CMD_START, date);
95  new_codec = MP4_360P_H264_720P_CODEC;
96  }
97  else
98  {
99  snprintf(record_command, sizeof(record_command), "%d", USERBOX_CMD_STOP);
100  new_codec = H264_360P_CODEC;
101  }
102 
103  vp_os_mutex_lock(&twist_lock);
104  ARDRONE_TOOL_CONFIGURATION_ADDEVENT(video_codec, &new_codec, NULL);
105  ARDRONE_TOOL_CONFIGURATION_ADDEVENT(userbox_cmd, record_command, NULL);
106  vp_os_mutex_unlock(&twist_lock);
107 
108  response.result = true;
109  return true;
110 }
111 
112 bool SetLedAnimationCallback(ardrone_autonomy::LedAnim::Request& request,
113  ardrone_autonomy::LedAnim::Response& response)
114 {
115  LED_ANIMATION_IDS anim_id = ledAnimMap[request.type % 14]; // Don't trick me
116  vp_os_mutex_lock(&twist_lock);
117  ardrone_at_set_led_animation(anim_id,
118  static_cast<float>(fabs(request.freq)),
119  static_cast<uint32_t>(abs(request.duration)));
120  vp_os_mutex_unlock(&twist_lock);
121  response.result = true;
122  return true;
123 }
124 
125 bool SetFlightAnimationCallback(ardrone_autonomy::FlightAnim::Request &request,
126  ardrone_autonomy::FlightAnim::Response &response)
127 {
128  char param[20];
129  const int anim_type = request.type % ARDRONE_NB_ANIM_MAYDAY;
130  const int anim_duration = (request.duration > 0) ? request.duration : MAYDAY_TIMEOUT[anim_type];
131  snprintf(param, sizeof(param), "%d,%d", anim_type, anim_duration);
132  vp_os_mutex_lock(&twist_lock);
133  ARDRONE_TOOL_CONFIGURATION_ADDEVENT(flight_anim, param, NULL);
134  vp_os_mutex_unlock(&twist_lock);
135  response.result = true;
136  return true;
137 }
138 
139 bool FlatTrimCallback(std_srvs::Empty::Request &request,
140  std_srvs::Empty::Response &response)
141 {
142  vp_os_mutex_lock(&twist_lock);
143  ardrone_at_set_flat_trim();
144  vp_os_mutex_unlock(&twist_lock);
145  fprintf(stderr, "\nFlat Trim Set.\n");
146 }
147 
148 void CmdVelCallback(const geometry_msgs::TwistConstPtr &msg)
149 {
150  vp_os_mutex_lock(&twist_lock);
151  // Main 4DOF
152  cmd_vel.linear.x = std::max(std::min(-msg->linear.x, 1.0), -1.0);
153  cmd_vel.linear.y = std::max(std::min(-msg->linear.y, 1.0), -1.0);
154  cmd_vel.linear.z = std::max(std::min(msg->linear.z, 1.0), -1.0);
155  cmd_vel.angular.z = std::max(std::min(-msg->angular.z, 1.0), -1.0);
156  // These 2DOF just change the auto hover behaviour
157  // No bound() required
158  cmd_vel.angular.x = msg->angular.x;
159  cmd_vel.angular.y = msg->angular.y;
160  vp_os_mutex_unlock(&twist_lock);
161 }
162 
163 void LandCallback(const std_msgs::Empty &msg)
164 {
165  vp_os_mutex_lock(&twist_lock);
166  needs_land = true;
167  vp_os_mutex_unlock(&twist_lock);
168 }
169 
170 void ResetCallback(const std_msgs::Empty &msg)
171 {
172  vp_os_mutex_lock(&twist_lock);
173  needs_reset = true;
174  vp_os_mutex_unlock(&twist_lock);
175 }
176 
177 void TakeoffCallback(const std_msgs::Empty &msg)
178 {
179  vp_os_mutex_lock(&twist_lock);
180  needs_takeoff = true;
181  vp_os_mutex_unlock(&twist_lock);
182 }
183 
184 C_RESULT open_teleop(void)
185 {
186  return C_OK;
187 }
188 
189 C_RESULT update_teleop(void)
190 {
191  // This function *toggles* the emergency state, so we only want to toggle the emergency
192  // state when we are in the emergency state (because we want to get out of it).
193  vp_os_mutex_lock(&twist_lock);
194  if (needs_reset)
195  {
196  ardrone_tool_set_ui_pad_select(1);
197  needs_reset = false;
198  }
199  else if (needs_takeoff)
200  {
201  ardrone_tool_set_ui_pad_start(1);
202  needs_takeoff = false;
203  }
204  else if (needs_land)
205  {
206  ardrone_tool_set_ui_pad_start(0);
207  needs_land = false;
208  }
209  else
210  {
211  float left_right = static_cast<float>(cmd_vel.linear.y);
212  float front_back = static_cast<float>(cmd_vel.linear.x);
213  float up_down = static_cast<float>(cmd_vel.linear.z);
214  float turn = static_cast<float>(cmd_vel.angular.z);
215 
216  bool is_changed = !(
217  (fabs(left_right - old_left_right) < _EPS) &&
218  (fabs(front_back - old_front_back) < _EPS) &&
219  (fabs(up_down - old_up_down) < _EPS) &&
220  (fabs(turn - old_turn) < _EPS));
221 
222  // These lines are for testing, they should be moved to configurations
223  // Bit 0 of control_flag == 0: should we hover?
224  // Bit 1 of control_flag == 1: should we use combined yaw mode?
225 
226  int32_t control_flag = 0x00;
227  int32_t combined_yaw = 0x00;
228 
229  // Auto hover detection based on ~0 values for 4DOF cmd_vel
230  int32_t hover = (int32_t)
231  (
232  (fabs(left_right) < _EPS) &&
233  (fabs(front_back) < _EPS) &&
234  (fabs(up_down) < _EPS) &&
235  (fabs(turn) < _EPS) &&
236  // Set angular.x or angular.y to a non-zero value to disable entering hover
237  // even when 4DOF control command is ~0
238  (fabs(cmd_vel.angular.x) < _EPS) &&
239  (fabs(cmd_vel.angular.y) < _EPS));
240 
241  control_flag |= ((1 - hover) << 0);
242  control_flag |= (combined_yaw << 1);
243  // ROS_INFO (">>> Control Flag: %d", control_flag);
244 
245  old_left_right = left_right;
246  old_front_back = front_back;
247  old_up_down = up_down;
248  old_turn = turn;
249  // is_changed = true;
250  if ((is_changed) || (hover))
251  {
252  ardrone_tool_set_progressive_cmd(control_flag, left_right, front_back, up_down, turn, 0.0, 0.0);
253  }
254  }
255  vp_os_mutex_unlock(&twist_lock);
256  return C_OK;
257 }
258 
259 C_RESULT close_teleop(void)
260 {
261  return C_OK;
262 }
263 
264 input_device_t teleop =
265 {
266  "Teleop",
267  open_teleop,
270 };
float old_turn
bool ToggleCamCallback(std_srvs::Empty::Request &request, std_srvs::Empty::Response &response)
void ResetCallback(const std_msgs::Empty &msg)
bool param(const std::string &param_name, T &param_val, const T &default_val)
int cam_state
C_RESULT open_teleop(void)
bool SetCamChannelCallback(ardrone_autonomy::CamSelect::Request &request, ardrone_autonomy::CamSelect::Response &response)
bool needs_land
void LandCallback(const std_msgs::Empty &msg)
#define DEFAULT_CAM_STATE
Definition: teleop_twist.h:73
const LED_ANIMATION_IDS ledAnimMap[14]
bool SetFlightAnimationCallback(ardrone_autonomy::FlightAnim::Request &request, ardrone_autonomy::FlightAnim::Response &response)
float old_left_right
C_RESULT close_teleop(void)
int set_navdata_demo_value
float old_up_down
#define DEFAULT_NAVDATA_DEMO
Definition: teleop_twist.h:74
C_RESULT update_teleop(void)
bool FlatTrimCallback(std_srvs::Empty::Request &request, std_srvs::Empty::Response &response)
vp_os_mutex_t twist_lock
Definition: ardrone_sdk.cpp:35
bool SetRecordCallback(ardrone_autonomy::RecordEnable::Request &request, ardrone_autonomy::RecordEnable::Response &response)
int32_t detect_enable_placeholder
ARDRONE_TOOL_CONFIGURATION_ADDEVENT(profile_id, buffer, NULL)
bool SetLedAnimationCallback(ardrone_autonomy::LedAnim::Request &request, ardrone_autonomy::LedAnim::Response &response)
input_device_t teleop
void CmdVelCallback(const geometry_msgs::TwistConstPtr &msg)
int32_t detect_enemy_color
#define _EPS
Definition: teleop_twist.h:37
int32_t detect_disable_placeholder
int32_t detect_indoor_hull
float old_front_back
bool needs_takeoff
int32_t detect_dtype
int32_t detect_vert_type
bool needs_reset
geometry_msgs::Twist cmd_vel
int32_t detect_hori_type
void TakeoffCallback(const std_msgs::Empty &msg)


ardrone_autonomy
Author(s): Mani Monajjemi, Mani Monajjemi
autogenerated on Mon Jun 10 2019 12:39:49