OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / lejos / nxt / BasicMotor.java
1 package lejos.nxt;
2
3 /**
4  * An abstraction for a motor without a tachometer,
5  * such as an RCX motor.
6  * 
7  * @author Lawrie Griffiths
8  *
9  */
10 public abstract class BasicMotor 
11 {
12         
13         int _mode = 4;
14         int _power = 50;
15         BasicMotorPort _port;
16
17         /**
18          * Sets power.
19          * 
20          * @param power power setting: 0 - 100
21          */
22         public void setPower(int power)
23         {
24                 _power = power;
25                 _port.controlMotor(_power, _mode);
26         }
27          
28         /**
29          * Returns the current power setting.
30          * 
31          * @return power value 0-100
32          */
33         public int getPower()
34         {
35                 return _power;
36         }
37
38         /**
39          * Causes motor to rotate forward.
40          */
41         public void forward()
42         { 
43                 _mode = 1;
44                 updateState();
45         }
46           
47         /**
48          * Return true if motor is forward.
49          */
50         public boolean isForward()
51         {
52                 return (_mode == 1);
53         }
54
55         /**
56          * Causes motor to rotate backwards.
57          */
58         public void backward()
59         {
60                 _mode = 2;
61                 updateState();
62         }
63
64         /**
65          * Return true if motor is backward.
66          */
67         public boolean isBackward()
68         {
69                 return (_mode == 2);
70         }
71
72         /**
73          * Reverses direction of the motor. It only has
74          * effect if the motor is moving.
75          */
76         public void reverseDirection()
77         {
78                 if (_mode == 1 || _mode == 2)
79             {
80                         _mode = (3 - _mode);
81                         updateState();
82             }
83         }
84
85         /**
86          * Returns true iff the motor is in motion.
87          * 
88          * @return true iff the motor is currently in motion.
89          */
90         public boolean isMoving()
91         {
92                 return (_mode == 1 || _mode == 2);        
93         }
94
95         /**
96          * Causes motor to float. The motor will lose all power,
97          * but this is not the same as stopping. Use this
98          * method if you don't want your robot to trip in
99          * abrupt turns.
100          */   
101         public void flt()
102         {
103                 _mode = 4;
104                 updateState();
105         }
106
107         /**
108          * Returns true iff the motor is in float mode.
109          * 
110          * @return true iff the motor is currently in float mode.
111          */
112         public boolean isFloating()
113         {
114                 return _mode == 4;        
115         }
116           
117         /**
118          * Causes motor to stop, pretty much
119          * instantaneously. In other words, the
120          * motor doesn't just stop; it will resist
121          * any further motion.
122          * Cancels any rotate() orders in progress
123          */
124         public void stop()
125         {
126                 _mode = 3;
127             updateState();
128         }
129           
130         /**
131          * Return true if motor is stopped.
132          */
133         public boolean isStopped()
134         {
135                 return (_mode == 3);
136         }
137
138         void updateState()
139         {
140                 _port.controlMotor(_power, _mode);
141         }
142           
143         /**
144          * Returns the mode.
145          * 
146          * @return mode 1=forward, 2=backward, 3=stopped, 4=floating
147          */
148         public int getMode()
149         { 
150                 return _mode;
151     }
152 }
153