View unanswered posts | View active topics It is currently Tue Sep 29, 2020 12:38 pm






Reply to topic  [ 7 posts ] 
Problem with datatypes since updating to 2.01 
Author Message
Rookie

Joined: Wed Jan 27, 2010 3:22 pm
Posts: 4
Post Problem with datatypes since updating to 2.01
Hello all,
we have updated to the latest version of RobotC (2.01) and now several programs that worked perfectly under version 1.45 do no longer work.
There seem to have been changes in the way variables are handled by the new compiler.

for example I get a warning error: numeric constant ('144') out of unsigned char range. It will be truncated.

The line was:
const byte kI2CAddress = 0x90;

The driver and programming handles the I2C-communication for our iBall-sensors (Lightsensors) from Quantum Torque and is used in a soccer playing robot.

I attached the demo program for the sensor supplied by the manufacturer of the sensor.

any help would be greatly appreciated!
THX

Uwe Friedrich
Realschule Helmbrechts - Germany


Attachments:
iball_demo.c [2.93 KiB]
Downloaded 452 times
Sun Jan 31, 2010 2:36 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Problem with datatypes since updating to 2.01
Just turn it into an unsigned byte and you'll be good to go!

Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jan 31, 2010 2:48 pm
Profile WWW
Rookie

Joined: Wed Jan 27, 2010 3:22 pm
Posts: 4
Post Re: Problem with datatypes since updating to 2.01
Hi Xander,

thx for your quick reply, but I have already tried using ubyte instead of byte.
Result: No compiler errors but the sensor won't work.

I would be very thankful if you could have a look at the code for the demo program provided by the manufacturer.
The problem lies in the way the communication is done and which bytes are taken out of the I2C-replies.
I'm sorry but my experiences in programming c is just too limited.

THX again
Uwe


Here's the code:
Code:
/*
** www.quantumtorque.com
**
** RobotC demonstration program for "iball" sensor connected to Lego® NXT
** assumes iball is connected to sensor port 1 with primary I2C address 0x90
** iball readings displayed are in the range 0-1024
** Brett Myatt 2007
*/

#define NUMBYTES 3                                                // number of bytes that iball returns
#define BYTE1_MAX 7                                           // maximum value of first byte returned (high byte)
#define BYTE3_CONSTANT 128                                   // third byte iball returns is always 128

const tSensors port = S1;                                       // use Lego® NXT sensor port 1
const byte     kI2CAddress = 0x90;                        // iball I2C address

static const byte command[] = {1, kI2CAddress};    // read iball by sending I2C address


/* wait for I2C bus to be ready */
void waitForI2CBus()
{
   TI2CStatus nStatus;
   nStatus = nI2CStatus[port];
   while (true) {
      nStatus = nI2CStatus[port];

      switch (nStatus) {
         // no error, ok to continue
         case NO_ERR:
            return;

         // while pending, keep looping and checking status
         case STAT_COMM_PENDING:
            break;

         // must be an error, wait a bit and then continue on
         default:
            PlaySound(soundLowBuzz);
            wait10Msec(5);
            return;
      }
   }
}

int read_iball(const tSensors port, const byte &commmand) {

  byte readBytes;
  int reading;
  byte iball_data[NUMBYTES];

  readBytes = NUMBYTES;
  reading = 0;

  waitForI2CBus();

  // request iball reading
  sendI2CMsg(port, command[0], readBytes);

  waitForI2CBus();

  // returns 3 bytes: 1st byte = high byte, 2nd byte = low byte, 3rd byte = 128
  readI2CReply(port, iball_data[0], readBytes);

  // check to make sure correct number of bytes were received
  if (readBytes == NUMBYTES) {

    // now do a few checks to make sure data looks valid
    if ((( iball_data[0]&0x00FF) <= BYTE1_MAX) && ((iball_data[2]&0x00FF) == BYTE3_CONSTANT)) {

         // calculate and scale reading in range 0-1024
         reading = ((iball_data[0]&0x00FF)<<7) + ((iball_data[1]&0x00FF)>>1);
    }
  }
  return reading;
}

task main()
{
   byte leftoverMsg[1];                                          // array to hold left over I2C bytes
   SensorType[port] = sensorI2CCustomStd;               // use low speed I2C communications
   wait10Msec(10);                                                 // short startup delay

   /* make sure I2C read buffer is empty                                                          */
   while (nI2CStatus[port] == STAT_COMM_PENDING);
   while (nI2CBytesReady[port] > 0) readI2CReply(port, leftoverMsg[0], 1);


   while (true) {
   /* request and display iball reading                                              */
      nxtDisplayTextLine(6, "reading: %4d", (short)read_iball(S1, command[0]));
      wait1Msec(10);
   }

}


aThe mist


Mon Feb 01, 2010 12:11 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Problem with datatypes since updating to 2.01
Hey there Uwe,

Here's a modified version of the demo program. I can't test it as I don't have one of these sensors. Let me know if you get it working.

