How does one fix a path-scanning robot going in circles?

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

Moderators: roger, 99jonathan, imaqine

How does one fix a path-scanning robot going in circles?

Postby skyminer » Sun Dec 02, 2012 8:16 pm

As a science fair project I chose to make a robot that follows a maze and reports the data to a Bluetooth-connected PC. The connection code is fine. It's just the minor bit about making it follow a line. The maze itself is made of 1/2 inch electrical tape on poster board, so it can tell the dark (getLightValue()<50%) paths apart from the light (!(getLightValue()<50%)). The robot is a modified Tribot (the included starter kit for the NXT 1.0), with the A-Motor removed and replaced with a set of light sensors. There are three light sensors, two back with the wheels, and one set an inch farther forward and centered. Unfortunately, the following code has the robot go in circles as soon as it falls off a line or comes across a corner. Any triple-commented (///) lines are used for the comm system, totally useless in the current debugging stages.
Code: Select all
//Author:▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒skyminer
//Version:   1.0.1

import java.io.*;
import lejos.nxt.*;
import lejos.nxt.comm.*;

class PathScanner {
   public static int xPos = 0; //The X position of the robot (0 is the start)
   public static int yPos = 0; //The Y position of the robot (0 is the start)
   public static int dir = 1; //The direction of the robot (1 is the start)(0 = north, 1 = east, 2 = south, 3 = west)
   public static boolean n, e, s, w; //True if there is a path in the direction indicated from the current cell
   public static boolean isScanComplete = false; //Initializes a boolean variable that shuts down the scan once true

///   public static BTConnection comm; //Initializes the connection
///   public static DataOutputStream out; //Initializes the connection's output stream
///   public static DataInputStream in; //Initializes the connection's input stream

   public static final LightSensor R = new LightSensor(SensorPort.S1, true); //Initializes the right light sensor
   public static final LightSensor C = new LightSensor(SensorPort.S2, true); //Initializes the center light sensor
   public static final LightSensor L = new LightSensor(SensorPort.S3, true); //Initializes the left light sensor

   public static void main(String[] args) {
      LCD.drawString("PathScanner", 0, 7);
///      comm = Bluetooth.waitForConnection(); //Connects the NXT to the PC
///      out = comm.openDataOutputStream(); //Opens the connection's output stream
///      in = comm.openDataInputStream(); //Open's the connection's input stream

      while(!isScanComplete) { //Starts the scanning loop
         Motor.C.rotateTo(280, true);
         Motor.B.rotateTo(280, true); //Starts the robot moving forward
         while(Motor.C.isMoving() && Motor.B.isMoving()) {
            if(!(C.getLightValue() < 50) || ((R.getLightValue() < 50) && (L.getLightValue() < 50))) {
               Motor.C.stop();
               Motor.B.stop();
            }
            if(R.getLightValue() < 50) {
               Motor.C.stop();
               while(!(R.getLightValue() < 50)) {}
               Motor.C.rotateTo(280, true);
            }
            if(L.getLightValue() < 50) {
               Motor.B.stop();
               while(!(L.getLightValue() < 50)) {}
               Motor.B.rotateTo(280, true);
            }
         }
         Motor.C.resetTachoCount();
         Motor.B.resetTachoCount();
         Motor.C.rotate(60, true);
         Motor.B.rotate(60, false);

         //Scans the cell data
         boolean r_on = R.getLightValue() < 50;
         boolean c_on = C.getLightValue() < 50;
         boolean l_on = L.getLightValue() < 50;

         //Configures the cell data
///         if(dir == 0) {
///            n = c_on;
///            e = r_on;
///            s = true;
///            w = l_on;
///         }
///         if(dir == 1) {
///            n = l_on;
///            e = true;
///            s = r_on;
///            w = c_on;
///         }
///         if(dir == 2) {
///            n = c_on;
///            e = l_on;
///            s = true;
///            w = r_on;
///         }
///         if(dir == 3) {
///            n = r_on;
///            e = c_on;
///            s = l_on;
///            w = true;
///         }

         //Gets boolean isScanComplete and performs the necessary operations
///         try {
///            isScanComplete = in.readBoolean();
///         } catch(IOException error) {
///            System.out.println(" write error " + error);
///         }
///         if(isScanComplete) {
///            break;
///         }

         //Sends the cell data
///         try {
///            out.writeInt(xPos);
///            out.writeInt(yPos);
///            out.writeBoolean(n);
///            out.writeBoolean(e);
///            out.writeBoolean(s);
///            out.writeBoolean(w);
///         } catch(IOException error) {
///            System.out.println(" write error " + error);
///         }

         //Prepares for the next move
         if(l_on) { //Turn left
            Motor.C.rotateTo(-150);
            Motor.B.rotateTo(150);
            if(!(dir == 0)) {
               dir--;
            } else {
               dir = 3;
            }
         }
         if(r_on) { //Turn right
            Motor.C.rotateTo(150);
            Motor.B.rotateTo(-150);
            if(!(dir == 3)) {
               dir++;
            } else {
               dir = 0;
            }
         }
         if(!l_on && !c_on && !r_on) { //Turn around
            Motor.C.rotateTo(-300);
            Motor.B.rotateTo(300);
            if(dir == 0) {
               dir = 2;
            } else if(dir == 1) {
               dir = 3;
            } else if(dir == 2) {
               dir = 0;
            } else {
               dir = 1;
            }
         }
         Motor.C.resetTachoCount();
         Motor.B.resetTachoCount();
      }
   }
}   


This version is in fact a total rework of a previous version, which had the same bug, going in circles. Due to my "amazing" organization skills, I seem to have lost it. However, all that was different is that there were absolutely no Motor.stops() methods, and coded likewise.
skyminer
New User
 
Posts: 2
Joined: Sun Dec 02, 2012 7:28 pm

Re: How does one fix a path-scanning robot going in circles?

Postby Aswin » Thu Dec 13, 2012 11:56 pm

Have you tried displaying the sensor values on screen? We're these as expected?

It will help you and us if you comment the code like this:
Code: Select all
//If just the right sensor is on the line, then the robot must turn right


Does it always turn in the same direction? Which one?

Aswin
My NXT blog: http://nxttime.wordpress.com/
Aswin
Active User
 
Posts: 122
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 5 guests

more stuff