Project

General

Profile

Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (6.27 KB)

1 65c7f52b pdeo
/**
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 65c7f52b pdeo
26
/**
27
 * @file SonarControl.cpp
28 c492be62 Alex Zirbel
 * @brief Contains sonar function implementation
29 65c7f52b pdeo
 * 
30 3a73516c Alex
 * @defgroup sonarcontrol SonarControl
31
 * @brief Functions which a behavior can use to control the sonar.
32
 * @ingroup behavior
33 65c7f52b pdeo
 *
34
 * @author Colony Project, CMU Robotics Club
35
 * @author Priyanka Deo
36 c492be62 Alex Zirbel
 * @author Alex Zirbel
37 3a73516c Alex
 *
38
 * @{
39 65c7f52b pdeo
 **/
40
41
#include "SonarControl.h"
42
43 b00761a0 Alex Zirbel
#define MAX_ATTEMPTS 10
44
45 c492be62 Alex Zirbel
using namespace std;
46
47 65c7f52b pdeo
/**
48
 * @brief Initialize the sonar module of libscout.
49
 *
50
 * Initialize the libscout node as a publisher of sonar_set_scan and 
51
 * sonar_toggle and a client of query_sonar.
52
 */
53 c492be62 Alex Zirbel
SonarControl::SonarControl(const ros::NodeHandle& libscout_node,
54
                           string scoutname)
55 65c7f52b pdeo
    : node(libscout_node)
56
{
57 b00761a0 Alex Zirbel
    sonar_set_scan_client =
58 071926c2 Alex
        node.serviceClient< ::messages::sonar_set_scan>(scoutname+"/set_sonar_scan");
59 b00761a0 Alex Zirbel
    sonar_toggle_client = 
60 071926c2 Alex
        node.serviceClient< ::messages::sonar_toggle>(scoutname + "/toggle_sonar");
61 b00761a0 Alex Zirbel
    sonar_distance_sub = node.subscribe(scoutname + "/sonar_distance",
62
                                        QUEUE_SIZE,
63
                                        &SonarControl::distance_callback,
64
                                        this);
65 c492be62 Alex Zirbel
66
    // Initialize all the sonar readings to 0 and timestamps to undefined
67
    for (int i = 0; i < 48; i++)
68
    {
69
        readings[i] = 0;
70
        timestamps[i] = ros::Time::now(); // A common shortcut for no reading
71
    }
72 b00761a0 Alex Zirbel
73 9d9e04ba Alex
    ROS_INFO("Readings at 0, ready to go.");
74
75 b00761a0 Alex Zirbel
    /// @todo Test, and replace 10 with a const int
76
    num_attempts = MAX_ATTEMPTS;
77 c492be62 Alex Zirbel
}
78
79
/**
80
 * Update the array of sonar values, and the last read timestamps,
81
 * to reflect the new reading received.
82
 */
83 071926c2 Alex
void SonarControl::distance_callback(const ::messages::sonar_distance::ConstPtr& msg)
84 c492be62 Alex Zirbel
{
85
    // Error checking so that the array doesn't cause a segfault
86
    if (msg->pos < 0 || msg-> pos > 23)
87
    {
88
        ROS_ERROR("SonarControl received an invalid sonar position.");
89
        return;
90
    }
91
92
    readings[msg->pos] = msg->distance0;
93
    readings[msg->pos + 24] = msg->distance1;
94 c06735bb Hui Jun Tay
    timestamps[msg->pos] = msg->stamp;
95
    timestamps[msg->pos + 24] = msg->stamp;
96 65c7f52b pdeo
}
97
98
/**
99
 * @brief Sets the sonar to a position.
100
 *
101
 * @param position Value between 0-180 of degree position to set sonar
102
 */
103 c492be62 Alex Zirbel
void SonarControl::set_single(int position)
104 65c7f52b pdeo
{
105
    set_range(position, position);
106
}
107
108
/**
109
 * @brief Sets the sonar to scan between two positions
110
 *
111
 * @param start_pos The leftmost (smallest) value that the sonar can take
112
 * @param end_pos The rightmost (largest) value that the sonar can take
113
 */
