NXC: WriteI2CRegister compile error

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: WriteI2CRegister compile error

Post by HaWe »

(because it's more a compiler or syntax problem, I moved this post from the Tetrix hardware thread to the software forum...)

I just tried my first Tetrix Motor Control program, but it doesn't compile!
(Bricxcc 3.3.8.9. built 3.11.2010)

Code: Select all

// #include "nxcio.h"


void TXMotorOn(byte NXTport, byte TXmotor, char percentage)
{
  byte devAddr, modeReg, powerReg;
  char result;

  devAddr = (TXmotor+2)&14;
  modeReg = 0x44 + (TXmotor % 2)*3;
  powerReg= 0x45 + (TXmotor % 2);

  // NXTsensorPort, I2Caddress, 44H byte Motor_mode: 0=power control
  result=WriteI2CRegister(NXTport, devAddr, modeReg,  0);
  // NXTsensorPort, I2Caddress, 45H s/byte  Motor_power: e.g. 50 =50%
  result=WriteI2CRegister(NXTport, devAddr, powerReg, percentage);
}

task main(){

  TXMotorOn(0, 1, 50);
  Wait(2000);
  TXMotorOn(0, 1,-50);
  Wait(2000);
  TXMotorOn(0, 1,  0);
  Wait(2000);
  while (true);
}
# Error: Invalid constant expression: __TXMotorOn_7qG2_modeReg_7qG2_000
File "c:\Temp\temp.nxc" ; line 15
# set __WDSC_SensorRegister, __TXMotorOn_7qG2_modeReg_7qG2_000
#----------------------------------------------------------
# Error: Invalid constant expression: __TXMotorOn_7qG2_powerReg_7qG2_000
File "c:\Temp\temp.nxc" ; line 17
# set __WDSC_SensorRegister, __TXMotorOn_7qG2_powerReg_7qG2_000
#----------------------------------------------------------
2 errors during compilation
what's wrong???
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: WriteI2CRegister compile error

Post by mightor »

I did some experimentation and it seems that if the register is not known at compile time then it just won't work.

Code: Select all

result=WriteI2CRegister(NXTport, devAddr, powerReg, percentage);  
breaks

Code: Select all

result=WriteI2CRegister(NXTport, devAddr, 0x45, percentage); 
compiles fine.

So you need to use the standard method of I2C communication, not this short cut.

- 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: WriteI2CRegister compile error

Post by HaWe »

thx for your hint, Xander! I already was almost back to despair.

I will post it to the wishlist, but can you pls tell me how to rebuild the code to the standard method in the meantime?
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: WriteI2CRegister compile error

Post by mightor »

Try this, it hasn't been tested, of course. I based the code on other stuff I've written before.

Code: Select all

// #include "nxcio.h"


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

  devAddr = (TXmotor+2)&14;
  modeReg = 0x44 + (TXmotor % 2)*3;
  powerReg= 0x45 + (TXmotor % 2);

  // Create the message arrays
  ArrayBuild(modeMsg, devAddr, modeReg);
  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)
    return;

  // 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)
    return;
}

task main(){

  TXMotorOn(0, 1, 50);
  Wait(2000);
  TXMotorOn(0, 1,-50);
  Wait(2000);
  TXMotorOn(0, 1,  0);
  Wait(2000);
  while (true);
}
| 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)
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: WriteI2CRegister compile error

Post by mightor »

The first message needs to built up like this:

Code: Select all

ArrayBuild(modeMsg, devAddr, modeReg, 0);
My bad.

- 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)
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: WriteI2CRegister compile error

Post by mightor »

This code should do what you need:

Code: Select all

long MyWriteI2CRegister(byte port, byte address, byte register, byte regvalue) {
  const byte retLen = 0;
  byte msg[];

  // Create the message array
  ArrayBuild(msg, address, register, regvalue);

  // Send the first message as soon as the bus is ready
  while ((I2CCheckStatus(port) == STAT_COMM_PENDING) && (I2CCheckStatus(port) != NO_ERR)) Yield();
  return I2CWrite(port, retLen, msg);
}
Again, not tested :)

- 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: WriteI2CRegister compile error

Post by HaWe »

thx a lot again, Xander.
I tried the new code with the new ArrayBuild,
it compiles without error,
mux power is on,
the NXT screen says "running"

- but nothing happens. :cry:
I surely will therefore have to wait for John's Tetrix implementation.

---
acc. to your

Code: Select all

MyWriteI2CRegister(byte port, byte address, byte register, byte regvalue)
will it be possible to pass also a int or a long as a regvalue (e.g., for passing encoder values)?
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: NXC: WriteI2CRegister compile error

Post by mightor »

doc-helmut wrote:will it be possible to pass also a int or a long as a regvalue (e.g., for passing encoder values)?
Nope, as long as NXC doesn't support function overloading it can't be done with a single function name.

- 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: WriteI2CRegister compile error

Post by HaWe »

ok, thx, but I must admit: it is even much harder than I thought with the I2C motor program for Tetrix
But that's not a WriteI2CRegister compile issue any longer.

- thread can be closed -
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: WriteI2CRegister compile error

Post by afanofosc »

I am fixing this problem in WriteI2CRegister so that you can pass in a variable or a constant.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests