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?
Tried to make multiple channels available, but bugs in the new code make me go back. A Little improvement over the last code is where. One note in this code. HSeed.c is a library that exports 3 functions. This library only allows 1 Task to use it at a time, so without mutexes or Semaphore or something similar. I am working the time i can to make one that allows multi Taks. I already can pass the token to each NXT but not sending data. viewtopic.php?p=3092#p3092
_________________My sensors and librarys for robotc http://hng.av.it.pt/~tgoncalves/robotc/cgit.cgi/
|
| Thu Oct 16, 2008 4:22 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?
(edit: see below!)
_________________ 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 Nov 07, 2008 6:31 pm, edited 2 times in total.
|
| Wed Nov 05, 2008 11:19 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?
so, lets see - my thread opening question was My question was NOT just how to make a RS485 connection, but how to use the 2nd nxt as a multiplexer. So I'm sorry, I actually can't find the code for motor controlling or sensor reading of a 2nd NXT... could you plz explain it to me?  what we need is 1 command for motor remote control like SetHSRemoteMotor(nxtID, port, speed)and 1 command for remote sensor polling like GetHSRemoteSensorValue(nxtID, port, &value)Everything else should be done by an included header file like #include "RS485_API.h" .
_________________ 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 Nov 07, 2008 8:39 pm, edited 2 times in total.
|
| Wed Nov 05, 2008 11:24 am |
|
 |
|
ogait87
Rookie
Joined: Tue Aug 26, 2008 6:53 am Posts: 13
|
 Re: 1.40 public: RS485 connection for M/S-muxing? samples?
Sorry. I did not read the first posts of the topic Docilio made me a resume and he was just horried at that moment in making a bi directional comunication width 2 NXT. I have to apologie too because the last code did not work. Or it is my impression or the basic functions to control de the RS485 had side effects not counted. But now I have it working and a simple API to control a remote (ou more remotes) NXT. HSpeed.c is a API for NXT to NXT communication (no more than 1 task allowed to use) teste_ligacao.c and teste_ligacao2.c are 2 programs to test the connection. teste_ligacao2.c can serve as an example but do not bother read them. Remote.c is your RS485_API.h and it uses HSpeed.c to comunicate to the other NXT but i requires to be the unique to use HSpeed funtions (for now). slave.c is the program to be run in the slave NXT. master.c is an example program to show how to use the Remote.c I hope is not too complicated to see how to use these functions....
_________________My sensors and librarys for robotc http://hng.av.it.pt/~tgoncalves/robotc/cgit.cgi/
|
| Fri Nov 07, 2008 6:13 pm |
|
 |
|
ogait87
Rookie
Joined: Tue Aug 26, 2008 6:53 am Posts: 13
|
 Re: 1.40 public: RS485 connection for M/S-muxing? samples?
Only 3 attachements, so i need another post 
_________________My sensors and librarys for robotc http://hng.av.it.pt/~tgoncalves/robotc/cgit.cgi/
|
| Fri Nov 07, 2008 6:13 pm |
|
 |
|
mightor
Moderator
Joined: Wed Mar 05, 2008 8:14 am Posts: 2858 Location: Rotterdam, The Netherlands
|
 Re: 1.40 public: RS485 connection for M/S-muxing? samples?
