root / scout / cliffsensor / src / cliffsensor.cpp @ 3493418f
History | View | Annotate | Download (3.96 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 | c492be62 | Alex Zirbel | */
|
25 | 04f50f8a | Priya | |
26 | /**
|
||
27 | * @file cliffsensor.cpp
|
||
28 | * @brief Contains code to control the cliffsensor.
|
||
29 | *
|
||
30 | * Implementation of functions for cliffsensor use.
|
||
31 | *
|
||
32 | * @author Colony Project, CMU Robotics Club
|
||
33 | * @author Priyanka Deo
|
||
34 | **/
|
||
35 | |||
36 | #include "ros/ros.h" |
||
37 | #include "cliffsensor.h" |
||
38 | #include <cstdlib> |
||
39 | |||
40 | /**
|
||
41 | * @defgroup cliffsensor Cliffsensor
|
||
42 | * @brief Functions for using the cliffsensors
|
||
43 | *
|
||
44 | * @{
|
||
45 | **/
|
||
46 | |||
47 | /* Cliffsensor state variables
|
||
48 | */
|
||
49 | static int front_raw; /**< The current raw value data of the front cliffsensor. */ |
||
50 | static int left_raw; /**< The current raw value data of the left cliffsensor. */ |
||
51 | static int back_raw; /**< The current raw value data of the right cliffsensor. */ |
||
52 | static bool is_cliff; /**< Boolean to represent whether or not there is a cliff. */ |
||
53 | |||
54 | /**
|
||
55 | 75a7571a | Priya | * @brief Changes cliff sensor status
|
56 | 04f50f8a | Priya | *
|
57 | 75a7571a | Priya | * Changes cliff sensor status based on subscription to topic cliff_status_changed
|
58 | 04f50f8a | Priya | *
|
59 | 75a7571a | Priya | * @param msg The message from the cliff_status_changed topic, containing
|
60 | * status of all cliff sensors.
|
||
61 | 04f50f8a | Priya | */
|
62 | 75a7571a | Priya | void changed_cliff_status(const cliffsensor::cliff_status_changed::ConstPtr& msg) |
63 | 04f50f8a | Priya | { |
64 | 75a7571a | Priya | return;
|
65 | 04f50f8a | Priya | } |
66 | |||
67 | /**
|
||
68 | 75a7571a | Priya | * @brief Outputs status of cliffsensors
|
69 | 04f50f8a | Priya | *
|
70 | 75a7571a | Priya | * Serves the service query_cliff by responding to service requests with the
|
71 | * status of the cliff sensors.
|
||
72 | 04f50f8a | Priya | * @param req The request. The only field is the units requested.
|
73 | * @param res The response. The fields will be filled with values.
|
||
74 | */
|
||
75 | 75a7571a | Priya | bool cliff_query(cliffsensor::query_cliff::Request &req,
|
76 | cliffsensor::query_cliff::Response &res) |
||
77 | 04f50f8a | Priya | { |
78 | 75a7571a | Priya | int threshold = 500; //TODO fix after figuring out what sensors give us |
79 | res.front_raw = front_raw; |
||
80 | res.left_raw = left_raw; |
||
81 | res.right_raw = back_raw; |
||
82 | res.cliff_status = is_cliff; |
||
83 | 04f50f8a | Priya | |
84 | 75a7571a | Priya | ROS_DEBUG("Cliffsensor status queried");
|
85 | 04f50f8a | Priya | return true; |
86 | } |
||
87 | |||
88 | /**
|
||
89 | 75a7571a | Priya | * @brief Cliffsensor driver. This is a ROS node that controls cliffsensor status.
|
90 | 04f50f8a | Priya | *
|
91 | 75a7571a | Priya | * This is the main function for the cliffsensors node. It is run when the node
|
92 | * starts and initializes the cliffsensors. It then subscribes to the
|
||
93 | * cliff_status_changed topics, and advertises the
|
||
94 | * query_cliff service.
|
||
95 | 04f50f8a | Priya | *
|
96 | * @param argc The number of command line arguments (should be 1)
|
||
97 | * @param argv The array of command line arguments
|
||
98 | **/
|
||
99 | int main(int argc, char **argv) |
||
100 | { |
||
101 | 75a7571a | Priya | /* Initialize in ROS the cliffsensor driver node */
|
102 | ros::init(argc, argv, "cliffsensor_driver");
|
||
103 | 04f50f8a | Priya | |
104 | 75a7571a | Priya | /* Advertise that this serves the query_cliff service */
|
105 | 04f50f8a | Priya | ros::NodeHandle n; |
106 | 75a7571a | Priya | ros::ServiceServer service = n.advertiseService("query_cliff",
|
107 | cliff_query); |
||
108 | 04f50f8a | Priya | |
109 | 75a7571a | Priya | /* Subscribe to the cliff_status_changed topic */
|
110 | ros::Subscriber sub0 = n.subscribe("cliff_status_changed",
|
||
111 | 4, changed_cliff_status);
|
||
112 | 04f50f8a | Priya | |
113 | 75a7571a | Priya | /* Initialize hardware for cliffsensors */
|
114 | 04f50f8a | Priya | // Hardware init functions here
|
115 | |||
116 | 75a7571a | Priya | ROS_INFO("Ready to query cliffsensors.");
|
117 | 04f50f8a | Priya | ros::spin(); |
118 | |||
119 | return 0; |
||
120 | } |
||
121 | |||
122 | /** @} **/ |