icommand and threads

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

Moderators: 99jonathan, roger, imaqine

icommand and threads

Postby socrates » Sun Dec 02, 2007 5:43 pm

Is multithreading viable with icommand?

On the PC I am running two threads, one which controls a motor and one which reads the ultrasonic sensor continuously.

However, they both stop almost immediately. Is this because NXTCommand is not synchronised?

Regards,
Rupert
socrates
New User
 
Posts: 12
Joined: Sun Oct 28, 2007 7:36 pm

Postby PatrickLismore » Mon Dec 03, 2007 2:55 pm

have you considered using JCSP ME,

using classes like Parallel, One2OneChannelInt,

When you have several processes connect via channels they all seem to do it synchronously.
User avatar
PatrickLismore
Active User
 
Posts: 81
Joined: Tue Oct 16, 2007 7:28 pm
Location: Prague

Postby socrates » Mon Dec 03, 2007 10:45 pm

I can't see those in the API, though I managed to work around with a synchronized handler thus,

Code: Select all

package uk.co.moons.handlers;

import java.util.Vector;
import icommand.nxt.*;

/**
 *
 * @author Rupert Young
 */
public class CommandHandler {
   
   
    public static final int MOTOR_FORWARD = 0,
            MOTOR_BACKWARD = 1,
            MOTOR_ROTATE = 2,
            MOTOR_STOP = 3,
            MOTOR_SPEED = 4,
            US_DISTANCE= 5;
   
   
    private static CommandHandler singleton = new CommandHandler();
   
    /** Creates a new instance of CommandHandler */
    private  CommandHandler() {
    }
   
    public synchronized int command(Object obj, int cmd, int val){
        int rtn = 0;
        switch( cmd ){
            case MOTOR_STOP:
                ((Motor)obj).stop();
                break;
            case MOTOR_FORWARD:
                ((Motor)obj).forward();
                break;
            case MOTOR_BACKWARD:
                ((Motor)obj).backward();
                break;
            case MOTOR_ROTATE:
                System.out.println("+++ rotating "+ val);
                ((Motor)obj).rotate(val);
                ((Motor)obj).stop();
                //System.out.println("+++ count "+ Motor.A.getRotationCount());
                break;
            case US_DISTANCE:
                rtn = ((UltrasonicSensor)obj).getDistance();
                break;
        }
       
        return rtn;
    }
   
    public static CommandHandler getSingleton() {
        return singleton;
    }
   
   
}


called like so,

Code: Select all
reading = CommandHandler.getSingleton().command( sensor, CommandHandler.US_DISTANCE, 0);


Regards,
Rupert
socrates
New User
 
Posts: 12
Joined: Sun Oct 28, 2007 7:36 pm

Postby bbagnall » Wed Dec 12, 2007 6:04 pm

Rupert, you can look at the code for iCommand here:

http://nxtcommand.svn.sourceforge.net/v ... /nxt/comm/

NXTCommand and NXTComm are the main communications classes. If you want to try some improvements, please do so and submit the code to me for future releases.
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 2 guests

more stuff