View unanswered posts | View active topics It is currently Tue Jul 29, 2014 8:32 am






Reply to topic  [ 57 posts ]  Go to page 1, 2, 3, 4  Next
HT Color sensor: -> calibrating? 
Author Message
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post HT Color sensor: -> calibrating?
hi,
I tried a little with the HT Color Sensor, using a sample prog which I changed I little.
Testing the colors I found out that the returned green and red values always are too small, compared to blue,
so the colors appear "bluish".

e.g.
Code:
real_color  red  green  blue     recognized
white         70   70   100       3  = cyan
green         20    0   120       1  = violet
violet        10    0    90       1/2= violet/blue
cyan          16    8   120       2  = blue
yellow        80   50   105       11 = lightViolet
orange        20    0    60       3  = cyan
red           60    0    20       10 = pink
light blue    40    2   120       1  = violet
 


The color "names " may be a little faulty, but at the white color you may see clearly, that the 3 color values are not identical as they should be.

how can this be corrected by calibrating?

Code:
//////////////////////////////////////////////////////////////////////////////////////////
//
//                        Hitechnic Color Sensor Example
//                        ==============================
//
// This file is a small application program that tests the NXT Color Sensor from HiTechnic.
//
// The program:
//  1. Configures sensor port 'S2' as a HiTechnic color sensor.
//  2. Displays the current color components (red, green, blue) on the NXT LCD.
//  3. Checks to see whether the current color matches one of two predefined colors. If so
//     a unique tone is played on the NXT speaker to indicate which of the two colors were
//     matched.
//
// This sample provides a good framework for easy cut and paste and
//
//////////////////////////////////////////////////////////////////////////////////////////
 
#pragma platform (NXT, FTC)
 
const tSensors kColorSensorPort   = S2; // We want the color sensor to be on port "S2"
 
#include "AdvancedSensors.c"
 
    //
    // Get the colors from the sensor and display on the NXT LCD
    //
    int nRed;
    int nGreen;
    int nBlue;
    short nColor;
    string strColor;
 

void Color2String(short color, string &sbuf)
{
 
  if (color==0) sbuf="black";
  else
  if (color==1) sbuf="violet";
  else
  if (color==2) sbuf="blue";
  else
  if (color==3) sbuf="cyan";
  else
  if (color==4) sbuf="green";
  else
  if (color==5) sbuf="YeGreen";
  else
  if (color==6) sbuf="yellow";
  else
  if (color==7) sbuf="orange";
  else
  if (color==8) sbuf="red";
  else
  if (color==9) sbuf="magenta";
  else
  if (color==10) sbuf="pink";
  else
  if (color==11) sbuf="lViolet";
  else
  if (color==12) sbuf="lCyan";
  else
  if (color==13) sbuf="lYellow";
  else
  if (color==14) sbuf="lOrange";
  else
  if (color==15) sbuf="lRed";
  else
  if (color==16) sbuf="lPink";
  else
  if (color==17) sbuf="white";
 
}
 

task main()
{
  long nCycles = 0;
 
  SensorType[kColorSensorPort] = sensorI2CHiTechnicColor; // Configure the sensor
 
  nxtDisplayTextLine(1, "Port: S%d", kColorSensorPort + 1);
  while (true)
  {
 
    getRGB(kColorSensorPort, nRed, nGreen, nBlue);
    nColor=SensorValue[kColorSensorPort];
    Color2String(nColor,strColor);
 
    nxtDisplayTextLine(2, "Color:%3d %s", nColor, strColor);
    nxtDisplayTextLine(3, "Red:  %3d", nRed);
    nxtDisplayTextLine(4, "Green:%3d", nGreen);
    nxtDisplayTextLine(5, "Blue: %3d", nBlue);
 
    //
    // Use utility routine to see if current color detected is close enough
    // to a specific target color.
    //
 
    //
    // To determine the color constants to match on, manually "watch" the output from the
    // color sensor and record the components of a target color. Edit the data below
    //
    // Adjust the "kRange" constant as appropriate to determine how close the target colors have to
    // be to detect a specific color.
    //
    const int kRedTarget1    =  20;
    const int kGreenTarget1  =   0;
    const int kBlueTarget1   =   0;
 
    const int kRedTarget2    =  52;
    const int kGreenTarget2  =   0;
    const int kBlueTarget2   =   0;
 
    const int kColorRange    =  10;
 
    if (bColorMatched(kColorSensorPort, kRedTarget1, kGreenTarget1, kBlueTarget1, kColorRange))
      PlayTone(400, 20);
 
    else if (bColorMatched(kColorSensorPort, kRedTarget2, kGreenTarget2, kBlueTarget2, kColorRange))
      PlayTone(800, 20);
 
    else
      ClearSounds();
 
    // Display the number of cycles on the LCD. It runs pretty fast.
 
    ++nCycles;
    nxtDisplayTextLine(7, "Cycles: %d",  nCycles);
  }
  return;
}
 

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Nov 14, 2008 2:37 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: HT Color sensor: -> calibrating?
I tried to get support from Hitechnic, but unfortunately they don't support RobotC. Here is what they wrote:

