View unanswered posts | View active topics It is currently Fri Dec 19, 2014 2:37 pm






Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Colour sensor 
Author Message
Rookie

Joined: Sat Feb 03, 2007 5:06 pm
Posts: 1
Post Colour sensor
Hi. I want to buy a colour sensor and want to know, how to yuse it with RobotC.

Thanks


Thu Mar 01, 2007 5:13 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
I think in its simplest form the Hitechnic color sensor can be configured as a standard "ultrasonic sensor". You don't get all the features but it is the easiest way to get started.

A full featured driver should be coming soon.


Thu Mar 08, 2007 10:01 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
Hitechnic has posted how to use their color sensor

just go to http://www.hitechnic.com/ and click products, then the color sensor.

It provides how to use the sensor in sonar mode and the registers for I2C communication.

Here ya go :D

_________________
Mmmm Legos B-)

My Robot Projects:
http://www.freewebs.com/robotprojects/


Sun Mar 11, 2007 8:44 pm
Profile WWW
Expert

Joined: Sun Aug 19, 2007 2:43 pm
Posts: 142
Location: New Jersey
Post How to set the register values for hitechnic color sensor?
Unfortunately, the hitechnic is point back to this site for any reference! There is no sample code, or how to set the sensor register values.

Just program it as the ultrasonic sensor is unreliable. I tested it... it cannot tell the difference between green and black, or silver and white. Not better than a regular light sensor.

Then, in robolab, I used only red, or green, or blue channel. It only works sporadically. Any idea!?


Tue Mar 18, 2008 7:49 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
You may want to check out this project for help, dtrotzjr has made some drivers to run many hitechnic sensors including the color sensor.

viewtopic.php?t=212

Hope this helps B-)
Scott

_________________
Mmmm Legos B-)

My Robot Projects:
http://www.freewebs.com/robotprojects/


Tue Mar 18, 2008 3:06 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
oh my God, do you really need that much RobotC code just to call a color sensor value ? ? ? :shock:
In Nxt-G it's JUST 1 BLOCK like all the others!

Why can't you get it like
Code:
setSensor (S1, HTColorSensor);
i=SensorValue(S1)

:?:

_________________
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)}


Tue Mar 18, 2008 5:04 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
The code is user made, and is his drivers for 4 sensors, its not all for 1 sensor.

The command you proposed is not quite practical, because to access the full functionality of the sensor, you need to use the I2C communication protocol. To get the values of the red, green, blue, you have to read different registry values on the sensor.

Remember, many I2C sensors have an analog mode where you can do just what you suggested:

setSensor (S1, sensorSONAR);
i=SensorValue(S1)

Scott B-)

_________________
Mmmm Legos B-)

My Robot Projects:
http://www.freewebs.com/robotprojects/


Tue Mar 18, 2008 7:20 pm
Profile WWW
Expert

Joined: Sun Aug 19, 2007 2:43 pm
Posts: 142
Location: New Jersey
Post 
Unfortunately, the "color number" is not precise enough to tell the following colors apart: hunter green vs green, silver(foil) vs white. Therefore you must read the next few bytes to get the red , green reading as well. Or, even with the raw values if they are not good enough.

Therefore:
readI2CReply(nDDPortIndex, nReplyBytes[0], 1);
should be changed to :
readI2CReply(nDDPortIndex, nReplyBytes[0], 4);

where nReplyBytes was declaried to be byte nReplyBytes[4];
.....................

I want to read in the 4 bytes startng from 0x42, because :

// since RobotC does not take "&" address syntax, I assume it will just read into the next 4 bytes.

--------------------

Unfortunately, all 4 bytes seem to get the same value!????


The point is that I need the precision. therefore, I want to read the reading for each color - at least red and green readings.

Idea????


Tue Mar 18, 2008 10:41 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
Ahh, your problem is that the registry value returned from the color sensor is just one byte, and you are asking to return it 4 times, that is why you get the same value for all 4 replys.

What you need to do is read the red blue green light levels individually, there are different registry's just for these operations. The I2C registry numbers can be found at the bottom of the linked page. I believe you want registry's 43, 44, and 45. Registry 42 returns the color number that corresponds to the chart on the linked page.

