sweep_amtec.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009
00003  * Robert Bosch LLC
00004  * Research and Technology Center North America
00005  * Palo Alto, California
00006  */
00007 
00008 #include <boost/thread/mutex.hpp>
00009 
00010 #include "ros/ros.h"
00011 
00012 // messages
00013 #include <amtec/AmtecState.h>
00014 
00015 // services
00016 #include <amtec/GetStatus.h>
00017 #include <amtec/Halt.h>
00018 #include <amtec/Home.h>
00019 #include <amtec/Reset.h>
00020 #include <amtec/SetPosition.h>
00021 #include <amtec/SetVelocity.h>
00022 #include <amtec/TargetAcceleration.h>
00023 #include <amtec/TargetVelocity.h>
00024 
00025 #define PT_AT_POINT_DIST        0.15
00026 
00027 class SweepAmtec
00028 {
00029 public:
00030   ros::NodeHandle node_;
00031 
00032   // subscribers
00033   ros::Subscriber amtec_pan_state_sub_;
00034   ros::Subscriber amtec_tilt_state_sub_;
00035 
00036   // amtec state
00037   amtec::AmtecState amtec_pan_state_;
00038   amtec::AmtecState amtec_tilt_state_;
00039   boost::mutex amtec_state_mutex_;
00040 
00041   double pan_start_;
00042   double pan_stop_;
00043   double pan_accel_;
00044   double pan_vel_;
00045 
00046   SweepAmtec()
00047   {
00048     amtec_pan_state_sub_ = node_.subscribe("/amtec/pan_state", 10, &SweepAmtec::amtecPanState, this);
00049     amtec_tilt_state_sub_ = node_.subscribe("/amtec/tilt_state", 10, &SweepAmtec::amtecTiltState, this);
00050 
00051     ros::NodeHandle private_ns("~");
00052     private_ns.param("pan_start", pan_start_, -0.6);
00053     private_ns.param("pan_stop", pan_stop_,  0.6);
00054     private_ns.param("pan_acceleration", pan_accel_, 4.0);
00055     private_ns.param("pan_velocity", pan_vel_, 2.5);
00056   }
00057 
00058   virtual ~SweepAmtec()
00059   {
00060 
00061   }
00062 
00063   void amtecPanState(const amtec::AmtecStateConstPtr& in)
00064   {
00065     amtec_state_mutex_.lock();
00066     amtec_pan_state_ = *in;
00067     amtec_state_mutex_.unlock();
00068   }
00069 
00070   void amtecTiltState(const amtec::AmtecStateConstPtr& in)
00071   {
00072     amtec_state_mutex_.lock();
00073     amtec_tilt_state_ = *in;
00074     amtec_state_mutex_.unlock();
00075   }
00076 
00077   bool amtecSetTargetVelocity(double velocity_pan, double velocity_tilt)
00078   {
00079     ros::NodeHandle n;
00080     ros::ServiceClient client = n.serviceClient<amtec::TargetVelocity>("/amtec/target_velocity");
00081     amtec::TargetVelocity srv;
00082     srv.request.velocity_pan = velocity_pan;
00083     srv.request.velocity_tilt = velocity_tilt;
00084     return client.call(srv);
00085   }
00086 
00087   bool amtecSetTargetAcceleration(double acceleration_pan, double acceleration_tilt)
00088   {
00089     ros::NodeHandle n;
00090     ros::ServiceClient client = n.serviceClient<amtec::TargetAcceleration>("/amtec/target_acceleration");
00091     amtec::TargetAcceleration srv;
00092     srv.request.acceleration_pan = acceleration_pan;
00093     srv.request.acceleration_tilt = acceleration_tilt;
00094     return client.call(srv);
00095   }
00096 
00097   bool amtecSetPosition(double pan, double tilt)
00098   {
00099     ros::NodeHandle n;
00100     ros::ServiceClient client = n.serviceClient<amtec::SetPosition>("/amtec/set_position");
00101     amtec::SetPosition srv;
00102 
00103     while (pan > 190.0)
00104       pan -= 360.0;
00105     while (pan < -190.0)
00106       pan += 360.0;
00107 
00108     srv.request.position_pan = pan;
00109     srv.request.position_tilt = tilt;
00110     return client.call(srv);
00111   }
00112 
00113   bool sweep()
00114   {
00115     ROS_INFO("set target velocity %f %f", pan_vel_, 0.1);
00116     amtecSetTargetVelocity(pan_vel_, 0.1);
00117 
00118 
00119     amtecSetTargetAcceleration(pan_accel_, 0.5);
00120     amtecSetTargetVelocity(pan_vel_, 0.1);
00121     int scan_state = 0;
00122     double pan_target = pan_start_;
00123     amtecSetPosition(pan_target, 0.0);
00124     ROS_INFO("sweeping to start....");
00125 
00126     ros::Rate r(100);
00127     while(node_.ok())
00128     {
00129       amtec_state_mutex_.lock();
00130       double current_pan = amtec_pan_state_.position;
00131       amtec_state_mutex_.unlock();
00132 
00133       double pan_target_wrapped = pan_target;
00134       while (pan_target_wrapped > M_PI)
00135         pan_target_wrapped -= 2*M_PI;
00136       while (pan_target_wrapped < -M_PI)
00137         pan_target_wrapped += 2*M_PI;
00138 
00139       double pan_dist = fabs(pan_target_wrapped - current_pan);
00140       //fprintf(stderr, "p=%f t=%f d=%f (pd=%f td=%f)\n", current_pan, current_tilt, dist, pan_dist, tilt_dist);
00141       switch (scan_state) {
00142       case 0: // moving to left position
00143       {
00144         if (pan_dist < PT_AT_POINT_DIST) {
00145           scan_state = 1;
00146           amtecSetTargetVelocity(pan_vel_, 0.1);
00147           pan_target = pan_stop_;
00148           amtecSetPosition(pan_target, 0.0);
00149           ROS_DEBUG("sweeping to stop....");
00150         }
00151         break;
00152       }
00153       case 1: // moving to left position
00154       {
00155         if (pan_dist < PT_AT_POINT_DIST) {
00156           scan_state = 0;
00157           amtecSetTargetVelocity(pan_vel_, 0.1);
00158           pan_target = pan_start_;
00159           amtecSetPosition(pan_target, 0.0);
00160           ROS_DEBUG("sweeping to start....");
00161         }
00162         break;
00163       }
00164       }
00165 
00166       // spin once to allow callbacks
00167       ros::spinOnce();
00168       r.sleep();
00169     }
00170 
00171     return true;
00172   }
00173 };
00174 
00175 int main(int argc, char **argv)
00176 {
00177   ros::init(argc, argv, "sweep");
00178   SweepAmtec s;
00179   s.sweep();
00180   return 0;
00181 }


amtec
Author(s): Charles DuHadway, Benjamin Pitzer (Maintained by Benjamin Pitzer)
autogenerated on Mon Oct 6 2014 10:10:36