Statistics
| Branch: | Revision:

scoutos / scout / libscout / src / HeadlightControl.cpp @ 3a73516c

History | View | Annotate | Download (3.67 KB)

1
/**
2
 * Copyright (c) 2011 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 */
25

    
26
/**
27
 * @file HeadlightControl.cpp
28
 * @brief Contains headlights declarations and functions
29
 * 
30
 * @defgroup headlightcontrol HeadlightControl
31
 * @brief Functions which a behavior can use to control the headlights.
32
 * @ingroup behavior
33
 *
34
 * @author Colony Project, CMU Robotics Club
35
 * @author Ben Wasserman
36
 * @author Alex Zirbel
37
 */
38

    
39
#include "HeadlightControl.h"
40

    
41
using namespace std;
42

    
43
/** ROS publisher and client declaration */
44

    
45
/**
46
 * @brief Initialize the headlights module of libscout.
47
 *
48
 * Initialize the libscout node as a publisher of set_headlights.
49
 */
50
HeadlightControl::HeadlightControl(const ros::NodeHandle& libscout_node,
51
                                   string scoutname)
52
    : node(libscout_node)
53
{
54
    set_headlights_pub = node.advertise< ::messages::set_headlights>
55
        (scoutname + "/set_headlights", QUEUE_SIZE, true);
56
}
57

    
58
/**
59
 * @brief Set headlight colors as a single RGB value
60
 *
61
 * Sets the colors of the headlights as a web color value. Can selectively
62
 * select which motors to set, and which to remain at previous color.
63
 * @param color The color the headlights should be set to expressed as 0xRRGGBB
64
 * @param which A bitmask of which headlights should be set
65
 */
66
void HeadlightControl::set(int color, int which)
67
{
68
    int red, green, blue;
69

    
70
    red = (color & RED) >> REDSHIFT;
71
    green = (color & GREEN) >> GREENSHIFT;
72
    blue = (color & BLUE) >> BLUESHIFT;
73

    
74
    set_rgb(red, green, blue, which);
75
}
76

    
77
/**
78
 * @brief Set headlight colors as separate RGB values
79
 *
80
 * Sets the colors of the headlights as a web color value. Can selectively
81
 * select which motors to set, and which to remain at previous color.
82
 * @param red The red value the headlights should be set to
83
 * @param green The green value the headlights should be set to
84
 * @param blue The blue value the headlights should be set to
85
 * @param which A bitmask of which headlights should be set
86
 */
87
void HeadlightControl::set_rgb(int red, int green, int blue, int which)
88
{
89
    ::messages::set_headlights msg;
90

    
91
    if(which & HL_LEFT)
92
    {
93
        msg.left_red = red;
94
        msg.left_green = green;
95
        msg.left_blue = blue;
96
    }
97
    else
98
    {
99
        msg.left_red = NO_SET;
100
        msg.left_green = NO_SET;
101
        msg.left_blue = NO_SET;
102
    }
103
    if(which & HL_RIGHT)
104
    {
105
        msg.right_red = red;
106
        msg.right_green = green;
107
        msg.right_blue = blue;
108
    }
109
    else
110
    {
111
        msg.right_red = NO_SET;
112
        msg.right_green = NO_SET;
113
        msg.right_blue = NO_SET;
114
    }
115

    
116
    /* Publishes message to set_motors topic */
117
    set_headlights_pub.publish(msg);
118
    ros::spinOnce();
119
}
120

    
121
/** @} */