1.45..1.40 public: RS485 connection for M/S-muxing? samples?
| Author |
Message |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 1.45..1.40 public: RS485 connection for M/S-muxing? samples?
hello, where can I find code samples for a fast RS485 connection between 2 nxt for - controlling 3 motors at the nxt slave - reading sensor values of the nxt slave (all kinds: motorEncoder, Touch, ultrasonic, lightactive, HTCompass etc.)  I'm using 1.40 public
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
Last edited by Ford Prefect on Fri Dec 19, 2008 9:53 am, edited 3 times in total.
|
| Sun Aug 24, 2008 10:33 am |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
I am also interested on that,
btw, how that conection is physically made? how that works?
Thanks
|
| Mon Aug 25, 2008 5:54 am |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: 1.40 public: how to use RS485 connection? code samples?
RS485 is a high speed serial comm port; you may use a standard adaptor cable and connect both nxt sensor ports #4 with each other (#4 = high speed ports).
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Mon Aug 25, 2008 6:12 am |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
Hmm, nice, do you got any example of anything using it? I Could help on that if i got something to start...
Thanks
|
| Mon Aug 25, 2008 6:18 am |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: 1.40 public: how to use RS485 connection? code samples?
this is a NXC nxt master program:  |  |  |  | Code: // Lego NXT RS485 demo Master // modified from John Hansen nxcsamples // http://bricxcc.sourceforge.net/nbc/nxcsamples/485.zip
void SendRS485Bool(const bool value) { byte msg[]; ArrayBuild(msg, value, 0); SetHSOutputBuffer(0, 2, msg); SetHSOutputBufferOutPtr(0); SetHSOutputBufferInPtr(2); SetHSState(HS_SEND_DATA); SetHSFlags(HS_UPDATE); //send it }
void SendRS485Number(const int value) { string msg = Flatten(value); SetHSOutputBuffer(0, 5, msg); SetHSOutputBufferOutPtr(0); SetHSOutputBufferInPtr(5); SetHSState(HS_SEND_DATA); SetHSFlags(HS_UPDATE); //send it }
void SendRS485String(const string msg) { byte mlen = ArrayLen(msg); SetHSOutputBuffer(0, mlen, msg); SetHSOutputBufferOutPtr(0); SetHSOutputBufferInPtr(mlen); SetHSState(HS_SEND_DATA); SetHSFlags(HS_UPDATE); //send it }
void WaitForMessageToBeSent() { while(HSOutputBufferOutPtr() < HSOutputBufferInPtr()) Wait(1); }
task main() { // set port 4 to RS485 type SetSensorType(IN_4, SENSOR_TYPE_HIGHSPEED); SetHSState(HS_INITIALISE); SetHSFlags(HS_UPDATE); // set ports 1 and 2 to touch sensor type SetSensor(S1, SENSOR_TOUCH); SetSensor(S2, SENSOR_TOUCH);
byte mlen; // message length string buffer; // message buffer string SlaveMsgNum; // incoming message number int SlaveMsgLen; // incoming message length string SlaveNum; // out going message number string msg; // out going message string SlaveMsg; // message text int TimeOut; int x; int PollCount; int PollTotal; // total number of slave to poll bool ReadMsg; int ar_Polling[3]; // declare an array
// set array of slave numbers // each slave NXT must be in this list ar_Polling[0] = 2; ar_Polling[1] = 5;
PollTotal = 1; // Total number of slaves - 1 PollCount = 0; Wait(10);
while (true) { // Poll one slave in ar_Polling on each pass SlaveNum = NumToStr(ar_Polling[PollCount]); SlaveMsg = "P"; // P = Polling msg = SlaveNum + "," + SlaveMsg;
// if touch sensor 1 pressed create message for slave number = 5 if(Sensor(S1) > 0 && ar_Polling[PollCount] == 5) { msg = "5,C"; } // if touch sensor 2 pressed create message for slave number = 2 if(Sensor(S2) > 0 && ar_Polling[PollCount] == 2) { msg = "2,C"; }
// display out going message TextOut(0, LCD_LINE1, msg, true);
ReadMsg = true; // Send message SendRS485String(msg); WaitForMessageToBeSent();
msg = ""; TimeOut = 100; // set time out if not response from slave
// start with empty input buffer SetHSInputBufferInPtr(0); SetHSInputBufferOutPtr(0); // wait for a message to arrive. mlen = 0; while (mlen == 0 && TimeOut > 0){ TimeOut = TimeOut-1; if(TimeOut == 0) { // no message ReadMsg = false; SlaveMsgNum = NumToStr(ar_Polling[PollCount]); SlaveMsg = "error"; } mlen = HSInputBufferInPtr(); } Wait(2); // wait to get the whole message mlen = HSInputBufferInPtr();
// get message if(ReadMsg == true) { GetHSInputBuffer(0, mlen, buffer); // clear the incoming buffer SetHSInputBufferInPtr(0); // display buffer message TextOut(0, LCD_LINE2, buffer);
// phrase message SlaveMsgLen = StrLen(buffer); x = 0; while(x < SlaveMsgLen){ if(SubStr(buffer, x, 1) == ",") { // look for comma SlaveMsgNum = SubStr(buffer, 0, x); SlaveMsg = SubStr(buffer, x+1, SlaveMsgLen); break; } x = x+1; }
// read message and act on it if(SubStr(SlaveMsg, 0, 1) == "R") { // look for R if(SubStr(SlaveMsg, 2, 1) == "2") { // if from slave 2 // Turn on Motor A OnRev(OUT_A, 75); // turn Motor port A in reverse } if(SubStr(SlaveMsg, 2, 1) == "5") { // if from slave 5 // Turn on Motor A OnFwd(OUT_A, 75); // turn Motor port A forward } } }
// display message TextOut(0, LCD_LINE3, SlaveMsgNum); TextOut(0, LCD_LINE5, SlaveMsg); PollCount = PollCount+1; if(PollCount > PollTotal) { PollCount = 0; } Wait(500); // time delay so humans can watch it
Coast(OUT_A); } } |  |  |  |  |
====================================== and this is the NXC slave program:  |  |  |  | Code: // Lego NXT RS485 demo Slave // modified from John Hansen nxcsamples // http://bricxcc.sourceforge.net/nbc/nxcsamples/485.zip
void SendRS485String(const string msg) { byte mlen = ArrayLen(msg); SetHSOutputBuffer(0, mlen, msg); SetHSOutputBufferOutPtr(0); SetHSOutputBufferInPtr(mlen); SetHSState(HS_SEND_DATA); SetHSFlags(HS_UPDATE); //send it }
void WaitForMessageToBeSent() { while(HSOutputBufferOutPtr() < HSOutputBufferInPtr()) Wait(1); }
task main() { // set port 4 to RS485 type SetSensorType(IN_4, SENSOR_TYPE_HIGHSPEED); SetHSState(HS_INITIALISE); SetHSFlags(HS_UPDATE); // set ports 1 and 2 to touch sensor type SetSensor(S1, SENSOR_TOUCH); SetSensor(S2, SENSOR_TOUCH);
byte mlen; // message length string buffer; // message buffer string msg; // incoming message string SlaveMsgNum; // incoming message number string SlaveMsg; // incoming message text int SlaveMsgLen; // incoming message length int SlaveNum; // number of slave NXT string strSlaveNum; // string form of slave NXT number string SendMsg; // outgoing message int x; int result; // result of Open File byte handle; // handle for Open File string buf; // buf for Open File
// Get slave number // each slave NXT must have a different number // can be assigned to SlaveNum the compiled to each slave NXT //SlaveNum = 5; // or a txt file can be uploaded to each slave NXT with a // different number in it then result = OpenFileRead("SlaveNum.txt", handle, handle); ReadLnString(handle,buf); result = CloseFile(handle); SlaveNum = StrToNum(buf); strSlaveNum = NumToStr(SlaveNum);
// start with empty input buffer SetHSInputBufferInPtr(0); SetHSInputBufferOutPtr(0);
Wait(10); while (true) { // wait for a message to arrive. mlen = 0; while (mlen == 0) mlen = HSInputBufferInPtr();
Wait(2); // wait to get the whole message mlen = HSInputBufferInPtr();
// read it. GetHSInputBuffer(0, mlen, buffer); // clear the incoming buffer SetHSInputBufferInPtr(0);
// display buffer message TextOut(75, LCD_LINE1, strSlaveNum, true); TextOut(0, LCD_LINE1, buffer);
// get SlaveNum of message SlaveMsgLen = StrLen(buffer); x = 0; while(x < SlaveMsgLen){ if(SubStr(buffer, x, 1) == ",") { // look for comma SlaveMsgNum = SubStr(buffer, 0, x); SlaveMsg = SubStr(buffer, x+1, SlaveMsgLen); break; } x = x+1; }
// Check sensors SendMsg = ""; if(Sensor(S2) > 0) { // if touch sensor 2 is pressed SendMsg = "R-" + strSlaveNum + "-S2"; // create message to send to master }
// Message for Me if(StrToNum(SlaveMsgNum) == SlaveNum) { TextOut(0, LCD_LINE3, SlaveMsgNum); TextOut(0, LCD_LINE5, SlaveMsg); // phrase message if(SubStr(SlaveMsg, 0, 1) == "C") { // Turn on Motor A OnFwd(OUT_A, 75); // turn on port A }
// Respone to master polling message if(StrLen(SendMsg) == 0) { // if no return message SendMsg = "A-" + strSlaveNum; // A = acknowledge } // send return message to master msg = strSlaveNum + "," + SendMsg; Wait(10); SendRS485String(msg); WaitForMessageToBeSent(); } Wait(10); Coast(OUT_A); // turn off port A } } |  |  |  |  |
========================== These are the programs used in the You tube video at http://www.youtube.com/watch?v=Kg56R6XzVdA demonstrating using RS485 port to network NXT bricks. You will need 'NBC compiler' from http://bricxcc.sourceforge.net/nbc/ to run these programs.
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Mon Aug 25, 2008 6:28 am |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
Seems to be nice, the idea now is use that on nxt, with ROBOTC, right?
|
| Mon Aug 25, 2008 6:33 am |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: 1.40 public: how to use RS485 connection? code samples?
correct 
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Mon Aug 25, 2008 6:45 am |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
The Problem don't seems to be create the small functions of testing, the problem is create functions like:
SetHSOutputBuffer(0, mlen, msg); SetHSOutputBufferOutPtr(0); SetHSOutputBufferInPtr(mlen); SetHSState(HS_SEND_DATA); SetHSFlags(HS_UPDATE); //send it
that problably on 1.40 Public, aren't availlable any simillar
Now that i think about it, i remember a German Robot on Robocup2008 this year, that uses 2 NXT on the goalkepper, probably linked like that...
|
| Mon Aug 25, 2008 6:51 am |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: 1.40 public: how to use RS485 connection? code samples?
yes, that's 1 part of the problem  I actually read in the 1.30 release (I think), that RS485 meanwhile should be implemented, but I can't find anything about that in the RobotC- 
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Mon Aug 25, 2008 7:03 am |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
Actually there is an example code:  |  |  |  | Quote: //////////////////////////////////////////////////////////////////////////////////////////////////////// // // Simple Sensor Port S4 High-Speed RS-485 "Raw" Data Messaging // ////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma platform(NXT)
long nXmitSuccesses = 0; long nRcvChars = 0;
void setupHighSpeedLink(const bool bMaster) { // // Initialize port S$ to "high speed" mode. // nxtEnableHSPort(); //nxtSetHSBaudRate(); nxtHS_Mode = bMaster ? hsMsgModeMaster : hsMsgModeSlave; return; }
//////////////////////////////////////////////////////////////////////////////////////////////////////// // // Transmit Bytes // ////////////////////////////////////////////////////////////////////////////////////////////////////////
void Transmit() { static const ubyte nData[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xff};
while (true) { if (nxtWriteRawHS(nData[0], sizeof(nData), 0) == ioRsltSuccess) nXmitSuccesses += sizeof(nData); // // Display progress results on the NXT LCD screen. // nxtDisplayBigStringAt(0, 31, "Xmit"); nxtDisplayBigStringAt(0, 15, "%d", nXmitSuccesses); wait1Msec(15); // To reduce the screen flicker } }
//////////////////////////////////////////////////////////////////////////////////////////////////////// // // Receive Bytes // ////////////////////////////////////////////////////////////////////////////////////////////////////////
void Receive() { const int kHexDigitsPerLine = 8;
int nNumbBytesAvail; int nNumbBytesToRead; int nNumbBytesActuallyRead; byte BytesRead[kHexDigitsPerLine]; while (true) { // // Loop continuously, reading one byte at a time. The interface will support larger reads. // nNumbBytesAvail = nxtGetAvailHSBytes(); if (nNumbBytesAvail <= 0) { nNumbBytesActuallyRead = 0; wait1Msec(5); continue; }
if (nNumbBytesAvail < sizeof(BytesRead)) nNumbBytesToRead = nNumbBytesAvail; else nNumbBytesToRead = sizeof(BytesRead); nNumbBytesActuallyRead = nxtReadRawHS(BytesRead[0], nNumbBytesToRead); nRcvChars += nNumbBytesActuallyRead; wait1Msec(5); // // Display the last eight bytes read on the NXT LCD in hex format. // string sString; string sTemp;
// // Display progress results on the NXT LCD screen. // nxtDisplayBigStringAt(0, 31, "Rcv"); nxtDisplayBigStringAt(0, 15, "%d", nRcvChars);
// // Display the bytes read on the NXT LCD in hex format. // const int kHexDigitsPerLine = 8;
sString = ""; for (int i = 0; i < nNumbBytesActuallyRead; ++i) { StringFormat(sTemp, "%02X", BytesRead[i] & (short) 0x00FF); sString += sTemp; } nxtDisplayTextLine(2, "%16s", sString); } }
//////////////////////////////////////////////////////////////////////////////////////////////////////// // // Main Task // ////////////////////////////////////////////////////////////////////////////////////////////////////////
task main() { string sFriendlyName; bool bTransmit;
// // Adjust this to fit your NXT brick // getFriendlyName(sFriendlyName); bTransmit = (sFriendlyName == "Dick2");
eraseDisplay(); bNxtLCDStatusDisplay = true; // Enable top status line display
setupHighSpeedLink(true); // // Configure the link for raw read and write. User program will have complete control over the link. // User program will be responsible for managing the half-duplex operation and must prevent collisions! // nxtHS_Mode = hsRawMode;
if (bTransmit) Transmit(); else Receive(); return; }
|  |  |  |  |
It's the file "HS 485 LowLevel.c" on Sample/BlueTooth/ Do you got NXT there to test?
|
| Tue Aug 26, 2008 7:00 am |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: 1.40 public: how to use RS485 connection? code samples?
oh, I never thought to find this in the Bluetooth Folder - I exspected this more to be in a sensor folder or where ever.
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Tue Aug 26, 2008 7:05 am |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: 1.40 public: how to use RS485 connection? code samples?
PS: I actually don't understand which code has to be run on the master and which on the slave brick. Maybe I'll wait for some kind of more understandable a/o convenient code 
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Tue Aug 26, 2008 7:10 am |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
Nice, i don't have any NXT on here... let me know how it goes... By the way, do you know any example of NXT + CMUCAM2 on Port 4 ? As RobotC and CMUCam are from same University, should exist a manual to use both  I already got both, i would like to use it...
|
| Tue Aug 26, 2008 7:13 am |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
I think that this code is Master/Slave on here: You see if it's NXT one or two. If it is one, bTransmit = false, if it is two (in this case Dick2) bTransmit = true then i think that could exist an error: Cause if you send "true" to setupHighSpeedLink, both would be masters, so, it's better to do like this: setupHighSpeedLink(bTransmit); or setupHighSpeedLink(true); //on MASTER setupHighSpeedLink(false); //on SLAVE Then is just trying...
|
| Tue Aug 26, 2008 7:20 am |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: 1.40 public: how to use RS485 connection? code samples?
hi, 1) no I unfortunately don't know anything about cmuCam 2) I like programming mathmatical algorithms as you may have noticed, BUT 3) I'm really bad in hardware or low level programming - I simply got not this kind of imagination, and I simply don't (want to) understand all this static const ubyte nData[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xff} while (true) {if (nxtWriteRawHS(nData[0], sizeof(nData), 0) == ioRsltSuccess) nXmitSuccesses += sizeof(nData) -stuff (I surely want to take a look at this in a library to see what's going on, of course, but that's it...!) 4) So what I need is sort of simple hardware access like  |  |  |  | Code: set_sensor_type (BrickNumber, PortNumber, SensorType) set_sensor_mode (BrickNumber, PortNumber, SensorMode) get_sensor_value (BrickNumber, PortNumber, &SensorValue) set_motor_speed (BrickNumber, PortNumber, &MotorSpeedValue) get_motor_encoder (BrickNumber, PortNumber, &MotorEncoderValue) |  |  |  |  |
about anything else I really can't break my weired head 
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Tue Aug 26, 2008 9:05 am |
|
|
Who is online |
Users browsing this forum: No registered users and 7 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
|
|