When I read the status register, I periodically see that bits 0 and 3 are both set indicating that the motor is operating in both position and speed control. Does this make sense?
My code (for two motors acting like windshield wipers) is as follows:
Code: Select all
#include "NXTMMX-lib.nxc"
// Device Address
#define DIST_ADDR 0x02
#define SensorPort S4
#define SENSORTHRESHOLD 30
#define TAPETHRESHOLD 52
#define LOWSPEED 55
#define MEDSPEED 70
#define HIGHSPEED 81
#define MAXSPEED 100
#define CW 1
#define CCW 2
const byte Addr = 0x06;
task main() // main task:
{
int v;
long lir,lr; // left initial rotation, left rotation
long lang; // left angle
string msg;
long a,b,x,c,d;
bool leftll, leftul, rightll, rightul ;
int dir ;
float ang ;
float y ;
byte m1, m2 ;
// Initiialization code
MMX_Init( SensorPort, Addr, 0 );
m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
NumOut(1, LCD_LINE1, m1, 0);
NumOut(20, LCD_LINE1, m1, 0);
MMX_Run_Degrees (SensorPort, Addr, MMX_Motor_1, MMX_Direction_Reverse,
10, 130, MMX_Completion_Wait_For, MMX_Next_Action_Brake);
MMX_Run_Degrees (SensorPort, Addr, MMX_Motor_2, MMX_Direction_Reverse,
10, 200, MMX_Completion_Wait_For, MMX_Next_Action_Brake);
m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
NumOut(1, LCD_LINE2, m1, 0);
NumOut(20, LCD_LINE2, m2, 0);
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Forward, 10);
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Reverse, 10);
dir = CCW ;
m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
NumOut(1, LCD_LINE3, m1, 0);
NumOut(20, LCD_LINE3, m2, 0);
Wait(200) ;
while(true)
{
a = -MMX_ReadTachometerPosition(SensorPort, Addr, MMX_Motor_1);
b = -MMX_ReadTachometerPosition(SensorPort, Addr, MMX_Motor_2);
if (a > 200 && dir == CCW) {
MMX_Stop(SensorPort, Addr, MMX_Motor_1, MMX_Next_Action_Float);
rightul = true ;
}
if (a < 130 && dir == CW) {
MMX_Stop(SensorPort, Addr, MMX_Motor_1, MMX_Next_Action_Float);
rightll = true ;
}
if (b > 200 && dir == CW) {
MMX_Stop(SensorPort, Addr, MMX_Motor_2, MMX_Next_Action_Float);
leftul = true ;
}
if (b < 130 && dir == CCW) {
MMX_Stop(SensorPort, Addr, MMX_Motor_2, MMX_Next_Action_Float);
leftll = true ;
}
if (rightul && leftll)
{
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Forward, 0);
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Reverse, 0);
Wait(300) ;
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Forward, 10);
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Reverse, 10);
m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
NumOut(1, LCD_LINE4, m1, 0);
NumOut(20, LCD_LINE4, m2, 0);
Wait(200) ;
m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
NumOut(1, LCD_LINE5, m1, 0);
NumOut(20, LCD_LINE5, m2, 0);
rightul = leftll = false ;
dir = CW ;
}
if (rightll && leftul)
{
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Reverse, 0);
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Forward, 0);
Wait(300) ;
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_1,MMX_Direction_Reverse, 10);
MMX_Run_Unlimited( SensorPort, Addr, MMX_Motor_2,MMX_Direction_Forward, 10);
m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
NumOut(1, LCD_LINE4, m1, 0);
NumOut(20, LCD_LINE4, m2, 0);
Wait(200);
m1 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_1) ;
m2 = MMX_MotorStatus(SensorPort, Addr, MMX_Motor_2) ;
NumOut(1, LCD_LINE5, m1, 0);
NumOut(20, LCD_LINE5, m2, 0);
rightll = leftul = false ;
dir = CCW ;
}
}
}