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