root / scout / encoders / src / encoders.cpp @ 3a73516c
History | View | Annotate | Download (4.32 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 encoders.cpp
|
28 |
* @brief Encoders
|
29 |
*
|
30 |
* @defgroup encoders Encoders
|
31 |
* @brief Functions for using the encoders
|
32 |
*
|
33 |
* @author Colony Project, CMU Robotics Club
|
34 |
* @author Alex Zirbel
|
35 |
* @author Tom Mullins
|
36 |
|
37 |
* @{
|
38 |
**/
|
39 |
|
40 |
#include <fstream> |
41 |
#include <ros/ros.h> |
42 |
#include "encoders.h" |
43 |
|
44 |
/* Encoder instances */
|
45 |
/* @todo make sure these are the correct numbers */
|
46 |
static Encoder encoder_fl(0); |
47 |
static Encoder encoder_fr(1); |
48 |
static Encoder encoder_bl(2); |
49 |
static Encoder encoder_br(3); |
50 |
|
51 |
/**
|
52 |
* @brief Encoder constructor
|
53 |
*
|
54 |
* Opens device file, which is read at every call to get_ticks
|
55 |
*
|
56 |
* @param n The encoder number to read, between 0 and 3 inclusive
|
57 |
*/
|
58 |
Encoder::Encoder(int n)
|
59 |
{ |
60 |
sprintf(filename, "/sys/class/encoder/enc%d/ticks", n);
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* @brief Returns the current tick count
|
65 |
*
|
66 |
* This will actually read from the encoder ticks file to get the latest value
|
67 |
* from the driver
|
68 |
*/
|
69 |
int Encoder::get_ticks()
|
70 |
{ |
71 |
// open device file
|
72 |
std::fstream fticks(filename, std::ios::in); |
73 |
|
74 |
int ticks;
|
75 |
fticks >> ticks; |
76 |
|
77 |
return ticks;
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* @brief Resets the encoder
|
82 |
*
|
83 |
* Sets the encoder's ticks value back to 0
|
84 |
*/
|
85 |
void Encoder::reset()
|
86 |
{ |
87 |
std::fstream fticks(filename, std::ios::out); |
88 |
fticks << 0;
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* @brief Outputs current encoder data
|
93 |
*
|
94 |
* Serves the service query_encoders by responding to service requests with the
|
95 |
* encoder values.
|
96 |
* @param req The request. The units that the response should be in.
|
97 |
* @param res The response. The fields will be filled with values.
|
98 |
*/
|
99 |
bool handle_encoders_query(::messages::query_encoders::Request &req,
|
100 |
::messages::query_encoders::Response &res) |
101 |
{ |
102 |
/* Put index, velocity, and distance data in message */
|
103 |
res.fl_distance = -encoder_fl.get_ticks(); |
104 |
res.fr_distance = encoder_fr.get_ticks(); |
105 |
res.bl_distance = encoder_bl.get_ticks(); |
106 |
res.br_distance = encoder_br.get_ticks(); |
107 |
|
108 |
/* @todo maybe return value based on whether reads succeeded */
|
109 |
|
110 |
ROS_DEBUG("Encoder values queried");
|
111 |
ROS_INFO("Encoder values queried %d %d %d %d", res.fl_distance, res.fr_distance, res.bl_distance, res.br_distance);
|
112 |
return true; |
113 |
} |
114 |
|
115 |
/**
|
116 |
* @brief Resets all four encoders
|
117 |
*
|
118 |
* Serves the service reset_encoders
|
119 |
* @param req The request. Empty.
|
120 |
* @param res The response. Empty.
|
121 |
*/
|
122 |
bool handle_reset_query(::messages::reset_encoders::Request &req,
|
123 |
::messages::reset_encoders::Response &res) |
124 |
{ |
125 |
encoder_fl.reset(); |
126 |
encoder_fr.reset(); |
127 |
encoder_bl.reset(); |
128 |
encoder_br.reset(); |
129 |
return true; |
130 |
} |
131 |
|
132 |
/**
|
133 |
* @brief Encoders driver. This is a ROS node that controls encoders.
|
134 |
*
|
135 |
* This is the main function for the encoders node. It is run when the node
|
136 |
* starts and initializes the encoders. It then publishes to the
|
137 |
* encoder_state topic and advertises the query_encoders service.
|
138 |
*/
|
139 |
int main(int argc, char **argv) |
140 |
{ |
141 |
/* Initialize in ROS the encoders driver node */
|
142 |
ros::init(argc, argv, "encoders_driver");
|
143 |
|
144 |
/* Advertise that this serves the query_encoders service */
|
145 |
ros::NodeHandle n; |
146 |
ros::ServiceServer service = |
147 |
n.advertiseService("query_encoders",
|
148 |
handle_encoders_query); |
149 |
|
150 |
ROS_INFO("Ready to set encoders.");
|
151 |
|
152 |
ros::spin(); |
153 |
|
154 |
return 0; |
155 |
} |
156 |
|
157 |
/** @} */
|