http://www.hitechnic.com/contents/en-us/d18.html

Good Luck, hope this helps B-)
Scott

_________________
Mmmm Legos B-)

My Robot Projects:
http://www.freewebs.com/robotprojects/


Tue Mar 18, 2008 10:56 pm
Profile WWW
Expert

Joined: Sun Aug 19, 2007 2:43 pm
Posts: 142
Location: New Jersey
Post 
OOO!!! So, I have to send it 3 times if I want all 3 colors reading?

===============================
// to get the red reading

sOutput.nMsgSize = 2;
sOutput.nDeviceAddress = 0x02;
sOutput.nLocationPtr = 0x43;
sendI2CMsg(nDDPortIndex, sOutput.nMsgSize, 1);
..............

nDriverState[nDDPortIndex] = stateI2CPollWaitReply;
readI2CReply(nDDPortIndex, nReplyBytes[0], 1);

byte redvalue = nReplyBytes[0];
..............
====================================
// to get the green reading

sOutput.nMsgSize = 2;
sOutput.nDeviceAddress = 0x02;
sOutput.nLocationPtr = 0x44;
sendI2CMsg(nDDPortIndex, sOutput.nMsgSize, 1);
..............

nDriverState[nDDPortIndex] = stateI2CPollWaitReply;
readI2CReply(nDDPortIndex, nReplyBytes[0], 1);

byte greenvalue = nReplyBytes[0];
===============================================

So, if I want the, e.g., raw red reading.. I should :

sOutput.nMsgSize = 2;
sOutput.nDeviceAddress = 0x02;
sOutput.nLocationPtr = 0x46;
// 0x46 and 0x47 will store the raw red reading...

sendI2CMsg(nDDPortIndex, sOutput.nMsgSize, 1);
..............

nDriverState[nDDPortIndex] = stateI2CPollWaitReply;
readI2CReply(nDDPortIndex, nReplyWord, 2);

Word rawRedWalue = nReplyWord;
=======================================

I'm going to test it out now...


Tue Mar 18, 2008 11:32 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
starwarslegokid wrote:
The code is user made, and is his drivers for 4 sensors, its not all for 1 sensor.(...)
Remember, many I2C sensors have an analog mode where you can do just what you suggested:
setSensor (S1, sensorSONAR);
i=SensorValue(S1)

The I2C protocoll is much too complicate to handle. This really needs to be simplified a lot.
Why can't you implement a sensor protocol that allows some easy access to all sensors, equal if I/O, analog, or I2C?
In case of I2C returns mor than 1 value, an array or structure may be used, such as

Code:
int IArray[10];
setSensor (S1, HTColorSensor);
IArray=SensorValue(S1); // instead of i=SensorValue(S1)

As Dick Swan himself said, RobotC has been designed for beginners and novices.
An I2C protocoll is far too a low hardware- and bit-shifting level and appears to be more suitable for kind of professional embedded C programmers.

Novices and beginners need something like that 1 sensor block in NXT-G, but transfered to a convenient script language:

To get any sensor value, one line of RobotC code must fit!

_________________
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 Mar 19, 2008 3:47 am
Profile
Expert

Joined: Sun Aug 19, 2007 2:43 pm
Posts: 142
Location: New Jersey
Post 
sorry.... I have spent 2 hours on this... not working ...
======================