Quote:
We actually don’t support RobotC as it is a third party product and is not associated with HiTechnic or LEGO. All support for RobotC is provided by the producers of RobotC. I note that you have posted on the RobotC forums with this and other questions and that is the place to get support.

Also note that RobotC uses a non-standard I2C speed to communicate with the sensors and we (HiTechnic) and LEGO do not endorse or support this method of accessing the sensors. In a number of instances we have found that reported sensor problems have been the result of this non-standard I2C speed so if you encounter any problems with any of the sensors the first thing to do is set the I2C speed to the LEGO standard.

Regards,

Steve
(HiTechnic)


So this - again - is a support request specifically to the RobotC developers!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Nov 28, 2008 1:05 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: HT Color sensor: -> calibrating?
what a ****ing support!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Wed Dec 10, 2008 1:45 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 601
Post Re: HT Color sensor: -> calibrating?
ROBOTC supports all of the features of the color sensor through I2C messaging. I would test out and see what I could do about the color sensor, except the three we have in our office no longer work for some reason.

NXT-G doesn't have some magical "calibration" tool. I would anticipate that it just sets the Normalized color values available here:
4DH byte Normalized red reading
4EH byte Normalized green reading
4FH byte Normalized blue reading

From our communication with HiTechnic in the past regarding the color sensor, they apparently have to be shipped back to HiTechnic to be "recalibrate". DickSwan might have more information regarding that.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Fri Dec 12, 2008 10:11 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: HT Color sensor: -> calibrating?
I got additional information on this subject, but I don't understand how to write an I²C program because for me this is far too complicated and cumbersome to do in RobotC:
Hitechnic wrote:
You can recalibrate the color sensor by using an I2C write command and write 0x43 to location 0x41 (?????????). Note that the sensor MUST be facing a white target surface at the prescribed distance before executing the calibrate otherwise the sensor may not work at all until successfully recalibrated...
Once the calibrate command has been sent to the sensor it will complete the calibration in about ½ a second and then reset and be ready to use as normal.

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Dec 12, 2008 10:33 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: HT Color sensor: -> calibrating?
Ford,

I2C isn't all that cumbersome :) I am sure I could cobble something together in a couple of minutes. I can't test it as I don't have one of these sensors.

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]


Fri Dec 12, 2008 10:55 am
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: HT Color sensor: -> calibrating?
OT:
Why isn't it possible to install just 2 easy-to-use macros like
Code:
writeI2Cvalue(byte value, byte location, byte port)
// and
readI2Cvalue(byte &value, byte location, byte port)


which don't need any of those two pages full of unintelligible code (except in a library or header file)?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Dec 12, 2008 11:17 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: HT Color sensor: -> calibrating?
Ford Prefect wrote:
OT:
Why isn't it possible to install just 2 easy-to-use macros like
Code:
writeI2Cvalue(byte value, byte location, byte port)
// and
readI2Cvalue(byte &value, byte location, byte port)


which don't need any of those two pages full of unintelligible code (except in a library or header file)?

I have such a header file :) Right now it only has two functions, but I think that having two functions like yours would be a pretty simple addition. I would change the order of the parameters somewhat to make it match the rest of my functions. After all, it's *my* header file :P

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]


Fri Dec 12, 2008 12:40 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: HT Color sensor: -> calibrating?
Here it is. It has only been compiled, not actually tested as I don't own a colour sensor.

#include <std_warnings_and_disclaimers.h>

Regards,
Xander

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

#include "i2c.h"

void calibrateSensor(tSensors _link) {
  // we need to send the command 0x43 to register 0x41
  byte_array _data;
  _data[0] = 3;           // the size of our message
  _data[1] = _link;       // the sensor port our sensor is attached to
  _data[2] = 0x41;        // the register we're writing to
  _data[3] = 0x43;        // the command we're sending (calibrate)

  writeI2Cvalue(_link, _data, 0);
}

task main() {
  calibrateSensor(HTC);
  while(true) {
    wait1Msec(10);
  }
}


Code:
#ifndef __I2C_H__
#define __I2C_H__

#define MAX_ARR_SIZE 16
typedef byte byte_array[MAX_ARR_SIZE];

void clearI2CBus(tSensors _link);
void waitForI2CBus(tSensors _link);
void writeI2Cvalue(tSensors _link, byte_array &_data, int _replylen);
void readI2Cvalue(tSensors _link, byte_array &_data, int _replylen);

