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.communication.messages.usarcommands.DriveSkid;
8   import cz.cuni.amis.pogamut.usar2004.communication.messages.usarcommands.Initialize;
9   import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.NfoMessage;
10  import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.SensorMessage;
11  import cz.cuni.amis.pogamut.usar2004.utils.*;
12  import java.util.ArrayList;
13  import java.util.logging.Level;
14  
15  /**
16   *
17   * P2DX robot utilizing logic controller. It drives to the most open direction
18   * and reverses if an obstacle is too close.
19   *
20   * @author vejmanm
21   */
22  @AgentScoped
23  public class P2DXLogicController extends USAR2004BotLogicController
24  {
25      /**
26       * Method triggered after Game is initialized and STARTPOSES obtained. Tt
27       * will spawn the robot into the environment.
28       *
29       * @param nfom NfoMessge containing STARTPOSES
30       */
31      @Override
32      public void robotInitialized(NfoMessage nfom)
33      {
34          getAct().act(new Initialize("USARBot.P2DX", "P2DX - logic controller", nfom.getStartPoses().get(0).getName()));
35      }
36  
37      @Override
38      public void initializeController(USAR2004Bot bot)
39      {
40          super.initializeController(bot);
41      }
42  
43      /**
44       * Initialization of modules used within this robot.
45       *
46       * @param bot Necessary parameter for hooking listeners and for sending
47       * commands
48       */
49      @Override
50      public void prepareBot(USAR2004Bot bot)
51      {
52          super.prepareBot(bot);
53  
54          //laserSensor = new SensorLaser(bot);
55          log = new USAR2004BotLogicController();
56          getWorldView().addEventListener(SensorMessage.class, sensorList);
57  
58          //Map<Class, Map<WorldObjectId, IViewable>> allVisible = getWorldView().getAllVisible();
59      }
60      private USAR2004BotLogicController log;
61  
62      /**
63       * this method is triggered by receipt of STA message. This is where all
64       * behaviour is controlled
65       */
66      @Override
67      public void logic()
68      {
69          System.out.println("P2DXLogicController.logic()");
70          getAct().act(new DriveSkid(left, right, false, false, false));
71      }
72      IWorldEventListener<SensorMessage> sensorList = new IWorldEventListener<SensorMessage>()
73      {
74          @Override
75          public void notify(SensorMessage t)
76          {
77              if(t.getLaserRanges().size() > 1)
78              {
79                  ArrayList<Double> f = new ArrayList<Double>();
80                  f.addAll(t.getLaserRanges());
81                  RangesReady(f);
82              }
83          }
84      };
85      int left = 0, right = 0;
86  
87      /**
88       * Sets higher speed for right motor and lower speed for left motor.
89       */
90      private void driveLeft()
91      {
92          right = 15;
93          left = 5;
94      }
95  
96      /**
97       * Sets higher speed for left motor and lower speed for right motor.
98       */
99      private void driveRight()
100     {
101         right = 4;
102         left = 15;
103     }
104 
105     /**
106      * Sets equal speeds for both side motors.
107      */
108     private void driveStraight()
109     {
110         right = 15;
111         left = 15;
112     }
113 
114     /**
115      * For ten cycles it reverses slowly backwards and then for ten cycles it
116      * turns around.
117      */
118     private void driveBackwards()
119     {
120         if(step > 10)
121         {
122             right = -3;
123             left = -3;
124         }
125         else
126         {
127             right = -3;
128             left = 3;
129         }
130         step--;
131         if(step == 0)
132         {
133             step = 20;
134             reversActive = false;
135         }
136 
137     }
138     private int step = 20;
139     boolean reversActive = false;
140 
141     /**
142      * Devides ranges into thirds and decides which way to set motor speeds.
143      *
144      * @param Ranges list of ranges from Range scanner.
145      */
146     public void RangesReady(ArrayList<Double> Ranges)
147     {
148 
149         int third = Ranges.size() / 3;
150         double mleft = 0, mright = 0, mstraight = 0;
151         for(int i = 0; i < Ranges.size(); i++)
152         {
153             if(i < third)
154             {
155                 mright += Ranges.get(i);
156             }
157             else if(i < 2 * third)
158             {
159                 mstraight += Ranges.get(i);
160             }
161             else
162             {
163                 mleft += Ranges.get(i);
164             }
165         }
166         if(reversActive || mleft + mstraight < 200 || mright + mstraight < 200)
167         {
168             reversActive = true;
169             driveBackwards();
170         }
171         else
172         {
173             if(Math.max(mleft, mright) == mleft)
174             {
175                 if(Math.max(mleft, mstraight) == mleft)
176                 {
177                     driveLeft();
178                 }
179                 else
180                 {
181                     driveStraight();
182                 }
183             }
184             else
185             {
186 
187                 if(Math.max(mright, mstraight) == mright)
188                 {
189                     driveRight();
190                 }
191                 else
192                 {
193                     driveStraight();
194                 }
195             }
196         }
197     }
198 
199     public static void main(String[] args)
200     {
201         new USAR2004BotRunner(P2DXLogicController.class, "PogamutBotBot", "127.0.0.1", 3000).setMain(true).setLogLevel(Level.WARNING).startAgent();
202     }
203 }