Some questions about NXJ

Post your NXJ projects, project ideas, etc here!

Moderators: roger, 99jonathan, imaqine

Some questions about NXJ

Postby zedex18 » Thu Aug 09, 2012 7:54 am

Hello everyone. I followed the tutorial and would create a simple code for a sentinel robot. My NXT has 2 weels and a us detector. I want it going straight until he detect something at ~30 cm from it. Then it goes back and turn a 1/3 turn on itself.

Currently my code works but I have some problems I want to solve :

    I want it to stop at any moment by pressing a screen button, but when it is going back it does not read any button. I have to wait it going straight. How can I solve it ?
    When I want to stop motors, there is a little delay between 2 commands so when robot detect something, it still turn a little on itself. How can I correct this ?
    When it is going back, I use controlled rotation (with angles). But there is a little delay between each commands, I want all done without any interruption. How can I proceed ?

Here is my code commented :

Code: Select all
package sentinel;

import lejos.nxt.*;   // Motors, buttons and LCD
import lejos.robotics.objectdetection.*;   // US
import lejos.util.Delay;   // msDelay()
import java.util.*;   // Random()

/* This programm is a sentinel : the robot should go straight in a direction.
 * When it detect something, it go back, turn a 1/3 and go straight.
 *
 * Notes :
 *
 * Created on 11.06.2012
 * Codeur : CHEN Cedric
 */
public class Sentinel
{
   public static void main(String[] args)
   {
      int distance = 30;   // us detects on 30 cm.
      int distanceMax = 100;   
      int speed = 2*360;   // 2 round per second.
      int angle = 666;      // Do not know why, but when weel turn of 666°, the robot turn of 1/3.
      int rd;   // random.
      
      // Initialisation of speed
      Motor.B.setSpeed(speed);
      Motor.C.setSpeed(speed);
      
      // ultrasonic constructor
      UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4);
      FeatureDetector fd = new RangeFeatureDetector(us, distanceMax, 500);
      
      // random constructor
      Random random = new Random();
      
      /************************************************************
       *         Programm
       ***********************************************************/
      LCD.drawString("Timmy !!", 5, 3);   // Print rotot's name
      Button.waitForAnyPress();   // wait before start
      Delay.msDelay(500);
      
      // while we do not press a buton to stop
      while(Button.readButtons() == 0)
      {
         Feature result = fd.scan();
         
         // if it detects nothing
         if(result == null || result.getRangeReading().getRange() > distance)
         {
            // still going straight
            Motor.B.forward();
            Motor.C.forward();
         }
         else
         {
            // we stop motors
            Motor.B.stop();
            Motor.C.stop();   // Here is the delay I talked about.
            
            // 2 weel rounds to go back
            Motor.B.rotate(-2*360, true);
            Motor.C.rotate(-2*360, true);
            while (Motor.B.isMoving() || Motor.C.isMoving());

            // Here is an invisible delay I want to delete.
            
            // random integer we change in +/-1 for the "turn around"
            rd = random.nextInt(2);
            if (rd == 0)   rd = 1;
            else         rd = -1;
            
            // turn aroud
            Motor.B.rotate(rd * angle, true);
            Motor.C.rotate(-rd * angle, true);
            while (Motor.B.isMoving() || Motor.C.isMoving());
         }
      }
      
      Motor.B.stop();
      Motor.C.stop();
   }

}


Thanks you for your help.
zedex18
New User
 
Posts: 8
Joined: Fri Jun 08, 2012 3:13 pm

Re: Some questions about NXJ

Postby PallottiSoccerPLayer » Sat Feb 09, 2013 9:23 pm

To your first question:
Maybe it would have helped to add a condition to the following loop:

while (( Motor.B.isMoving() || Motor.C.isMoving() )&& Button.readButtons() == 0 );

To your second and third question:
Try DifferentialiPilot next time, I think it compansates such delays.

yw
PallottiSoccerPLayer
New User
 
Posts: 5
Joined: Sat Feb 09, 2013 6:21 pm

Re: Some questions about NXJ

Postby gloomyandy » Sat Feb 09, 2013 9:26 pm

The stop method you are using will wait for the motor to stop. Use the alternative form that allows you to specify that you do not want it to wait for the motor to stop.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 0 guests

cron
more stuff