/*
  =============================================================================
  Clear the I2C bus before we send new data.

 */
void clearI2CBus(tSensors _link) {
  ubyte _tmp = 0;
  while (nI2CBytesReady[_link] > 0)
    readI2CReply(_link, _tmp, 1);
}

/*
  =============================================================================
  We wait for the I2C bus to be ready for the next message or emit a
  loud annoying buzz sound if there's an error.
  Taken from RobotC example code and modified.

 */
void waitForI2CBus(tSensors _link)
{
   byte _tmp = 0;
   TI2CStatus nStatus;

   while (true)
   {
      nStatus = nI2CStatus[_link];
      switch (nStatus)
      {
      case NO_ERR:
      return;

      case STAT_COMM_PENDING:
         break;

      case ERR_COMM_BUS_ERR:
         PlaySound(soundLowBuzz);
         while (bSoundActive) {}
         // clear out any old buffers.
      clearI2CBus(_link);
         return;
      }
   }
}

void writeI2Cvalue(tSensors _link, byte_array &_data, int _replylen) {
  clearI2CBus(_link);
  waitForI2CBus(_link);
  sendI2CMsg(_link, _data[0], _replylen);
  waitForI2CBus(_link);
}

void readI2Cvalue(tSensors _link, byte_array &_data, int _replylen) {
  readI2CReply(_link, _data[0], _replylen);
  waitForI2CBus(_link);
}


#endif // __I2C_H__


Attachments:
fordprefect.zip [1.16 KiB]
Downloaded 399 times

_________________
| 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]
Fri Dec 12, 2008 1:10 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: HT Color sensor: -> calibrating?
Hi Xander,
you're a genius!

The good news: the program runs ;-)

Before Calibrating, pointing to a real white sheet of paper, the screen showed before:
Code:
real_color  red  green  blue     recognized
white         70   50   110       3  = cyan // 1.0 cm distance
white         80   60   130       3  = cyan // 1.5 cm distance


after running your calibration program, it currently showes:
Code:
real_color  red  green  blue     recognized
white         70   48   120       3  = cyan
// up to
white         66   12   82        3  = cyan


So unfortunately there is actually not a big improvement... :cry:
It's not supposed to be your fault, I guess, I think it's the calibration routine itself - or the sensor is faulty!

Or maybe Hitechnic is right and the faulty colors come from a wrong I²C speed.
How can the I²C speed be set to the Lego standard?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Dec 12, 2008 2:54 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 601
Post Re: HT Color sensor: -> calibrating?
You make the Macro definitions you want, and we'll include them in the next build.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Fri Dec 12, 2008 3:04 pm
Profile
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 601
Post Re: HT Color sensor: -> calibrating?
The "Low Speed" which is the NXT-G standard won't make a difference... but if you feel like trying it...

Code:
const tSensors kColorSensorPort   = S2;
SensorType[kColorSensorPort] = sensorI2CCustom;


I don't believe it's a 9V sensor, but if it is, you can use "sensorI2CCustom9V" as the type.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Fri Dec 12, 2008 3:18 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: HT Color sensor: -> calibrating?
I changed Xander's program to

Code:

#include "i2c.h"

const tSensors HTC   = S1;

void calibrateSensor(tSensors _link) {
  // we need to send the command 0x43 to register 0x41
  byte_array _data;
  _data[0] = 3;           // the size of our message
  _data[1] = _link;       // the sensor port our sensor is attached to
  _data[2] = 0x41;        // the register we're writing to
  _data[3] = 0x43;        // the command we're sending (calibrate)

  writeI2Cvalue(_link, _data, HTC);
}

task main() {

  SensorType[HTC] = sensorI2CCustom;

  calibrateSensor(HTC);

  wait1Msec(2000);

}


Surprisingly there's now a low tone (200 Hz?) for about 1 second, but the result stays the same:

Code:
real_color  red  green  blue     recognized
white         60   30   110       3  = cyan

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Dec 12, 2008 3:29 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: HT Color sensor: -> calibrating?
Quote:
Surprisingly there's now a low tone (200 Hz?) for about 1 second, but the result stays the same:

Code:
real_color  red  green  blue     recognized
white         60   30   110       3  = cyan

The low tone means there's an I2C error.

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]


Fri Dec 12, 2008 4:03 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: HT Color sensor: -> calibrating?
ok, thx Xander, with your original code there was no tone though even the calibration didnt work :( .

@tfriez:
tfriez wrote:
You make the Macro definitions you want, and we'll include them in the next build.

This I unfortunately don't understand quite right, what shall I do, and what exactly will you do in the next build?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Dec 12, 2008 4:11 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 57 posts ]  Go to page 1, 2, 3, 4  Next

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.