00001
00002
00003
00004 #include <unistd.h>
00005 #include <signal.h>
00006
00007
00008
00009
00010 #include <actionlib/server/simple_action_server.h>
00011
00012
00013
00014
00015 #include "ledcom.h"
00016
00017
00018
00019
00020 #include "bwi_msgs/LEDClear.h"
00021 #include "bwi_msgs/LEDSetStatus.h"
00022 #include "bwi_msgs/LEDTestStrip.h"
00023
00024
00025
00026
00027 #include "bwi_msgs/LEDControlAction.h"
00028
00029
00030
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
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
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
00087
00088
00089 void check_run_status()
00090 {
00091 if(run_on)
00092 {
00093 leds.clear();
00094
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
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
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
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
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
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
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
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
00241 while(as_.isActive())
00242 {
00243
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
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
00280 switch(goal->type.led_animations)
00281 {
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 case bwi_msgs::LEDAnimations::LEFT_TURN:
00422 {
00423
00424 while(!as_.isPreemptRequested() && !timeout && ros::ok())
00425 {
00426
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
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
00448 usleep(400000);
00449
00450 leds.clear();
00451
00452 usleep(100000);
00453 }
00454 break;
00455 }
00456
00457
00458 case bwi_msgs::LEDAnimations::RIGHT_TURN:
00459 {
00460
00461 while(!as_.isPreemptRequested() && !timeout && ros::ok())
00462 {
00463
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
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
00485 usleep(400000);
00486
00487 leds.clear();
00488
00489 usleep(100000);
00490 }
00491 break;
00492 }
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933 case bwi_msgs::LEDAnimations::REVERSE:
00934 {
00935
00936 while(!as_.isPreemptRequested() && !timeout && ros::ok())
00937 {
00938
00939
00940 ros::Duration time_running = ros::Time::now() - start;
00941 feedback_.time_running = time_running;
00942 as_.publishFeedback(feedback_);
00943
00944
00945 for (float b = 0.0; b < 0.5; b += 0.02)
00946 {
00947
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
00969 usleep(22500);
00970 }
00971
00972 usleep(500000);
00973
00974
00975 for (float b = 0.5; b >= 0.0; b -= 0.02)
00976 {
00977
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
00999 usleep(22500);
01000 }
01001
01002 usleep(500000);
01003 }
01004 break;
01005 }
01006
01007
01008 case bwi_msgs::LEDAnimations::BLOCKED:
01009 {
01010
01011 while(!as_.isPreemptRequested() && !timeout && ros::ok())
01012 {
01013
01014
01015 ros::Duration time_running = ros::Time::now() - start;
01016 feedback_.time_running = time_running;
01017 as_.publishFeedback(feedback_);
01018
01019
01020 for (float b = 0.0; b <= 0.5; b += 0.02)
01021 {
01022
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
01032 if (b != 0.5) { usleep(60000); }
01033 }
01034
01035 usleep(45000);
01036
01037
01038 for (float b = 0.5; b >= 0.0; b -= 0.02)
01039 {
01040
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
01050 if (b != 0.0) { usleep(60000); }
01051 }
01052
01053 usleep(45000);
01054 }
01055 break;
01056 }
01057
01058 case bwi_msgs::LEDAnimations::UP:
01059 {
01060
01061 while(!as_.isPreemptRequested() && !timeout && ros::ok())
01062 {
01063
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
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
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
01158 usleep(100000);
01159 }
01160 }
01161 }
01162 break;
01163 }
01164
01165 case bwi_msgs::LEDAnimations::DOWN:
01166 {
01167
01168 while(!as_.isPreemptRequested() && !timeout && ros::ok())
01169 {
01170
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
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
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
01266 usleep(100000);
01267 }
01268 }
01269 }
01270 break;
01271 }
01272
01273
01274 case bwi_msgs::LEDAnimations::NEED_ASSIST:
01275 {
01276
01277 while(!as_.isPreemptRequested() && !timeout && ros::ok())
01278 {
01279
01280
01281 ros::Duration time_running = ros::Time::now() - start;
01282 feedback_.time_running = time_running;
01283 as_.publishFeedback(feedback_);
01284
01285
01286 for (float b = 0.0; b <= 0.3; b += 0.02)
01287 {
01288
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
01298 if (b != 0.3) { usleep(70000); }
01299 }
01300
01301 usleep(75000);
01302
01303
01304 for (float b = 0.3; b >= 0.0; b -= 0.02)
01305 {
01306
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
01316 if (b != 0.0) { usleep(70000); }
01317 }
01318
01319 usleep(75000);
01320 }
01321 break;
01322 }
01323 }
01324
01325
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
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
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
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
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
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
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
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
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
01554
01555 signal(SIGINT, ledSigintHandler);
01556
01557 ros::NodeHandle privateNode("~");
01558
01559
01560
01561 privateNode.param<int>("led_count",led_count,60);
01562 privateNode.param<string>("serial_port",serial_port,"/dev/metromini");
01563
01564
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
01572
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
01586
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
01600
01601 privateNode.param<int>("circular_u_start",circular_u_start,75);
01602 privateNode.param<int>("circular_u_end",circular_u_end,105);
01603
01604
01605
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
01610
01611
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
01617 LEDControlAction led_control_server(ros::this_node::getName());
01618
01619 ros::Rate r(10);
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 }