View unanswered posts | View active topics It is currently Tue Jul 29, 2014 2:49 pm






Reply to topic  [ 12 posts ] 
More on the HiTechnic IR Sensor 
Author Message
Rookie

Joined: Wed Jan 09, 2008 10:34 pm
Posts: 10
Post More on the HiTechnic IR Sensor
The folks at HiTechnic helpfully sent some code over in NXC, which should make the IR link work. As I don't have NXC, but use Robot C, I'm trying to port the code over but no luck.

Below is the code from HiTechnic, and below that my attempt at converting to RobotC. Any help would be greatly appreciated.

First the HiTechnic Code:
Quote:
#include "NXCDefs.h"
byte cmnds[] = {0x80,0x92,0x49,0x24,0x24,0x92,0x10,0x84,0x80,0x00,
0x80,0x92,0x49,0x24,0x24,0x90,0x84,0x21,0x08,0x00,
0x80,0x92,0x49,0x24,0x24,0x84,0x84,0x24,0x80,0x00,
0x80,0x92,0x49,0x24,0x24,0x24,0x84,0x84,0x80,0x00,
0x80,0x92,0x49,0x24,0x24,0x24,0x21,0x21,0x08,0x00,
0x80,0x92,0x49,0x24,0x24,0x21,0x21,0x24,0x80,0x00,
0x80,0x92,0x49,0x24,0x21,0x24,0x90,0x84,0x80,0x00,
0x80,0x92,0x49,0x24,0x21,0x24,0x24,0x21,0x08,0x00,
0x80,0x92,0x49,0x24,0x21,0x21,0x24,0x24,0x80,0x00,
0x80,0x92,0x42,0x49,0x09,0x24,0x84,0x21,0x08,0x00,
0x80,0x92,0x42,0x49,0x09,0x24,0x21,0x08,0x48,0x00,
0x80,0x92,0x42,0x49,0x09,0x21,0x21,0x09,0x08,0x00,
0x80,0x92,0x42,0x49,0x09,0x09,0x21,0x21,0x08,0x00,
0x80,0x92,0x42,0x49,0x09,0x09,0x08,0x48,0x48,0x00,
0x80,0x92,0x42,0x49,0x09,0x08,0x48,0x49,0x08,0x00,
0x80,0x92,0x42,0x49,0x08,0x49,0x24,0x21,0x08,0x00,
0x80,0x92,0x42,0x49,0x08,0x49,0x09,0x08,0x48,0x00,
0x80,0x92,0x42,0x49,0x08,0x48,0x49,0x09,0x08,0x00,
0x80,0x92,0x12,0x49,0x09,0x24,0x84,0x24,0x80,0x00,
0x80,0x92,0x12,0x49,0x09,0x24,0x21,0x09,0x08,0x00,
0x80,0x92,0x12,0x49,0x09,0x21,0x21,0x08,0x48,0x00,
0x80,0x92,0x12,0x49,0x09,0x09,0x21,0x24,0x80,0x00,
0x80,0x92,0x12,0x49,0x09,0x09,0x08,0x49,0x08,0x00,
0x80,0x92,0x12,0x49,0x09,0x08,0x48,0x48,0x48,0x00,
0x80,0x92,0x12,0x49,0x08,0x49,0x24,0x24,0x80,0x00,
0x80,0x92,0x12,0x49,0x08,0x49,0x09,0x09,0x08,0x00,
0x80,0x92,0x12,0x49,0x08,0x48,0x49,0x08,0x48,0x00,
0x80,0x92,0x10,0x92,0x42,0x49,0x21,0x09,0x08,0x00,
0x80,0x92,0x10,0x92,0x42,0x49,0x08,0x42,0x48,0x00,
0x80,0x92,0x10,0x92,0x42,0x48,0x48,0x42,0x10,0x80,
0x80,0x92,0x10,0x92,0x42,0x42,0x48,0x49,0x08,0x00,
0x80,0x92,0x10,0x92,0x42,0x42,0x42,0x12,0x48,0x00,
0x80,0x92,0x10,0x92,0x42,0x42,0x12,0x12,0x10,0x80,
0x80,0x92,0x10,0x92,0x42,0x12,0x49,0x09,0x08,0x00,
0x80,0x92,0x10,0x92,0x42,0x12,0x42,0x42,0x48,0x00,
0x80,0x92,0x10,0x92,0x42,0x12,0x12,0x42,0x10,0x80};

int temp;
int toggle=0;
int channel; // set channel to 0, 1, 2 or 3
int command1; // set command1 to 0 (stop), 1 (for) or 2 (rev) for motor 1
int command2; // set command1 to 0 (stop), 1 (for) or 2 (rev) for motor 2
byte inI2Ccmd[];
byte outbuf[];

void Motorcommand()
{
temp=90*channel+30*command1+10*command2;
ArrayInit(inI2Ccmd, 0, 15);
inI2Ccmd[0] = 0x02; // write to IR Link
inI2Ccmd[1] = 0x43; // start position in TX buffer
inI2Ccmd[2] = cmnds[temp]; // selected command
inI2Ccmd[3] = cmnds[temp+1];
inI2Ccmd[4] = cmnds[temp+2];
inI2Ccmd[5] = cmnds[temp+3];
inI2Ccmd[6] = cmnds[temp+4];
inI2Ccmd[7] = cmnds[temp+5];
inI2Ccmd[8] = cmnds[temp+6];
inI2Ccmd[9] = cmnds[temp+7];
inI2Ccmd[10] = cmnds[temp+8];
inI2Ccmd[11] = cmnds[temp+9];
inI2Ccmd[12] = 10; // put the TX buffer length
inI2Ccmd[13] = 0x02; // mode 2 is Motor comm
inI2Ccmd[14] = 0x01; // trigger transmit
temp=0;
I2CBytes(IN_1, inI2Ccmd, temp, outbuf); // send ! PF Commands are sent 4 times
Wait(50); // wait for TX
I2CBytes(IN_1, inI2Ccmd, temp, outbuf); // send !
Wait(50); // wait for TX
I2CBytes(IN_1, inI2Ccmd, temp, outbuf); // send !
Wait(50); // wait for TX
I2CBytes(IN_1, inI2Ccmd, temp, outbuf); // send !
Wait(50); // wait for TX
}

task main()
{
SetSensorLowspeed(IN_1);
channel=0;
while (TRUE)
{
command1=0;
command2=1;
Motorcommand();
Wait(500);
command1=1;
command2=0;
Motorcommand();
Wait(500);
command1=0;
command2=2;
Motorcommand();
Wait(500);
command1=2;
command2=0;
Motorcommand();
Wait(500);
}


}


Now my attempt to convert to RobotC:
Quote:
//*!!Sensor, S1, ir, sensorI2CCustomStd9V, , !!*//
//*!! !!*//
//*!!Start automatically generated configuration code. !!*//
const tSensors ir = (tSensors) S1; //sensorI2CCustomStd9V //*!!!!*//
//*!!CLICK to edit 'wizard' created sensor & motor configuration. !!*//

int channel; // set channel to 0, 1, 2 or 3

byte cmnds[360] = {0x80,0x92,0x49,0x24,0x24,0x92,0x10,0x84,0x80,0x00,
0x80,0x92,0x49,0x24,0x24,0x90,0x84,0x21,0x08,0x00,
0x80,0x92,0x49,0x24,0x24,0x84,0x84,0x24,0x80,0x00,
0x80,0x92,0x49,0x24,0x24,0x24,0x84,0x84,0x80,0x00,
0x80,0x92,0x49,0x24,0x24,0x24,0x21,0x21,0x08,0x00,
0x80,0x92,0x49,0x24,0x24,0x21,0x21,0x24,0x80,0x00,
0x80,0x92,0x49,0x24,0x21,0x24,0x90,0x84,0x80,0x00,
0x80,0x92,0x49,0x24,0x21,0x24,0x24,0x21,0x08,0x00,
0x80,0x92,0x49,0x24,0x21,0x21,0x24,0x24,0x80,0x00,
0x80,0x92,0x42,0x49,0x09,0x24,0x84,0x21,0x08,0x00,
0x80,0x92,0x42,0x49,0x09,0x24,0x21,0x08,0x48,0x00,
0x80,0x92,0x42,0x49,0x09,0x21,0x21,0x09,0x08,0x00,
0x80,0x92,0x42,0x49,0x09,0x09,0x21,0x21,0x08,0x00,
0x80,0x92,0x42,0x49,0x09,0x09,0x08,0x48,0x48,0x00,
0x80,0x92,0x42,0x49,0x09,0x08,0x48,0x49,0x08,0x00,
0x80,0x92,0x42,0x49,0x08,0x49,0x24,0x21,0x08,0x00,
0x80,0x92,0x42,0x49,0x08,0x49,0x09,0x08,0x48,0x00,
0x80,0x92,0x42,0x49,0x08,0x48,0x49,0x09,0x08,0x00,
0x80,0x92,0x12,0x49,0x09,0x24,0x84,0x24,0x80,0x00,
0x80,0x92,0x12,0x49,0x09,0x24,0x21,0x09,0x08,0x00,
0x80,0x92,0x12,0x49,0x09,0x21,0x21,0x08,0x48,0x00,
0x80,0x92,0x12,0x49,0x09,0x09,0x21,0x24,0x80,0x00,
0x80,0x92,0x12,0x49,0x09,0x09,0x08,0x49,0x08,0x00,
0x80,0x92,0x12,0x49,0x09,0x08,0x48,0x48,0x48,0x00,
0x80,0x92,0x12,0x49,0x08,0x49,0x24,0x24,0x80,0x00,
0x80,0x92,0x12,0x49,0x08,0x49,0x09,0x09,0x08,0x00,
0x80,0x92,0x12,0x49,0x08,0x48,0x49,0x08,0x48,0x00,
0x80,0x92,0x10,0x92,0x42,0x49,0x21,0x09,0x08,0x00,
0x80,0x92,0x10,0x92,0x42,0x49,0x08,0x42,0x48,0x00,
0x80,0x92,0x10,0x92,0x42,0x48,0x48,0x42,0x10,0x80,
0x80,0x92,0x10,0x92,0x42,0x42,0x48,0x49,0x08,0x00,
0x80,0x92,0x10,0x92,0x42,0x42,0x42,0x12,0x48,0x00,
0x80,0x92,0x10,0x92,0x42,0x42,0x12,0x12,0x10,0x80,
0x80,0x92,0x10,0x92,0x42,0x12,0x49,0x09,0x08,0x00,
0x80,0x92,0x10,0x92,0x42,0x12,0x42,0x42,0x48,0x00,
0x80,0x92,0x10,0x92,0x42,0x12,0x12,0x42,0x10,0x80};

int temp;
int toggle=0;

int command1; // set command1 to 0 (stop), 1 (for) or 2 (rev) for motor 1
int command2; // set command1 to 0 (stop), 1 (for) or 2 (rev) for motor 2
byte inI2Ccmd[15];
byte outbuf;

void Motorcommand()
{
temp=90*channel+30*command1+10*command2;
inI2Ccmd[0] = 0x02; // write to IR Link
inI2Ccmd[1] = 0x43; // start position in TX buffer
inI2Ccmd[2] = cmnds[temp]; // selected command
inI2Ccmd[3] = cmnds[temp+1];
inI2Ccmd[4] = cmnds[temp+2];
inI2Ccmd[5] = cmnds[temp+3];
inI2Ccmd[6] = cmnds[temp+4];
inI2Ccmd[7] = cmnds[temp+5];
inI2Ccmd[8] = cmnds[temp+6];
inI2Ccmd[9] = cmnds[temp+7];
inI2Ccmd[10] = cmnds[temp+8];
inI2Ccmd[11] = cmnds[temp+9];
inI2Ccmd[12] = 10; // put the TX buffer length
inI2Ccmd[13] = 0x02; // mode 2 is Motor comm
inI2Ccmd[14] = 0x01; // trigger transmit
temp=0;
sendI2CMsg(ir, inI2Ccmd, outbuf); // send ! PF Commands are sent 4 times
wait1Msec(50); // wait for TX
sendI2CMsg(ir, inI2Ccmd, outbuf); // send !
wait1Msec(50); // wait for TX
sendI2CMsg(ir, inI2Ccmd, outbuf); // send !
wait1Msec(50); // wait for TX
sendI2CMsg(ir, inI2Ccmd, outbuf); // send !
wait1Msec(50); // wait for TX
}

task main()
{

for (channel = 0; channel < 4; channel++)
{
command1=0;
command2=1;
Motorcommand();
wait10Msec(200);
command1=1;
command2=0;
Motorcommand();
wait10Msec(200);
command1=0;
command2=2;
Motorcommand();
wait10Msec(200);
command1=2;
command2=0;
Motorcommand();
wait10Msec(200);
}


}


Sat Feb 02, 2008 12:16 pm
Profile
Rookie

Joined: Wed Jan 09, 2008 10:34 pm
Posts: 10
Post Progress!
Using the information from HiTechnic (thanks, Steve) and the TestI2C utility, I was able to get two motors running at full speed by sending this message:

Quote:
02 43 00 80 92 49 24 24 24 21 21 08 10 02 01


to the IRLink. I am having trouble sending the message using the sendIC2msg function to send this to the IRLink. Any thoughts?

This code does not work:

Code:
task main()
{
   byte msgx[15] = {02,43,00,80,92,49,24,24,24,21,21,08,10,02,01}   ;
   while(1){
         sendI2Cmsg(S1,4,msgx);
         wait1Msec(50);
      }
}


Sun Feb 03, 2008 5:16 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
try changing the sendI2c message to this:

sendI2CMsg(S1, msgx[0], 0);

the second parameter is the message, and it has to have the brackets and the 0 inside. the last parameter is the number of bytes that is expected to be sent back by the sensor, you can change this. Let me know if it worked :-)

Scott B-)

_________________
Mmmm Legos B-)

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


Mon Feb 04, 2008 6:03 pm
Profile WWW
Rookie

Joined: Wed Jan 09, 2008 10:34 pm
Posts: 10
Post 
No luck. Are pointers supported? I think I need to pass the whole array rather than just the value of the first element.


Mon Feb 04, 2008 10:03 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
const byte msg[] = {NumberOfBytesInMessage, SensorAddress, CommandRegistry, Command};


const byte msg[] ={15, 0x02, 0x43, 0x00, 0x80, 0x92, 0x49, 0x24, 0x24, 0x24, 0x21, 0x21, 0x08, 0x10, 0x02, 0x01};

_________________
Mmmm Legos B-)

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


Last edited by starwarslegokid on Thu Feb 07, 2008 10:29 pm, edited 2 times in total.



Mon Feb 04, 2008 11:09 pm
Profile WWW
Rookie

Joined: Wed Jan 09, 2008 10:34 pm
Posts: 10
Post 
Still no luck. Here's the code that I'm now using:

Code:
const tSensors IR                   = (tSensors) S1;   //sensorI2CCustomStd //*!!!!*//
//*!!CLICK to edit 'wizard' created sensor & motor configuration.                !!*//

task main()
{
   const byte msgx[] = {0x12, 0x02, 0x41, 0x02, 0x43,0x00,0x80,0x92,0x49,0x24,0x24,0x24,0x21,0x21,0x08,0x10,0x02,0x01}   ;
   while(1){
         sendI2CMsg(S1, msgx[0], 4);
         wait1Msec(5);
      }
}


I've used 0x12 as the message length bec. the array is 18 bytes long. I've used 0x02 an 0x41 as the sensor address and command registery respectively because those are the numbers that I'm seeing in the sample code.

For reasons I don't understand, the global variable window shows msgx[6] having a value of 0xFFFFFF80 and msgx[7] as having a value of 0xFFFFFF92 when the programs runs. The window shows all the other values as corresponding to what is listed in the initial array defiition.

Any ideas?


Thu Feb 07, 2008 8:00 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
Quote:
I've used 0x12 as the message length bec. the array is 18 bytes long.


since you are sending an 18 byte message, then the message size should be 18, im unsure why you are using 0x12.

I just realized i made a mistake on my post above to not include the 0x before all the byte numbers, I changed it. :-)

Scott

_________________
Mmmm Legos B-)

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


Thu Feb 07, 2008 10:22 pm
Profile WWW
Rookie

Joined: Wed Jan 09, 2008 10:34 pm
Posts: 10
Post 
Quote:
since you are sending an 18 byte message, then the message size should be 18, im unsure why you are using 0x12.


Not at my home computer, so I can't try using 18, but isn't 0x12 just 18 in hex? does the message length need to be in decimal?


Thu Feb 07, 2008 10:40 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 
Sorry for the confusion, the number of bytes is a normal integer, like 5 or 18, everything after that however does have to be a byte. Sorry about that B-)

Scott

_________________
Mmmm Legos B-)

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


Thu Feb 07, 2008 10:53 pm
Profile WWW
Rookie

Joined: Wed Jan 09, 2008 10:34 pm
Posts: 10
Post 
Success!

Many thanks for all your help and patience starwarslegokid!


Fri Feb 08, 2008 7:31 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
sorry, I don't understand anything.
looks like assembler or even much worse. :evil:
can't you write this with normal keywords in normal RobotC syntax?
Like
IRMotor(MotorCmd(port))
IRSensorValue(SensorType(Port))

or sth. similar?

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


Last edited by Ford Prefect on Fri Mar 21, 2008 5:42 am, edited 2 times in total.



Thu Mar 20, 2008 12:16 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
PS: here some code how it SHOULD work:

the common way to steer a motor is, for example:
Code:
motor[0]=80;


if you got 4 RCX's (1,2,3,4) and a motor to be steered by IR-Link attached to RCX[1] -Port 0 (A) , there should exist an IR-Motor-Command (IRmotor), to work like:

Code:
IRmotor[RCX(1,0)]=80;



same RCX, but Port 2 (C), and reverse:

Code:
IRmotor[RCX(1,2)]=-80;



In case of 3 motors (1,2,3) with built-in IR-receiver, it should work like this:

Code:
IRmotor[IrMot(1)]=-80;



and if you got 5 trains, e.g. that way:

Code:
IRmotor[TrainMot(1)]=-80;

_________________
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 Mar 21, 2008 5:28 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 12 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.