Project

General

Profile

Statistics
| Branch: | Revision:

root / scout / motors / src / motors.cpp @ 126fea96

History | View | Annotate | Download (5.83 KB)

1 c406f16b Ben
/**
2 0121ead7 bwasserm
 * Copyright (c) 2011 Colony Project
3 c406f16b Ben
 * 
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 c406f16b Ben
26
/**
27
 * @file motors.cpp
28 18e2028b Alex
 * @brief Contains code to control the motors.
29 c406f16b Ben
 *
30
 * Implementation of functions for motor use.
31
 *
32
 * @author Colony Project, CMU Robotics Club
33 3ec16d35 Ben Wasserman
 * @author Ben Wasserman
34 560d2317 Tom Mullins
 * @author Tom Mullins
35 126fea96 Alex

36
 * @defgroup motors Motors
37
 * @brief Functions for using the motors
38
 *
39
 * @{
40 9b11c5b3 Alex Zirbel
 */
41 c406f16b Ben
42 a8480867 Alex Zirbel
#include <ros/ros.h>
43 c406f16b Ben
#include <cstdlib>
44 a8480867 Alex Zirbel
#include "motors.h"
45 c406f16b Ben
46 cc9e9213 Tom Mullins
using namespace std;
47
48 c406f16b Ben
/**
49 560d2317 Tom Mullins
 * @brief Motor constructor
50
 *
51
 * Opens device file and reads initial PWM value
52
 *
53
 * @param filename The name of the PWM device file
54 c9f87aaf bwasserm
 */
55 ed37d345 Tom Mullins
Motor::Motor(int pwm_gpt, int in1_gpio, int in2_gpio)
56 560d2317 Tom Mullins
{
57 fcd68ec1 Tom Mullins
    char buf[60];
58
59
    // open device files
60
    sprintf(buf, "/dev/pwm%d", pwm_gpt);
61
    fpwm.open(buf, ios::out);
62
    sprintf(buf, "/sys/class/gpio/gpio%d/value", in1_gpio);
63
    fin1.open(buf, ios::out);
64
    sprintf(buf, "/sys/class/gpio/gpio%d/value", in2_gpio);
65
    fin2.open(buf, ios::out);
66
67
    /* I set speed to 0 here rather than reading in what the current speed is
68
     * because the pwm driver does not support seeking in the device files.
69
     * Doing both reading and writing causes fpwm to attempt seeking, which
70
     * fails, causing fpwm to refuse to do any more io. So, we can only write.
71
     */
72
    speed = 0;
73
74
    // ensure we are in a consistent state by writing to hardware
75
    set_speed(speed);
76 560d2317 Tom Mullins
}
77
78
/**
79 8913c26d Tom Mullins
 * @brief Motor destructor
80
 *
81
 * Sets the motor's speed to zero in preparation for the node exiting
82
 */
83
Motor::~Motor()
84
{
85
    set_speed(0);
86
}
87
88
/**
89 560d2317 Tom Mullins
 * @brief Returns current motor speed
90
 *
91
 * Note that this doesn't read from the hardware
92
 */
93
int Motor::get_speed()
94
{
95 fcd68ec1 Tom Mullins
    return speed;
96 560d2317 Tom Mullins
}
97
98
/**
99
 * @brief Sets motor speed
100
 *
101
 * This will set the member this->speed and write the new speed to the hardware
102
 *
103
 * @param new_speed The speed to set, between +/- MAXSPEED inclusive
104
 */
105
void Motor::set_speed(int new_speed)
106
{
107 fcd68ec1 Tom Mullins
    int pwm, in1, in2;
108
109
    speed = new_speed;
110
111
    // convert to hardware units
112
    if (speed == 0)
113
    {
114
        /// @todo should this be off (00) or short brake (11)?
115
        pwm = 0;
116
        in1 = 0;
117
        in2 = 0;
118
    }
119
    else if (speed > 0)
120
    {
121
        // CW
122
        pwm = speed;
123
        in1 = 1;
124
        in2 = 0;
125
    }
126
    else
127
    {
128
        // CCW
129
        pwm = -speed;
130
        in1 = 0;
131
        in2 = 1;
132
    }
133
134
    // write to hardware
135
    fpwm << pwm << flush;
136
    fin1 << in1 << flush;
137
    fin2 << in2 << flush;
138 560d2317 Tom Mullins
}
139
140 8913c26d Tom Mullins
/// @todo change these to the correct motor locations / directions
141 560d2317 Tom Mullins
// Motor state variables
142 ed37d345 Tom Mullins
static Motor motor_fl( 8, 70, 71);
143
static Motor motor_fr( 9, 72, 73);
144
static Motor motor_bl(11, 74, 75);
145
static Motor motor_br(10, 76, 77);
146 cc9e9213 Tom Mullins
147 18e2028b Alex
/**
148
 * @brief Sets motor speed
149 c406f16b Ben
 *
150
 * Sets the motor speeds based on subscription to the set_motors topic.
151
 *
152 18e2028b Alex
 * @param msg The message from the set_motors topic, containing speeds and
153 c406f16b Ben
 *  motor configuration settings.
154
 */