Ogait,
Thanks a lot for the code and all the effort you put into writing it!
Regards, Xander
_________________| Some people, when confronted with a problem, think, "I know, I'll use threads," | and then two they hav erpoblesms. (@nedbat)| My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
| Fri Nov 07, 2008 6:21 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: RS485 connection for M/S-muxing? samples?
thx a lot for the code! For reading I still think it's good to publish the files than only to link for download:  |  |  |  | Code: // master.c //////////////////////////////////////////////////////////////////////////////////////////////////////// // // Remote NXT Control Client Example // // Program to serve as an example to how to use the 'Remote.c' functions // // Autor : Tiago Costa Gonçalves (ogait87) // Release: 0.0.1 // Date : 07/11/2008 // //////////////////////////////////////////////////////////////////////////////////////////////////////// #include "Remote.c"
task main() { // Setup string sFriendlyName; getFriendlyName(sFriendlyName); IAmNXT = sFriendlyName == "HRF2" ? 0 : 1; MaxNXT = 2; setupHighSpeedLink();
// int other = (IAmNXT+1) % MaxNXT; ubyte motor_pos; ubyte sensor_value;
// int count = 0; while( 1 ) { count++; nxtDisplayTextLine(2, "i: %d", count);
SetHSRemoteMotor( other, 0, 50); wait1Msec(500); SetHSRemoteMotor( other, 0, 00); wait1Msec(500);
GetHSRemoteEncoderValue( other, 0, motor_pos); nxtDisplayTextLine(4, "Motor Pos: %d", motor_pos);
GetHSRemoteSensorValue( other, 0, sensor_value); nxtDisplayTextLine(5, "Sensor Value: %d", sensor_value); } }
|  |  |  |  |
 |  |  |  | Code: // slave.c #pragma config(Sensor, S1, ss, sensorTouch) #pragma config(Motor, motorA, aaa, tmotorNxtEncoderClosedLoop) #pragma config(Motor, motorB, aab, tmotorNxtEncoderClosedLoop) #pragma config(Motor, motorC, aac, tmotorNxtEncoderClosedLoop) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
//////////////////////////////////////////////////////////////////////////////////////////////////////// // // Remote NXT Control Client Server // // Task to process a remote controler command // // Autor : Tiago Costa Gonçalves (ogait87) // Release: 0.0.1 // Date : 07/11/2008 // //////////////////////////////////////////////////////////////////////////////////////////////////////// #include "HSpeed.c"
task main() { // Setup string sFriendlyName; getFriendlyName(sFriendlyName); IAmNXT = sFriendlyName == "HRF2" ? 0 : 1; MaxNXT = 2; setupHighSpeedLink();
// int count=0; while( 1 ) { HSpeedRxDataLen = 0; while( HSpeedRxDataLen == 0 ) wait1Msec(10);
count++; nxtDisplayTextLine(2, "i: %d Len: %d", count, HSpeedRxDataLen);
if ( HSpeedRxDataLen == 2 && HSpeedRxData[0] >= 1 && HSpeedRxData[0] <= 3 ) { motor[HSpeedRxData[0]-1] = HSpeedRxData[1]; nxtDisplayTextLine(4, "M: %d Speed: %d", HSpeedRxData[0]-1, HSpeedRxData[1]); }
if ( HSpeedRxDataLen == 1 && HSpeedRxData[0] >= 4 && HSpeedRxData[0] <= 6 ) { HSpeedSendByte(HSpeedRxHead, nMotorEncoder[HSpeedRxData[0]-4]); nxtDisplayTextLine(5, "MPos: %d MPos: %d", (int)HSpeedRxData[0]-4, nMotorEncoder[HSpeedRxData[0]-4]); }
if ( HSpeedRxDataLen == 1 && HSpeedRxData[0] >= 7 && HSpeedRxData[0] <= 10 ) { HSpeedSendByte(HSpeedRxHead, SensorValue[HSpeedRxData[0]-7]); nxtDisplayTextLine(6, "SP: %d Value: %d", (int)HSpeedRxData[0]-7, SensorValue[HSpeedRxData[0]-7]); } } }
|  |  |  |  |
 |  |  |  | Code: // remote.c //////////////////////////////////////////////////////////////////////////////////////////////////////// // // Remote NXT Control API // // Functions to control a remote NXT // // Autor : Tiago Costa Gonçalves (ogait87) // Release: 0.0.1 // Date : 07/11/2008 // //////////////////////////////////////////////////////////////////////////////////////////////////////// #include "HSpeed.c"
void SetHSRemoteMotor(ubyte remote, ubyte motor, ubyte speed) { HSpeedSendByte(remote, 1 + 0*3 + motor, speed); }
void GetHSRemoteEncoderValue(ubyte remote, ubyte motor, ubyte &pos) { HSpeedSendByte(remote, 1 + 1*3 + motor); HSpeedRxDataLen = 0; while( HSpeedRxDataLen == 0 ) wait1Msec(10); pos = HSpeedRxData; HSpeedRxDataLen = 0; }
void GetHSRemoteSensorValue(ubyte remote, ubyte sensorPort, ubyte &value) { HSpeedSendByte(remote, 1 + 2*3 + sensorPort); HSpeedRxDataLen = 0; while( HSpeedRxDataLen == 0 ) wait1Msec(10); value = HSpeedRxData; HSpeedRxDataLen = 0; }
|  |  |  |  |
 |  |  |  | Code: // highspeed.c //////////////////////////////////////////////////////////////////////////////////////////////////////// // // Port S4 High-Speed RS-485 Full Duplex Token Ring // // Autor : Tiago Costa Gonçalves (ogait87) // Release: 0.0.6 // Date : 07/11/2008 // ////////////////////////////////////////////////////////////////////////////////////////////////////////
