Multithreading Problem

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Multithreading Problem

Postby nicolasj92 » Sun Mar 02, 2014 7:51 pm

Hey Guys,

i have a strange problem...
I wrote a program which uses one thread for every motor regulator (2 at the time --> 1 for each Motor).
I extended the thread class to create these threads and they are just simple PD regulators for precise movement (I cant use the included motor regulators for my purpose)

I tested each Motor controller on its own and they seem to work fine but when i start them together the java console (its a pc project) says "Error in write" without further explanations..
the program continues but the motor controllers don't do anything...

Does somebody experienced something similar? are there special restrictions with multithreading in Lejos?
I think it maybe has to do something with the controllers trying to send nxt commands parallel?

Thanks in advance

sample code from one of the controllers: (same for the other controller)

public void run() {
int pos = 0; // current position
int e = 0; // current error
int ealt = 0; // old error
int d = 0; // current differential
double Kp = 1.5; // proportional gain
double Kd = 0.5; // differential gain
int y = 0; // calculated Motor Power

while (true) {
Delay.msDelay(10);
pos = Motor.A.getTachoCount();
e = comA - pos;
d = e - ealt;
y = (int) Math.round(Kp * e + Kd * (d / 10));
ealt = e;
System.out.println("message from thread Target A: " + e);
if (Math.abs(y) > 2) {
if (y >= 0) {
if(y > 100){
y = 100;
}
Motor.A.forward();
Motor.A.setPower(y);
} else {
if(y < -100){
y = -100;
}
Motor.A.backward();
Motor.A.setPower(-y);
}
}else{
Motor.A.stop();
}
}

}
nicolasj92
New User
 
Posts: 7
Joined: Thu Feb 27, 2014 6:44 pm

Re: Multithreading Problem

Postby gloomyandy » Sun Mar 02, 2014 8:16 pm

Are you running this over Bluetooth or USB?

Out of interest why can't you use the built in controller?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3950
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Multithreading Problem

Postby nicolasj92 » Sun Mar 02, 2014 8:31 pm

I am running it over USB do you think that would make a difference?
The axis should drive to given positions which change very fast and I can't wait for every position to be reached because it should be a fluid movement (non linear) I don't know how to explain it better
nicolasj92
New User
 
Posts: 7
Joined: Thu Feb 27, 2014 6:44 pm

Re: Multithreading Problem

Postby gloomyandy » Sun Mar 02, 2014 8:45 pm

What makes you think you need to wait for the position to be reached before issuing a new command? I'm not sure I understand your reason for not using the standard regulator, if you explain it I may be able to point out ways you can use it. I'm surprised that you get good motor regulation using a remote motor in this way, normally the round trip time would be too high for this to work well.

Yes it will make a difference it looks like the USB version of the sendRequest method is not synchronized:
http://sourceforge.net/p/lejos/code/HEA ... .java#l496
the Bluetooth version is. You could fix it by changing this method to be synchronized and rebuilding the pccomms project.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3950
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Multithreading Problem

Postby nicolasj92 » Sun Mar 02, 2014 9:43 pm

I am actually TRYing to build a 3D printer out of lego (just the positioning parts). So i want to write a program which drives to the coordinates given in the gcode (kind of numerical control)
I took a look in the api documentation and noticed that the rotateto method features immediate return so maybe you are right and i can use the standard regulators and don't have to use my regulator threads :-)
(when i tested them individually they seemed to work fine but i didn't do any perfomance analysis yet)
i will try the standard regulator and i will try to use bluetooth..
thanks for the reply
nicolasj92
New User
 
Posts: 7
Joined: Thu Feb 27, 2014 6:44 pm

Re: Multithreading Problem

Postby gloomyandy » Sun Mar 02, 2014 11:17 pm

If what you are trying to do is to enable continuous smooth movement of all three motors in a coordinated way then I'm not sure that using rotateTo or whatever type commands is the way to go. By definition these commands are designed to bring the motor to a stop. To create smooth movement you will need to vary the speeds of all three motors as the "tool" moves through the desired path. You may be able to do this by varying the speed and acceleration of the three motors as required as the tool moves along the path, but synchronizing all of this will not be easy. The good new is that as you are using gcode you can analyze all of the moves required ahead of time and build up the sequence of motor operations rather than having o do them on the fly.

Good luck!

PS you may be interested in this project I worked on a few years ago:
http://www.gloomy-place.com/cnc.htm
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3950
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff