root / scout / libscout / src / LinesensorControl.cpp @ 360e3b6c
History | View | Annotate | Download (2.75 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 LinesensorControl.cpp
|
28 |
* @brief Contains line following function implementation
|
29 |
*
|
30 |
* @author Colony Project, CMU Robotics Club
|
31 |
* @author Alex Zirbel
|
32 |
*/
|
33 |
|
34 |
#include "LinesensorControl.h" |
35 |
|
36 |
using namespace std; |
37 |
|
38 |
static double last_ret; |
39 |
|
40 |
/**
|
41 |
* @brief Initialize the line following module of libscout.
|
42 |
*/
|
43 |
LinesensorControl::LinesensorControl(const ros::NodeHandle& libscout_node,
|
44 |
string scoutname)
|
45 |
: node(libscout_node) |
46 |
{ |
47 |
query_client = |
48 |
node.serviceClient<linesensor::query_linesensor>(scoutname+"/query_linesensor");
|
49 |
} |
50 |
|
51 |
/**
|
52 |
* @brief Returns the current readings of the line follwing sensor.
|
53 |
*/
|
54 |
vector<uint32_t> LinesensorControl::query() |
55 |
{ |
56 |
// Set scan range
|
57 |
linesensor::query_linesensor srv; |
58 |
|
59 |
if (!query_client.call(srv))
|
60 |
{ |
61 |
ROS_ERROR("LinesensorControl query failed.");
|
62 |
} |
63 |
|
64 |
return srv.response.readings;
|
65 |
} |
66 |
|
67 |
/**
|
68 |
* @brief Returns the average readings of the line following sensors.
|
69 |
*/
|
70 |
double LinesensorControl::readline()
|
71 |
{ |
72 |
unsigned int total_read = 0; |
73 |
unsigned int weighted_total = 0; |
74 |
|
75 |
vector<uint32_t> readings = query(); |
76 |
|
77 |
if (readings.size() < 8) |
78 |
{ |
79 |
return last_ret;
|
80 |
} |
81 |
|
82 |
for (int i = 0; i < 8; i++) |
83 |
{ |
84 |
total_read += readings[i]; |
85 |
weighted_total += i * readings[i]; |
86 |
} |
87 |
|
88 |
if (total_read == 0) |
89 |
{ |
90 |
return last_ret;
|
91 |
} |
92 |
|
93 |
double ret_val = last_ret = ((double) weighted_total / total_read) - 3.5; |
94 |
return ret_val;
|
95 |
} |
96 |
|
97 |
bool LinesensorControl::fullline()
|
98 |
{ |
99 |
vector<uint32_t> readings = query(); |
100 |
for(int i=0; i<8; i++) |
101 |
{ |
102 |
if(readings[i] < 200) |
103 |
return false; |
104 |
} |
105 |
return true; |
106 |
} |
107 |
|