//#define HSPEED_DEBUG
#ifndef __HSPEED_C__ #define __HSPEED_C__
#ifndef HSPEED_TIMEOUT_BASE #define HSPEED_TIMEOUT_BASE 1000 #endif
#ifndef HSPEED_TIMEOUT_RAND #define HSPEED_TIMEOUT_RAND 1000 #endif
// // Public // int MaxNXT = 1; int IAmNXT = 0;
ubyte HSpeedRxHead[1]; ubyte HSpeedRxData[16]; ubyte HSpeedRxDataLen = 0;
task HSpeed;
void setupHighSpeedLink();
void HSpeedSendByte(ubyte destino, ubyte data); void HSpeedSendByte(ubyte destino, ubyte data_a, ubyte data_b); void HSpeedSendByte(ubyte destino, ubyte data_a, ubyte data_b, ubyte data_c);
///////////////////////////////////////////////////////////////////////////////////////// // Private ///////////////////////////////////////////////////////////////////////////////////////// ubyte HSpeedTxData[1+16]; ubyte HSpeedTxLen = 0;
ubyte chr[16];
void setupHighSpeedLink() { nxtEnableHSPort(); //nxtSetHSBaudRate(); nxtHS_Mode = hsMsgModeMaster ; nxtHS_Mode = hsRawMode;
StartTask(HSpeed);
return; }
void HSpeedSendByte(ubyte destino, ubyte data) { while(HSpeedTxLen != 0) EndTimeSlice();
HSpeedTxData[0] = (IAmNXT << 6) | (destino << 4) | 1; HSpeedTxData[1] = data; HSpeedTxLen = 1; }
void HSpeedSendByte(ubyte destino, ubyte data_a, ubyte data_b) { while(HSpeedTxLen != 0) EndTimeSlice();
HSpeedTxData[0] = (IAmNXT << 6) | (destino << 4) | 2; HSpeedTxData[1] = data_a; HSpeedTxData[2] = data_b; HSpeedTxLen = 2; }
void HSpeedSendByte(ubyte destino, ubyte data_a, ubyte data_b, ubyte data_c) { while(HSpeedTxLen != 0) EndTimeSlice();
HSpeedTxData[0] = (IAmNXT << 6) | (destino << 4) | 3; HSpeedTxData[1] = data_a; HSpeedTxData[2] = data_b; HSpeedTxData[3] = data_c; HSpeedTxLen = 3; }
void HSpeedMaster() { #ifdef HSPEED_DEBUG static int master_count = 0; master_count++; nxtDisplayTextLine(6, "MASTER: %d", master_count); #endif
if ( HSpeedTxLen > 0) { nxtWriteRawHS(HSpeedTxData, 1+HSpeedTxLen, 0); HSpeedTxLen = 0; } else { nxtWriteRawHS((IAmNXT << 6), 1, 0); } }
bool HSpeed_ReadRawHS(int n) { int i, timeout = HSPEED_TIMEOUT_BASE + random[HSPEED_TIMEOUT_RAND];
ClearTimer(T1); for(i=0; i<n; ) { i += nxtReadRawHS(chr[i], n-i); if ( time1[T1] >= timeout ) { return false; } }
return true; }
task HSpeed() { int senderID, reciverID, length;
#ifdef HSPEED_DEBUG int head_count = 0; int loop_count = 0; int timeout_count = 0; #endif
if ( IAmNXT == 0 ) // So start roling the comunication { HSpeedMaster(); }
while(true) { #ifdef HSPEED_DEBUG loop_count++; nxtDisplayTextLine(3, "LOOP: %d", loop_count); #endif
// Receve 1 byte, the header if ( !HSpeed_ReadRawHS(1) ) { #ifdef HSPEED_DEBUG timeout_count++; nxtDisplayTextLine(7, "TIMEOUT1: %d", timeout_count); #endif HSpeedMaster(); // so be me now :) continue; }
// Get Head fields senderID = (chr[0] >> 6) & 0x3; reciverID = (chr[0] >> 4) & 0x3; length = (chr[0] >> 0) & 0xF;
#ifdef HSPEED_DEBUG nxtDisplayTextLine(4, "SRC:%d DEST:%d", senderID, reciverID); head_count++; nxtDisplayTextLine(5, "LEN: %d COUNT: %d", length, head_count); #endif
if ( length > 0 ) { if ( !HSpeed_ReadRawHS(length) ) { #ifdef HSPEED_DEBUG timeout_count++; nxtDisplayTextLine(7, "TIMEOUT2: %d", timeout_count); #endif HSpeedMaster(); // so be me now :) continue; }
if ( reciverID == IAmNXT ) { HSpeedRxHead[0] = senderID; memcpy(HSpeedRxData, chr, length); HSpeedRxDataLen = length; } }
// I am the next NXT to transmit? if ( (senderID + 1) % MaxNXT == IAmNXT ) { HSpeedMaster(); } } }
#endif
|  |  |  |  |
Last edited by Ford Prefect on Fri Nov 07, 2008 8:04 pm, edited 1 time in total.
|
| Fri Nov 07, 2008 6:24 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: RS485 connection for M/S-muxing? samples?
(...continuing...)  |  |  |  | Code: // test_ligacao2.c //////////////////////////////////////////////////////////////////////////////////////////////////////// // // Port S4 High-Speed RS-485 Full Duplex Token Ring Tester // // Program to test the efficiency of the HSpeed Connection // // Autor : Tiago Costa Gonçalves (ogait87) // Release: 0.0.2 // Date : 07/11/2008 // //////////////////////////////////////////////////////////////////////////////////////////////////////// #define HSPEED_TIMEOUT_BASE 10000 //#define HSPEED_DEBUG
#include "HSpeed.c"
//////////////////////////////////////////////////////////////////////////////////////////////////////// // // recieve Data // //////////////////////////////////////////////////////////////////////////////////////////////////////// void recieve() { int counta = 0;
while(true) { if( HSpeedRxDataLen != 0 ) { counta++; nxtDisplayTextLine(6, "Rx: ID %d Len %d", HSpeedRxHead[0] , HSpeedRxDataLen); nxtDisplayTextLine(7, "Rx: %d %d" , (int)HSpeedRxData[0], counta ); HSpeedRxDataLen = 0; } } } //////////////////////////////////////////////////////////////////////////////////////////////////////// // // send Data // //////////////////////////////////////////////////////////////////////////////////////////////////////// void send() { int count = 0;
ClearTimer(T2); while(true) { if( time1[T2] > 20 ) { count++; HSpeedSendByte( (IAmNXT+1) % MaxNXT, count); nxtDisplayTextLine(4, "Send: %d", count); ClearTimer(T2); } } }
//////////////////////////////////////////////////////////////////////////////////////////////////////// // // Main Task // //////////////////////////////////////////////////////////////////////////////////////////////////////// task main() { // Setup string sFriendlyName; getFriendlyName(sFriendlyName); IAmNXT = sFriendlyName == "HRF2" ? 0 : 1; MaxNXT = 2; setupHighSpeedLink();
// eraseDisplay(); bNxtLCDStatusDisplay = true; // Enable top status line display
nxtDisplayTextLine(2, "IAmNXT: %d MAX: %d", IAmNXT, MaxNXT);
if ( IAmNXT == 0 ) { send(); } else { recieve(); } }
|  |  |  |  |
_________________ 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 Nov 07, 2008 8:04 pm, edited 1 time in total.
|
| Fri Nov 07, 2008 6:32 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: RS485 connection for M/S-muxing? samples?
man, what a program! I guess I need some days even to test this. Great job! Congratulations! Nominated for the first RobotC Improvement Emmy Award! 
_________________ 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 Nov 07, 2008 7:08 pm, edited 2 times in total.
|
| Fri Nov 07, 2008 6:39 pm |
|
 |
