Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / sonar / src / sonar.cpp @ b00761a0

History | View | Annotate | Download (3.79 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
 * @file sonar.cpp
28
 * @brief Sonar
29
 *
30
 * Implementation of functions for sonar use.
31
 *
32
 * @author Colony Project, CMU Robotics Club
33
 * @author Alex Zirbel
34
 **/
35

    
36
#include <ros/ros.h>
37
#include <cstdlib>
38

    
39
#include "sonar.h"
40

    
41
/**
42
 * @defgroup sonar Sonar
43
 * @brief Functions for using the sonar
44
 *
45
 * @{
46
 **/
47

    
48
/** Current position of the sonar. Should only contain values in [0,23]. */
49
int8_t sonar_stepper_pos;
50
bool is_on;
51

    
52
/**
53
 * Initialize the sonar system. This must be called before sonar_read.
54
 * 
55
 * @see sonar_read
56
 * @see sonar_read_raw
57
 **/
58
void sonar_init(void)
59
{
60
    ROS_INFO("Sonar_init called [unimplemented].");
61
    sonar_stepper_pos = 0;
62
}
63

    
64
/**
65
 * Turns the sonar on and off.
66
 */
67
bool handle_sonar_toggle(sonar::sonar_toggle::Request  &req,
68
                         sonar::sonar_toggle::Response &res)
69
{
70
    if (req.set_on && !is_on)
71
    {
72
        ROS_INFO("Turning on the sonar [unimplemented]");
73
    }
74
    else if (req.set_off && is_on)
75
    {
76
        ROS_INFO("Turning off the sonar [unimplemented]");
77
    }
78

    
79
    res.ack = true;
80

    
81
    return true;
82
}
83

    
84
/**
85
 * @brief Sets the sonar scan range. Values from 0 to 23 are valid.
86
 * 
87
 * Handles setting the sonar scan range. The scan range is specified by
88
 * the front sonar, so a scan range of 0 to 23 is a full scan.
89
 * After the scan range is set, the robot will continue reading values
90
 * only from that range until the range is reset. Old values can be identified
91
 * by the header on the distance reading message.
92
 */
93
bool handle_sonar_set_scan(sonar::sonar_set_scan::Request  &req,
94
                           sonar::sonar_set_scan::Response &res)
95
{
96
    // Code to set the sonar to scan from
97
    // req.stop_l to req.stop_r
98

    
99
    ROS_INFO("Setting sonar scan range to [%s, %s] [unimplemented]",
100
             req.stop_l,
101
             req.stop_r);
102
    
103
    res.ack = true;
104

    
105
    return true;
106
}
107

    
108
/**
109
 * Initialize the sonar driver. This must be called before any sonar
110
 * requests are made. It will then run in the background until the sonar node is
111
 * shut down.
112
 **/
113
void main(int argc, char **argv)
114
{
115
    is_on = true;
116

    
117
    /* Initialize hardware for sonar */
118
    // Hardware init functions here
119
    sonar_init();
120

    
121
    /* Initialize the sonar driver node in ROS */
122
    ros::init(argc, argv, "sonar_driver");
123

    
124
    /* Advertise messages and services */
125
    ros::NodeHandle n;
126
    ros:ServiceServer service0 = n.advertiseService("sonar_toggle",
127
                                                    handle_sonar_toggle);
128
    ros:ServiceServer service1 = n.advertiseService("sonar_set_scan",
129
                                                    handle_sonar_set_scan);
130

    
131
    ROS_INFO("Sonar: Ready");
132
    // Sonar reading and publishing here
133
    ros::spin();
134

    
135
    return 0;
136
}
137

    
138
/** @} **/