Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / encoders / src / encoders.cpp @ 14241f84

History | View | Annotate | Download (3.69 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
/**
28
 * @file encoders.cpp
29
 * @brief Encoders
30
 *
31
 * Implementation of functions for encoder use.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 * @author Benjamin Wasserman
35
 **/
36

    
37
#include "ros/ros.h"
38
#include "encoders.h"
39
//#include "libscout/src/constants.h"
40
#include <cstdlib>
41

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

    
48
/* Encoder state variables */
49
/** \todo Fix types: static */
50
int encoder_fl; /**< The current state of the front left encoder. */
51
int encoder_fr; /**< The current state of the front right encoder. */
52
int encoder_bl; /**< The current state of the back left encoder. */
53
int encoder_br; /**< The current state of the back right encoder. */
54

    
55
/*!
56
 * \brief Encoders driver. This is a ROS node that controls encoders.
57
 *
58
 * This is the main function for the encoders node. It is run when the node
59
 * starts and initializes the encoders. It then publishes to the
60
 * encoder_state topic and advertises the query_encoders service.
61
 * 
62
 * \param argc The number of command line arguments (should be 1)
63
 * \param argv The array of command line arguments
64
 **/
65
int main(int argc, char **argv){
66
  /* Initialize in ROS the encoders driver node */
67
  ros::init(argc, argv, "encoders_driver");
68
  
69
  /* Advertise that this serves the query_encoders service */
70
  ros::NodeHandle n;
71
  ros::ServiceServer service = n.advertiseService("query_encoders", encoders_query);
72

    
73
  /* Subscribe to the encoder_state topic */
74
  ros::Subscriber sub0 = n.subscribe("encoder_state", 4, encoder_state);
75

    
76
  /* Initialize hardware for motors */
77
  // Hardware init functions here
78

    
79
  ROS_INFO("Ready to set encoders.");
80
  ros::spin();
81

    
82
  return 0;
83
}
84

    
85
/*!
86
 * \brief Gets encoder state
87
 *
88
 * Sets the encoder state based on subscription to the encoder_state topic.
89
 *
90
 * \param msg The message from the encoder_state topic, containing encoder
91
 * configuration settings.
92
 */
93
void encoder_state(const encoders::encoder_state::ConstPtr& msg){ 
94
  encoder_fl = msg->fl_encoder;
95
  encoder_fr = msg->fr_encoder;
96
  encoder_bl = msg->bl_encoder;
97
  encoder_br = msg->br_encoder;
98
}
99

    
100
/*!
101
 * \brief Outputs current encoder data
102
 *
103
 * Serves the service query_encoders by responding to service requests with the
104
 * encoder values.
105
 * \param req The request. Should be empty.
106
 * \param res The response. The fields will be filled with values.
107
 */
108
bool encoders_query(encoders::query_encoders::Request &req,
109
    encoders::query_encoders::Response &res){
110
  
111
  res.fl_encoder = encoder_fl;
112
  res.fr_encoder = encoder_fr;
113
  res.bl_encoder = encoder_bl;
114
  res.br_encoder = encoder_br;
115

    
116
  ROS_DEBUG("Encoder values queried");
117
  return true;
118
}
119