|
ogait87
Rookie
Joined: Tue Aug 26, 2008 6:53 am Posts: 13
|
 Re: 1.40 public: RS485 connection for M/S-muxing? samples?
Thanks I think it is easy to use this. 1º Put HSpeed.c Remote.c slave.c master.c in the same dir 2º Give a name to each NXT 3º Change in slave.c and master.c "HRF2" to the the name of the Master NXT (the one that is going to control de other) 4º Download slave.c to the NXT to be controlled and connect a motor in port A and a push sensor in sensor port 1. 5º Download master.c to the NXT to be the controller. 6º Do not forget to connect both sensor port 4 7º Start the slave program and then the master program in the other NXT 8º Enjoy
_________________My sensors and librarys for robotc http://hng.av.it.pt/~tgoncalves/robotc/cgit.cgi/
|
| Fri Nov 07, 2008 6:58 pm |
|
 |
|
docilio
Expert
Joined: Sun Sep 09, 2007 10:12 am Posts: 116
|
 Re: 1.40 public: RS485 connection for M/S-muxing? samples?
That's my friend !!! I think that this code is fine. It was a good investment giving you an extra NXT  I wil test it with 4 nxt (i guess i can do it)
|
| Fri Nov 07, 2008 7:34 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: RS485 connection for M/S-muxing? samples?
hi, I tested you code! The good news: the motor is going forward in steps each 1/2 second as it should.
But unfortunately the values are not shown correctly at my master's display: the motor encoder stays all the time at a value of maybe 41272, and the sensor always shows a value of 413 either if it has been pressd or not.
Have I done something faulty?
_________________ 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 Nov 08, 2008 9:20 am |
|
 |
