Now all motors work fine even simultaneously and without intermediate aditional Wait - and no error messages appear any more!
This is the final source code so far for TXMotorOn (= Tetrix motor on fwd /rev with PWM):
Code: Select all
#define printf5( _x, _y, _format1,_format2,_format3,_format4,_format5,_value1,_value2,_value3,_value4,_value5) { \
string sval1 = FormatNum(_format1, _value1); \
string sval2 = FormatNum(_format2, _value2); \
string sval3 = FormatNum(_format3, _value3); \
string sval4 = FormatNum(_format4, _value4); \
string sval5 = FormatNum(_format5, _value5); \
string s =sval1+sval2+sval3+sval4+sval5; \
TextOut(_x, _y, s); \
}
void TXMotorOn(byte NXTport, byte TXmotor, char percentage)
{
byte retLen = 0;
int i, j;
char modeMsg[];
char powerMsg[];
byte devAddr, modeReg, powerReg;
char IOresult;
// addresses and registers
devAddr = (TXmotor+2)&14;
modeReg = 0x44 + (TXmotor % 2)*3;
powerReg= 0x45 + (TXmotor % 2);
ArrayBuild(modeMsg, devAddr, modeReg, 0);
ArrayBuild(powerMsg, devAddr, powerReg, percentage);
// Send the first message as soon as the bus is ready
TextOut(0,56,"M Rg md pow error");
while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Wait(1);
IOresult=I2CWrite(NXTport, retLen, modeMsg);
if (IOresult != NO_ERR)
{ printf5(0,48-(8*TXmotor),"%d","%3d","%2d","%5d","%4d",TXmotor,modeMsg[1],modeMsg[2],powerMsg[2],IOresult);
return; } // for Error debug
printf5(0,48-(8*TXmotor),"%d","%3d","%2d","%5d","%4d",TXmotor,modeMsg[1],modeMsg[2],powerMsg[2],IOresult);
// Send the second message when the first one is done
ArrayBuild(powerMsg, devAddr, powerReg, percentage);
while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Wait(1);
IOresult=I2CWrite(NXTport, retLen, powerMsg);
if (IOresult != NO_ERR)
{ printf5(0,48-(8*TXmotor),"%d","%3d","%2d","%5d","%4d",TXmotor,modeMsg[1],modeMsg[2],powerMsg[2],IOresult);
return; } // for Error debug
printf5(0,48-(8*TXmotor),"%d","%3d","%2d","%5d","%4d",TXmotor,modeMsg[1],modeMsg[2],powerMsg[2],IOresult);
}
task main(){
SetSensorType(S1, SENSOR_TYPE_LOWSPEED);
ResetSensor(S1);
Wait(20);
TXMotorOn(0, 0, 50);
TXMotorOn(0, 1, 20);
Wait(2000);
TXMotorOn(0, 0, 0); // brake
TXMotorOn(0, 1, 0); // brake
Wait(2000);
TXMotorOn(0, 0, -50);
TXMotorOn(0, 1, -20);
Wait(2000);
TXMotorOn(0, 0, 0); // coast
TXMotorOn(0, 1, -128); // brake
Wait(2000);
while (true);
}