1.45..1.40 public: RS485 connection for M/S-muxing? samples?
| Author |
Message |
|
ogait87
Rookie
Joined: Tue Aug 26, 2008 6:53 am Posts: 13
|
 Re: 1.40 public: how to use RS485 connection? code samples?
I have some working code by now. Me and Docilio already tested with 2 NXT but function for max 4 (changeable). Any feedback would be apreciated. The ideia is a task controling the comunication and 2 buffers and a token ring tipology. The next one to speak is the one with the next id in the message head (sender field). If there is no message to be sent, it sends a null message just to give the other NXT the control of the line. This program cam be used for multi master aplications.  |  |  |  | Code: //////////////////////////////////////////////////////////////////////////////////////////////////////// // // Port S4 High-Speed RS-485 Full Duplex Token Ring // // Autor : Tiago Gonçalves (ogait87) // Release: 0.0.3 // Date : 30/08/2008 // ////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma platform(NXT)
//#define HSPEED_DEBUG
long nXmitSuccesses = 0; long nRcvChars = 0;
void setupHighSpeedLink(const bool bMaster) { // // Initialize port S4 to "high speed" mode. // nxtEnableHSPort(); //nxtSetHSBaudRate(); nxtHS_Mode = bMaster ? hsMsgModeMaster : hsMsgModeSlave;
// // 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;
return; }
//////////////////////////////////////////////////////////////////////////////////////////////////////// // // Driver Task // //////////////////////////////////////////////////////////////////////////////////////////////////////// ubyte HSpeedTxData[17] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int HSpeedTxDataLen = 0; ubyte HSpeedRxData[17] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int HSpeedRxDataLen = 0;
int MaxNXT = 0; int IAmNXT = 0;
task HSpeed() { bool IAmMaster = false; int timeout; int dest, len; ubyte TxData = (IAmNXT << 6); ubyte lixo = 0x00; ubyte head; int i; ClearTimer(T1);
#ifdef HSPEED_DEBUG int master_count = 0; int head_count = 0; int loop_count = 0; int timeout_count = 0; #endif
while(true) { #ifdef HSPEED_DEBUG loop_count++; nxtDisplayTextLine(4, "LOOP: %d", loop_count); #endif if ( IAmMaster ) { if ( HSpeedTxDataLen > 0) { HSpeedTxData[0] = HSpeedTxData[0] << 4; HSpeedTxData[0] = HSpeedTxData[0] | (IAmNXT << 6); HSpeedTxData[0] = HSpeedTxData[0] | HSpeedTxDataLen; nxtWriteRawHS(HSpeedTxData, HSpeedTxDataLen+1, 0); // == ioRsltSuccess HSpeedTxDataLen = 0; } else { nxtWriteRawHS(TxData, 1, 0); } // Dar tempo ao recetor receber e procesar a mensagem IAmMaster = false; #ifdef HSPEED_DEBUG master_count++; nxtDisplayTextLine(6, "MASTER: %d", master_count); #endif wait1Msec(100); } timeout = random[1000] + 2000; ClearTimer(T1);
while( nxtGetAvailHSBytes() <= 0 && time1[T1] < timeout) wait1Msec(10);
// Houve erro na comunicao e niguem sabe quem e a vez de falar if ( time1[T1] >= timeout ) { IAmMaster = true; #ifdef HSPEED_DEBUG timeout_count++; nxtDisplayTextLine(7, "TIMEOUT: %d", timeout_count); #endif continue; }
// Recebi pelo menos 1 byte nxtReadRawHS(head, 1);
#ifdef HSPEED_DEBUG head_count++; nxtDisplayTextLine(5, "HEAD: %d C: %d", (int) head&0xFF, head_count); #endif
// Sou o proximo? if ( ((head >> 6) + 1) % MaxNXT == IAmNXT) { IAmMaster = true; }
// Processar o corpo da mensagem len = head&0x0F; if( len != 0 ) { dest = (head>>4) & 0x3; #ifdef HSPEED_DEBUG nxtDisplayTextLine(3,"PM Master:%d", (byte) IAmMaster); #endif i = 0; while( i < len) { while(nxtGetAvailHSBytes() <= 0) wait1Msec(10);
nxtReadRawHS(lixo, 1);
if ( dest == IAmNXT ) HSpeedRxData[i+1] = lixo;
i++; }
if ( dest == IAmNXT ) { HSpeedRxData[0] = head>>6; HSpeedRxDataLen = len; } else { HSpeedRxDataLen = 0; } #ifdef HSPEED_DEBUG if ( dest == IAmNXT ) { nxtDisplayTextLine(3, "PARA MIM"); } else { nxtDisplayTextLine(3, "IGNORE"); } #endif } wait1Msec(100); } }
//////////////////////////////////////////////////////////////////////////////////////////////////////// // // Main Task // ////////////////////////////////////////////////////////////////////////////////////////////////////////
task main() { string sFriendlyName; int count = 0; long loop_count = 0; long send_count=0;
setupHighSpeedLink(true);
MaxNXT = 2;
getFriendlyName(sFriendlyName); IAmNXT = sFriendlyName == "HRF1" ? 0 : 1;
eraseDisplay(); bNxtLCDStatusDisplay = true; // Enable top status line display
nxtDisplayTextLine(2, "IAmNXT: %d MAX: %d", IAmNXT, MaxNXT);
StartTask(HSpeed);
ClearTimer(T2);
while(true) { nxtDisplayTextLine(3, "Loop: %d", loop_count++);
while(HSpeedRxDataLen == 0 && time1[T2] < 1000) { nxtDisplayTextLine(3, "Loop: %d %d", HSpeedRxDataLen, time1[T2]); wait1Msec(10); }
if ( HSpeedRxDataLen != 0 ) // Recebi Algo { count ++; nxtDisplayTextLine(6, "Rx: ID %d Len %d", (int)HSpeedRxData[0], HSpeedRxDataLen); nxtDisplayTextLine(7, "Rx: %d", count); HSpeedRxDataLen = 0; }
if ( time1[T2] > 1000) { send_count++; HSpeedTxData[0] = IAmNXT == 0 ? 1 : 0; HSpeedTxData[1] = 1; HSpeedTxDataLen = 1; nxtDisplayTextLine(4, "Send: %d", send_count); ClearTimer(T2); } } } |  |  |  |  |
_________________My sensors and librarys for robotc http://hng.av.it.pt/~tgoncalves/robotc/cgit.cgi/
|
| Thu Sep 04, 2008 6:52 pm |
|
 |
|
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, looks fine  ) but 2 questions before I can start testing: where do you have to define the nxt ID's for every single brick? Do have to give the bricks special names as before?
_________________ 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."
|
| Fri Sep 05, 2008 6:29 am |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
On that code the nxt ID is give as the other code(with the bricks name) to more than 2, we need only to change this IAmNXT to a diferent code...
|
| Fri Sep 05, 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
|
 RS485 standardization