|
ogait87
Rookie
Joined: Tue Aug 26, 2008 6:53 am Posts: 13
|
 Re: 1.40 public: RS485 connection for M/S-muxing? samples?
For a quick answer i guess it is due to the different compiler version. I was using the 1.40 version of the compiler.
Ether way, that values should never appear since i use ubyte type. Maybe a conversion error? The motor encoder should change by it self but due the way the example master program was build, the touch sensor is only acquired just before the motor starts. Ether way, only 0 or 1 should appear.
Have you made a custom master.c?
_________________My sensors and librarys for robotc http://hng.av.it.pt/~tgoncalves/robotc/cgit.cgi/
|
| Sat Nov 08, 2008 10: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: RS485 connection for M/S-muxing? samples?
Of course I had only changed the name of my master (simply "002", my slave is "003"), nothing else. I'm using 1.45 But ubyte always has been a bad choice in RobotC, because in RobotC it doesn't exist any unsigned value at all (although the usage won't suprisingly lead to a compiler error). So now I changed two code lines by an added "(int)" for value conversion from byte to int: now the values are more credible, at least the touch sensor value now changes between 1/0 when pressed/not pressed. THAT'S FINE. But consider that the motor encoder indeed is a long (32 bit = 4 byte) integer value. Using BYTE (CHAR) as a return value of this, naturally the values at the master's display jump in a way like -4 -7 -95 -121 -114 -117 -125 +127 and so on. so the GetMotorEncoderValue should poll 32 bit integers instead of 1 byte (or ubyte) = 8 bit. Other sensor values (Light, Compass) have a short integer value as a result (0...1023). Try it out by changing the attached Sensor from "Touch" to "Sonar". So I generally would suggest to use 16 bit = 2 byte int as a return value of GetSensorValueand 32 bit = 4 byte long as a return value of GetMotorEncoderValueregards, Helmut
_________________ 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 Nov 08, 2008 10:25 am |
|
 |
|
spaceballs
Rookie
Joined: Sat Apr 19, 2008 11:51 am Posts: 43
|
 Re: 1.40 public: RS485 connection for M/S-muxing? samples?
ogait, many thanks and congratulations from me, too! As Helmut and I am working on bigger robot projects with many Lego sensors and Lego motors it's a great help for of us.
The reason why your program sometimes hangs when using different tasks for polling and controlling, is now clear: because of missing comm synchronization. As I heard, you're meanwhile working on this issue, too, I'm gladly looking forward to this!
cu spaceballs
|
| Tue Nov 11, 2008 7:06 am |
|
|
Who is online |
Users browsing this forum: No registered users and 4 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
|
|