Don't like ads? PRO users don't see any ads ;-)
Guest

NLT 7B

By: a guest on Jun 14th, 2012  |  syntax: JavaScript  |  size: 2.34 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package javaBot.Nano.Rescue;
  2.  
  3. import com.muvium.apt.PeriodicTimer;
  4.  
  5. /**
  6.  * Opdracht - 6 Eenvoudige Line Follower met enkele sensor
  7.  *
  8.  * De lijnvolger gebruikt de reflectiesensoren (fieldsensoren)
  9.  * om de kleur van het veld onder de robot te bepalen.
  10.  * Hij volgt de zwarte lijn, eerst met een enkele sensor.
  11.  * In opdracht 7 ga je de lijn met twee sensoren volgen.
  12.  * Daarnaast ga je ook een subroutine gebruiken.
  13.  * Als laatste ga je ook de gele lijn volgen.
  14.  */
  15.  
  16. public class LineFollowerBehavior08 extends Behavior
  17. {
  18.         private BaseController  joBot;
  19.         private int     state   = 0;
  20.         private int speed       = 50;
  21.         private int fl      = 0;
  22.         private int blkLs       = 400;   // Value of black on your field
  23.         private int yelLs       = 700;
  24.         private int fr;
  25.         private int ds;
  26.         private int count       = 0;
  27.  
  28.         public LineFollowerBehavior08(BaseController initJoBot, PeriodicTimer
  29.                         initServiceTick,int servicePeriod)
  30.         {
  31.                 super(initJoBot, initServiceTick, servicePeriod);
  32.                 joBot = initJoBot;
  33.         }
  34.  
  35.         // Maak een eenvoudige lijnvolger met een enkele sensor
  36.         // Opdracht 6
  37.        
  38.         public void doBehavior() {
  39.                 if (state == 0) {
  40.                         System.out.println("Enkelvoudige Line Follower");
  41.                         joBot.setStatusLeds(false, false, false);
  42.                         joBot.drive(speed, speed);      // Rijd rechtuit
  43.                         joBot.setFieldLeds(true);       // Zet field leds aan
  44.                         state = 1;
  45.                 }
  46.  
  47.                 if (state == 1) {      
  48.                         fl = joBot.getSensorValue(BaseController.SENSOR_FL); // Left sensor
  49.                         fr = joBot.getSensorValue(BaseController.SENSOR_FR); // Right sensor
  50.                         ds = joBot.getSensorValue(BaseController.SENSOR_DS);
  51.                        
  52.                         if (fl >= blkLs) {
  53.                                 joBot.drive(0, 50);     // Go left
  54.                                 joBot.setLed(BaseController.LED_YELLOW, true);
  55.                         }
  56.  
  57.                         if (fl < blkLs) {
  58.                                 joBot.drive(0, 50);
  59.                                 joBot.setLed(BaseController.LED_YELLOW, false);
  60.                                
  61.                         }
  62.                         if (fr < blkLs) {
  63.                                 joBot.drive(50, 0);
  64.                         }
  65.                 }      
  66.        
  67.         if (fl >= yelLs) {
  68.                 joBot.drive(0, 50);     // Go left
  69.                 joBot.setLed(BaseController.LED_YELLOW, true);
  70.         }
  71.  
  72.         if (fl > yelLs) {
  73.                 joBot.drive(0, 50);
  74.                 joBot.setLed(BaseController.LED_YELLOW, false);
  75.                
  76.         }
  77.         if (fr > yelLs) {
  78.                 joBot.drive(50, 0);
  79.         }
  80.         if (ds > 400){
  81.                 state = 2;
  82.         }
  83.         if (state == 2){
  84.                 joBot.drive(50, 0);
  85.                 joBot.printLCD("state=2");
  86.                 joBot.setStatusLeds(true, true, true);
  87.                 if (count++ >=10){
  88.                         joBot.drive(0, 50);
  89.                         if (count >= 100);
  90.                         state = 1;
  91.                 }
  92.                                
  93.                        
  94.                 }
  95.                
  96.         }
  97.         }