NXC: I²C drivers for Hitechnic Tetrix motor muxer ?

Discussion specific to the intelligent brick, sensors, motors, and more.
gloomyandy
Posts: 323
Joined: 29 Sep 2010, 05:03

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by gloomyandy »

Looks to me like you are getting an error from the i2cwrite function. Probably worth adding an extra print if you get an error return. At a guess I would say that you can't do two writes to the device very close together and adding the sound function delays things enough so that the hardware has sorted itself out and is ready to accept the next command. Strange stuff hardware (especially "toy" hardware that is probably only tested with the standard firmware and programming language)...

Andy
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by HaWe »

you're probably right -
but strangely I get the error at the first i2cwrite (modeMsg), not the direct following one (powerMsg)

Code: Select all

#define printf2( _x, _y, _format1, _format2, _value1, _value2) { \
  string sval1 = FormatNum(_format1, _value1); \
  string sval2 = FormatNum(_format2, _value2); \
  string s =sval1+sval2; \
  TextOut(_x, _y, s); \
}

void TXMotorOn(byte NXTport, byte TXmotor, char percentage)
{
  const byte retLen = 0;
  int i, j;
  char modeMsg[];
  char powerMsg[];
  byte devAddr, modeReg, powerReg;
  char result;

  // constant values for testing
  devAddr = 0x02;
  modeReg = 0x44 ;
  powerReg= 0x45 ;
  
    // Create the message arrays
  ArrayBuild(modeMsg, devAddr, modeReg, 0);
  ArrayBuild(powerMsg, devAddr, powerReg, percentage);

  // Send the first message as soon as the bus is ready
  while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
  if(I2CWrite(NXTport, retLen, modeMsg) != NO_ERR)  
    { printf2(0,8,"rErr%2d"," vErr=%4d",modeMsg[1],modeMsg[2]); return;  } // Error msg displayed!!

  printf2(0,56,"mReg%2d"," mVal=%4d",modeMsg[1],modeMsg[2]);
  
  // Send the second message when the first one is done
  while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
  if(I2CWrite(NXTport, retLen, powerMsg) != NO_ERR)   
    { printf2(0,0,"pErr%2d"," vErr=%4d",powerMsg[1],j=powerMsg[2]); return;  } //*no* Error displayed!!
  
  printf2(0,48,"pReg%2d"," pVal=%4d",powerMsg[1],j=powerMsg[2]);
}

task main(){

  SetSensorType(S1, SENSOR_TYPE_LOWSPEED);


  TXMotorOn(0, 1, 50);
  Wait(2000);

  TXMotorOn(0, 1,  0);  // stop
  Wait(1000);

  TXMotorOn(0, 1,-50);
  Wait(2000);

  TXMotorOn(0, 1, -128); // coast
  Wait(2000);

  while (true);
}
that means (CMIIW) that already the first of the two i2cwrites fails, then the execution of the subroutine breaks and returns to main - so the 2nd i2cwrite has no chance at all to be executed any more...

but why-oh-why does it then execute the 3rd cmd (go reverse -50) ???

that's really weird... :evil:
Last edited by HaWe on 29 Dec 2010, 21:32, edited 1 time in total.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by HaWe »

