brickOS C++ v0.9.0
MotorPair.H
Go to the documentation of this file.
1
7//
8// The contents of this file are subject to the Mozilla Public License
9// Version 1.0 (the "License"); you may not use this file except in
10// compliance with the License. You may obtain a copy of the License
11// at http://www.mozilla.org/MPL/
12//
13// Software distributed under the License is distributed on an "AS IS"
14// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
15// the License for the specific language governing rights and
16// limitations under the License.
17//
18// This software was developed as part of the legOS project.
19//
20// Contributor: Pat Welch (legOS@mousebrains.com)
21
22#ifndef _MotorPair_H_
23#define _MotorPair_H_
24
25#include <config.h>
26
27#if defined(CONF_DMOTOR)
28
29#include <conio.h> // for the delay() function
30#include <c++/Motor.H>
31
57class MotorPair {
58public:
59
62 MotorPair(const Motor::Port lport, const Motor::Port rport)
63 : mLeft(lport), mRight(rport) {}
64
68
70 void speed(const int s) const { mLeft.speed(s); mRight.speed(s); }
71
74 void direction(const MotorDirection dir) const {
75 if (dir == ::fwd) {
76 mLeft.direction(::fwd);
77 mRight.direction(::rev);
78 } else if (dir == ::rev) {
79 mLeft.direction(::rev);
80 mRight.direction(::fwd);
81 } else {
82 mLeft.direction(dir);
83 mRight.direction(dir);
84 }
85 }
86
88 void forward() const { direction(::fwd); }
89
91 void reverse() const { direction(::rev); }
92
94 void brake() const {
95 mLeft.direction(::brake);
96 mRight.direction(::brake);
97 }
98
100 void off() const {
101 mLeft.direction(::off);
102 mRight.direction(::off);
103 }
104
107 void left() const {
108 mLeft.direction(::fwd);
109 mRight.direction(::fwd);
110 }
111
114 void pivotLeft() const {
115 mLeft.brake();
116 mRight.direction(::rev);
117 }
118
121 void right() const {
122 mLeft.direction(::rev);
123 mRight.direction(::rev);
124 }
125
128 void pivotRight() const {
129 mLeft.direction(::fwd);
130 mRight.brake();
131 }
132
133
136 void forward(const int s) const { forward(); speed(s); }
137
140 void reverse(const int s) const { reverse(); speed(s); }
141
145 void left(const int s) const { left(); speed(s); }
146
150 void pivotLeft(const int s) const { pivotLeft(); speed(s); }
151
155 void right(const int s) const { right(); speed(s); }
156
160 void pivotRight(const int s) const { pivotRight(); speed(s); }
161
164 void brake(const int ms) const { brake(); delay(ms); }
165
171
172private:
173 const Motor mLeft;
174 const Motor mRight;
175};
176
177#else // CONF_DMOTOR
178#warning Enable CONF_DMOTOR to use MotorPair.H
179#endif // CONF_DMOTOR
180#endif // _MotorPair_H_
C++ Motor Class Interface.
Pair-of-motors control interface.
Definition MotorPair.H:57
void pivotRight(const int s) const
turn right at speed {s} but pivot around right wheel
Definition MotorPair.H:160
void forward() const
move forward
Definition MotorPair.H:88
void pivotRight() const
turn right about the right wheel of the robot
Definition MotorPair.H:128
void brake() const
stop without coasting
Definition MotorPair.H:94
MotorPair(const Motor::Port lport, const Motor::Port rport)
define a pair of motors specifying the connection of each to the RCX
Definition MotorPair.H:62
void left() const
turn left about the center of the robot
Definition MotorPair.H:107
void off() const
stop but allow coasting
Definition MotorPair.H:100
void brake(const int ms) const
apply the brakes to both motors then delay for {ms} mSec
Definition MotorPair.H:164
void speed(const int s) const
set the speed of our two motors
Definition MotorPair.H:70
void pivotLeft(const int s) const
turn left at speed {s} but pivot around left wheel
Definition MotorPair.H:150
void pivotLeft() const
turn left about the left wheel of the robot
Definition MotorPair.H:114
void left(const int s) const
turn left at speed {s}
Definition MotorPair.H:145
void direction(const MotorDirection dir) const
set the direction of our two motors
Definition MotorPair.H:74
void reverse() const
move reverse
Definition MotorPair.H:91
void reverse(const int s) const
move reverse (go backwards) at speed {s}
Definition MotorPair.H:140
void forward(const int s) const
move forward at speed {s}
Definition MotorPair.H:136
~MotorPair()
destroy this MotorPair instance
Definition MotorPair.H:67
Limits
the minimum and maximum speeds a motor can go, its limits.
Definition MotorPair.H:167
@ max
maximum motor speed setting
Definition MotorPair.H:169
@ min
minimum motor speed setting
Definition MotorPair.H:168
void right(const int s) const
turn right at speed {s}
Definition MotorPair.H:155
void right() const
turn right about the center of the robot
Definition MotorPair.H:121
Motor control interface.
Definition Motor.H:38
Port
valid port designators
Definition Motor.H:43
@ min
Minimum velocity (0)
Definition Motor.H:53
@ max
Maximum velocity (255)
Definition Motor.H:54
const void speed(const unsigned char speed) const
set the motor speed
Definition Motor.H:80
const void direction(const MotorDirection dir) const
set the motor direction
Definition Motor.H:86
const void brake() const
set the motor to brake
Definition Motor.H:116
Interface: console input / output.
void delay(unsigned ms)
delay approximately ms mSec
MotorDirection
the motor directions
Definition dmotor.h:44
@ fwd
forward
Definition dmotor.h:46
@ rev
reverse
Definition dmotor.h:47