hi, looks very fine! I tested it and as far as I can see there's something happening  but to be honest, I can't see WHAT'S happening :$ to improve your code and to make the code more standardized, I'll suggest the following: 1) generally the alowed friendlynames should only be "00", "01", "02",.."255" for EVERY RS485 protocoll written in future by anyone, 2) maybe the names may be set by a routine in the code itself (sfriendlyname="03" or whatever) 3) master always is nxt "00" in order to superior controlling purposes 4) the RS485 code should be written to a header file ("RS485com.h") which can be included as #include "RS484com.h" by any user program 5) the header file contains all needed interface functions: - the RS485 protocoll should be activated by a function void RS485Init(), which starts an endless task for the comm message sending / receiving switching; - RS485SendBytes(byte[12] myByteArray) sends 12 bytes or ubytes - RS485GetBytes (byte[12] myByteArray) receives 12 bytes or ubytes - the series of bytes in the ByteArrays have certain meanings as descibed befor (adressee, sender, length_of_data, sort_of_data, message, ..) which still has to be optimized EDIT: imagine: there are several bricks, each brick sends various events by various tasks. Now many messages come to a receiver - how can the receiver know what data this is and what to do with it?
If it's a sensor value: from which sensor port? If it's a calculated variable value: of which variable (you might give them numbers for transmission)
is it an integer value? is it a float value?
The event might be the following: e.g., you may have a ultra sonic distance Sensor at your nxt 01 (port 0=S1), and in case an obstacle has been detected, a message containig this SensorValue has to be transmitted to the "Master":
Then imagine, there may be an infrared Sensor on brick 02 port 02 which just detected a IR soccer ball; immediately he also sends his discovery to the master at almost the same time. So 2 messages arrive at the addressee and he must be able to distinguish which which message contains what kind of data (e.g., display this value on the master's screen or start/stop a motor)
this following might be the 1st message, a 2nd message may look similar: do you think we can establish such a standard? 
_________________ 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 Oct 17, 2008 11:17 am, edited 3 times in total.
|
| Sat Sep 06, 2008 7:15 am |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
hi Ford
as ogait told , it's the primary code... So, we will upgrade it.
By now, its workinG =)
|
| Sat Sep 06, 2008 4:00 pm |
|
 |
