scoutos / scout / motors / src / motors.cpp @ c406f16b
History | View | Annotate | Download (3.39 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2007 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 motors.cpp
|
29 |
* @brief Motors
|
30 |
*
|
31 |
* Implementation of functions for motor use.
|
32 |
*
|
33 |
* @author Colony Project, CMU Robotics Club
|
34 |
* @author Benjamin Wasserman
|
35 |
**/
|
36 |
|
37 |
#include "ros/ros.h" |
38 |
#include "motors.h" |
39 |
#include <cstdlib> |
40 |
|
41 |
/**
|
42 |
* @defgroup motors Motors
|
43 |
* @brief Functions for using the motors
|
44 |
*
|
45 |
* @{
|
46 |
**/
|
47 |
|
48 |
/*!
|
49 |
* \brief Motors driver. This is a ROS node that controls motor speeds.
|
50 |
*
|
51 |
* This is the main function for the motors node. It is run when the node
|
52 |
* starts and initializes the motors. It then subscribes to the
|
53 |
* set_motors, and set_motor_speeds topics, and advertises the
|
54 |
* query_motors service.
|
55 |
*
|
56 |
* \param argc The number of command line arguments (should be 1)
|
57 |
* \param argv The array of command line arguments
|
58 |
**/
|
59 |
int main(int argc, char **argv){ |
60 |
/* Initialize in ROS the motors driver node */
|
61 |
ros::init(argc, argv, "motors_driver");
|
62 |
|
63 |
/* Advertise that this serves the query_motors service */
|
64 |
ros::NodeHandle n; |
65 |
ros::ServiceServer service = n.advertiseService("query_motors", motors_query);
|
66 |
|
67 |
/* Subscribe to the set_motors topic */
|
68 |
ros::Subscriber sub0 = n.subscribe("set_motors", 4, motors_set); |
69 |
|
70 |
/* Initialize hardware for motors */
|
71 |
// Hardware init functions here
|
72 |
|
73 |
ROS_INFO("Ready to set motors.");
|
74 |
ros::spin(); |
75 |
|
76 |
return 0; |
77 |
} |
78 |
|
79 |
/*!
|
80 |
* \brief Sets motor speed
|
81 |
*
|
82 |
* Sets the motor speeds based on subscription to the set_motors topic.
|
83 |
*
|
84 |
* \param msg The message from the set_motors topic, containing speeds and
|
85 |
* motor configuration settings.
|
86 |
*/
|
87 |
void motors_set(const motors::set_motors::ConstPtr& msg){ |
88 |
/** \todo Edit to only set requested motors, not all */
|
89 |
motor_fl_speed = msg->fl_speed; |
90 |
motor_fr_speed = msg->fr_speed; |
91 |
motor_bl_speed = msg->bl_speed; |
92 |
motor_br_speed = msg->br_speed; |
93 |
} |
94 |
|
95 |
/*!
|
96 |
* \brief Outputs current motor speeds
|
97 |
*
|
98 |
* Serves the service query_motors by responding to service requests with the
|
99 |
* speeds of the motors.
|
100 |
* \param req The request. Should be empty.
|
101 |
* \param res The response. The fields will be filled with values.
|
102 |
*/
|
103 |
bool motors_query(motors::query_motors::Request &req,
|
104 |
motors::query_motors::Response &res){ |
105 |
|
106 |
res.fl_speed = motor_fl_speed; |
107 |
res.fr_speed = motor_fr_speed; |
108 |
res.bl_speed = motor_bl_speed; |
109 |
res.br_speed = motor_br_speed; |
110 |
|
111 |
ROS_INFO("Motor speeds queried");
|
112 |
return true; |
113 |
} |
114 |
|
115 |
|
116 |
/**
|
117 |
* }
|
118 |
**/
|