beacon

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

Moderators: roger, gloomyandy, skoehler

moo
New User
Posts: 23
Joined: Thu Apr 06, 2017 2:43 am

beacon

Postby moo » Sun May 07, 2017 6:57 pm

I am trying to make robot follow the beacon but noticed that beacon heading seems to flicker all the time? (25,0,-5,etc)

I don't have this issue when I use Tools Menu under the brick.

What am i doing wrong?


Code: Select all

import javax.swing.plaf.basic.BasicButtonListener;

import lejos.hardware.Key;
import lejos.hardware.KeyListener;
import lejos.hardware.Button;
import lejos.hardware.lcd.LCD;
import lejos.hardware.motor.*;
import lejos.hardware.port.*;
import lejos.hardware.sensor.*;
import lejos.hardware.Button;
import lejos.hardware.Sound;
import lejos.robotics.Color;
import lejos.robotics.ColorAdapter;
import lejos.robotics.RangeFinderAdapter;
import lejos.robotics.SampleProvider;
import lejos.robotics.navigation.*;
import lejos.utility.Delay;


class EUPRobot extends Thread {
   EV3LargeRegulatedMotor rightMotor;
   EV3LargeRegulatedMotor leftMotor;
   EV3MediumRegulatedMotor shootMotor;
   EV3IRSensor ir_sensor;
   RangeFinderAdapter frontView;
   EV3ColorSensor colorSensor;
   EV3TouchSensor touchSensor;
   
   SensorMode beaconMode;
   void initialize()
   {
      System.out.println("Initialize start");
      rightMotor=new EV3LargeRegulatedMotor(MotorPort.C);
      leftMotor = new EV3LargeRegulatedMotor(MotorPort.B);
      shootMotor = new EV3MediumRegulatedMotor(MotorPort.A);
      ir_sensor = new EV3IRSensor(SensorPort.S4);
      frontView = new RangeFinderAdapter(ir_sensor);
      colorSensor=new EV3ColorSensor(SensorPort.S3);
      touchSensor = new EV3TouchSensor(SensorPort.S1);
      beaconMode = ir_sensor.getSeekMode();
   }
     public void run()
     {

         LCD.clear();
          Button.ESCAPE.addKeyListener(new KeyListener() {
              public void keyPressed(Key k){
                System.exit(0);
              }
              public void keyReleased(Key k){
                   System.exit(0);
                 }          
          }
          );
         

         LCD.drawString("EushAppa ROBOT", 0, 2);
             
         while(true)
         {
            // Get the IR commands
            int channel=-1;
            byte [] cmds = new byte[4];
            ir_sensor.getRemoteCommands(cmds, 0, cmds.length);
            
            // Figure out which channel is active:
            int command = 0;
            for(int i=0;i<4;i++) {
               if(cmds[i] > 0) {
                  channel = i+1;
                  command = cmds[i];
               }
            }
   
            float[] beacon=this.getBeaconReadings();
            LCD.drawString("Distance " + this.getDistance(), 0, 2);
            LCD.drawString("Beacon "+ beacon[0] + " x " + beacon[1] , 0, 4);
            LCD.drawString("color ref: " + this.reflectedLight() + " amb:"+this.ambientLight(), 0, 7);
            
            float direction  = beacon[0];
            
   
            if(direction<-10)
            {
               moveRightForward();
            }
            else if(direction>10)
            {
               moveLeftForward();
            }

            
         /*   
            1 TOP-LEFT
            2 BOTTOM-LEFT
            3 TOP-RIGHT
            4 BOTTOM-RIGHT
            5 TOP-LEFT + TOP-RIGHT
            6 TOP-LEFT + BOTTOM-RIGHT
            7 BOTTOM-LEFT + TOP-RIGHT
            8 BOTTOM-LEFT + BOTTOM-RIGHT
            9 CENTRE/BEACON
            10 BOTTOM-LEFT + TOP-LEFT
            11 TOP-RIGHT + BOTTOM-RIGHT
            */
            
            switch(command)
            {
            case 1:
               moveLeftForward();
               break;
            case 2:
               moveLeftBackward();
               break;
            case 3:
               moveRightForward();
               break;
            case 4:
               moveRightBackward();
               break;
            case 5:
               moveLeftForward();
               moveRightForward();
               break;
            case 6:
               moveLeftForward();
               moveRightBackward();
               break;
            case 7:
               moveLeftBackward();
               moveRightForward();
               break;
            case 8:
               moveLeftBackward();
               moveRightBackward();
               break;
            
               
            default:
               flt();
            }
            
         }
        
     }
   float[] RGB()
   {
       SampleProvider sp = colorSensor.getRGBMode();
       float[] sample = new float[sp.sampleSize()];
      sp.fetchSample(sample, 0);
      
       return sample;
   }
   float getColorID()
   {
      SampleProvider sp = colorSensor.getColorIDMode();        
      float[] sample = new float[sp.sampleSize()];
      sp.fetchSample(sample, 0);
      
      return sample[0];
   }
   float ambientLight()
   {
      SampleProvider sp = colorSensor.getAmbientMode();        
      float[] sample = new float[sp.sampleSize()];
      sp.fetchSample(sample, 0);
      
      return sample[0];
   }
   float reflectedLight()
   {
      SampleProvider sp = colorSensor.getRedMode();        
      float[] sample = new float[sp.sampleSize()];
      sp.fetchSample(sample, 0);
      
      return sample[0];
   }
   void flt()
   {
      rightMotor.flt();
      leftMotor.flt();
   }
   void moveLeftForward()
   {
      leftMotor.forward();
   }
   void moveLeftBackward()
   {
      leftMotor.backward();
   }
   void moveRightForward()
   {
      rightMotor.forward();
   }
   void moveRightBackward()
   {
      rightMotor.backward();
   }
   float getDistance()
   {
      RangeFinderAdapter rf = new RangeFinderAdapter(ir_sensor.getDistanceMode());
      return rf.getRange();
   }
   float[] getBeaconReadings()
   {

         float[] data = new float[beaconMode.sampleSize()];
         beaconMode.fetchSample(data, 0);
         return data;
   }
}