|
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, don't misunderstand me - it's a very good job you've done - all of you! I myself never had managed to come so far! And I need to say that I'm glad to see that my thread opening question had found so many interested people to work on this  It's just my intention to say that it might be the best way that all of you who are working on this communication code (docilio, ogait, Koldo, nxt) maybe should agree to a common syntax how to send and receive messages. If everyone is optimizing his own code by his own way there surely could be reached very good results, but 1 code won't work ever with the other. Maybe we can try to find a common platform for the "token ring" to work with the "motor remote control" ? docilio, ogait, Koldo, nxt: What do you think?
_________________ 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."
|
| Sat Sep 06, 2008 5:17 pm |
|
 |
|
ogait87
Rookie
Joined: Tue Aug 26, 2008 6:53 am Posts: 13
|
 Re: RS485 standardization
Probably my fault. I even forgot to make the comments in English. Probably we haven't explained why we used friendlynames. The task HSpeed need that each NXT has a unique number. Since the NXT I have used had names, it was simpler to use a single source file and have a line of code do generate de unique number. I some applications i can be easy to have multiple source files, each one include the HSpeed.c and in the main task sets the IAmNXT a different number in each source file. HSpeed task don't bother to know how you grant a unique number, it just need one. friendlynames was a easy way to do that. I am using a token ring scheme and i don now see why to have a NXT with more control than the others NXT. In another way to control the bus maybe. I am just spiking in controlling the bus. In an application where you have NXT just relaying sensors and motors it makes sense to have a "Master" NXT, but not in the buss sense  It is possible do just include a .h file with just the headers or the .h has to have code?  |  |  |  | Ford Prefect wrote: 5) the header file contains all needed interface functions: - the RS485 protocoll should be activated by a function void RS485Init(), which starts an endless task for the comm message sending / receiving switching; - RS485SendBytes(byte[12] myByteArray) sends 12 bytes or ubytes - RS485GetBytes (byte[12] myByteArray) receives 12 bytes or ubytes - the series of bytes in the ByteArrays have certain meanings as descibed befor (adressee, sender, length_of_data, sort_of_data, message, ..) which still has to be optimized EDIT: imagine: there are several bricks, each brick sends various events by various tasks. Now many messages come to a receiver - how can the receiver know what data this is and what to do with it?
If it's a sensor value: from which sensor port? If it's a calculated variable value: of which variable (you might give them numbers for transmission)
is it a 4 Byte integer value? is it a 8 Byte float value?
The event might be the following: e.g., you may have a ultra sonic distance Sensor at your nxt 01 (port 0=S1), and in case an obstacle has been detected, a message containig this SensorValue has to be transmitted to the "Master":
Then imagine, there may be an infrared Sensor on brick 02 port 02 which just detected a IR soccer ball; immediately he also sends his discovery to the master at almost the same time. So 2 messages arrive at the addressee and he must be able to distinguish which which message contains what kind of data (e.g., display this value on the master's screen or start/stop a motor)
this following might be the 1st message, a 2nd message may look similar: do you think we can establish such a standard?  |  |  |  |  |
I Think i can be done. some is already done , some is to be done (simpler interface) and you point a interesting problem, witch was ignored when the code had been made. I was just a data link, not a tcp like protocol.  but that is early implemented. Thanks for the appreciation. I soon i have a new code i publish it were.
_________________My sensors and librarys for robotc http://hng.av.it.pt/~tgoncalves/robotc/cgit.cgi/
Last edited by ogait87 on Sat Sep 06, 2008 6:48 pm, edited 1 time in total.
|
| Sat Sep 06, 2008 6:23 pm |
|
 |
|
ogait87
Rookie
Joined: Tue Aug 26, 2008 6:53 am Posts: 13
|
 Re: 1.40 public: how to use RS485 connection? code samples?
Sorry double posting but this was i tink it is more organized. I think we shouldn't try to include the "motor remote control" in this protocol. If someone wish to make a sersor/motor relay protocol on the top of this one is free to do so, i just have to define how to organize data, collect data and send it via HSpeed. A standard is a good idea. It allows to build others protocol on top of this one more easily.
_________________My sensors and librarys for robotc http://hng.av.it.pt/~tgoncalves/robotc/cgit.cgi/
|
| Sat Sep 06, 2008 6:31 pm |
|
 |
|
ogait87
Rookie
Joined: Tue Aug 26, 2008 6:53 am Posts: 13
|
 Re: RS485 standardization
to de deleted
_________________My sensors and librarys for robotc http://hng.av.it.pt/~tgoncalves/robotc/cgit.cgi/
|
| Sat Sep 06, 2008 6:48 pm |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: how to use RS485 connection? code samples?
The idea is that any comunication in here should respect that protocol (not finished)
Now it's just create a message lable like "01:sensor", "02:order","03:motor code" ...
|
| Sat Sep 06, 2008 6:49 pm |
|
 |
|
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?
maybe you could take my transmission block, and if you wish we can add another 4 bytes to this to be more flexible for the future: TBlock[7]= encoder types: TBlock[4]= value types:
_________________ 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 Sun Sep 07, 2008 5:10 pm, edited 20 times in total.
|
| Sun Sep 07, 2008 5: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?
Thanks for sugestions...
Ogait will read it and after it could create the end file...
what he created till now was a way to things work (they talk), not a way to share motors or sensors...
In some days, a final or beta version will be write...
Thanks
|
| Sun Sep 07, 2008 5:31 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, do you meanwhile have a workaround? Shall we have a 16 or 32 byte data block additional to the 8 byte header block to tranfer even more accurate ints and floats? Maybe if you'll post your working model we all can have a look upon this and improve it together?
_________________ 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."
|
| Thu Sep 18, 2008 5:34 am |
|
 |
|
Dick Swan
Creator
Joined: Fri Feb 09, 2007 9:21 am Posts: 613
|
 Re: 1.40 public: how to use RS485 connection? code samples?
This is a very interesting thread. I'm very interested in adding new functionality to ROBOTC firmware to facilitate RS485 communications. it's really a matter of coming up with something of interest to most users.
I was thinking of something along the lines of the existing Bluetooth mailbox messaging scheme except over RS-485. The higher level functionality is providing a message based interface.
The applications that can be developed are limitless.
One application that I've thought about is using a slave NXT as a motor and sensor expander. With the recent 1.43 ROBOTC release this is a lot easier as it introduces support for the HiTechnic Motor Controller (HMC). A HMC connects to a sensor port, communicates over I2C and supports two motors with encoders. Up to four HMC can be daisy chained on a single sensor port. There is an integrated driver for the HMC within the ROBOTC firmware. As part of the implementation, support was added to allow other types of motor controllers like this application; for this application, the driver might be best as a ROBOTC program with a separate task running handling the RS485 messaging to/from the slave NXT.
Another application is an RS485 connection to some of the available video cameras designed for robotics use.
|
| Mon Sep 22, 2008 11:30 pm |
|
 |
|
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, I'm not sure what you actually try to tell us (my English is unfortunately not good enough): Do I understand you right that this RS485 TCP concept finds your interest and you want to implement sort of "token ring server" running on a "slave nxt"? Or not? Unfortunately the muliplexers alone are no solution for my project, because the 15k variable limit is too poor and I need to implement different sub-programs on different nxt's (e.g., 1 is running the navigator, 1 is running the neural net, 1 is controlling the cam and the 5- or 6- axis arm), and all 3 have to communicate. If I had 50 or 60k variable space on 1 nxt, there was no problem using many multiplexers.
On the other hand, also the motor muxers of Hitechnic are no solution yet, because - I have 8 nxt motors at least, - nxt motors can not be pugged to the HT muxers, - even if I took VEX or TETRIX gear motors instead, then I needed to have 4 muxers daisy chained = 4*80 $= 320 $ plus the costs for the motors and the encoders... that is much too expensive - and after all gear motors can't be plugged to the Lego technic bars or beams
So a RS485 TCP could be the best solution for this all .
Maybe docilio & Co will find also a way to develop this - or even found it?
_________________ 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 Sep 23, 2008 2:56 am |
|
|
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
|
|