root / scout / buttons / src / buttons.cpp @ 8de28e68
History | View | Annotate | Download (3.54 KB)
1 | 04f50f8a | Priya | /**
|
---|---|---|---|
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 buttons.cpp
|
||
28 | * @brief Contains code to control the buttons.
|
||
29 | *
|
||
30 | * Implementation of functions for button use.
|
||
31 | *
|
||
32 | * @author Colony Project, CMU Robotics Club
|
||
33 | * @author Priyanka Deo
|
||
34 | **/
|
||
35 | |||
36 | #include "ros/ros.h" |
||
37 | #include "buttons.h" |
||
38 | #include <cstdlib> |
||
39 | |||
40 | /**
|
||
41 | * @defgroup buttons Buttons
|
||
42 | * @brief Functions for using the buttons
|
||
43 | *
|
||
44 | * @{
|
||
45 | **/
|
||
46 | |||
47 | /* Button state variables
|
||
48 | */
|
||
49 | static int button1_pressed; /**< Whether or not button 1 is pressed. */ |
||
50 | static int button2_pressed; /**< Whether or not button 2 is pressed. */ |
||
51 | |||
52 | /**
|
||
53 | 8de28e68 | Priya | * @brief Handles button events
|
54 | 04f50f8a | Priya | *
|
55 | 8de28e68 | Priya | * Handles button events based on subscription to the button_event topic.
|
56 | 04f50f8a | Priya | *
|
57 | 8de28e68 | Priya | * @param msg The message from the button_event topic, containing buttons
|
58 | * statuses.
|
||
59 | 04f50f8a | Priya | */
|
60 | 8de28e68 | Priya | void event_button(const buttons::button_event::ConstPtr& msg) |
61 | 04f50f8a | Priya | { |
62 | 8de28e68 | Priya | //TODO: What goes here?
|
63 | 04f50f8a | Priya | } |
64 | |||
65 | 8de28e68 | Priya | |
66 | 04f50f8a | Priya | /**
|
67 | 8de28e68 | Priya | * @brief Outputs whether or not the buttons are pressed.
|
68 | 04f50f8a | Priya | *
|
69 | 8de28e68 | Priya | * Serves the service query_buttons by responding to service requests
|
70 | * with the status of the buttons
|
||
71 | 04f50f8a | Priya | * @param req The request. The only field is the units requested.
|
72 | * @param res The response. The fields will be filled with values.
|
||
73 | */
|
||
74 | 8de28e68 | Priya | bool buttons_query(buttons::query_buttons::Request &req,
|
75 | buttons::query_buttons::Response &res) |
||
76 | 04f50f8a | Priya | { |
77 | int units = req.units;
|
||
78 | 8de28e68 | Priya | res.button1_pressed = button1_pressed; |
79 | res.button2_pressed = button2_pressed; |
||
80 | 04f50f8a | Priya | |
81 | 8de28e68 | Priya | ROS_DEBUG("Button status queried");
|
82 | 04f50f8a | Priya | return true; |
83 | } |
||
84 | |||
85 | |||
86 | /**
|
||
87 | 8de28e68 | Priya | * @brief Buttons driver. This is a ROS node that queries button status.
|
88 | 04f50f8a | Priya | *
|
89 | 8de28e68 | Priya | * This is the main function for the buttons node. It is run
|
90 | * when the node starts and initializes the buttons. It then
|
||
91 | * subscribes to the event_button topics, and advertises the
|
||
92 | * query_buttons service.
|
||
93 | 04f50f8a | Priya | *
|
94 | * @param argc The number of command line arguments (should be 1)
|
||
95 | * @param argv The array of command line arguments
|
||
96 | **/
|
||
97 | int main(int argc, char **argv) |
||
98 | { |
||
99 | 8de28e68 | Priya | /* Initialize in ROS the buttons driver node */
|
100 | ros::init(argc, argv, "buttons_driver");
|
||
101 | 04f50f8a | Priya | |
102 | 8de28e68 | Priya | /* Advertise that this serves the query_buttons service */
|
103 | 04f50f8a | Priya | ros::NodeHandle n; |
104 | 8de28e68 | Priya | ros::ServiceServer service = n.advertiseService("query_buttons",
|
105 | buttons_query); |
||
106 | 04f50f8a | Priya | |
107 | 8de28e68 | Priya | /* Subscribe to the button_event topic */
|
108 | ros::Subscriber sub0 = n.subscribe("button_event", 4, event_button); |
||
109 | 04f50f8a | Priya | |
110 | 8de28e68 | Priya | /* Initialize hardware for buttons */
|
111 | 04f50f8a | Priya | // Hardware init functions here
|
112 | |||
113 | 8de28e68 | Priya | ROS_INFO("Ready to query buttons.");
|
114 | 04f50f8a | Priya | ros::spin(); |
115 | |||
116 | return 0; |
||
117 | } |
||
118 | |||
119 | /** @} **/ |