led_control_server.cpp
Go to the documentation of this file.
00001 /*******************************************************
00002 *                    C Headers                         *
00003 ********************************************************/
00004 #include <unistd.h>
00005 #include <signal.h>
00006 
00007 /*******************************************************
00008 *                    ROS Headers                       *
00009 ********************************************************/
00010 #include <actionlib/server/simple_action_server.h>
00011 
00012 /*******************************************************
00013 *                   segbot_led Headers                 *
00014 ********************************************************/
00015 #include "ledcom.h"
00016 
00017 /*******************************************************
00018 *                   Service Headers                    *
00019 ********************************************************/
00020 #include "bwi_msgs/LEDClear.h"
00021 #include "bwi_msgs/LEDSetStatus.h"
00022 #include "bwi_msgs/LEDTestStrip.h"
00023 
00024 /*******************************************************
00025 *                   Action Headers                     *
00026 ********************************************************/
00027 #include "bwi_msgs/LEDControlAction.h"
00028 
00029 /*******************************************************
00030 *                   Message Headers                    *
00031 ********************************************************/
00032 #include "bwi_msgs/LEDActionResult.h"
00033 #include "bwi_msgs/LEDAnimations.h"
00034 #include "bwi_msgs/LEDStatus.h"
00035 #include "bwi_msgs/LEDTestType.h"
00036 
00037 /*******************************************************
00038 *                 Global Variables                     *
00039 ********************************************************/
00040 LedCOM leds;
00041 int led_count;
00042 string serial_port;
00043 bool run_on = false;
00044 bool camera_on = false;
00045 bool connected = false;
00046 
00047 // LED Segments
00048 int front_center_left_start;
00049 int front_center_left_end;
00050 
00051 int front_center_right_start;
00052 int front_center_right_end;
00053 
00054 int back_center_left_start;
00055 int back_center_left_end;
00056 
00057 int back_center_right_start;
00058 int back_center_right_end;
00059 
00060 int front_left_beam_start;
00061 int front_left_beam_end;
00062 
00063 int front_right_beam_start;
00064 int front_right_beam_end ;
00065 
00066 int back_left_beam_start;
00067 int back_left_beam_end;
00068 
00069 int back_right_beam_start;
00070 int back_right_beam_end;
00071 
00072 int camera_indicator_start;
00073 int camera_indicator_end;
00074 
00075 int back_center_start;
00076 int back_center_end;
00077 
00078 int circular_u_start;
00079 int circular_u_end;
00080 
00081 int top_of_u_start;
00082 int top_of_u_end;
00083 
00084 /*******************************************************
00085 *                                                      *
00086 *                 Helper Functions                     *
00087 *                                                      *
00088 ********************************************************/
00089 void check_run_status()
00090 {
00091   if(run_on)
00092   {
00093     leds.clear();
00094     // Microseconds
00095     usleep(100000);
00096 
00097     for (int i = led_count; i >= 0; i--)
00098     {
00099         leds.setHSV(i, 120, 1, .05);
00100     }
00101 
00102     leds.flush();
00103     // Microseconds
00104     usleep(100000);
00105 
00106     ROS_INFO("Set run indicator on strip");
00107   }
00108 }
00109 
00110 void check_camera_status()
00111 {
00112   if(camera_on)
00113   {
00114     leds.clear();
00115     // Microseconds
00116     usleep(100000);
00117 
00118     for (int i = camera_indicator_start; i <= camera_indicator_end; i++)
00119     {
00120       leds.setHSV(i, 240, 1, .1);
00121     }
00122 
00123     leds.flush();
00124     // Microseconds
00125     usleep(100000);
00126 
00127     ROS_INFO("Set camera indicator on strip");
00128   }
00129 }
00130 
00131 void connect(string port, int baud)
00132 {
00133   while(!connected)
00134   {
00135     try
00136     {
00137         leds.connect(port, baud);
00138         connected = true;
00139         // Need to sleep to wait for connection to take full effect
00140         sleep(3);
00141     }
00142     catch(const serial::IOException &e)
00143     {
00144         ROS_ERROR("EXCEPTION CAUGHT: serial::IOException could not open a connection.");
00145         ROS_ERROR_STREAM("Original exception: " << e.what());
00146         ROS_ERROR_STREAM("Ensure device is connected and using port, " << port << ", with baud setting, " << baud << ".");
00147         ROS_ERROR("Retrying to open connection after waiting 2 seconds.");
00148         sleep(1);
00149     }
00150   }
00151 
00152   try
00153   {
00154     leds.setLEDCount(led_count);
00155     // Ensures strip is clear on initial start up
00156     leds.clear();
00157     sleep(1);
00158   }
00159   catch(const serial::SerialException &e)
00160   {
00161     connected = false;
00162   }
00163 
00164   ROS_INFO("Ready to control LED strip with %d leds.", led_count);
00165 
00166 }
00167 
00168 void ledSigintHandler(int sig)
00169 {
00170   camera_on = false;
00171   ros::shutdown();
00172 }
00173 
00174 /*******************************************************
00175 *                                                      *
00176 *                   Action Server                      *
00177 *                                                      *
00178 ********************************************************/
00179 class LEDControlAction
00180 {
00181 protected:
00182 
00183   ros::NodeHandle nh_;
00184 
00185   actionlib::SimpleActionServer<bwi_msgs::LEDControlAction> as_;
00186   std::string action_name_;
00187 
00188   bwi_msgs::LEDControlFeedback feedback_;
00189   bwi_msgs::LEDControlResult result_;
00190 
00191 public:
00192 
00193   ros::Timer timeout_timer;
00194 
00195   bool timeout = false;
00196   bool success = true;
00197 
00198   void timerCallback(const ros::TimerEvent& event)
00199   {
00200     timeout = true;
00201     success = true;
00202     ROS_INFO("Goal has reached timeout.");
00203   }
00204 
00205   LEDControlAction(std::string name) :
00206     as_(nh_, name, boost::bind(&LEDControlAction::executeCB, this, _1), false),
00207     action_name_(name)
00208   {
00209     timeout_timer = nh_.createTimer(ros::Duration(100), boost::bind(&LEDControlAction::timerCallback, this, _1));
00210     timeout_timer.stop();
00211     as_.start();
00212   }
00213 
00214   ~LEDControlAction(void)
00215   {
00216   }
00217 
00218   void executeCB(const bwi_msgs::LEDControlGoalConstPtr &goal)
00219   {
00220     ROS_INFO_STREAM(action_name_ << " : Executing LED Action: " << goal->type);
00221 
00222     leds.clear();
00223     // Microseconds
00224     usleep(100000);
00225 
00226     timeout_timer.stop();
00227 
00228     ros::Time start = ros::Time::now();
00229 
00230     if (goal->timeout > ros::Duration(0))
00231     {
00232       timeout = false;
00233       ROS_INFO_STREAM("Creating timeout for: " << goal->timeout);
00234       timeout_timer.setPeriod(goal->timeout);
00235       timeout_timer.start();
00236     }
00237 
00238     try
00239     {
00240       // Will run as long as goal is active
00241       while(as_.isActive())
00242       {
00243         // Preempted Execution Logic
00244         if (as_.isPreemptRequested())
00245         {
00246           ROS_INFO("%s: Preempted", action_name_.c_str());
00247           result_.result = bwi_msgs::LEDActionResult::PREEMPTED;
00248           std::ostringstream stringStream;
00249           stringStream << "Action Preempted. " << goal->type;
00250           result_.status = stringStream.str();
00251           as_.setPreempted(result_);
00252           timeout_timer.stop();
00253           leds.clear();
00254           ROS_INFO("Cleared LED Strip");
00255           success = false;
00256           timeout = false;
00257           check_camera_status();
00258           break;
00259         }
00260 
00261         // ROS Failure Logic
00262         if (!ros::ok())
00263         {
00264           ROS_INFO("%s: Preempted due to ROS failure", action_name_.c_str());
00265           result_.result = bwi_msgs::LEDActionResult::SHUTDOWN;
00266           std::ostringstream stringStream;
00267           stringStream << "Action terminated due to ROS Shutdown. " << goal->type;
00268           result_.status = stringStream.str();
00269           as_.setPreempted(result_);
00270           timeout_timer.stop();
00271           leds.clear();
00272           ROS_INFO("Cleared LED Strip");
00273           check_camera_status();
00274           success = false;
00275           timeout = false;
00276           break;
00277         }
00278 
00279         // Determines which animation to execute based on goal->type value
00280         switch(goal->type.led_animations)
00281         {
00282           // Left Turn Sequential Animation
00283 /*          case bwi_msgs::LEDAnimations::LEFT_TURN:
00284             {
00285               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
00286               while(!as_.isPreemptRequested() && !timeout && ros::ok())
00287               {
00288                 // Creates an animation of leds which travels left along the strip
00289 
00290                 ros::Duration time_running = ros::Time::now() - start;
00291                 feedback_.time_running = time_running;
00292                 as_.publishFeedback(feedback_);
00293 
00294                 int j = back_center_left_start+1;
00295 
00296                 for (int i = front_center_left_start-1; i < front_center_left_end+1;)
00297                 {
00298                   // Terminate goal if preempted, timeout is reached, or ros fails
00299                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00300 
00301                   if (i == front_center_left_start-1)
00302                   {
00303                     leds.setHSV(i, 22, 1, .1);
00304                     leds.setHSV(i+1, 22, 1, .1);
00305 
00306                     leds.setHSV(j, 22, 1, .1);
00307                     leds.setHSV(j-1, 22, 1, .1);
00308 
00309                     i+=2;
00310                     j-=2;
00311                   }
00312                   else
00313                   {
00314                     leds.setHSV(i, 22, 1, .1);
00315                     leds.setHSV(j, 22, 1, .1);
00316                     i+=1;
00317                     j-=1;
00318                   }
00319 
00320                   if (i > front_center_left_start-1)
00321                   {
00322                     leds.setRGB(i-2, 0, 0, 0);
00323                     leds.setRGB(j+2, 0, 0, 0);
00324                   }
00325 
00326                   leds.flush();
00327                   // Microseconds
00328                   usleep(100000);
00329 
00330                   if (i == front_center_left_end+1)
00331                   {
00332                     leds.setRGB(i, 0, 0, 0);
00333                     leds.setRGB(i-1, 0, 0, 0);
00334 
00335                     leds.setRGB(j, 0, 0, 0);
00336                     leds.setRGB(j+1, 0, 0, 0);
00337 
00338                     i+=1;
00339                     j-=1;
00340 
00341                     leds.flush();
00342                     // Microseconds
00343                     usleep(100000);
00344                   }
00345                 }
00346               }
00347               break;
00348             }
00349 
00350           // Right Turn Sequential Animation
00351           case bwi_msgs::LEDAnimations::RIGHT_TURN:
00352             {
00353               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
00354               while(!as_.isPreemptRequested() && !timeout && ros::ok())
00355               {
00356                 // Creates an animation of leds which travels right along the strip
00357 
00358                 ros::Duration time_running = ros::Time::now() - start;
00359                 feedback_.time_running = time_running;
00360                 as_.publishFeedback(feedback_);
00361 
00362                 int j = front_center_right_start+1;
00363 
00364                 for (int i = back_center_right_start-1; i < back_center_right_end+1;)
00365                 {
00366                   // Terminate goal if preempted, timeout is reached, or ros fails
00367                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00368 
00369                   if (i == back_center_right_start-1)
00370                   {
00371                     leds.setHSV(i, 22, 1, .1);
00372                     leds.setHSV(i+1, 22, 1, .1);
00373 
00374                     leds.setHSV(j, 22, 1, .1);
00375                     leds.setHSV(j-1, 22, 1, .1);
00376 
00377                     i+=2;
00378                     j-=2;
00379                   }
00380                   else
00381                   {
00382                     leds.setHSV(i, 22, 1, .1);
00383                     leds.setHSV(j, 22, 1, .1);
00384                     i+=1;
00385                     j-=1;
00386                   }
00387 
00388                   if (i > back_center_right_start-1)
00389                   {
00390                     leds.setRGB(i-2, 0, 0, 0);
00391                     leds.setRGB(j+2, 0, 0, 0);
00392                   }
00393 
00394                   leds.flush();
00395                   // Microseconds
00396                   usleep(100000);
00397 
00398                   if (i == back_center_right_end+1)
00399                   {
00400                     leds.setRGB(i, 0, 0, 0);
00401                     leds.setRGB(i-1, 0, 0, 0);
00402 
00403                     leds.setRGB(j, 0, 0, 0);
00404                     leds.setRGB(j+1, 0, 0, 0);
00405 
00406                     i+=1;
00407                     j-=1;
00408 
00409                     leds.flush();
00410                     // Microseconds
00411                     usleep(100000);
00412                   }
00413                 }
00414               }
00415               break;
00416             }*/
00417 
00418           // Blinking Versions of Turn Signals
00419 
00420           // Left Turn Blinking Animation
00421           case bwi_msgs::LEDAnimations::LEFT_TURN:
00422             {
00423               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
00424               while(!as_.isPreemptRequested() && !timeout && ros::ok())
00425               {
00426                 // Creates an animation of leds which travels left along the strip
00427 
00428                 ros::Duration time_running = ros::Time::now() - start;
00429                 feedback_.time_running = time_running;
00430                 as_.publishFeedback(feedback_);
00431 
00432                 int j = back_center_left_start;
00433 
00434                 for (int i = front_center_left_start; i <= front_center_left_end;)
00435                 {
00436                   // Terminate goal if preempted, timeout is reached, or ros fails
00437                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00438 
00439                   leds.setHSV(i, 22, 1, .1);
00440                   leds.setHSV(j, 22, 1, .1);
00441 
00442                   i++;
00443                   j--;
00444                 }
00445 
00446                 leds.flush();
00447                 // Microseconds
00448                 usleep(400000);
00449 
00450                 leds.clear();
00451                 // Microseconds
00452                 usleep(100000);
00453               }
00454               break;
00455             }
00456 
00457           // Right Turn Blinking Animation
00458           case bwi_msgs::LEDAnimations::RIGHT_TURN:
00459             {
00460               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
00461               while(!as_.isPreemptRequested() && !timeout && ros::ok())
00462               {
00463                 // Creates an animation of leds which travels right along the strip
00464 
00465                 ros::Duration time_running = ros::Time::now() - start;
00466                 feedback_.time_running = time_running;
00467                 as_.publishFeedback(feedback_);
00468 
00469                 int j = front_center_right_start;
00470 
00471                 for (int i = back_center_right_start; i <= back_center_right_end;)
00472                 {
00473                   // Terminate goal if preempted, timeout is reached, or ros fails
00474                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00475 
00476                   leds.setHSV(i, 22, 1, .1);
00477                   leds.setHSV(j, 22, 1, .1);
00478 
00479                   i++;
00480                   j--;
00481                 }
00482 
00483                 leds.flush();
00484                 // Microseconds
00485                 usleep(400000);
00486 
00487                 leds.clear();
00488                 // Microseconds
00489                 usleep(100000);
00490               }
00491               break;
00492             }
00493 
00494           // Circular Versions of Turn Signals [One Set]
00495 
00496 /*          // Left Turn Circular Animation [One Set]
00497           case bwi_msgs::LEDAnimations::LEFT_TURN:
00498             {
00499               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
00500               while(!as_.isPreemptRequested() && !timeout && ros::ok())
00501               {
00502                 // Creates an animation of leds which travels left along the strip
00503 
00504                 ros::Duration time_running = ros::Time::now() - start;
00505                 feedback_.time_running = time_running;
00506                 as_.publishFeedback(feedback_);
00507 
00508                 int k = circular_u_end+(1+(top_of_u_end-top_of_u_start));
00509                 int l = top_of_u_start-1;
00510                 int m = top_of_u_end+1;
00511 
00512                 for (int i = circular_u_start-1; i < k+3;)
00513                 {
00514                   // Terminate goal if preempted, timeout is reached, or ros fails
00515                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00516 
00517                   if(i < circular_u_end+1)
00518                   {
00519                     if (i == circular_u_start-1)
00520                     {
00521                       leds.setHSV(i, 22, 1, .1);
00522                       leds.setHSV(i+1, 22, 1, .1);
00523                       leds.setHSV(i+2, 22, 1, .1);
00524                       leds.setHSV(i+3, 22, 1, .1);
00525 
00526                       i+=4;
00527                     }
00528                     else
00529                     {
00530                       leds.setHSV(i, 22, 1, .1);
00531                       i+=1;
00532                     }
00533 
00534                     if (i > circular_u_start-1)
00535                     {
00536                       leds.setRGB(i-4, 0, 0, 0);
00537                     }
00538 
00539                     leds.flush();
00540                     // Microseconds
00541                     usleep(100000);
00542                   }
00543                   else
00544                   {
00545                     if (i == circular_u_end+1)
00546                     {
00547                       leds.setRGB(i, 0, 0, 0);
00548                       leds.setRGB(i-1, 0, 0, 0);
00549                       leds.setRGB(i-2, 0, 0, 0);
00550                       leds.setRGB(i-3, 0, 0, 0);
00551 
00552                       i+=1;
00553 
00554                       leds.flush();
00555                       // Microseconds
00556                       usleep(100000);
00557                     }
00558 
00559                     if (i == circular_u_end+2)
00560                     {
00561                       leds.setHSV(l, 22, 1, .1);
00562                       leds.setHSV(l+1, 22, 1, .1);
00563                       leds.setHSV(l+2, 22, 1, .1);
00564                       leds.setHSV(l+3, 22, 1, .1);
00565 
00566                       i+=4;
00567                       l+=4;
00568                     }
00569                     else
00570                     {
00571                       leds.setHSV(l, 22, 1, .1);
00572                       i+=1;
00573                       l+=1;
00574                     }
00575 
00576                     if (i > circular_u_end+2)
00577                     {
00578                       leds.setRGB(l-4, 0, 0, 0);
00579                     }
00580 
00581                     leds.flush();
00582                     // Microseconds
00583                     usleep(100000);
00584 
00585                     if (i == k+3)
00586                     {
00587                       leds.setRGB(l, 0, 0, 0);
00588                       leds.setRGB(l-1, 0, 0, 0);
00589                       leds.setRGB(l-2, 0, 0, 0);
00590                       leds.setRGB(l-3, 0, 0, 0);
00591 
00592                       i+=1;
00593                       l+=1;
00594 
00595                       leds.flush();
00596                       // Microseconds
00597                       usleep(100000);
00598                     }
00599                   }
00600                 }
00601               }
00602               break;
00603             }
00604 
00605           // Right Turn Circular Animation [One Set]
00606           case bwi_msgs::LEDAnimations::RIGHT_TURN:
00607             {
00608               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
00609               while(!as_.isPreemptRequested() && !timeout && ros::ok())
00610               {
00611                 // Creates an animation of leds which travels left along the strip
00612 
00613                 ros::Duration time_running = ros::Time::now() - start;
00614                 feedback_.time_running = time_running;
00615                 as_.publishFeedback(feedback_);
00616 
00617                 int k = circular_u_start-(1+(top_of_u_end-top_of_u_start));
00618                 int l = top_of_u_start-1;
00619                 int m = top_of_u_end+1;
00620 
00621                 for (int i = circular_u_end+1; i > k-3;)
00622                 {
00623                   // Terminate goal if preempted, timeout is reached, or ros fails
00624                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00625 
00626                   if(i > circular_u_start-1)
00627                   {
00628                     if (i == circular_u_end+1)
00629                     {
00630                       leds.setHSV(i, 22, 1, .1);
00631                       leds.setHSV(i-1, 22, 1, .1);
00632                       leds.setHSV(i-2, 22, 1, .1);
00633                       leds.setHSV(i-3, 22, 1, .1);
00634 
00635                       i-=4;
00636                     }
00637                     else
00638                     {
00639                       leds.setHSV(i, 22, 1, .1);
00640                       i-=1;
00641                     }
00642 
00643                     if (i < circular_u_end+1)
00644                     {
00645                       leds.setRGB(i+4, 0, 0, 0);
00646                     }
00647 
00648                     leds.flush();
00649                     // Microseconds
00650                     usleep(100000);
00651                   }
00652                   else
00653                   {
00654                     if (i == circular_u_start-1)
00655                     {
00656                       leds.setRGB(i, 0, 0, 0);
00657                       leds.setRGB(i+1, 0, 0, 0);
00658                       leds.setRGB(i+2, 0, 0, 0);
00659                       leds.setRGB(i+3, 0, 0, 0);
00660 
00661                       i-=1;
00662 
00663                       leds.flush();
00664                       // Microseconds
00665                       usleep(100000);
00666                     }
00667 
00668                     if (i == circular_u_start-2)
00669                     {
00670                       leds.setHSV(m, 22, 1, .1);
00671                       leds.setHSV(m-1, 22, 1, .1);
00672                       leds.setHSV(m-2, 22, 1, .1);
00673                       leds.setHSV(m-3, 22, 1, .1);
00674 
00675                       i-=4;
00676                       m-=4;
00677                     }
00678                     else
00679                     {
00680                       leds.setHSV(m, 22, 1, .1);
00681                       i-=1;
00682                       m-=1;
00683                     }
00684 
00685                     if (i < circular_u_start-2)
00686                     {
00687                       leds.setRGB(m+4, 0, 0, 0);
00688                     }
00689 
00690                     leds.flush();
00691                     // Microseconds
00692                     usleep(100000);
00693 
00694                     if (i == k-3)
00695                     {
00696                       leds.setRGB(m, 0, 0, 0);
00697                       leds.setRGB(m+1, 0, 0, 0);
00698                       leds.setRGB(m+2, 0, 0, 0);
00699                       leds.setRGB(m+3, 0, 0, 0);
00700 
00701                       i-=1;
00702                       m-=1;
00703 
00704                       leds.flush();
00705                       // Microseconds
00706                       usleep(100000);
00707                     }
00708                   }
00709                 }
00710               }
00711               break;
00712             }*/
00713 
00714 /*          // Circular Versions of Turn Signals [Four Sets]
00715 
00716           // Left Turn Circular Animation [Four  Sets]
00717           case bwi_msgs::LEDAnimations::LEFT_TURN:
00718             {
00719               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
00720               while(!as_.isPreemptRequested() && !timeout && ros::ok())
00721               {
00722                 // Creates an animation of leds which travels left along the strip
00723 
00724                 ros::Duration time_running = ros::Time::now() - start;
00725                 feedback_.time_running = time_running;
00726                 as_.publishFeedback(feedback_);
00727 
00728                 int j = circular_u_start-1;
00729                 int k = circular_u_end+(1+(top_of_u_end-top_of_u_start));
00730                 int l = top_of_u_start-1;
00731                 int m = top_of_u_end+1;
00732 
00733                 for (int i = 1; i <= 12;)
00734                 {
00735                   // Terminate goal if preempted, timeout is reached, or ros fails
00736                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00737 
00738                   if (i == 1)
00739                   {
00740                     leds.setHSV(j, 22, 1, .1);
00741                     leds.setHSV(j+1, 22, 1, .1);
00742                     leds.setHSV(j+2, 22, 1, .1);
00743                     leds.setHSV(j+3, 22, 1, .1);
00744 
00745                     leds.setHSV(j+10, 22, 1, .1);
00746                     leds.setHSV(j+11, 22, 1, .1);
00747                     leds.setHSV(j+12, 22, 1, .1);
00748                     leds.setHSV(j+13, 22, 1, .1);
00749 
00750                     leds.setHSV(j+20, 22, 1, .1);
00751                     leds.setHSV(j+21, 22, 1, .1);
00752                     leds.setHSV(j+22, 22, 1, .1);
00753                     leds.setHSV(j+23, 22, 1, .1);
00754 
00755                     leds.setHSV(l, 22, 1, .1);
00756                     leds.setHSV(l+1, 22, 1, .1);
00757                     leds.setHSV(l+2, 22, 1, .1);
00758                     leds.setHSV(l+3, 22, 1, .1);
00759 
00760                     i+=4;
00761                     j+=4;
00762                     l+=4;
00763                   }
00764                   else
00765                   {
00766                     leds.setHSV(j, 22, 1, .1);
00767                     leds.setHSV(j+10, 22, 1, .1);
00768                     leds.setHSV(j+20, 22, 1, .1);
00769                     leds.setHSV(l, 22, 1, .1);
00770                     i+=1;
00771                     j+=1;
00772                     l+=1;
00773                   }
00774 
00775                   if (i > 1)
00776                   {
00777                     leds.setRGB(j-4, 0, 0, 0);
00778                     leds.setRGB((j+10)-4, 0, 0, 0);
00779                     leds.setRGB((j+20)-4, 0, 0, 0);
00780                     leds.setRGB(l-4, 0, 0, 0);
00781                   }
00782 
00783                   leds.flush();
00784                   // Microseconds
00785                   usleep(100000);
00786 
00787                    if (i == 12)
00788                   {
00789                     leds.setRGB(j, 0, 0, 0);
00790                     leds.setRGB(j-1, 0, 0, 0);
00791                     leds.setRGB(j-2, 0, 0, 0);
00792                     leds.setRGB(j-3, 0, 0, 0);
00793 
00794                     leds.setRGB(j+10, 0, 0, 0);
00795                     leds.setRGB((j+10)-1, 0, 0, 0);
00796                     leds.setRGB((j+10)-2, 0, 0, 0);
00797                     leds.setRGB((j+10)-3, 0, 0, 0);
00798 
00799 
00800                     leds.setRGB(j+20, 0, 0, 0);
00801                     leds.setRGB((j+20)-1, 0, 0, 0);
00802                     leds.setRGB((j+20)-2, 0, 0, 0);
00803                     leds.setRGB((j+20)-3, 0, 0, 0);
00804 
00805                     leds.setRGB(l, 0, 0, 0);
00806                     leds.setRGB(l-1, 0, 0, 0);
00807                     leds.setRGB(l-2, 0, 0, 0);
00808                     leds.setRGB(l-3, 0, 0, 0);
00809 
00810 
00811                     i+=1;
00812                     j+=1;
00813                     l+=1;
00814 
00815                     leds.flush();
00816                     // Microseconds
00817                     usleep(100000);
00818                   }
00819                 }
00820               }
00821               break;
00822             }
00823 
00824           // Right Turn Circular Animation [Four  Sets]
00825           case bwi_msgs::LEDAnimations::RIGHT_TURN:
00826             {
00827               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
00828               while(!as_.isPreemptRequested() && !timeout && ros::ok())
00829               {
00830                 // Creates an animation of leds which travels left along the strip
00831 
00832                 ros::Duration time_running = ros::Time::now() - start;
00833                 feedback_.time_running = time_running;
00834                 as_.publishFeedback(feedback_);
00835 
00836                 int j = circular_u_end+1;
00837                 int k = circular_u_start-(1+(top_of_u_end-top_of_u_start));
00838                 int l = top_of_u_end+1;
00839                 int m = top_of_u_start-1;
00840 
00841                 for (int i = 1; i <= 12;)
00842                 {
00843                   // Terminate goal if preempted, timeout is reached, or ros fails
00844                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00845 
00846                   if (i == 1)
00847                   {
00848                     leds.setHSV(j, 22, 1, .1);
00849                     leds.setHSV(j-1, 22, 1, .1);
00850                     leds.setHSV(j-2, 22, 1, .1);
00851                     leds.setHSV(j-3, 22, 1, .1);
00852 
00853                     leds.setHSV(j-10, 22, 1, .1);
00854                     leds.setHSV(j-11, 22, 1, .1);
00855                     leds.setHSV(j-12, 22, 1, .1);
00856                     leds.setHSV(j-13, 22, 1, .1);
00857 
00858                     leds.setHSV(j-20, 22, 1, .1);
00859                     leds.setHSV(j-21, 22, 1, .1);
00860                     leds.setHSV(j-22, 22, 1, .1);
00861                     leds.setHSV(j-23, 22, 1, .1);
00862 
00863                     leds.setHSV(l, 22, 1, .1);
00864                     leds.setHSV(l-1, 22, 1, .1);
00865                     leds.setHSV(l-2, 22, 1, .1);
00866                     leds.setHSV(l-3, 22, 1, .1);
00867 
00868                     i+=4;
00869                     j-=4;
00870                     l-=4;
00871                   }
00872                   else
00873                   {
00874                     leds.setHSV(j, 22, 1, .1);
00875                     leds.setHSV(j-10, 22, 1, .1);
00876                     leds.setHSV(j-20, 22, 1, .1);
00877                     leds.setHSV(l, 22, 1, .1);
00878                     i+=1;
00879                     j-=1;
00880                     l-=1;
00881                   }
00882 
00883                   if (i > 1)
00884                   {
00885                     leds.setRGB(j+4, 0, 0, 0);
00886                     leds.setRGB((j-10)+4, 0, 0, 0);
00887                     leds.setRGB((j-20)+4, 0, 0, 0);
00888                     leds.setRGB(l+4, 0, 0, 0);
00889                   }
00890 
00891                   leds.flush();
00892                   // Microseconds
00893                   usleep(100000);
00894 
00895                    if (i == 12)
00896                   {
00897                     leds.setRGB(j, 0, 0, 0);
00898                     leds.setRGB(j+1, 0, 0, 0);
00899                     leds.setRGB(j+2, 0, 0, 0);
00900                     leds.setRGB(j+3, 0, 0, 0);
00901 
00902                     leds.setRGB(j+10, 0, 0, 0);
00903                     leds.setRGB((j-10)+1, 0, 0, 0);
00904                     leds.setRGB((j-10)+2, 0, 0, 0);
00905                     leds.setRGB((j-10)+3, 0, 0, 0);
00906 
00907 
00908                     leds.setRGB(j-20, 0, 0, 0);
00909                     leds.setRGB((j-20)+1, 0, 0, 0);
00910                     leds.setRGB((j-20)+2, 0, 0, 0);
00911                     leds.setRGB((j-20)+3, 0, 0, 0);
00912 
00913                     leds.setRGB(l, 0, 0, 0);
00914                     leds.setRGB(l+1, 0, 0, 0);
00915                     leds.setRGB(l+2, 0, 0, 0);
00916                     leds.setRGB(l+3, 0, 0, 0);
00917 
00918 
00919                     i+=1;
00920                     j-=1;
00921                     l-=1;
00922 
00923                     leds.flush();
00924                     // Microseconds
00925                     usleep(100000);
00926                   }
00927                 }
00928               }
00929               break;
00930             }
00931 */
00932           // Reverse Animtion
00933           case bwi_msgs::LEDAnimations::REVERSE:
00934             {
00935               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
00936               while(!as_.isPreemptRequested() && !timeout && ros::ok())
00937               {
00938                 // Creates a pulsing animation
00939 
00940                 ros::Duration time_running = ros::Time::now() - start;
00941                 feedback_.time_running = time_running;
00942                 as_.publishFeedback(feedback_);
00943 
00944                 // Increase brightness
00945                 for (float b = 0.0; b < 0.5; b += 0.02)
00946                 {
00947                   // Terminate goal if preempted, timeout is reached, or ros fails
00948                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00949 
00950                   int j = back_left_beam_start;
00951                   int k = back_center_end;
00952 
00953                   for (int i = back_right_beam_end; i >= back_right_beam_start; i--)
00954                   {
00955                     leds.setHSV(i, 360, 1, b);
00956                     leds.setHSV(j, 360, 1, b);
00957 
00958                     if (k >= back_center_start)
00959                     {
00960                       leds.setHSV(k, 360, 1, b);
00961                       k--;
00962                     }
00963 
00964                     j--;
00965                   }
00966 
00967                   leds.flush();
00968                   // Microseconds
00969                   usleep(22500);
00970                 }
00971                 // Microseconds
00972                 usleep(500000);
00973 
00974                 // Decreases Brightness
00975                 for (float b = 0.5; b >= 0.0; b -= 0.02)
00976                 {
00977                   // Terminate goal if preempted, timeout is reached, or ros fails
00978                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
00979 
00980                   int j = back_left_beam_start;
00981                   int k = back_center_end;
00982 
00983                   for (int i = back_right_beam_end; i >= back_right_beam_start; i--)
00984                   {
00985                     leds.setHSV(i, 360, 1, b);
00986                     leds.setHSV(j, 360, 1, b);
00987 
00988                     if (k >= back_center_start)
00989                     {
00990                       leds.setHSV(k, 360, 1, b);
00991                       k--;
00992                     }
00993 
00994                     j--;
00995                   }
00996 
00997                   leds.flush();
00998                   // Microseconds
00999                   usleep(22500);
01000                 }
01001                 // Microseconds
01002                 usleep(500000);
01003               }
01004               break;
01005             }
01006 
01007           // Blocked Animation
01008           case bwi_msgs::LEDAnimations::BLOCKED:
01009             {
01010               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
01011               while(!as_.isPreemptRequested() && !timeout && ros::ok())
01012               {
01013                 // Creates a pulsing animation
01014 
01015                 ros::Duration time_running = ros::Time::now() - start;
01016                 feedback_.time_running = time_running;
01017                 as_.publishFeedback(feedback_);
01018 
01019                 // Increase brightness
01020                 for (float b = 0.0; b <= 0.5; b += 0.02)
01021                 {
01022                   // Terminate goal if preempted, timeout is reached, or ros fails
01023                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
01024 
01025                   for (int i = led_count; i >= 0; i--)
01026                   {
01027                     leds.setHSV(i, 360, 1, b);
01028                   }
01029 
01030                   leds.flush();
01031                   // Microseconds
01032                   if (b != 0.5) { usleep(60000); }
01033                 }
01034                 // Microseconds
01035                 usleep(45000);
01036 
01037                 // Decreases Brightness
01038                 for (float b = 0.5; b >= 0.0; b -= 0.02)
01039                 {
01040                   // Terminate goal if preempted, timeout is reached, or ros fails
01041                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
01042 
01043                   for (int i = led_count; i >= 0; i--)
01044                   {
01045                     leds.setHSV(i, 360, 1, b);
01046                   }
01047 
01048                   leds.flush();
01049                   // Microseconds
01050                   if (b != 0.0) { usleep(60000); }
01051                 }
01052                 // Microseconds
01053                 usleep(45000);
01054               }
01055               break;
01056             }
01057           // Up
01058           case bwi_msgs::LEDAnimations::UP:
01059             {
01060               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
01061               while(!as_.isPreemptRequested() && !timeout && ros::ok())
01062               {
01063                 // Creates an animation of leds which travels up along the strip
01064 
01065                 ros::Duration time_running = ros::Time::now() - start;
01066                 feedback_.time_running = time_running;
01067                 as_.publishFeedback(feedback_);
01068 
01069                 int l = front_left_beam_start;
01070                 int k = front_right_beam_start;
01071                 int j = back_left_beam_start;
01072 
01073                 for (int i = back_right_beam_start; i <= back_right_beam_end;)
01074                 {
01075                   // Terminate goal if preempted, timeout is reached, or ros fails
01076                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
01077 
01078                   if (i == back_right_beam_start)
01079                   {
01080                     leds.setHSV(i, 240, 1, .1);
01081                     leds.setHSV(i+1, 240, 1, .1);
01082                     leds.setHSV(i+2, 240, 1, .1);
01083                     leds.setHSV(i+3, 240, 1, .1);
01084 
01085                     leds.setHSV(l, 240, 1, .1);
01086                     leds.setHSV(l+1, 240, 1, .1);
01087                     leds.setHSV(l+2, 240, 1, .1);
01088                     leds.setHSV(l+3, 240, 1, .1);
01089 
01090                     leds.setHSV(k, 240, 1, .1);
01091                     leds.setHSV(k-1, 240, 1, .1);
01092                     leds.setHSV(k-2, 240, 1, .1);
01093                     leds.setHSV(k-3, 240, 1, .1);
01094 
01095                     leds.setHSV(j, 240, 1, .1);
01096                     leds.setHSV(j-1, 240, 1, .1);
01097                     leds.setHSV(j-2, 240, 1, .1);
01098                     leds.setHSV(j-3, 240, 1, .1);
01099 
01100                     i+=4;
01101                     l+=4;
01102                     k-=4;
01103                     j-=4;
01104                   }
01105                   else
01106                   {
01107                     leds.setHSV(i, 240, 1, .1);
01108                     leds.setHSV(l, 240, 1, .1);
01109                     leds.setHSV(k, 240, 1, .1);
01110                     leds.setHSV(j, 240, 1, .1);
01111                     i+=1;
01112                     l+=1;
01113                     k-=1;
01114                     j-=1;
01115                   }
01116 
01117                   if (i > back_right_beam_start)
01118                   {
01119                     leds.setRGB(i-4, 0, 0, 0);
01120                     leds.setRGB(l-4, 0, 0, 0);
01121                     leds.setRGB(k+4, 0, 0, 0);
01122                     leds.setRGB(j+4, 0, 0, 0);
01123                   }
01124 
01125                   leds.flush();
01126                   // Microseconds
01127                   usleep(100000);
01128 
01129                   if (i == back_right_beam_end)
01130                   {
01131                     leds.setRGB(i, 0, 0, 0);
01132                     leds.setRGB(i-1, 0, 0, 0);
01133                     leds.setRGB(i-2, 0, 0, 0);
01134                     leds.setRGB(i-3, 0, 0, 0);
01135 
01136                     leds.setRGB(l, 0, 0, 0);
01137                     leds.setRGB(l-1, 0, 0, 0);
01138                     leds.setRGB(l-2, 0, 0, 0);
01139                     leds.setRGB(l-3, 0, 0, 0);
01140 
01141                     leds.setRGB(k, 0, 0, 0);
01142                     leds.setRGB(k+1, 0, 0, 0);
01143                     leds.setRGB(k+2, 0, 0, 0);
01144                     leds.setRGB(k+3, 0, 0, 0);
01145 
01146                     leds.setRGB(j, 0, 0, 0);
01147                     leds.setRGB(j+1, 0, 0, 0);
01148                     leds.setRGB(j+2, 0, 0, 0);
01149                     leds.setRGB(j+3, 0, 0, 0);
01150 
01151                     i+=1;
01152                     l+=1;
01153                     k-=1;
01154                     j-=1;
01155 
01156                     leds.flush();
01157                     // Microseconds
01158                     usleep(100000);
01159                   }
01160                 }
01161               }
01162               break;
01163             }
01164           // Down
01165           case bwi_msgs::LEDAnimations::DOWN:
01166             {
01167               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
01168               while(!as_.isPreemptRequested() && !timeout && ros::ok())
01169               {
01170                 // Creates an animation of leds which travels down along the strip
01171 
01172                 ros::Duration time_running = ros::Time::now() - start;
01173                 feedback_.time_running = time_running;
01174                 as_.publishFeedback(feedback_);
01175 
01176                 int l = front_left_beam_end;
01177                 int k = front_right_beam_end;
01178                 int j = back_left_beam_end;
01179 
01180                 for (int i = back_right_beam_end; i >= back_right_beam_start;)
01181                 {
01182                   // Terminate goal if preempted, timeout is reached, or ros fails
01183                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
01184 
01185                   if (i == back_right_beam_end)
01186                   {
01187                     leds.setHSV(i, 240, 1, .1);
01188                     leds.setHSV(i-1, 240, 1, .1);
01189                     leds.setHSV(i-2, 240, 1, .1);
01190                     leds.setHSV(i-3, 240, 1, .1);
01191 
01192                     leds.setHSV(l, 240, 1, .1);
01193                     leds.setHSV(l-1, 240, 1, .1);
01194                     leds.setHSV(l-2, 240, 1, .1);
01195                     leds.setHSV(l-3, 240, 1, .1);
01196 
01197                     leds.setHSV(k, 240, 1, .1);
01198                     leds.setHSV(k+1, 240, 1, .1);
01199                     leds.setHSV(k+2, 240, 1, .1);
01200                     leds.setHSV(k+3, 240, 1, .1);
01201 
01202                     leds.setHSV(j, 240, 1, .1);
01203                     leds.setHSV(j+1, 240, 1, .1);
01204                     leds.setHSV(j+2, 240, 1, .1);
01205                     leds.setHSV(j+3, 240, 1, .1);
01206 
01207                     i-=4;
01208                     l-=4;
01209                     k+=4;
01210                     j+=4;
01211 
01212                   }
01213                   else
01214                   {
01215                     leds.setHSV(i, 240, 1, .1);
01216                     leds.setHSV(l, 240, 1, .1);
01217                     leds.setHSV(k, 240, 1, .1);
01218                     leds.setHSV(j, 240, 1, .1);
01219                     i-=1;
01220                     l-=1;
01221                     k+=1;
01222                     j+=1;
01223                   }
01224 
01225                   if (i < back_right_beam_end)
01226                   {
01227                     leds.setRGB(i+4, 0, 0, 0);
01228                     leds.setRGB(l+4, 0, 0, 0);
01229                     leds.setRGB(k-4, 0, 0, 0);
01230                     leds.setRGB(j-4, 0, 0, 0);
01231                   }
01232 
01233                   leds.flush();
01234                   // Microseconds
01235                   usleep(100000);
01236 
01237                   if (i == back_right_beam_start)
01238                   {
01239                     leds.setRGB(i, 0, 0, 0);
01240                     leds.setRGB(i+1, 0, 0, 0);
01241                     leds.setRGB(i+2, 0, 0, 0);
01242                     leds.setRGB(i+3, 0, 0, 0);
01243 
01244                     leds.setRGB(l, 0, 0, 0);
01245                     leds.setRGB(l+1, 0, 0, 0);
01246                     leds.setRGB(l+2, 0, 0, 0);
01247                     leds.setRGB(l+3, 0, 0, 0);
01248 
01249                     leds.setRGB(k, 0, 0, 0);
01250                     leds.setRGB(k-1, 0, 0, 0);
01251                     leds.setRGB(k-2, 0, 0, 0);
01252                     leds.setRGB(k-3, 0, 0, 0);
01253 
01254                     leds.setRGB(j, 0, 0, 0);
01255                     leds.setRGB(j-1, 0, 0, 0);
01256                     leds.setRGB(j-2, 0, 0, 0);
01257                     leds.setRGB(j-3, 0, 0, 0);
01258 
01259                     i-=1;
01260                     l-=1;
01261                     k+=1;
01262                     j+=1;
01263 
01264                     leds.flush();
01265                     // Microseconds
01266                     usleep(100000);
01267                   }
01268                 }
01269               }
01270               break;
01271             }
01272 
01273           // Need Assistance Animation
01274           case bwi_msgs::LEDAnimations::NEED_ASSIST:
01275             {
01276               // Executes as long as timeout has not been reached, Goal is not Preempted, and ROS is OK
01277               while(!as_.isPreemptRequested() && !timeout && ros::ok())
01278               {
01279                 // Creates a pulsing animation
01280 
01281                 ros::Duration time_running = ros::Time::now() - start;
01282                 feedback_.time_running = time_running;
01283                 as_.publishFeedback(feedback_);
01284 
01285                 // Increase brightness
01286                 for (float b = 0.0; b <= 0.3; b += 0.02)
01287                 {
01288                   // Terminate goal if preempted, timeout is reached, or ros fails
01289                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
01290 
01291                   for (int i = led_count; i >= 0; i--)
01292                   {
01293                     leds.setHSV(i, 240, 1, b);
01294                   }
01295 
01296                   leds.flush();
01297                   // Microseconds
01298                   if (b != 0.3) { usleep(70000); }
01299                 }
01300                 // Microseconds
01301                 usleep(75000);
01302 
01303                 // Decreases Brightness
01304                 for (float b = 0.3; b >= 0.0; b -= 0.02)
01305                 {
01306                   // Terminate goal if preempted, timeout is reached, or ros fails
01307                   if(as_.isPreemptRequested() || timeout || !ros::ok()) { break; }
01308 
01309                   for (int i = led_count; i >= 0; i--)
01310                   {
01311                     leds.setHSV(i, 240, 1, b);
01312                   }
01313 
01314                   leds.flush();
01315                   // Microseconds
01316                   if (b != 0.0) { usleep(70000); }
01317                 }
01318                 // Microseconds
01319                 usleep(75000);
01320               }
01321               break;
01322             }
01323         }
01324 
01325         // Successful Execution Logic
01326         if(success || timeout)
01327         {
01328           ROS_INFO("%s: Succeeded", action_name_.c_str());
01329           result_.result = bwi_msgs::LEDActionResult::SUCCESS;
01330           std::ostringstream stringStream;
01331           stringStream << "Action completed successfully. " << goal->type;
01332           result_.status = stringStream.str();
01333           as_.setSucceeded(result_);
01334           timeout_timer.stop();
01335           leds.clear();
01336           ROS_INFO("Cleared LED Strip");
01337           check_run_status();
01338           check_camera_status();
01339           timeout = false;
01340         }
01341       }
01342     }
01343     catch(const serial::SerialException &e)
01344     {
01345       result_.result = bwi_msgs::LEDActionResult::FAILURE;
01346       std::ostringstream stringStream;
01347       stringStream << "Action, " << goal->type << ", failed due failure with serial communication to LED microcontroller.";
01348       result_.status = stringStream.str();
01349       ROS_ERROR("Action execution failed, unable to write to microcontroller,");
01350       timeout_timer.stop();
01351       connected = false;
01352       as_.setPreempted(result_);
01353       success = false;
01354       timeout = false;
01355       ROS_ERROR("Ensure LED microcontroller is connected.");
01356       ROS_ERROR("Attempting to reconnect to LED microcontroller.");
01357     }
01358   }
01359 };
01360 
01361 /*******************************************************
01362 *                                                      *
01363 *                Service Callbacks                     *
01364 *                                                      *
01365 ********************************************************/
01366 bool clear_strip(bwi_msgs::LEDClear::Request  &req,
01367                  bwi_msgs::LEDClear::Response &res)
01368 {
01369   try
01370   {
01371     leds.clear();
01372     ROS_INFO("Cleared LED Strip");
01373     check_run_status();
01374     check_camera_status();
01375     res.success = true;
01376     return true;
01377   }
01378   catch(const serial::SerialException &e)
01379   {
01380     res.success = false;
01381     res.status = "Failure with serial communication to LED microcontroller";
01382     ROS_ERROR("Service execution failed, unable to write to microcontroller,");
01383     ROS_ERROR("Ensure LED microcontroller is connected.");
01384     ROS_ERROR("Attempting to reconnect to LED microcontroller.");
01385     connected = false;
01386   }
01387 }
01388 
01389 bool test_strip(bwi_msgs::LEDTestStrip::Request  &req,
01390                 bwi_msgs::LEDTestStrip::Response &res)
01391 {
01392   try
01393   {
01394     switch(req.type.test_type)
01395     {
01396       // Set Every Fifth LED
01397       case bwi_msgs::LEDTestType::SET_EVERY_FIFTH:
01398         {
01399           leds.clear();
01400           sleep(1);
01401 
01402           for (int l=5; l < led_count; l+=5)
01403           {
01404             if (l%10 == 0)
01405             {
01406               leds.setHSV(l, 120, 1, .1);
01407             }
01408             else
01409             {
01410               leds.setHSV(l, 250, 1, .1);
01411             }
01412           }
01413           leds.flush();
01414           sleep(1);
01415 
01416           ROS_INFO("Set every fifth LED on strip");
01417           res.success = true;
01418           return true;
01419         }
01420       // Set First Five LEDs
01421       case bwi_msgs::LEDTestType::SET_FIRST_FIVE:
01422         {
01423           leds.clear();
01424           sleep(1);
01425 
01426           leds.setHSV(0, 240, 1, .1);
01427           leds.setHSV(1, 120, 1, .1);
01428           leds.setHSV(2, 240, 1, .1);
01429           leds.setHSV(3, 120, 1, .1);
01430           leds.setHSV(4, 240, 1, .1);
01431 
01432           leds.flush();
01433           sleep(1);
01434 
01435           ROS_INFO("Set first five LEDs on strip");
01436           res.success = true;
01437           return true;
01438         }
01439       // Test LEDs on Strip
01440       case bwi_msgs::LEDTestType::TEST_STRIP:
01441         {
01442           ROS_INFO("Testing LED Strip, will take about 30 seconds to complete.");
01443 
01444           leds.clear();
01445           sleep(1);
01446 
01447           for (int i=0; i < 360; i+=15)
01448           {
01449             for (int l=0; l < led_count; l++)
01450             {
01451               leds.setHSV(l, i, 1, .1);
01452             }
01453             leds.flush();
01454             sleep(1);
01455           }
01456 
01457           leds.clear();
01458           ROS_INFO("Cleared LED Strip");
01459           ROS_INFO("Tested Colors on LED strip");
01460           res.success = true;
01461           return true;
01462         }
01463       default:
01464         {
01465           res.success = false;
01466           std::ostringstream stringStream;
01467           stringStream << "Unknown type requested: " << req.type.test_type;
01468           res.success = false;
01469           res.status = stringStream.str();
01470           return false;
01471         }
01472     }
01473   }
01474   catch(const serial::SerialException &e)
01475   {
01476     res.success = false;
01477     res.status = "Failure with serial communication to LED microcontroller";
01478     ROS_ERROR("Service execution failed, unable to write to microcontroller,");
01479     ROS_ERROR("Ensure LED microcontroller is connected.");
01480     ROS_ERROR("Attempting to reconnect to LED microcontroller.");
01481     connected = false;
01482   }
01483 }
01484 
01485 bool set_status(bwi_msgs::LEDSetStatus::Request  &req,
01486                 bwi_msgs::LEDSetStatus::Response &res)
01487 {
01488   try
01489   {
01490     switch(req.type.status)
01491     {
01492       // Run On
01493       case bwi_msgs::LEDStatus::RUN_ON:
01494         {
01495           run_on = true;
01496           check_run_status();
01497           res.success = true;
01498           return true;
01499         }
01500       // Run Off
01501       case bwi_msgs::LEDStatus::RUN_OFF:
01502         {
01503           run_on = false;
01504           leds.clear();
01505           ROS_INFO("Set run indicator off and cleared LED strip");
01506           res.success = true;
01507           return true;
01508         }
01509       // Camera On
01510       case bwi_msgs::LEDStatus::CAMERA_ON:
01511         {
01512           camera_on = true;
01513           check_camera_status();
01514           res.success = true;
01515           return true;
01516         }
01517       // Camera Off
01518       case bwi_msgs::LEDStatus::CAMERA_OFF:
01519         {
01520           camera_on = false;
01521           leds.clear();
01522           ROS_INFO("Set camera indicator off and cleared LED strip");
01523           res.success = true;
01524           return true;
01525         }
01526       default:
01527         {
01528           res.success = false;
01529           std::ostringstream stringStream;
01530           stringStream << "Unknown type requested: " << req.type.status;
01531           res.success = false;
01532           res.status = stringStream.str();
01533           return false;
01534         }
01535     }
01536   }
01537   catch(const serial::SerialException &e)
01538   {
01539     res.success = false;
01540     res.status = "Failure with serial communication to LED microcontroller";
01541     ROS_ERROR("Service execution failed, unable to write to microcontroller,");
01542     ROS_ERROR("Ensure LED microcontroller is connected.");
01543     ROS_ERROR("Attempting to reconnect to LED microcontroller.");
01544     connected = false;
01545   }
01546 }
01547 
01548 int main(int argc, char **argv)
01549 {
01550   ros::init(argc, argv, "led_control_server");
01551   ros::NodeHandle n;
01552 
01553   // Override the default ros sigint handler.
01554   // This must be set after the first NodeHandle is created.
01555   signal(SIGINT, ledSigintHandler);
01556 
01557   ros::NodeHandle privateNode("~");
01558 
01559   // Node Parameters
01560   // LED and Microcontroller Initialization
01561   privateNode.param<int>("led_count",led_count,60);
01562   privateNode.param<string>("serial_port",serial_port,"/dev/metromini");
01563 
01564   // LED Segments
01565   privateNode.param<int>("camera_indicator_start",camera_indicator_start,29);
01566   privateNode.param<int>("camera_indicator_end",camera_indicator_end,30);
01567 
01568   privateNode.param<int>("back_center_start",back_center_start,85);
01569   privateNode.param<int>("back_center_end",back_center_end,94);
01570 
01571   // For center segments, start is in the center and end is the left/right most led of the segment
01572   // Point of reference is from facing the segment
01573   privateNode.param<int>("front_center_left_start",front_center_left_start,30);
01574   privateNode.param<int>("front_center_left_end",front_center_left_end,34);
01575 
01576   privateNode.param<int>("front_center_right_start",front_center_right_start,29);
01577   privateNode.param<int>("front_center_right_end",front_center_right_end,15);
01578 
01579   privateNode.param<int>("back_center_left_start",back_center_left_start,89);
01580   privateNode.param<int>("back_center_left_end",back_center_left_end,75);
01581 
01582   privateNode.param<int>("back_center_right_start",back_center_right_start,90);
01583   privateNode.param<int>("back_center_right_end",back_center_right_end,94);
01584 
01585   // For beam segments, start is bottom most led and end is top most led of the segment
01586   // Point of reference is from facing the segment
01587   privateNode.param<int>("front_left_beam_start",front_left_beam_start,60);
01588   privateNode.param<int>("front_left_beam_end",front_left_beam_end,73);
01589 
01590   privateNode.param<int>("front_right_beam_start",front_right_beam_start,119);
01591   privateNode.param<int>("front_right_beam_end",front_right_beam_end,106);
01592 
01593   privateNode.param<int>("back_left_beam_start",back_left_beam_start,59);
01594   privateNode.param<int>("back_left_beam_end",back_left_beam_end,46);
01595 
01596   privateNode.param<int>("back_right_beam_start",back_right_beam_start,0);
01597   privateNode.param<int>("back_right_beam_end",back_right_beam_end,13);
01598 
01599   // For Circular segments, start is left most led of the "U" shaped segment and end is right most led of the "U" shaped segment
01600   // Point of reference is from facing the segment
01601   privateNode.param<int>("circular_u_start",circular_u_start,75);
01602   privateNode.param<int>("circular_u_end",circular_u_end,105);
01603 
01604   // For top segment, start is left most led and end is right most led of the segment
01605   // Point of reference is from facing the segment
01606   privateNode.param<int>("top_of_u_start",top_of_u_start,25);
01607   privateNode.param<int>("top_of_u_end",top_of_u_end,34);
01608 
01609   // Initial connection to microcontroller
01610 
01611   // Service Server advertisers
01612   ros::ServiceServer clear_service = n.advertiseService("led_clear", clear_strip);
01613   ros::ServiceServer test_strip_service = n.advertiseService("led_test", test_strip);
01614   ros::ServiceServer set_status_service = n.advertiseService("led_set_status", set_status);
01615 
01616   // Action Server advertisers
01617   LEDControlAction led_control_server(ros::this_node::getName());
01618 
01619   ros::Rate r(10); // 10 hz
01620 
01621   while(ros::ok())
01622   {
01623       if(!connected)
01624       {
01625         connect(serial_port, 115200);
01626       }
01627       ros::spinOnce();
01628       r.sleep();
01629   }
01630 
01631   return 0;
01632 }


segbot_led
Author(s): Pato Lankenau , Rolando Fernandez
autogenerated on Thu Jun 6 2019 21:37:07