NXShield  v1.07
NXShield Library Reference by OpenElectrons.com
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
NXTMMX.h
1 
2 // NXTMMX.h
3 //
4 // This is a class for controlling the Lego Motor Multiplexer, made by Mindsensors.
5 // See http://www.mindsensors.com/index.php?module=pagemaster&PAGE_user_op=view_page&PAGE_id=134 .
6 
7 // Initial version: 2010-06-07 by Clinton Blackmore
8 // Large ports of the code is ported from the NXC library for the device,
9 // written by Deepak Patil.
10 
11 /*
12  This library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU Lesser General Public
14  License as published by the Free Software Foundation; either
15  version 2.1 of the License, or (at your option) any later version.
16 
17  This library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  Lesser General Public License for more details.
21 
22  You should have received a copy of the GNU Lesser General Public
23  License along with this library; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26 
27 #ifndef NXTMMX_H
28 #define NXTMMX_H
29 
30 // THE FOLLOWING CONSTANTS ARE ALL FROM THE NXC SOURCE CODE
31 
32 #define MMX_CONTROL_SPEED 0x01
33 #define MMX_CONTROL_RAMP 0x02
34 #define MMX_CONTROL_RELATIVE 0x04
35 #define MMX_CONTROL_TACHO 0x08
36 #define MMX_CONTROL_BRK 0x10
37 #define MMX_CONTROL_ON 0x20
38 #define MMX_CONTROL_TIME 0x40
39 #define MMX_CONTROL_GO 0x80
40 
41 #define MMX_COMMAND 0x41
42 #define MMX_VOLTAGE 0x41
43 
44 #define MMX_SETPT_M1 0x42
45 #define MMX_SPEED_M1 0x46
46 #define MMX_TIME_M1 0x47
47 #define MMX_CMD_B_M1 0x48
48 #define MMX_CMD_A_M1 0x49
49 
50 #define MMX_SETPT_M2 0x4A
51 #define MMX_SPEED_M2 0x4E
52 #define MMX_TIME_M2 0x4F
53 #define MMX_CMD_B_M2 0x50
54 #define MMX_CMD_A_M2 0x51
55 
56 /*
57  * Read registers.
58  */
59 #define MMX_POSITION_M1 0x62
60 #define MMX_POSITION_M2 0x66
61 #define MMX_STATUS_M1 0x72
62 #define MMX_STATUS_M2 0x73
63 #define MMX_TASKS_M1 0x76
64 #define MMX_TASKS_M2 0x77
65 
66 #define MMX_ENCODER_PID 0x7A
67 #define MMX_SPEED_PID 0x80
68 #define MMX_PASS_COUNT 0x86
69 #define MMX_TOLERANCE 0x87
70 
71 /* constants to be used by user programs */
72 /*
73  * Motor selection related constants
74  */
75 #define MMX_Motor_1 0x01
76 #define MMX_Motor_2 0x02
77 #define MMX_Motor_Both 0x03
78 
79 /*
80  * Next action related constants
81  */
82 // stop and let the motor coast.
83 #define MMX_Next_Action_Float 0x00
84 // apply brakes, and resist change to tachometer
85 #define MMX_Next_Action_Brake 0x01
86 // apply brakes, and restore externally forced change to tachometer
87 #define MMX_Next_Action_BrakeHold 0x02
88 
89 /*
90  * Direction related constants
91  */
92 #define MMX_Direction_Forward 0x01
93 #define MMX_Direction_Reverse 0x00
94 
95 /*
96  * Tachometer related constants
97  */
98 #define MMX_Move_Relative 0x01
99 #define MMX_Move_Absolute 0x00
100 
101 #define MMX_Completion_Wait_For 0x01
102 #define MMX_Completion_Dont_Wait 0x00
103 
104 /*
105  * Speed constants, these are just convenience constants,
106  * You can use any value between 0 and 100.
107  */
108 #define MMX_Speed_Full 90
109 #define MMX_Speed_Medium 60
110 #define MMX_Speed_Slow 25
111 
112 
113 #include "NXShieldI2C.h"
114 
118 class NXTMMX : public NXShieldI2C
119 {
120 public:
123  NXTMMX(uint8_t i2c_address = 0x06);
124 
126  uint8_t getBatteryVoltage();
127 
129  uint8_t issueCommand(char command);
130 
132  bool setEncoderTarget(uint8_t which_motor, long target);
133 
135  long getEncoderTarget(uint8_t which_motor);
136 
138  bool setSpeed(uint8_t which_motor, int speed);
139 
141  int8_t getSpeed(uint8_t which_motor);
142 
144  bool getTimeToRun(uint8_t which_motor, int seconds);
145 
147  uint8_t getTimeToRun(uint8_t which_motor);
148 
149  bool setCommandRegB(uint8_t which_motor, uint8_t value);
150  uint8_t getCommandRegB(uint8_t which_motor);
151  bool setCommandRegA(uint8_t which_motor, uint8_t value);
152  uint8_t getCommandRegA(uint8_t which_motor);
153 
155  int32_t getEncoderPosition(uint8_t which_motor);
156 
158  uint8_t getMotorStatusByte(uint8_t which_motor);
159 
161  uint8_t getMotorTasksRunningByte(uint8_t which_motor);
162 
164  bool setEncoderPID(uint16_t Kp, uint16_t Ki, uint16_t Kd);
165 
167  bool setSpeedPID(uint16_t Kp, uint16_t Ki, uint16_t Kd);
168 
170  bool setPassCount(uint8_t pass_count);
171 
173  bool setTolerance(uint8_t tolerance);
174 
176  bool reset();
177 
179  bool startMotorsInSync();
180 
182  bool resetEncoder(uint8_t which_motor);
183 
185  bool setSpeedTimeAndControl(uint8_t which_motors, int speed, uint8_t duration, uint8_t control);
186 
188  bool setEncoderSpeedTimeAndControl(uint8_t which_motors, long encoder, int speed, uint8_t duration, uint8_t control);
189 
191  bool isTimeDone(uint8_t which_motors);
192 
194  void waitUntilTimeDone(uint8_t which_motors);
195 
197  bool isTachoDone(uint8_t which_motors);
198 
200  void waitUntilTachoDone(uint8_t which_motors);
201 
203  void runUnlimited(uint8_t which_motors, uint8_t direction, int speed);
204 
206  void runSeconds(uint8_t which_motors, uint8_t direction, int speed, uint8_t duration, uint8_t wait_for_completion, uint8_t next_action);
207 
209  void runTachometer(uint8_t which_motors, uint8_t direction, int speed, long tachometer, uint8_t relative, uint8_t wait_for_completion, uint8_t next_action);
210 
212  void runDegrees(uint8_t which_motors, uint8_t direction,int speed, long degrees, uint8_t wait_for_completion, uint8_t next_action);
213 
215  void runRotations( uint8_t which_motors, uint8_t direction, int speed, long rotations, uint8_t wait_for_completion, uint8_t next_action);
216 
218  bool stop(uint8_t which_motors, uint8_t next_action);
219 };
220 
221 #endif
uint8_t getBatteryVoltage()
Definition: NXTMMX.cpp:34
int32_t getEncoderPosition(uint8_t which_motor)
Definition: NXTMMX.cpp:108
This class implements I2C interfaces used by NXShield.
Definition: NXShieldI2C.h:32
void stop(void)
Definition: SoftI2cMaster.cpp:119
bool getTimeToRun(uint8_t which_motor, int seconds)
Definition: NXTMMX.cpp:71
bool resetEncoder(uint8_t which_motor)
Definition: NXTMMX.cpp:176
bool setEncoderTarget(uint8_t which_motor, long target)
Definition: NXTMMX.cpp:46
bool setSpeedPID(uint16_t Kp, uint16_t Ki, uint16_t Kd)
Definition: NXTMMX.cpp:139
This class interfaces with NXTMMX attached to NXShield.
Definition: NXTMMX.h:118
void waitUntilTachoDone(uint8_t which_motors)
Definition: NXTMMX.cpp:280
bool setSpeedTimeAndControl(uint8_t which_motors, int speed, uint8_t duration, uint8_t control)
Definition: NXTMMX.cpp:185
bool startMotorsInSync()
Definition: NXTMMX.cpp:170
void runSeconds(uint8_t which_motors, uint8_t direction, int speed, uint8_t duration, uint8_t wait_for_completion, uint8_t next_action)
Definition: NXTMMX.cpp:317
bool setEncoderSpeedTimeAndControl(uint8_t which_motors, long encoder, int speed, uint8_t duration, uint8_t control)
Definition: NXTMMX.cpp:226
long getEncoderTarget(uint8_t which_motor)
Definition: NXTMMX.cpp:51
void runUnlimited(uint8_t which_motors, uint8_t direction, int speed)
Definition: NXTMMX.cpp:306
bool setTolerance(uint8_t tolerance)
Definition: NXTMMX.cpp:155
bool isTachoDone(uint8_t which_motors)
Definition: NXTMMX.cpp:269
void runDegrees(uint8_t which_motors, uint8_t direction, int speed, long degrees, uint8_t wait_for_completion, uint8_t next_action)
Definition: NXTMMX.cpp:376
bool setPassCount(uint8_t pass_count)
Definition: NXTMMX.cpp:148
bool reset()
Definition: NXTMMX.cpp:164
void runTachometer(uint8_t which_motors, uint8_t direction, int speed, long tachometer, uint8_t relative, uint8_t wait_for_completion, uint8_t next_action)
Definition: NXTMMX.cpp:337
void waitUntilTimeDone(uint8_t which_motors)
Definition: NXTMMX.cpp:262
int8_t getSpeed(uint8_t which_motor)
Definition: NXTMMX.cpp:64
bool isTimeDone(uint8_t which_motors)
Definition: NXTMMX.cpp:251
bool setEncoderPID(uint16_t Kp, uint16_t Ki, uint16_t Kd)
Definition: NXTMMX.cpp:130
bool setSpeed(uint8_t which_motor, int speed)
Definition: NXTMMX.cpp:59
void runRotations(uint8_t which_motors, uint8_t direction, int speed, long rotations, uint8_t wait_for_completion, uint8_t next_action)
Definition: NXTMMX.cpp:389
uint8_t issueCommand(char command)
Definition: NXTMMX.cpp:39
uint8_t getMotorStatusByte(uint8_t which_motor)
Definition: NXTMMX.cpp:115
NXTMMX(uint8_t i2c_address=0x06)
Definition: NXTMMX.cpp:27
uint8_t getMotorTasksRunningByte(uint8_t which_motor)
Definition: NXTMMX.cpp:122