Code:
#pragma config(Sensor, S1,     port,                sensorLowSpeed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*
** www.quantumtorque.com
**
** RobotC demonstration program for "iball" sensor connected to Lego® NXT
** assumes iball is connected to sensor port 1 with primary I2C address 0x90
**
** (Note: a second iball sensor is available with alternative I2C address 0x92)
**
** iball readings displayed are in the range 0-1024
**
** Brett Myatt 2007
** Modified by Xander Soldaat 2010
*/

#define NUMBYTES        3           // number of bytes that iball returns
#define BYTE1_MAX       7           // maximum value of first byte returned (high byte)
#define BYTE3_CONSTANT  128         // third byte iball returns is always 128
#define I2C_ADDRESS     0x90        // iball I2C address

static const unsigned byte command[] = {1, I2C_ADDRESS};  // read iball by sending I2C address


/* wait for I2C bus to be ready */
void waitForI2CBus()
{
  TI2CStatus nStatus;
  nStatus = nI2CStatus[port];
  while (true) {
    nStatus = nI2CStatus[port];

    switch (nStatus) {
      // no error, ok to continue
      case NO_ERR:
        return;

      // while pending, keep looping and checking status
      case STAT_COMM_PENDING:
        break;

      // must be an error, wait a bit and then continue on
      default:
        PlaySound(soundLowBuzz);
        wait10Msec(5);
        return;
    }
  }
}

int read_iball(const tSensors port)
{
  int reading = 0;
  unsigned byte iball_data[NUMBYTES];

  waitForI2CBus();

  // request iball reading
  sendI2CMsg(port, command[0], NUMBYTES);

  waitForI2CBus();

  // returns 3 bytes: 1st byte = high byte, 2nd byte = low byte, 3rd byte = 128
  readI2CReply(port, iball_data[0], NUMBYTES);

  // now do a few checks to make sure data looks valid
  if ((( iball_data[0]&0x00FF) <= BYTE1_MAX) && ((iball_data[2]&0x00FF) == BYTE3_CONSTANT)) {
   
    // calculate and scale reading in range 0-1024
    reading = ((iball_data[0]&0x00FF)<<7) + ((iball_data[1]&0x00FF)>>1);
  }

  return reading;
}

task main()
{
  wait10Msec(10);                                 // short startup delay

  /* display heading                                                          */
  nxtDisplayTextLine(0, "www.");
  nxtDisplayTextLine(1, "quantumtorque");
  nxtDisplayTextLine(2, ".com");
  nxtDisplayTextLine(4, "iball Demo");

  while (true) {
  /* request and display iball reading                                        */
    nxtDisplayTextLine(6, "reading: %4d", read_iball(S1));

    wait1Msec(10);
  }

}


Regards,
Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Mon Feb 01, 2010 2:32 pm
Profile WWW
Rookie

Joined: Wed Jan 27, 2010 3:22 pm
Posts: 4
Post Re: Problem with datatypes since updating to 2.01
Hi Xander,

you're a genius! :D

The code you provided worked a charm! The iBall-sensors now give back the values. Great!