114
void SonarControl::set_range(int start_pos, int end_pos)
115
{
116 c492be62 Alex Zirbel
    // Check that the range is valid
117
    if (start_pos < 0 || end_pos < 0 || start_pos > 23 || end_pos > 23)
118
    {
119
        ROS_ERROR("Commanded SonarControl::set_range to a bad value.");
120
        return;
121
    }
122
123
    // Sort the start and end positions into increasing order
124
    if (start_pos > end_pos)
125
    {
126
        int temp = start_pos;
127
        start_pos = end_pos;
128
        end_pos = temp;
129
    }
130
131 b00761a0 Alex Zirbel
    // Make sure sonar is on
132
    set_on();
133 15b7e607 pdeo
134 c492be62 Alex Zirbel
    // Set scan range
135 071926c2 Alex
    ::messages::sonar_set_scan srv;
136 b00761a0 Alex Zirbel
    srv.request.stop_l = start_pos;
137
    srv.request.stop_r = end_pos;
138
139
    // Check if the service call failed or if the response was false
140
    if (!sonar_set_scan_client.call(srv) || srv.response.ack == false)
141
    {
142
        if (--num_attempts == 0)
143
        {
144
            ROS_ERROR("SonarControl::set_range() failed permanently.");
145
        }
146 65c7f52b pdeo
147 b00761a0 Alex Zirbel
        ROS_WARN("SonarControl::set_range() failed once.");
148
    }
149
}
150
151
/**
152
 * @brief Turn on sonar readings
153
 *
154
 * (Re)starts sonar panning and taking readings.
155
 */
156
void SonarControl::set_on()
157
{
158
    set_power(true);
159 65c7f52b pdeo
}
160
161
/**
162 15b7e607 pdeo
 * @brief Turn off sonar readings
163
 *
164
 * Stops sonar from panning and taking readings.
165 b00761a0 Alex Zirbel
 */
166
void SonarControl::set_off()
167
{
168
    set_power(false);
169
}
170
171
/**
172
 * @brief Turn sonar readings either off or on.
173
 *
174
 * Attempts to turn the readings off or on, num_attempts times.
175 15b7e607 pdeo
 *
176 b00761a0 Alex Zirbel
 * @param is_on True if power should be set on, false if should be off.
177 15b7e607 pdeo
 */
178 b00761a0 Alex Zirbel
void SonarControl::set_power(bool is_on)
179 15b7e607 pdeo
{
180 071926c2 Alex
    ::messages::sonar_toggle srv;
181 b00761a0 Alex Zirbel
    srv.request.set_on = is_on;
182
183
    // Check if the service call failed or if the response was false
184
    if (!sonar_toggle_client.call(srv) || srv.response.ack == false)
185
    {
186
        if (--num_attempts == 0)
187
        {
188
            ROS_ERROR("SonarControl::set_power() failed permanently.");
189
        }
190
191
        ROS_WARN("SonarControl::set_power() failed once.");
192
    }
193 9d9e04ba Alex
    ROS_INFO("SonarControl::set_power() succeeded.");
194 b00761a0 Alex Zirbel
195
    // Reset num_attempts
196
    num_attempts = MAX_ATTEMPTS;
197 15b7e607 pdeo
}
198 65c7f52b pdeo
199 3db79f25 Priya
int* SonarControl::get_sonar_readings()
200
{
201
  return readings;
202
}
203
204 65c7f52b pdeo
/**
205
 * @brief Converts value returne by sonar to physical distances.
206
 *
207
 * @param sonar_value The returned value of the sonar
208
 * @return The physical distance measured by the sonar.
209
 **/
210 c492be62 Alex Zirbel
//float sonar_to_dist(float sonar_value)
211
//{
212 65c7f52b pdeo
    //TODO impelement later based on sonar readings
213 c492be62 Alex Zirbel
//    return sonar_value;
214
//}
215 65c7f52b pdeo
216
/**
217
 * @brief Converts values from physical distances to values read by sonar
218
 *
219
 * @param distance The physical distance as measured.
220
 * @return The value read by the sonar that corresponds to the given distance
221
 **/
222 c492be62 Alex Zirbel
//float dist_to_sonar(float distance)
223
//{
224 65c7f52b pdeo
    //TODO implement later based on sonar readings
225 c492be62 Alex Zirbel
//    return distance;
226
//}
227 3a73516c Alex
228
/** @} */