BTW:
the Tetrix muxer works with NXT-G and RobotC.
IIRC the Mindsensors guys once made some changes to the I2c protocol.....? (can't find the thread right now....)
gloomyandy
Posts: 323
Joined: 29 Sep 2010, 05:03

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by gloomyandy »

It may be that the device need a little time to settle after it has been powered on/initialized... I notice that your working code has a sound being played between initializing the port and sending the first command, perhaps that gives time for things to settle?
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by mightor »

doc-helmut wrote:IIRC the Mindsensors guys once made some changes to the I2c protocol.....? (can't find the thread right now....)
The only change they made was to allow it to work with much longer cables. It was a small timing fix. The protocol has not changed.

If you have trouble with I2C commands, the first thing to try is put more time between each one.

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by HaWe »

ok, I will try, but as I already said:
I (actually it was YOU who wrote this code) always send a couple of i2c cmds - 1st the mode cmd, then the power cmd.
Now it's not the time BETWEEN the i2c cmds that causes the failure, it's already the 1st one of the couple (mode) that doesn't execute (instead the funtion executes the line behind

Code: Select all

if(I2CWrite(NXTport, retLen, modeMsg) != NO_ERR) {...return}
and breaks the subroutine.

If it was a matter of time delay between both of them, the 1st one should be executed anyway and the 2nd one should cause the error (show error msg and then return to main) - but it doesn't.

The "sound-check" that I inserted is started before the subroutine is called and so actually must not have any effect of the call inside the subroutine - but nevertheless it obviously has.

And I really don't get that. :?
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by mightor »

I wish I had one of each of these controllers so I could test this code. It would make debugging this a lot easier.

Try to capture the actual error code that call returns, we know it's not NO_ERR, but what is it?

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by HaWe »

hi,
I added some debugging lines, and it says
error=-32

Code: Select all

#define printf2( _x, _y, _format1, _format2, _value1, _value2) { \
  string sval1 = FormatNum(_format1, _value1); \
  string sval2 = FormatNum(_format2, _value2); \
  string s =sval1+sval2; \
  TextOut(_x, _y, s); \
}

void TXMotorOn(byte NXTport, byte TXmotor, char percentage)
{
  const byte retLen = 0;
  int i, j;
  char modeMsg[];
  char powerMsg[];
  byte devAddr, modeReg, powerReg;
  char IOresult;

  // constant values for testing
  devAddr = 0x02;
  modeReg = 0x44 ;
  powerReg= 0x45 ;
  
    // Create the message arrays
  ArrayBuild(modeMsg, devAddr, modeReg, 0);
  ArrayBuild(powerMsg, devAddr, powerReg, percentage);

  // Send the first message as soon as the bus is ready
  while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
  IOresult=I2CWrite(NXTport, retLen, modeMsg);
  if (IOresult != NO_ERR)
    { printf2(0,8,"mErr%2d"," Err=%4d",modeMsg[1],IOresult); return;  }  // Error debug: error =-32

  printf2(0,56,"mReg%2d"," mVal=%4d",modeMsg[1],modeMsg[2]);
  
  // Send the second message when the first one is done
  while ((I2CCheckStatus(NXTport) == STAT_COMM_PENDING) && (I2CCheckStatus(NXTport) != NO_ERR)) Yield();
  IOresult=I2CWrite(NXTport, retLen, powerMsg);
  if (IOresult != NO_ERR)
    { printf2(0,0,"pErr%2d"," Err=%4d",powerMsg[1],IOresult); return;  }  // Error debug: no error
  
  printf2(0,48,"pReg%2d"," pVal=%4d",powerMsg[1],j=powerMsg[2]);
}

task main(){

  SetSensorType(S1, SENSOR_TYPE_LOWSPEED);

  TXMotorOn(0, 1, 50);
  Wait(2000);

  TXMotorOn(0, 1,  0);  // brake
  Wait(1000);

  TXMotorOn(0, 1,-50);
  Wait(2000);

  TXMotorOn(0, 1, -128); // coast
  Wait(2000);
  while (true);
}
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by HaWe »

ps:
Xander, I can lend you an extra Tetrix muxer (=controller), and 2 extra Tetrix motors, and 1 extra encoder.

The stuff actually is not mine but from a friend (Frank from the German Mindstorms Forum who is also interested in a NXC driver). I also could send you my set, but it's better if you his stuff , so that I can test simultaneously the things you do.

Additionally I can lend you my own Servo muxer (plugged to the motor muxer) with 2 servos , in the moment in don't use them.

Send me a mail with your address, I will arrange that if you wish!
Last edited by HaWe on 31 Dec 2010, 06:45, edited 1 time in total.
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: I²C Tetrix drivers (motor controller)?

Post by mightor »

Helmut,

Let me think about it :) I have two spare motors and two spare servos at the moment. I was recently given a VEX Cortex Super Bundle to play with, so I have plenty of hardware.

I have a fair bit of stuff I am testing and playing with at the moment, I'll have to get back to you on your offer :)

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests