ROBOTC.net forums
http://www.robotc.net/forums/

More on the HiTechnic IR Sensor
http://www.robotc.net/forums/viewtopic.php?f=1&t=379
Page 1 of 1

Author:  JustAnotherNXTHacker [ Sat Feb 02, 2008 12:16 pm ]
Post subject:  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);
}


}

Author:  JustAnotherNXTHacker [ Sun Feb 03, 2008 5:16 pm ]
Post subject:  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);
      }
}

Author:  starwarslegokid [ Mon Feb 04, 2008 6:03 pm ]
Post subject: 

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

Author:  JustAnotherNXTHacker [ Mon Feb 04, 2008 10:03 pm ]
Post subject: 

No luck. Are pointers supported? I think I need to pass the whole array rather than just the value of the first element.

Author:  starwarslegokid [ Mon Feb 04, 2008 11:09 pm ]
Post subject: 

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};

Author:  JustAnotherNXTHacker [ Thu Feb 07, 2008 8:00 am ]
Post subject: 

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?

Author:  starwarslegokid [ Thu Feb 07, 2008 10:22 pm ]
Post subject: 

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

Author:  JustAnotherNXTHacker [ Thu Feb 07, 2008 10:40 pm ]
Post subject: 

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?

Author:  starwarslegokid [ Thu Feb 07, 2008 10:53 pm ]
Post subject: 

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

Author:  JustAnotherNXTHacker [ Fri Feb 08, 2008 7:31 am ]
Post subject: 

Success!

Many thanks for all your help and patience starwarslegokid!

Author:  Ford Prefect [ Thu Mar 20, 2008 12:16 pm ]
Post subject: 

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?

Author:  Ford Prefect [ Fri Mar 21, 2008 5:28 am ]
Post subject: 

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;

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/