root / scout / motors / src / motors_util.cpp @ f0a9ebb3
History | View | Annotate | Download (3.19 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 motors_util.cpp
|
28 |
* @brief Contains helpful functions this node, or others, can use to control
|
29 |
* the motors.
|
30 |
*
|
31 |
* @author Colony Project, CMU Robotics Club
|
32 |
* @author Ben Wasserman
|
33 |
* @author Alex Zirbel
|
34 |
**/
|
35 |
|
36 |
#include "motors.h" |
37 |
#include <cstdlib> |
38 |
|
39 |
/**
|
40 |
* @defgroup motors
|
41 |
* @{
|
42 |
**/
|
43 |
|
44 |
/**
|
45 |
* @brief Converts set speeds (of various units) to absolute speeds.
|
46 |
*
|
47 |
* @param speed The speed expressed in the desired units
|
48 |
* @param units The units the desired speed is measured in
|
49 |
* @return The absolute speed of the motor
|
50 |
**/
|
51 |
int motors_rel_to_abs(int rel_speed, int units) |
52 |
{ |
53 |
switch(units)
|
54 |
{ |
55 |
case MOTOR_ABSOLUTE:/* Speed given as absolute */ |
56 |
return rel_speed;
|
57 |
case MOTOR_PERCENT:/* Convert from percentage */ |
58 |
return rel_speed * MAXSPEED / 100; |
59 |
case MOTOR_MMS:/* Convert from mm/s */ |
60 |
/** @todo Make math to do this conversion **/
|
61 |
return rel_speed;
|
62 |
case MOTOR_CMS:/* Convert from cm/s */ |
63 |
/** @todo Make math to do this conversion **/
|
64 |
return rel_speed;
|
65 |
default: /* The units aren't recognized */ |
66 |
/** @todo Decide on default case. Either percent or absolute. **/
|
67 |
return rel_speed;
|
68 |
} |
69 |
} |
70 |
|
71 |
/**
|
72 |
* @brief Convert absolute speeds to speeds of various units.
|
73 |
*
|
74 |
* @param speed The speed expressed in absolute units.
|
75 |
* @param units The units the desired speed is measured in.
|
76 |
* @return The relative speed of the motor in desired units.
|
77 |
**/
|
78 |
int motors_abs_to_rel(int abs_speed, int units) |
79 |
{ |
80 |
switch(units)
|
81 |
{ |
82 |
case MOTOR_ABSOLUTE:/* Speed given as absolute */ |
83 |
return abs_speed;
|
84 |
case MOTOR_PERCENT:/* Convert from percentage */ |
85 |
return abs_speed * 100 / MAXSPEED; |
86 |
case MOTOR_MMS:/* Convert from mm/s */ |
87 |
/** @todo Make math to do this conversion **/
|
88 |
return abs_speed;
|
89 |
case MOTOR_CMS:/* Convert from cm/s */ |
90 |
/** @todo Make math to do this conversion **/
|
91 |
return abs_speed;
|
92 |
default: /* The units aren't recognized */ |
93 |
/** @todo Decide on default case. Either percent or absolute. **/
|
94 |
return abs_speed;
|
95 |
} |
96 |
} |
97 |
|
98 |
/** @} **/
|