155 6ebee82c Alex
void motors_set(const ::messages::set_motors::ConstPtr& msg)
156 18e2028b Alex
{
157 2814387f Alex Zirbel
    if(msg->fl_set)
158 c9f87aaf bwasserm
    {
159 fcd68ec1 Tom Mullins
        motor_fl.set_speed(msg->fl_speed);
160 c9f87aaf bwasserm
    }
161 2814387f Alex Zirbel
    if(msg->fr_set)
162 c9f87aaf bwasserm
    {
163 fcd68ec1 Tom Mullins
        motor_fr.set_speed(msg->fr_speed);
164 c9f87aaf bwasserm
    }
165 2814387f Alex Zirbel
    if(msg->bl_set)
166 c9f87aaf bwasserm
    {
167 fcd68ec1 Tom Mullins
        motor_bl.set_speed(msg->bl_speed);
168 c9f87aaf bwasserm
    }
169 2814387f Alex Zirbel
    if(msg->br_set)
170 c9f87aaf bwasserm
    {
171 fcd68ec1 Tom Mullins
        motor_br.set_speed(msg->br_speed);
172 c9f87aaf bwasserm
    }
173
174 2814387f Alex Zirbel
    ROS_DEBUG("Motor speeds set");
175 c406f16b Ben
}
176
177 18e2028b Alex
/**
178
 * @brief Outputs current motor speeds
179 c406f16b Ben
 *
180
 * Serves the service query_motors by responding to service requests with the
181
 * speeds of the motors.
182 c9f87aaf bwasserm
 * @param req The request. The only field is the units requested.
183 18e2028b Alex
 * @param res The response. The fields will be filled with values.
184 c406f16b Ben
 */
185 6ebee82c Alex
bool motors_query(::messages::query_motors::Request &req,
186
                  ::messages::query_motors::Response &res)
187 18e2028b Alex
{
188 560d2317 Tom Mullins
    res.fl_speed = motor_fl.get_speed();
189
    res.fr_speed = motor_fr.get_speed();
190
    res.bl_speed = motor_bl.get_speed();
191
    res.br_speed = motor_br.get_speed();
192 18e2028b Alex
193
    ROS_DEBUG("Motor speeds queried");
194 a8480867 Alex Zirbel
195 18e2028b Alex
    return true;
196
}
197
198
/**
199
 * @brief Motors driver. This is a ROS node that controls motor speeds.
200
 *
201
 * This is the main function for the motors node. It is run when the node
202
 * starts and initializes the motors. It then subscribes to the
203
 * set_motors, and set_motor_speeds topics, and advertises the
204
 * query_motors service.
205
 * 
206
 * @param argc The number of command line arguments (should be 1)
207
 * @param argv The array of command line arguments
208 9b11c5b3 Alex Zirbel
 */
209 18e2028b Alex
int main(int argc, char **argv)
210
{
211
    /* Initialize in ROS the motors driver node */
212
    ros::init(argc, argv, "motors_driver");
213
214
    /* Advertise that this serves the query_motors service */
215 2814387f Alex Zirbel
    ros::NodeHandle node;
216
    ros::ServiceServer service = node.advertiseService("query_motors",
217
                                                       motors_query);
218 18e2028b Alex
219
    /* Subscribe to the set_motors topic */
220 2814387f Alex Zirbel
    ros::Subscriber sub0 = node.subscribe("set_motors", QUEUE_SIZE, motors_set);
221 18e2028b Alex
222 a8480867 Alex Zirbel
    ROS_INFO("Motors node ready.");
223 18e2028b Alex
    ros::spin();
224 c406f16b Ben
225 18e2028b Alex
    return 0;
226 c406f16b Ben
}
227
228 9b11c5b3 Alex Zirbel
/** @} */