Quote:
void taskI2CDeviceDrivers()
{
// removed the other hitechnic sensor codes....

for (nDDPortIndex = S1; nDDPortIndex <= S4; ++nDDPortIndex){
switch (SensorSubType[nDDPortIndex])
{
case subTypeHiTechnicColor:

Offset=0x42;
lineN=1;
while (!deviceDriverHiTechnicColor()){
wait1Msec(10);
}

Offset=0x43;
lineN=2;
while (!deviceDriverHiTechnicColor()){
wait1Msec(10);
}

Offset=0x44;
lineN=3;
while (!deviceDriverHiTechnicColor()){
wait1Msec(10);
}

Offset=0x45;
lineN=4;
while (!deviceDriverHiTechnicColor()){
wait1Msec(10);
}

break;

default:
break;
}
}


Then in the deviceDriverHiTechnicColor() code...

Quote:
bool deviceDriverHiTechnicColor()
{
TI2C_Output sOutput;
byte nReplyBytes[5];

// .... rest is the same
case stateI2CPollSend:
if(nI2CStatus[nDDPortIndex] == STAT_COMM_PENDING)
return false;
sOutput.nMsgSize = 2;
sOutput.nLocationPtr = Offset; // Request Color Number

sendI2CMsg(nDDPortIndex, sOutput.nMsgSize, 1);
nDriverState[nDDPortIndex] = stateI2CPollWaitReply;
return false;

case stateI2CPollWaitReply:
switch(nI2CStatus[nDDPortIndex])
{
case NO_ERR:
readI2CReply(nDDPortIndex, nReplyBytes, 1);

SensorValue[nDDPortIndex] = nReplyBytes[0];
nDriverState[nDDPortIndex] = stateI2CPollSend;

nxtDisplayTextLine(lineN, "---: %d", (short)nReplyBytes[0]);

return true;
// rest stays untouched....

...
}


[size=9]
I wish I had time to hunt the right way to retrieve the data. I emailed to hitechnic... Hope they will figure something out as the online doc did say it works with RobotC. If this does not work, I'll have to return these sensors. The light sensor is more reliable than these when it comes to recognizing the colors, black, white and foil. The sole reason for using color sensor is to be able to tell the individual color and get the precision as well. It is rather disappointing... !


Wed Mar 19, 2008 9:16 am
Profile WWW
Expert

Joined: Sun Aug 19, 2007 2:43 pm
Posts: 142
Location: New Jersey
Post reading all black | white | green | silver foil
After all the problems, and even tested with a sample code provided by Stephen B. from HiTechnic, I found out that I happened to have a defective colour sensor, or at least not providing individual reading like another one I purchased last week.

With RobotC and reading the raw RGB, you can get some really precise reading to determine a pretty good spectrum of color, even hunter green (dark green) and black.


Thu Mar 20, 2008 8:19 am
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
hi,
glad to hear that it works!

But now for all the other RobotC programming beginners:
would you please be so kind and post your color sensor driver code to the forum "NXT Programming: Tips for Beginning with ROBOTC" ?
Maybe with an example how to initialize and use the interface routines?
Many thanks,
regards
Helmut

_________________
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)}


Thu Mar 20, 2008 8:34 am
Profile
Expert

Joined: Sun Aug 19, 2007 2:43 pm
Posts: 142
Location: New Jersey
Post RobotC is great for beginners and advanced users
The beautiful part of RobotC is that it can be used for beginners, but also for advanced users. Then, it even comes with a decent debugger. With US$30 software, we truly are getting an aweful good deal!

Beginners mean those who just play with the basic devices coming from the set, basic navigation, basic reading, basic data structure.. Once you start going into synchronization among multitasks, incorporating asynchronized tasks like events, inter-robots communication, and using other devices, you are entering a different realm. This realm does not stay in "beginners" mode anymore. Then, you still can stretch RobotC to do all those.

Color sensor is not from LEGO. Now, if it was indeed from LEGO, I agree that LEGO should have paid some developers to develop a single or just a couple of commands to retrieve the value for different color. But, it is from a third party. We robotics hobbiests are lucky enough that there are affordable resources out there like RobotC, HiTechnic and MindSensors to allow us to stretch the capabilties. Therefore, as far as asking for simple single or just a couple of commands to get the color sensor, the job should really the third party hardware providers like HiTechnic or MindSensors, but not RobotC. As they are the makers, and they decide the address memory model, not RobotC.

To be fair, RobotC is a great software. $30 is truly a good bargin. Then, there is one thing I do think RobotC needs to improve is the documentation which should come with the software even with $30 price. Does not need to be fancy, but just typical function specs would be nice and important to have. I would imagine they must have those function specs written before or during development.


Last edited by elizabeth.mabrey on Thu Mar 20, 2008 9:27 am, edited 3 times in total.



Thu Mar 20, 2008 8:40 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 18 posts ]  Go to page 1, 2  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.