root / scout / sonar / src / sonar.cpp @ eb9cff77
History | View | Annotate | Download (3.79 KB)
1 | 0121ead7 | bwasserm | /**
|
---|---|---|---|
2 | c492be62 | Alex Zirbel | * Copyright (c) 2011 Colony Project
|
3 | 0121ead7 | bwasserm | *
|
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 | 0121ead7 | bwasserm | |
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 | 85538662 | Alex | * @author Alex Zirbel
|
34 | 0121ead7 | bwasserm | **/
|
35 | |||
36 | c492be62 | Alex Zirbel | #include <ros/ros.h> |
37 | 0121ead7 | bwasserm | #include <cstdlib> |
38 | |||
39 | c492be62 | Alex Zirbel | #include "sonar.h" |
40 | |||
41 | 0121ead7 | bwasserm | /**
|
42 | * @defgroup sonar Sonar
|
||
43 | * @brief Functions for using the sonar
|
||
44 | *
|
||
45 | * @{
|
||
46 | **/
|
||
47 | |||
48 | 85538662 | Alex | /** Current position of the sonar. Should only contain values in [0,23]. */
|
49 | int8_t sonar_stepper_pos; |
||
50 | bool is_on;
|
||
51 | |||
52 | 0121ead7 | bwasserm | /**
|
53 | 85538662 | Alex | * Initialize the sonar system. This must be called before sonar_read.
|
54 | *
|
||
55 | * @see sonar_read
|
||
56 | * @see sonar_read_raw
|
||
57 | 0121ead7 | bwasserm | **/
|
58 | 85538662 | Alex | void sonar_init(void) |
59 | { |
||
60 | b00761a0 | Alex Zirbel | ROS_INFO("Sonar_init called [unimplemented].");
|
61 | 85538662 | Alex | sonar_stepper_pos = 0;
|
62 | 0121ead7 | bwasserm | } |
63 | |||
64 | /**
|
||
65 | 85538662 | Alex | * 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 | b00761a0 | Alex Zirbel | if (req.set_on && !is_on)
|
71 | { |
||
72 | 85538662 | Alex | ROS_INFO("Turning on the sonar [unimplemented]");
|
73 | b00761a0 | Alex Zirbel | } |
74 | else if (req.set_off && is_on) |
||
75 | { |
||
76 | 85538662 | Alex | ROS_INFO("Turning off the sonar [unimplemented]");
|
77 | b00761a0 | Alex Zirbel | } |
78 | 85538662 | Alex | |
79 | res.ack = true;
|
||
80 | 0121ead7 | bwasserm | |
81 | 85538662 | Alex | return true; |
82 | 0121ead7 | bwasserm | } |
83 | |||
84 | /**
|
||
85 | 85538662 | Alex | * @brief Sets the sonar scan range. Values from 0 to 23 are valid.
|
86 | 0121ead7 | bwasserm | *
|
87 | 85538662 | Alex | * 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 | 0121ead7 | bwasserm | |
99 | 85538662 | Alex | 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 | 0121ead7 | bwasserm | } |
107 | |||
108 | /**
|
||
109 | 85538662 | Alex | * 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 | 0121ead7 | bwasserm | **/
|
113 | 85538662 | Alex | void main(int argc, char **argv) |
114 | { |
||
115 | is_on = true;
|
||
116 | |||
117 | /* Initialize hardware for sonar */
|
||
118 | c492be62 | Alex Zirbel | // Hardware init functions here
|
119 | 85538662 | Alex | 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 | c492be62 | Alex Zirbel | // Sonar reading and publishing here
|
133 | 85538662 | Alex | ros::spin(); |
134 | |||
135 | return 0; |
||
136 | 0121ead7 | bwasserm | } |
137 | 85538662 | Alex | |
138 | /** @} **/ |