| EVShield v1.3 EVShield Library Reference by mindsensors.com

EVs_NXTMMX.h
1
2 // EVs_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 EVs_NXTMMX_H
28 #define EVs_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 "EVShieldI2C.h"
114
118 class EVs_NXTMMX : public EVShieldI2C
119 {
120 public:
123 EVs_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
SoftI2cMaster::stop
void stop(void)
Definition: SoftI2cMaster.cpp:119
EVs_NXTMMX::runTachometer
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: EVs_NXTMMX.cpp:339
EVs_NXTMMX::startMotorsInSync
bool startMotorsInSync()
Definition: EVs_NXTMMX.cpp:172
EVs_NXTMMX::getMotorTasksRunningByte
uint8_t getMotorTasksRunningByte(uint8_t which_motor)
Definition: EVs_NXTMMX.cpp:124
EVs_NXTMMX::setSpeed
bool setSpeed(uint8_t which_motor, int speed)
Definition: EVs_NXTMMX.cpp:61
EVs_NXTMMX::waitUntilTimeDone
void waitUntilTimeDone(uint8_t which_motors)
Definition: EVs_NXTMMX.cpp:264
EVs_NXTMMX::getSpeed
int8_t getSpeed(uint8_t which_motor)
Definition: EVs_NXTMMX.cpp:66
EVs_NXTMMX::getBatteryVoltage
uint8_t getBatteryVoltage()
Definition: EVs_NXTMMX.cpp:36
EVs_NXTMMX
This class interfaces with NXTMMX attached to EVShield.
Definition: EVs_NXTMMX.h:118
EVs_NXTMMX::runSeconds
void runSeconds(uint8_t which_motors, uint8_t direction, int speed, uint8_t duration, uint8_t wait_for_completion, uint8_t next_action)
Definition: EVs_NXTMMX.cpp:319
EVs_NXTMMX::setSpeedTimeAndControl
bool setSpeedTimeAndControl(uint8_t which_motors, int speed, uint8_t duration, uint8_t control)
Definition: EVs_NXTMMX.cpp:187
EVs_NXTMMX::runUnlimited
void runUnlimited(uint8_t which_motors, uint8_t direction, int speed)
Definition: EVs_NXTMMX.cpp:308
EVs_NXTMMX::reset
bool reset()
Definition: EVs_NXTMMX.cpp:166
EVs_NXTMMX::getMotorStatusByte
uint8_t getMotorStatusByte(uint8_t which_motor)
Definition: EVs_NXTMMX.cpp:117
EVs_NXTMMX::EVs_NXTMMX
EVs_NXTMMX(uint8_t i2c_address=0x06)
Definition: EVs_NXTMMX.cpp:29
EVShieldI2C
This class implements I2C interfaces used by EVShield.
Definition: EVShieldI2C.h:32
EVs_NXTMMX::waitUntilTachoDone
void waitUntilTachoDone(uint8_t which_motors)
Definition: EVs_NXTMMX.cpp:282
EVs_NXTMMX::setTolerance
bool setTolerance(uint8_t tolerance)
Definition: EVs_NXTMMX.cpp:157
EVs_NXTMMX::setPassCount
bool setPassCount(uint8_t pass_count)
Definition: EVs_NXTMMX.cpp:150
EVs_NXTMMX::issueCommand
uint8_t issueCommand(char command)
Definition: EVs_NXTMMX.cpp:41
EVs_NXTMMX::setEncoderPID
bool setEncoderPID(uint16_t Kp, uint16_t Ki, uint16_t Kd)
Definition: EVs_NXTMMX.cpp:132
EVs_NXTMMX::setEncoderSpeedTimeAndControl
bool setEncoderSpeedTimeAndControl(uint8_t which_motors, long encoder, int speed, uint8_t duration, uint8_t control)
Definition: EVs_NXTMMX.cpp:228
EVs_NXTMMX::runDegrees
void runDegrees(uint8_t which_motors, uint8_t direction, int speed, long degrees, uint8_t wait_for_completion, uint8_t next_action)
Definition: EVs_NXTMMX.cpp:378
EVs_NXTMMX::setEncoderTarget
bool setEncoderTarget(uint8_t which_motor, long target)
Definition: EVs_NXTMMX.cpp:48
EVs_NXTMMX::getTimeToRun
bool getTimeToRun(uint8_t which_motor, int seconds)
Definition: EVs_NXTMMX.cpp:73
EVs_NXTMMX::isTimeDone
bool isTimeDone(uint8_t which_motors)
Definition: EVs_NXTMMX.cpp:253
EVs_NXTMMX::runRotations
void runRotations(uint8_t which_motors, uint8_t direction, int speed, long rotations, uint8_t wait_for_completion, uint8_t next_action)
Definition: EVs_NXTMMX.cpp:391
EVs_NXTMMX::isTachoDone
bool isTachoDone(uint8_t which_motors)
Definition: EVs_NXTMMX.cpp:271
EVs_NXTMMX::getEncoderPosition
int32_t getEncoderPosition(uint8_t which_motor)
Definition: EVs_NXTMMX.cpp:110
EVs_NXTMMX::resetEncoder
bool resetEncoder(uint8_t which_motor)
Definition: EVs_NXTMMX.cpp:178
EVs_NXTMMX::getEncoderTarget
long getEncoderTarget(uint8_t which_motor)
Definition: EVs_NXTMMX.cpp:53
EVs_NXTMMX::setSpeedPID
bool setSpeedPID(uint16_t Kp, uint16_t Ki, uint16_t Kd)
Definition: EVs_NXTMMX.cpp:141