00001 #include "driver/PTUDriverMock.h"
00002 #include "ros/ros.h"
00003
00004 #define STEPS_UNTIL_GOAL_REACHED 20
00005
00006 namespace asr_flir_ptu_driver {
00007 PTUDriverMock::PTUDriverMock(const char* port, int baud, bool speedControl):
00008 PTUDriver::PTUDriver()
00009 {
00010 ROS_DEBUG("port: %s, baud: %d, speeControl: %d", port, baud, speedControl);
00011 is_stopped_count = 0;
00012 pan_angle = 0;
00013 tilt_angle = 0;
00014 pan_speed = 0;
00015 tilt_speed = 0;
00016 setLimitAnglesToHardwareConstraints();
00017 }
00018
00019 bool PTUDriverMock::isConnected()
00020 {
00021 return true;
00022 }
00023
00024 void PTUDriverMock::setSettings(int pan_base, int tilt_base,
00025 int pan_speed, int tilt_speed,
00026 int pan_upper, int tilt_upper,
00027 int pan_accel, int tilt_accel,
00028 int pan_hold, int tilt_hold,
00029 int pan_move, int tilt_move)
00030 {
00031 this->pan_speed = pan_speed;
00032 this->tilt_speed = tilt_speed;
00033 this->pan_base = pan_base;
00034 this->tilt_base = tilt_base;
00035 this->pan_acceleration = pan_accel;
00036 this->tilt_acceleration = tilt_accel;
00037 ROS_DEBUG("setSettings");
00038
00039
00040 }
00041 void PTUDriverMock::setLimitAngles(double pan_min, double pan_max, double tilt_min, double tilt_max) {
00042 ROS_DEBUG("setLimitAngles");
00043 ROS_DEBUG("double panMin:%f, double panMax:%f", pan_min, pan_max);
00044 ROS_DEBUG("double tiltMin:%f, double tiltMax:%f", tilt_min, tilt_max);
00045 this->pan_min_limit = pan_min;
00046 this->pan_max_limit = pan_max;
00047 this->tilt_min_limit = tilt_min;
00048 this->tilt_max_limit = tilt_max;
00049 }
00050
00051 void PTUDriverMock::setAbsoluteAngleSpeeds(double pan_speed, double tilt_speed) {
00052 ROS_DEBUG("setAbsoluteAngleSpeeds");
00053 ROS_DEBUG("double pan_speed:%f, double tilt_speed:%f", pan_speed, tilt_speed);
00054 this->pan_speed = pan_speed;
00055 this->tilt_speed = tilt_speed;
00056
00057 }
00058
00059 void PTUDriverMock::setAbsoluteAngleSpeeds(signed short pan_speed, signed short tilt_speed) {
00060 ROS_ERROR("This mock function should not be used");
00061 }
00062
00063 bool PTUDriverMock::setAbsoluteAngles(double pan_angle, double tilt_angle, bool no_forbidden_area_check) {
00064 if (isInForbiddenArea(pan_angle, tilt_angle)) {
00065 ROS_ERROR("PAN and TILT lie within forbidden area");
00066 return false;
00067 }
00068 if (!isWithinPanTiltLimits(pan_angle, tilt_angle)){
00069 ROS_ERROR("PAN/TILT out of pan/tilt bounds");
00070 return false;
00071 }
00072 ROS_DEBUG("setAbsoluteAngles");
00073 ROS_DEBUG("double pan_angle:%f, double tilt_angle:%f", pan_angle, tilt_angle);
00074 this->pan_distance = pan_angle - this->pan_angle;
00075 this->tilt_distance = tilt_angle - this->tilt_angle;
00076 this->pan_angle = pan_angle;
00077 this->tilt_angle = tilt_angle;
00078 is_stopped_count++;
00079
00080 ROS_INFO("Successfull set Pan and Tilt. PAN: %f, TILT: %f\n", pan_angle, tilt_angle);
00081 return true;
00082 }
00083 void PTUDriverMock::setSpeedControlMode(bool speed_control_mode) {
00084 ROS_DEBUG("setSpeedControlMode");
00085 }
00086 bool PTUDriverMock::isInSpeedControlMode() {
00087 return false;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 bool PTUDriverMock::isWithinPanTiltLimits(double pan, double tilt){
00102 if ((pan < pan_min_limit)
00103 || (pan > pan_max_limit)
00104 || (tilt < tilt_min_limit)
00105 || (tilt > tilt_max_limit)){
00106 return false;
00107 }
00108 else {
00109 return true;
00110 }
00111 }
00112
00113 bool PTUDriverMock::hasHalted() {
00114 if(is_stopped_count != 0) {
00115 is_stopped_count++;
00116 }
00117 if(is_stopped_count % STEPS_UNTIL_GOAL_REACHED == 0) {
00118 is_stopped_count = 0;
00119 return true;
00120 }
00121 else {
00122 return false;
00123 }
00124 }
00125
00126 bool PTUDriverMock::reachedGoal() {
00127 if(is_stopped_count % STEPS_UNTIL_GOAL_REACHED == 0) {
00128 return true;
00129 }
00130 else {
00131 return false;
00132 }
00133 }
00134
00135 bool PTUDriverMock::hasHaltedAndReachedGoal() {
00136 return hasHalted() && reachedGoal();
00137 }
00138
00139
00140 double PTUDriverMock::getCurrentAngle(char type) {
00141 if (type == PAN) {
00142 return pan_angle - (((STEPS_UNTIL_GOAL_REACHED - is_stopped_count) % STEPS_UNTIL_GOAL_REACHED)/STEPS_UNTIL_GOAL_REACHED * pan_distance);
00143 }
00144 return tilt_angle - (((STEPS_UNTIL_GOAL_REACHED - is_stopped_count) % STEPS_UNTIL_GOAL_REACHED)/STEPS_UNTIL_GOAL_REACHED * tilt_distance);
00145 }
00146
00147 double PTUDriverMock::getDesiredAngle(char type) {
00148 if (type == PAN) {
00149 return pan_angle;
00150 }
00151 return tilt_angle;
00152 }
00153
00154
00155
00156 double PTUDriverMock::getAngleSpeed(char type) {
00157 if (type == PAN) {
00158 if(is_stopped_count % STEPS_UNTIL_GOAL_REACHED == 0) {
00159 return 0.0;
00160 }
00161 else {
00162 return pan_speed;
00163 }
00164 }
00165 if(is_stopped_count % STEPS_UNTIL_GOAL_REACHED == 0) {
00166 return 0.0;
00167 }
00168 else {
00169 return tilt_speed;
00170 }
00171 }
00172
00173
00174
00175
00176 void PTUDriverMock::setLimitAnglesToHardwareConstraints() {
00177 ros::NodeHandle n("~");
00178 double pan_min_limit, pan_max_limit, tilt_min_limit, tilt_max_limit;
00179 n.getParam("mock_pan_min_hardware_limit", pan_min_limit);
00180 n.getParam("mock_pan_max_hardware_limit", pan_max_limit);
00181 n.getParam("mock_tilt_min_hardware_limit", tilt_min_limit);
00182 n.getParam("mock_tilt_max_hardware_limit", tilt_max_limit);
00183
00184 this->pan_min_limit = pan_min_limit;
00185 this->pan_max_limit = pan_max_limit;
00186 this->tilt_min_limit = tilt_min_limit;
00187 this->tilt_max_limit = tilt_max_limit;
00188
00189 ROS_DEBUG("pan_min_limit: %f, pan_max_limit: %f, tilt_min_limit: %f, tilt_max_limit: %f", pan_min_limit, pan_max_limit, tilt_min_limit, tilt_max_limit);
00190
00191 }
00192
00193
00194 std::vector<double> PTUDriverMock::determineLegitEndPoint(double end_point_pan_candidate, double end_point_tilt_candidate) {
00195 ROS_ERROR("DO NOT USE determineLegitEndPoint WITH PTU DRIVER MOCK");
00196 ROS_ERROR("Maybe you enabled path_prediction in one of the launch files you use. Path prediction is not intended to be used with the mock launch files");
00197 std::vector<double> dummy;
00198 dummy.push_back(end_point_pan_candidate);
00199 dummy.push_back(end_point_tilt_candidate);
00200 return dummy;
00201 }
00202
00203 bool PTUDriverMock::setValuesOutOfLimitsButWithinMarginToLimit(double * pan, double * tilt, double margin) {
00204 ROS_DEBUG("PAN: %f, TILT: %f\n", *pan, *tilt);
00205 if(((pan_max_limit - pan_min_limit) <= margin) || ((tilt_max_limit - tilt_min_limit) <= margin)) {
00206 ROS_ERROR("Margin Degree: %f is too big. Bigger than distance between TILT max and TILT min (%f Degree) or PAN max and PAN min (%f Degree)",margin, (tilt_max_limit - tilt_min_limit), (pan_max_limit - pan_min_limit));
00207 return false;
00208 }
00209 double pan_initial = *pan;
00210 double tilt_initial = *tilt;
00211 if((pan_initial < pan_min_limit) && (pan_initial >= (pan_min_limit - margin))) {
00212 *pan = pan_min_limit;
00213 ROS_WARN("PAN was out of limits, but within margin, so instead of the initial value %f the value %f is used", pan_initial, *pan);
00214 }
00215 else if ((pan_initial > pan_max_limit) && (pan_initial <= (pan_max_limit + margin))) {
00216 *pan = pan_max_limit;
00217 ROS_WARN("PAN was out of limits, but within margin, so instead of the initial value %f the value %f is used", pan_initial, *pan);
00218 }
00219 if((tilt_initial < tilt_min_limit) && (tilt_initial >= (tilt_min_limit - margin))) {
00220 *tilt = tilt_min_limit;
00221 ROS_WARN("TILT was out of limits, but within margin, so instead of the initial value %f the value %f is used", tilt_initial, *tilt);
00222 }
00223 else if((tilt_initial > tilt_max_limit) && (tilt_initial <= (tilt_max_limit + margin))) {
00224 *tilt = tilt_max_limit;
00225 ROS_WARN("TILT was out of limits, but within margin, so instead of the initial value %f the value %f is used", tilt_initial, *tilt);
00226 }
00227
00228 if(isWithinPanTiltLimits(*pan, *tilt)) {
00229 ROS_DEBUG("PAN: %f, TILT: %f\n", *pan, *tilt);
00230 return true;
00231 }
00232 else {
00233 return false;
00234 }
00235 }
00236
00237
00238
00239 long PTUDriverMock::getLimitAngle(char pan_or_tilt, char upper_or_lower) {
00240 if(pan_or_tilt == 'p') {
00241 if (upper_or_lower == 'l') {
00242 return pan_min_limit;
00243 }
00244 else if (upper_or_lower == 'u') {
00245 return pan_max_limit;
00246 }
00247 else {
00248 return std::numeric_limits<double>::max();
00249 }
00250 }
00251 else if (pan_or_tilt == 't') {
00252 if (upper_or_lower == 'l') {
00253 return tilt_min_limit;
00254 }
00255 else if (upper_or_lower == 'u') {
00256 return tilt_max_limit;
00257 }
00258 else {
00259 return std::numeric_limits<double>::max();
00260 }
00261 }
00262 else {
00263 return std::numeric_limits<double>::max();
00264 }
00265 }
00266 }