I tried to follow your programming with another sensor of quantum torque called cBall. But I don't get it working.
:(
Maybe you can give me a hand again and have a look at the original code provided by the manfacturer:

Code:
/*
** Program to test I2C Communications to "cball" sensor
** Assumes cball sensor with default address C0 is attached to port 1
**
**
*/

// use sensor port 1
const tSensors port = S1;

// default cball address
const byte     kI2CAddress = 0xB0;

/* Wait for I2C Bus to Be Ready */
void waitForI2CBus()
{
   TI2CStatus nStatus;
   nStatus = nI2CStatus[port];
   while (true) {
      nStatus = nI2CStatus[port];

      switch (nStatus) {
         // no error, ok to continue
         case NO_ERR:
            return;

         // while pending, keep looping and checking status
         case STAT_COMM_PENDING:
            break;

         // must be an error, wait a bit and then continue on
         default:
            PlaySound(soundLowBuzz);
            wait10Msec(10);
            return;
      }
   }
}

/* Main Task */
task main()
{
   // Setup the port
   SensorType[port] = sensorI2CCustomStd;

  // I2C Reply messages
   byte rMsg1[1], rMsg2[1], rMsg3[1], discard[1];

   // I2C sending message array
   byte i2cconfg[3];

   i2cconfg[0] = 2;
   i2cconfg[1] = kI2CAddress;

   wait10Msec(100);

   // Wait for I2C bus to be ready
   while (nI2CStatus[port] == STAT_COMM_PENDING);

   // Clear I2C read buffer
   while (nI2CBytesReady[port] > 0) readI2CReply(port, discard[0], 1);

   while(true) {
            i2cconfg[2] = 0x40;

            waitForI2CBus();
            sendI2CMsg(port, i2cconfg[0], 1);
            waitForI2CBus();
            readI2CReply(port, rMsg1[0], 1);

            nxtDisplayStringAt(0,40,  "MaxNumber:  %3d", (short)0x00FF & rMsg1[0]);

            wait10Msec(1);

            i2cconfg[2] = 0x41;

            waitForI2CBus();
            sendI2CMsg(port, i2cconfg[0], 1);
            waitForI2CBus();
            readI2CReply(port, rMsg2[0], 1);

            rMsg2[0] = rMsg2[0]>>4;

            if (rMsg2[0] == 0x02)
               nxtDisplayStringAt(0,20, "Proximity:  Far");
            else if (rMsg2[0] == 0x04)
               nxtDisplayStringAt(0,20, "Proximity: Near");
            else
               nxtDisplayStringAt(0,20, "Proximity:     ");

            wait10Msec(1);

            i2cconfg[2] = 0x42;

            waitForI2CBus();
            sendI2CMsg(port, i2cconfg[0], 1);
            waitForI2CBus();
            readI2CReply(port, rMsg3[0], 1);

            nxtDisplayStringAt(0,30, "Raw Value:  %3d", 0x00FF & rMsg3[0]);

            wait10Msec(1);
       }
}


Thank you very much again for your invaluable help!

Uwe


Wed Feb 03, 2010 3:22 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Problem with datatypes since updating to 2.01
Hiya Uwe,

This will probably work. Again, I couldn't test it. I contacted Quantum Torque a few days ago about writing supported and documented drivers for them, but they haven't replied to me yet. If I had access to their hardware, I would be happy to include them as part of the 3rd party driver suite.

Regards,
Xander

Code:
#pragma config(Sensor, S1,     CBALL,               sensorLowSpeed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*
** Program to test I2C Communications to "cball" sensor
** Assumes cball sensor with default address B0 is attached to port 1
**
** Modified for ROBOTC 2.0 by Xander Soldaat 2010
**
*/

// default cball address
#define IBALL_I2C_ADDRESS     0xB0
#define IBALL_REG_MAX_SIGNAL  0x40
#define IBALL_REG_MAX_INTENS  0x41
#define IBALL_REG_RAW_SIGNAL  0x42

/* Wait for I2C Bus to Be Ready */
void waitForI2CBus()
{
   while (true) {
      switch (nI2CStatus[CBALL]) {
         // no error, ok to continue
         case NO_ERR:
           return;
         
         // while pending, keep looping and checking status
         case STAT_COMM_PENDING:
        break;
         
         // must be an error, wait a bit and then continue on
         default:
            PlaySound(soundLowBuzz);
            wait10Msec(10);
            return;
      }
   }
}

/* Main Task */
task main()
{   
   // I2C Reply and request messages
  unsigned byte I2CReply[1];
  unsigned byte I2CRequest[3];
      
   I2CRequest[0] = 2;
   I2CRequest[1] = IBALL_I2C_ADDRESS;
   
   wait10Msec(100);
   
   while(true) {
    
     // Read the register containing the sensor number with
     // reciveing the max IR signal
     // Output: 0 - 8, 0 means no signal
      I2CRequest[2] = IBALL_REG_MAX_SIGNAL;
      
      waitForI2CBus();
      sendI2CMsg(CBALL, I2CRequest[0], 1);
      waitForI2CBus();
      readI2CReply(CBALL, I2CReply[0], 1);
      
      nxtDisplayStringAt(0,40,  "MaxNumber:  %3d", 0x00FF & I2CReply[0]);
      
      wait10Msec(1);
      
      // Read the intensity of the signal
      // Encoded in upper bits 7-4
      // Output:
      // 0x04 means strong signal
      // 0x02 means weak signal
      I2CRequest[2] = IBALL_REG_MAX_INTENS;
      
      waitForI2CBus();
      sendI2CMsg(CBALL, I2CRequest[0], 1);
      waitForI2CBus();
      readI2CReply(CBALL, I2CReply[0], 1);
      
      I2CReply[0] = I2CReply[0]>>4;
      
      if (I2CReply[0] == 0x02)
        nxtDisplayStringAt(0,20, "Proximity:  Far");
      else if (I2CReply[0] == 0x04)
        nxtDisplayStringAt(0,20, "Proximity: Near");
      else
        nxtDisplayStringAt(0,20, "Proximity:     ");
      
      wait10Msec(1);
      
      // Read the raw sensor value of the sensor with the
      // max IR signal.
      // Output: 0-255
      I2CRequest[2] = IBALL_REG_RAW_SIGNAL;
      
      waitForI2CBus();
      sendI2CMsg(CBALL, I2CRequest[0], 1);
      waitForI2CBus();
      readI2CReply(CBALL, I2CReply[0], 1);
      
      nxtDisplayStringAt(0,30, "Raw Value:  %3d", 0x00FF & I2CReply[0]);
      
      wait10Msec(1);
   }
}

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Thu Feb 04, 2010 2:46 am
Profile WWW
Rookie

Joined: Wed Jan 27, 2010 3:22 pm
Posts: 4
Post Re: Problem with datatypes since updating to 2.01
Hi Xander,

wow, again it works perfectly. Both programs for both sensors have been tested by me for quite a while. No problems! Superb work...

I really hope you get the hardware from quantum torque (Australia) to be able to include the sensors into your marvellous 3rd party driver suite.

THX so much

Uwe


Thu Feb 04, 2010 1:48 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.