scoutos / scout / libscout / src / libbuttons.cpp @ 76a1593f
History | View | Annotate | Download (3.51 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 |
/**
|
28 |
* @file libbuttons.cpp
|
29 |
* @brief Contains buttons declarations and functions
|
30 |
*
|
31 |
* Contains functions and definitions for the use of
|
32 |
* buttons
|
33 |
*
|
34 |
* @author Colony Project, CMU Robotics Club
|
35 |
**/
|
36 |
|
37 |
/* Author: Priyanka Deo
|
38 |
*/
|
39 |
|
40 |
#include "libbuttons.h" |
41 |
|
42 |
|
43 |
/* ROS node created in libscout.cpp */
|
44 |
extern ros::NodeHandle libscout_node;
|
45 |
|
46 |
/** ROS publisher and client declaration */
|
47 |
ros::Publisher button_event_publisher; |
48 |
ros::ServiceClient query_buttons_client; |
49 |
|
50 |
/*!
|
51 |
* \brief Initialize the buttons module of libscout.
|
52 |
*
|
53 |
* Initialize the libscout node as a publisher of button_event and a client of
|
54 |
* query_buttons.
|
55 |
*/
|
56 |
void libbuttons_init()
|
57 |
{ |
58 |
button_event_publisher = libscout_node.advertise<buttons::button_event>("libbuttons", 10); |
59 |
query_buttons_client = libscout_node.serviceClient<buttons::query_buttons>("libbuttons");
|
60 |
} |
61 |
|
62 |
/*!
|
63 |
* \brief Respond to button events
|
64 |
* Sends out button events if buttons are pressed. Tells which buttons
|
65 |
* have been pressed.
|
66 |
* \param which_button - Which button is pressed
|
67 |
* \param which A bitmask of which motors should be set
|
68 |
* \return Function status
|
69 |
*/
|
70 |
int event_button(int which_button) |
71 |
{ |
72 |
if(!ros::ok())
|
73 |
{ |
74 |
return LIB_ERROR;
|
75 |
} |
76 |
|
77 |
/* Create message */
|
78 |
buttons::button_event msg; |
79 |
switch(which_button)
|
80 |
{ |
81 |
case BUTTON1:
|
82 |
msg.button1_pressed = BUTTON_PRESSED; |
83 |
break;
|
84 |
case BUTTON2:
|
85 |
msg.button2_pressed = BUTTON_PRESSED; |
86 |
break;
|
87 |
default:
|
88 |
ROS_ERROR("Button %d does not exist\n", which_button);
|
89 |
return LIB_ERROR;
|
90 |
} |
91 |
|
92 |
/* Publishes message to button_event topic */
|
93 |
button_event_publisher.publish(msg); |
94 |
ros::spinOnce(); |
95 |
|
96 |
return LIB_OK;
|
97 |
} |
98 |
|
99 |
/*!
|
100 |
* \brief Query the current status of the buttons
|
101 |
*
|
102 |
* Sends a request to the query_buttons service which will reply with the
|
103 |
* current status of the buttons
|
104 |
*
|
105 |
* \param which_button - Which button we are querying
|
106 |
* \return status of the button queried, or LIB_ERROR on error
|
107 |
*/
|
108 |
|
109 |
int buttons_query(int which_button) |
110 |
{ |
111 |
buttons::query_buttons srv; |
112 |
if(query_buttons_client.call(srv))
|
113 |
{ |
114 |
switch(which_button)
|
115 |
{ |
116 |
case BUTTON1:
|
117 |
return srv.response.button1_pressed;
|
118 |
case BUTTON2:
|
119 |
return srv.response.button2_pressed;
|
120 |
default:
|
121 |
ROS_ERROR("Button %d does not exist", which_button);
|
122 |
return LIB_ERROR;
|
123 |
} |
124 |
} |
125 |
else
|
126 |
{ |
127 |
ROS_ERROR("Failed to call service query_buttons");
|
128 |
return LIB_ERROR;
|
129 |
} |
130 |
|
131 |
/* Code never reaches here */
|
132 |
return 0; |
133 |
} |