Aswin
leJOS Team Member
Posts: 310
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands
Contact:

Re: beacon

Postby Aswin » Mon May 08, 2017 6:53 am

Hi,

You use a rangefinderAdapter, frontview, directly on the IR sensor. You should initialize it using the distanceMode.
This is because the IR sensor support multiple modes, it will be in the mode that was previously set explicitly, using setMode or implicitly using one of its inner classes like the beaconMode or distanceMode. So frontier will sometimes read IR commands and sometimes distances.

Some tips
- you best not use the sensor directly, always use one of its SampleProviders.
- a SampleProvider only needs to be obtained once, you can reuse it during the life spann of the program.
- the same is true for the buffers you use to get a reading from the SampleProvider (fetchSample, getCommand).
- using the rangeFinderAdapter is not advised, we might get rid of it. You can use the FilterTerminal instead if you want to avoid using a buffer to get a sensor reading.
My NXT blog: http://nxttime.wordpress.com/

moo
New User
Posts: 23
Joined: Thu Apr 06, 2017 2:43 am

Re: beacon

Postby moo » Mon May 08, 2017 11:42 am

1. Can I have reference to multiple sensor modes (distance, beacon, remote commands) and ask for values?
2. What am I doing wrong in terms of reading beacon heading ?

Aswin
leJOS Team Member
Posts: 310
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands
Contact:

Re: beacon

Postby Aswin » Mon May 08, 2017 12:14 pm

1. Yes you can.
2. First try to clean up your code in respect to using SampleProviders. If the problem does not go away you can try to add a delay between using different SampleProviders and report back to us.
My NXT blog: http://nxttime.wordpress.com/

moo
New User
Posts: 23
Joined: Thu Apr 06, 2017 2:43 am

Re: beacon

Postby moo » Mon May 08, 2017 4:59 pm

Understood. I am checking 3 for IR sensor and another 3 for color sensor in a loop without any delay. Let me start with the simplest case (it beacon only) and try to figure out where it is breaking. Will report back next weekend
Last edited by moo on Mon May 08, 2017 5:00 pm, edited 1 time in total.


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest