Statistics
| Branch: | Revision:

root / scout / encoders / src / encoders.cpp @ f358d0b2

History | View | Annotate | Download (3.8 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
 * Implementation of functions for encoder use.
31
 *
32
 * @author Colony Project, CMU Robotics Club
33
 * @author Alex Zirbel
34
 * @author Tom Mullins
35
 **/
36

    
37
#include <ros/ros.h>
38

    
39
#include "encoders.h"
40

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

    
48
/* 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

    
55
/**
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
    sprintf(filename, "/sys/class/encoder/enc%d/ticks", n);
65
}
66

    
67
/**
68
 * @brief Returns the current tick count
69
 *
70
 * This will actually read from the encoder ticks file to get the latest value
71
 * from the driver
72
 */
73
int Encoder::get_ticks()
74
{
75
    // open device file
76
    fticks.open(filename, std::ios::in);
77

    
78
    int ticks;
79
    fticks >> ticks;
80

    
81
    fticks.close();
82
    return ticks;
83
}
84

    
85
/**
86
 * @brief Outputs current encoder data
87
 *
88
 * Serves the service query_encoders by responding to service requests with the
89
 * encoder values.
90
 * @param req The request. The units that the response should be in.
91
 * @param res The response. The fields will be filled with values.
92
 */
93
bool handle_encoders_query(encoders::query_encoders::Request  &req,
94
                           encoders::query_encoders::Response &res)
95
{
96
    /* Put index, velocity, and distance data in message */
97
    res.fl_distance = encoder_fl.get_ticks();
98
    res.fr_distance = encoder_fr.get_ticks();
99
    res.bl_distance = encoder_bl.get_ticks();
100
    res.br_distance = encoder_br.get_ticks();
101

    
102
    /* @todo maybe return value based on whether reads succeeded */
103

    
104
    ROS_DEBUG("Encoder values queried");
105
    ROS_INFO("Encoder values queried %d  %d  %d   %d", res.fl_distance, res.fr_distance, res.bl_distance, res.br_distance);
106
    return true;
107
}
108

    
109
/**
110
 * @brief Encoders driver. This is a ROS node that controls encoders.
111
 *
112
 * This is the main function for the encoders node. It is run when the node
113
 * starts and initializes the encoders. It then publishes to the
114
 * encoder_state topic and advertises the query_encoders service.
115
 */
116
int main(int argc, char **argv)
117
{
118
    /* Initialize in ROS the encoders driver node */
119
    ros::init(argc, argv, "encoders_driver");
120

    
121
    /* Advertise that this serves the query_encoders service */
122
    ros::NodeHandle n;
123
    ros::ServiceServer service =
124
        n.advertiseService("query_encoders",
125
                           handle_encoders_query);
126

    
127
    ROS_INFO("Ready to set encoders.");
128

    
129
    ros::spin();
130

    
131
    return 0;
132
}
133

    
134
/** @} */