View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.samples;
2   
3   import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
4   import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
5   import cz.cuni.amis.pogamut.usar2004.agent.*;
6   import cz.cuni.amis.pogamut.usar2004.agent.module.logic.USAR2004BotLogicController;
7   import cz.cuni.amis.pogamut.usar2004.agent.module.nfo.NfoStartPoses;
8   import cz.cuni.amis.pogamut.usar2004.agent.module.sensor.SensorLaser;
9   import cz.cuni.amis.pogamut.usar2004.communication.messages.usarcommands.DriveSkid;
10  import cz.cuni.amis.pogamut.usar2004.communication.messages.usarcommands.Initialize;
11  import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.NfoMessage;
12  import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.SensorMessage;
13  import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.StateMessage;
14  import cz.cuni.amis.pogamut.usar2004.utils.*;
15  import java.util.ArrayList;
16  import java.util.logging.Level;
17  
18  /**
19   *
20   * P2DX robot not using logic controller.
21   *
22   * @author vejmanm
23   */
24  @AgentScoped
25  public class P2DX extends USAR2004BotController
26  {
27      /**
28       * Method triggered after Game is initialized and STARTPOSES obtained. Tt
29       * will spawn the robot into the environment.
30       *
31       * @param nfom NfoMessge containing STARTPOSES
32       */
33      @Override
34      public void robotInitialized(NfoMessage nfom)
35      {
36          getAct().act(new Initialize("USARBot.P2DX", "P2DX sample robot", nfom.getStartPoses().get(0).getName()));
37      }
38  
39      @Override
40      public void initializeController(USAR2004Bot bot)
41      {
42          super.initializeController(bot);
43  
44          //startPoses = new NfoStartPoses(bot);
45          //begin = new NfoBeginMapInfo(bot);
46      }
47  
48      /**
49       * Initialization of modules used within this robot.
50       *
51       * @param bot Necessary parameter for hooking listeners and for sending
52       * commands
53       */
54      @Override
55      public void prepareBot(USAR2004Bot bot)
56      {
57          super.prepareBot(bot);
58  
59          getWorldView().addEventListener(StateMessage.class, statList);
60  
61          //laserSensor = new SensorLaser(bot);
62          log = new USAR2004BotLogicController();
63          getWorldView().addEventListener(SensorMessage.class, sensorList);
64          getWorldView().addEventListener(NfoMessage.class, nfoList);
65  
66          //Map<Class, Map<WorldObjectId, IViewable>> allVisible = getWorldView().getAllVisible();
67      }
68      //private NfoBeginMapInfo begin;
69      private NfoStartPoses startPoses;
70      private SensorLaser laserSensor;
71      private USAR2004BotLogicController log;
72  
73      private void logic()
74      {
75  //        if(begin.isReady())
76  //            System.out.println(begin.getLevel());
77      }
78      IWorldEventListener<StateMessage> statList = new IWorldEventListener<StateMessage>()
79      {
80          @Override
81          public void notify(StateMessage t)
82          {
83              logic();
84              getAct().act(new DriveSkid(left, right, false, false, false));
85          }
86      };
87      IWorldEventListener<NfoMessage> nfoList = new IWorldEventListener<NfoMessage>()
88      {
89          @Override
90          public void notify(NfoMessage t)
91          {
92              System.out.println("INFO INFO FOR STARTERS" + t.toString());
93              if(!first)
94              {
95                  //INIT {ClassName USARBot.P2DX} {Location 4.5,1.9,1.8}
96                  //getAct().act(new Initialize("USARBot.P2DX", "Blimp", new Location(4.5,1.9,1.8), null));
97  
98                  //getAct().act(new Initialize("USARBot.AirRobot", "Item" ,new Location(0.6903,4.7984, 7.8320),null));
99                  first = true;
100             }//new Location(-4.2861,40.1280,4.6800),null));//0.76,2.3,1.8),null));
101         }
102     };
103     IWorldEventListener<SensorMessage> sensorList = new IWorldEventListener<SensorMessage>()
104     {
105         @Override
106         public void notify(SensorMessage t)
107         {
108             if(t.getLaserRanges().size() > 1)
109             {
110                 ArrayList<Double> f = new ArrayList<Double>();
111                 f.addAll(t.getLaserRanges());
112                 RangesReady(f);
113             }
114         }
115     };
116     boolean first = false;
117     int left = 0, right = 0;
118 
119     /**
120      * Sets higher speed for right motor and lower speed for left motor.
121      */
122     private void driveLeft()
123     {
124         System.out.println("left");
125         right = 15;
126         left = 5;
127     }
128 
129     /**
130      * Sets higher speed for left motor and lower speed for right motor.
131      */
132     private void driveRight()
133     {
134         System.out.println("right");
135         right = 4;
136         left = 15;
137     }
138 
139     /**
140      * Sets equal speeds for both side motors.
141      */
142     private void driveStraight()
143     {
144         System.out.println("straight");
145         right = 15;
146         left = 15;
147     }
148 
149     /**
150      * For ten cycles it reverses slowly backwards and then for ten cycles it
151      * turns around.
152      */
153     private void driveBackwards()
154     {
155         System.out.println("back");
156         if(step > 10)
157         {
158             right = -3;
159             left = -3;
160         }
161         else
162         {
163             right = -3;
164             left = 3;
165         }
166         step--;
167         if(step == 0)
168         {
169             step = 20;
170             reversActive = false;
171         }
172 
173     }
174     private int step = 20;
175     boolean reversActive = false;
176 
177     public void RangesReady(ArrayList<Double> Ranges)
178     {
179 
180         int third = Ranges.size() / 3;
181         double mleft = 0, mright = 0, mstraight = 0;
182         for(int i = 0; i < Ranges.size(); i++)
183         {
184             if(i < third)
185             {
186                 mright += Ranges.get(i);
187             }
188             else if(i < 2 * third)
189             {
190                 mstraight += Ranges.get(i);
191             }
192             else
193             {
194                 mleft += Ranges.get(i);
195             }
196         }
197         if(reversActive || mleft + mstraight < 200 || mright + mstraight < 200)
198         {
199             reversActive = true;
200             driveBackwards();
201         }
202         else
203         {
204             if(Math.max(mleft, mright) == mleft)
205             {
206                 if(Math.max(mleft, mstraight) == mleft)
207                 {
208                     driveLeft();
209                 }
210                 else
211                 {
212                     driveStraight();
213                 }
214             }
215             else
216             {
217 
218                 if(Math.max(mright, mstraight) == mright)
219                 {
220                     driveRight();
221                 }
222                 else
223                 {
224                     driveStraight();
225                 }
226             }
227         }
228     }
229 
230     public static void main(String[] args)
231     {
232         new USAR2004BotRunner(P2DX.class, "PogamutBotBot", "127.0.0.1", 3000).setMain(true).setLogLevel(Level.WARNING).startAgent();
233     }
234 }