Project

General

Profile

Revision c492be62

IDc492be629f06e3cf71fdf832ce2cd5ac1ec779f8

Added by Alex Zirbel over 12 years ago

Updated the licensing information in many files.

This is a broken commit because I decided to do this at a bad time. Sorry! The build will be working after next commit, I promise (and I won't push till then). This commit reflects the licensing in the files after I used my auto-add/remove script.

View differences:

scout/libscout/src/SonarControl.cpp
21 21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
24
 */
25 25

  
26 26
/**
27 27
 * @file SonarControl.cpp
28
 * @brief Contains sonar function imeplementations
28
 * @brief Contains sonar function implementation
29 29
 * 
30 30
 * Contains functions for the use of sonar control.
31 31
 *
32 32
 * @author Colony Project, CMU Robotics Club
33 33
 * @author Priyanka Deo
34
 * @author Alex Zirbel
34 35
 **/
35 36

  
36 37
#include "SonarControl.h"
37 38

  
39
using namespace std;
40

  
38 41
/**
39 42
 * @brief Initialize the sonar module of libscout.
40 43
 *
41 44
 * Initialize the libscout node as a publisher of sonar_set_scan and 
42 45
 * sonar_toggle and a client of query_sonar.
43 46
 */
44
SonarControl::SonarControl(const ros::NodeHandle& libscout_node)
47
SonarControl::SonarControl(const ros::NodeHandle& libscout_node,
48
                           string scoutname)
45 49
    : node(libscout_node)
46 50
{
47 51
    sonar_set_scan_pub =
48
        node.advertise<sonar::sonar_set_scan>("set_sonar_scan", QUEUE_SIZE);
52
        node.advertise<sonar::sonar_set_scan>(scoutname + "/set_sonar_scan",
53
                                              QUEUE_SIZE);
49 54
    sonar_toggle_pub = 
50
	node.advertise<sonar::sonar_toggle>("toggle_sonar", QUEUE_SIZE);
51
    query_sonar =
52
        node.serviceClient<sonar::sonar_direction>("query_sonar");
55
        node.advertise<sonar::sonar_toggle>(scoutname + "/toggle_sonar",
56
                                            QUEUE_SIZE);
57
    sonar_distance_sub = node.subscribe(scoutname + "sonar_distance",
58
                                        QUEUE_SIZE, distance_callback);
59

  
60
    // Initialize all the sonar readings to 0 and timestamps to undefined
61
    for (int i = 0; i < 48; i++)
62
    {
63
        readings[i] = 0;
64
        timestamps[i] = ros::Time::now(); // A common shortcut for no reading
65
    }
66
}
67

  
68
/**
69
 * Update the array of sonar values, and the last read timestamps,
70
 * to reflect the new reading received.
71
 */
72
void SonarControl::distance_callback(const sonar::sonar_distance::ConstPtr& msg)
73
{
74
    // Error checking so that the array doesn't cause a segfault
75
    if (msg->pos < 0 || msg-> pos > 23)
76
    {
77
        ROS_ERROR("SonarControl received an invalid sonar position.");
78
        return;
79
    }
80

  
81
    readings[msg->pos] = msg->distance0;
82
    readings[msg->pos + 24] = msg->distance1;
83
    timestamps[msg->pos] = msg->header.stamp;
84
    timestamps[msg->pos + 24] = msg->header.stamp;
53 85
}
54 86

  
55 87
/**
......
57 89
 *
58 90
 * @param position Value between 0-180 of degree position to set sonar
59 91
 * @return NONE
92
 * @todo should return bool. See below.
60 93
 */
61
void SonarControl::set(int position)
94
void SonarControl::set_single(int position)
62 95
{
63 96
    set_range(position, position);
64 97
}
......
69 102
 * @param start_pos The leftmost (smallest) value that the sonar can take
70 103
 * @param end_pos The rightmost (largest) value that the sonar can take
71 104
 * @return NONE
105
 * @todo Also misimplemented, should return bool. Look at srv def in sonar/
72 106
 */
73 107
void SonarControl::set_range(int start_pos, int end_pos)
74 108
{
75
    /** Turn sonar on */
109
    // Check that the range is valid
110
    if (start_pos < 0 || end_pos < 0 || start_pos > 23 || end_pos > 23)
111
    {
112
        ROS_ERROR("Commanded SonarControl::set_range to a bad value.");
113
        return;
114
    }
115

  
116
    // Sort the start and end positions into increasing order
117
    if (start_pos > end_pos)
118
    {
119
        int temp = start_pos;
120
        start_pos = end_pos;
121
        end_pos = temp;
122
    }
123

  
124
    // Turn sonar on
76 125
    sonar::sonar_toggle on_msg;
77 126
    on_msg.set_on = true;
78 127
    sonar_toggle_pub.publish(on_msg);
79 128

  
80
    /** Set scan range */
129
    // Set scan range
81 130
    sonar::sonar_set_scan msg;
82

  
83
    //TODO Enable some checks of start and end position
84 131
    msg.stop_l = start_pos;
85 132
    msg.stop_r = end_pos;
86 133

  
87 134
    sonar_set_scan_pub.publish(msg);
88
    ros::spinOnce();
135
    ros::spinOnce();  ///<! Sure? Does this belong here?
89 136
}
90 137

  
91 138
/**
92 139
 * @brief Turn off sonar readings
93 140
 *
94 141
 * Stops sonar from panning and taking readings.
142
 * @todo Misimplemented. Needs to use a service call and return the ack as bool
95 143
 *
96 144
 * @return NONE
97 145
 */
98 146
float SonarControl::set_off()
99 147
{
100
    /** Turn sonar off */
101 148
    sonar::sonar_toggle on_msg;
102 149
    on_msg.set_on = false;
103 150
    sonar_toggle_pub.publish(on_msg);
104 151
}
105
/**
106
 * @brief Query the current front sonar readings
107
 *
108
 * Sends a request to the query_sonar service which will reply with the
109
 *  current readings of the sonar.
110
 *
111
 * @TODO Change so we can get multiple sonar readings with one call
112
 *
113
 * @return The reading of the front sonar
114
 */
115
float SonarControl::query_front()
116
{
117
    sonar::sonar_direction srv;
118
    if(query_sonar.call(srv))
119
    {
120
        srv.response.distance0;
121
    }
122
    else
123
    {
124
        ROS_FATAL("Failed to call service query_motors");
125
    }
126

  
127
    return 0;
128
}
129

  
130
/**
131
 * @brief Query the current back sonar readings
132
 *
133
 * Sends a request to the query_sonar service which will reply with the
134
 *  current readings of the sonar.
135
 *
136
 * @TODO Change so we can get multiple sonar readings with one call
137
 *
138
 * @return The reading of the back sonar
139
 */
140
float SonarControl::query_back()
141
{
142
    sonar::sonar_direction srv;
143
    if(query_sonar.call(srv))
144
    {
145
        srv.response.distance1;
146
    }
147
    else
148
    {
149
        ROS_FATAL("Failed to call service query_motors");
150
    }
151

  
152
    return 0;
153
}
154 152

  
155 153
/**
156 154
 * @brief Converts value returne by sonar to physical distances.
......
158 156
 * @param sonar_value The returned value of the sonar
159 157
 * @return The physical distance measured by the sonar.
160 158
 **/
161
float sonar_to_dist(float sonar_value)
162
{
159
//float sonar_to_dist(float sonar_value)
160
//{
163 161
    //TODO impelement later based on sonar readings
164
    return sonar_value;
165
}
162
//    return sonar_value;
163
//}
166 164

  
167 165
/**
168 166
 * @brief Converts values from physical distances to values read by sonar
......
170 168
 * @param distance The physical distance as measured.
171 169
 * @return The value read by the sonar that corresponds to the given distance
172 170
 **/
173
float dist_to_sonar(float distance)
174
{
171
//float dist_to_sonar(float distance)
172
//{
175 173
    //TODO implement later based on sonar readings
176
    return distance;
177
}
174
//    return distance;
175
//}

Also available in: Unified diff