1 @use PhysicalControl.
\r
7 @use SistemaAutonomo.
\r
9 @define CELDAS_MAX_VELOCITY 30.
\r
11 PhysicalControl : CeldasControl {
\r
12 % This class is used for building simple vehicle
\r
13 % simulations. To create a vehicle simulation,
\r
14 % subclass CeldasControl and use the init method to
\r
15 % create OBJECT(CeldasObstacle) and
\r
16 % OBJECT(CeldasVehicle) objects.
\r
20 floorShape (object).
\r
21 cloudTexture (object).
\r
25 self enable-lighting.
\r
26 #self enable-smooth-drawing.
\r
28 floorShape = new Shape.
\r
29 floorShape init-with-cube size (200, .2, 200).
\r
31 floor = new Stationary.
\r
32 floor register with-shape floorShape at-location (0, 0, 0).
\r
33 #floor catch-shadows.
\r
35 self point-camera at (0, 0, 0) from (3, 3, 24).
\r
37 #self enable-shadows.
\r
38 #self enable-reflections.
\r
40 cloudTexture = (new Image load from "images/clouds.png").
\r
41 self set-background-color to (.4, .6, .9).
\r
42 self set-background-texture-image to cloudTexture.
\r
46 MultiBody : CeldasLightVehicle (aka CeldasLightVehicles) {
\r
47 % This object is used in conjunction with OBJECT(CeldasControl) to
\r
48 % create simple vehicles.
\r
52 wheelShape (object).
\r
53 sensorShape (object).
\r
60 bodyShape = new Shape.
\r
61 bodyShape init-with-cube size (4.0, .75, 3.0).
\r
63 wheelShape = new Shape.
\r
64 wheelShape init-with-polygon-disk radius ( self get-wheel-radius ) sides 20 height ( self get-wheel-width ).
\r
67 sensorShape = new Shape.
\r
68 sensorShape init-with-polygon-cone radius .2 sides 5 height .5.
\r
71 bodyShape set-density to ( self get-density ).
\r
72 bodyLink = new Link.
\r
73 bodyLink set-shape to bodyShape.
\r
74 bodyLink set-mu to -1.0.
\r
75 bodyLink set-eT to .8.
\r
77 self set-root to bodyLink.
\r
79 self move to (0, 0.9, 0).
\r
80 self set-texture-scale to 1.5.
\r
85 - to get-wheel-width:
\r
88 - to get-wheel-radius:
\r
91 + section "Adding Wheels and Sensors to a Vehicle"
\r
93 + to add-wheel at location (vector):
\r
94 % Adds a wheel at location on the vehicle. This method returns
\r
95 % the wheel which is created, a OBJECT(CeldasWheel).
\r
97 wheel, joint (object).
\r
99 wheel = new CeldasWheel.
\r
100 wheel set-shape to wheelShape.
\r
102 joint = new RevoluteJoint.
\r
104 joint set-relative-rotation around-axis (1, 0, 0) by 1.5708.
\r
105 joint link parent bodyLink to-child wheel with-normal (0, 0, 1)
\r
106 with-parent-point location with-child-point (0, 0, 0).
\r
108 wheel set-eT to .8.
\r
109 wheel set-texture to 0.
\r
110 wheel set-joint to joint.
\r
111 joint set-strength-limit to (joint get-strength-hard-limit) / 2.
\r
112 wheel set-color to (.6, .6, .6).
\r
113 wheel set-mu to 100000.
\r
115 self add-dependency on joint.
\r
116 self add-dependency on wheel.
\r
118 push wheel onto wheels.
\r
122 + to add-sensor at location (vector) with-direction direction = (0,1,0)(vector) :
\r
123 % Adds a sensor at location on the vehicle. This method returns
\r
124 % the sensor which is created, a OBJECT(CeldasSensor).
\r
126 sensor, joint (object).
\r
128 sensor = new CeldasSensor.
\r
129 sensor set-direction to direction.
\r
131 sensor set-shape to sensorShape.
\r
133 joint = new RevoluteJoint.
\r
135 joint set-relative-rotation around-axis (0, 0, 1) by -1.57.
\r
136 joint link parent bodyLink to-child sensor with-normal (1, 0, 0)
\r
137 with-parent-point location with-child-point (0, 0, 0).
\r
139 joint set-double-spring with-strength 300 with-max 0.01 with-min -0.01.
\r
141 self add-dependency on joint.
\r
142 self add-dependency on sensor.
\r
144 sensor set-color to (0, 0, 0).
\r
146 #push sensor onto sensors.
\r
158 CeldasLightVehicle : CeldasVehicle (aka CeldasVehicles) {
\r
159 % A heavy duty version of OBJECT(CeldasLightVehicle), this
\r
160 % vehicle is heavier and harder to control, but more stable
\r
161 % at higher speeds.
\r
163 lSensor, rSensor, fSensor, bSensor (object).
\r
164 lfWheel,rfWheel,lbWheel,rbWheel (object).
\r
165 tleft,tright (int).
\r
171 datos-finales (hash).
\r
172 avanzando,retrocediendo,girando(int).
\r
177 - to get-wheel-width:
\r
180 - to get-wheel-radius:
\r
183 + to set-global-velocity to velocity (float):
\r
184 rfWheel set-velocity to velocity.
\r
185 lfWheel set-velocity to velocity.
\r
186 rbWheel set-velocity to velocity.
\r
187 lbWheel set-velocity to velocity.
\r
189 + to get-global-velocity:
\r
190 return ((rfWheel get-velocity) + (lfWheel get-velocity)) / 2.
\r
195 self rotate around-axis (0,1,0) by (-1.5709/10)*tright.
\r
197 if(tright==10): tright=0.
\r
203 self rotate around-axis (0,1,0) by (1.5709/10)*tleft.
\r
205 if(tleft==10): tleft=0.
\r
208 + to get-sensor-value:
\r
209 return (fSensor get-sensor-value).
\r
211 +to update-entorno:
\r
212 entorno{"sensor_f"} = (fSensor get-data).
\r
213 entorno{"sensor_b"} = (bSensor get-data).
\r
214 entorno{"sensor_r"} = (rSensor get-data).
\r
215 entorno{"sensor_l"} = (lSensor get-data).
\r
216 entorno{"movido"} = 0. # TODO
\r
217 sa update-entorno entorno entorno.
\r
220 sa = new SistemaAutonomo.
\r
223 teorias = 2 new Teorias.
\r
224 teorias{0} init named "Avanzar" with-action "avanzar".
\r
225 teorias{0} set-dato-inicial name "sensor_f" value 0.
\r
226 teorias{0} set-dato-inicial name "sensor_b" value ANY.
\r
227 teorias{0} set-dato-inicial name "sensor_r" value ANY.
\r
228 teorias{0} set-dato-inicial name "sensor_l" value ANY.
\r
229 teorias{0} set-dato-inicial name "movido" value ANY.
\r
230 teorias{0} set-dato-final name "sensor_f" value ANY.
\r
231 teorias{0} set-dato-final name "sensor_b" value ANY.
\r
232 teorias{0} set-dato-final name "sensor_r" value ANY.
\r
233 teorias{0} set-dato-final name "sensor_l" value ANY.
\r
234 teorias{0} set-dato-final name "movido" value 1.
\r
236 teorias{1} init named "Rotar a derecha" with-action "derecha".
\r
237 teorias{1} set-dato-inicial name "sensor_f" value 1.
\r
238 teorias{1} set-dato-inicial name "sensor_b" value ANY.
\r
239 teorias{1} set-dato-inicial name "sensor_r" value ANY.
\r
240 teorias{1} set-dato-inicial name "sensor_l" value ANY.
\r
241 teorias{1} set-dato-inicial name "movido" value ANY.
\r
242 teorias{1} set-dato-final name "sensor_f" value 0.
\r
243 teorias{1} set-dato-final name "sensor_b" value ANY.
\r
244 teorias{1} set-dato-final name "sensor_r" value ANY.
\r
245 teorias{1} set-dato-final name "sensor_l" value ANY.
\r
246 teorias{1} set-dato-final name "movido" value 0.
\r
248 sa add teoria teorias{0}.
\r
249 sa add teoria teorias{1}.
\r
251 fSensor = (self add-sensor at (2.0, .4, 0)).
\r
252 fSensor set-direction to (1,0,0).
\r
253 #fSensor set-direction to (0,0,1).
\r
254 fSensor set-id at 1.
\r
255 fSensor set-body at self.
\r
256 bSensor = (self add-sensor at (-2.0, .4, 0)).
\r
257 bSensor set-direction to (-1,0,0).
\r
258 #bSensor set-direction to (0,0,1).
\r
259 bSensor set-id at 2.
\r
260 bSensor set-body at self.
\r
261 lSensor = (self add-sensor at (0, .4, 1.5)).
\r
262 lSensor set-direction to (0,0,1).
\r
263 #lSensor set-direction to (1,0,0).
\r
264 lSensor set-id at 3.
\r
265 lSensor set-body at self.
\r
268 rSensor = (self add-sensor at (0, .4, -1.5)).
\r
269 rSensor set-direction to (0,0,-1).
\r
270 #rSensor set-direction to (-1,0,0).
\r
271 rSensor set-id at 4.
\r
272 rSensor set-body at self.
\r
275 lfWheel = (self add-wheel at (2, 0, -1.5)).
\r
276 lbWheel = (self add-wheel at (-2, 0, -1.5)).
\r
277 rfWheel = (self add-wheel at (2, 0, 1.5)).
\r
278 rbWheel = (self add-wheel at (-2, 0, 1.5)).
\r
280 tleft=tright=0. #Debe ser inicializado en 0 esta asi para probar!!!!!!!!!!!!!!!!!!!!!!!!
\r
286 valuef,valueb,valuer,valuel (float).
\r
289 valuef=fSensor get-data.
\r
290 valueb=bSensor get-data.
\r
291 valuel=lSensor get-data.
\r
292 valuer=rSensor get-data.
\r
295 datos-finales{"movido"} = 1.
\r
296 print "senforl: $valuel".
\r
299 sa update-datos-finales datos-finales datos-finales.
\r
303 print "iteracion 0".
\r
304 if (sa has-next-theory):
\r
307 print "hay teoria".
\r
308 teoria = sa get-next-theory.
\r
309 if ((teoria get-accion) == "avanza"): {
\r
310 #entorno{"sensor"} = 1.
\r
311 #entorno{"movido"} = 1.
\r
312 self set-global-velocity.
\r
315 if ((teoria get-accion) == "retrocede"): {
\r
316 self set-global-velocity to -5.
\r
322 if ((iterate==100) && (teoria)):
\r
325 self update-entorno.
\r
326 if (sa validate theory teoria): {
\r
330 print "Teoria no valida, salimos".
\r
340 Stationary : CeldasObstacle (aka CeldasObstacles) {
\r
341 % A CeldasObstacle is used in conjunction with OBJECT(CeldasControl)
\r
342 % and OBJECT(CeldasVehicle). It is what the OBJECT(CeldasSensor)
\r
343 % objects on the CeldasVehicle detect.
\r
345 % There are no special behaviors associated with the walls--they're
\r
346 % basically just plain OBJECT(Stationary) objects.
\r
350 direction (vector).
\r
353 + to init with-size theSize = (10, 3, .1) (vector) with-color theColor = (1, 0, 0) (vector) at-location theLocation = (0, 0, 0) (vector) with-rotation theRotation = [ ( 0, 0, 1 ), ( 0, 1, 0 ), ( 1, 0, 0 ) ] (matrix):
\r
354 self init-with-shape shape (new Shape init-with-cube size theSize) color theColor at-location theLocation with-rotation theRotation.
\r
357 + to init-with-shape shape theShape (object) color theColor = (1, 0, 0) (vector) at-location theLocation = (0, 0, 0) (vector) with-rotation theRotation = [ ( 1, 0, 0 ), ( 0, 1, 0 ), ( 0, 0, 1 ) ] (matrix):
\r
358 self register with-shape theShape at-location theLocation with-rotation theRotation.
\r
359 self set-color to theColor.
\r
364 + to set-direction at theDirection (vector):
\r
365 direction=theDirection.
\r
367 + to get-direction:
\r
371 Link : CeldasWheel (aka CeldasWheels) {
\r
372 % A CeldasWheel is used in conjunction with OBJECT(CeldasVehicle)
\r
373 % to build Celdas vehicles. This class is typically not instantiated
\r
374 % manually, since OBJECT(CeldasVehicle) creates one for you when you
\r
375 % add a wheel to the vehicle.
\r
384 - to set-joint to j (object):
\r
389 + section "Configuring the Wheel's Velocity"
\r
391 + to set-velocity to n (float):
\r
392 % Sets the velocity of this wheel.
\r
394 if n > CELDAS_MAX_VELOCITY: n = CELDAS_MAX_VELOCITY.
\r
397 joint set-joint-velocity to velocity.
\r
400 % Gets the velocity of this wheel.
\r
406 Link : CeldasSensor (aka CeldasSensors) {
\r
407 % A CeldasSensor is used in conjunction with OBJECT(CeldasVehicle)
\r
408 % to build Celdas vehicles. This class is typically not instantiated
\r
409 % manually, since OBJECT(CeldasVehicle) creates one for you when you
\r
410 % add a sensor to the vehicle.
\r
413 direction (vector).
\r
414 positiveDirection(vector).
\r
415 sensorAngle (float).
\r
422 direction = (1,0,1).
\r
423 positiveDirection= (1,0,1).
\r
426 draw = new Drawing.
\r
429 + section "Configuring the Sensor Values"
\r
430 + to set-id at n (int):
\r
433 + to set-body at robotBody(object):
\r
436 + to set-sensor-angle to n (float):
\r
437 % Sets the angle in which this sensor can detect obstacles. The default
\r
438 % value of 1.6 means that the sensor can see most of everything in
\r
439 % front of it. Setting the value to be any higher leads to general
\r
440 % wackiness, so I don't suggest it.
\r
444 + to set-direction to n (vector):
\r
446 positiveDirection::x=|n::x|.
\r
447 positiveDirection::y=|n::y|.
\r
448 positiveDirection::z=|n::z|.
\r
450 + section "Getting the Sensor Values"
\r
452 + to get-sensor-value:
\r
453 % Gets the sensor value. This should be used from post-iterate,
\r
454 % if not, the sensor reading correspond to the previous
\r
466 wallBegin,wallEnd,wallCenter (float).
\r
468 toObstacle(vector).
\r
473 posObstacle,destiny,yo(vector).
\r
479 foreach i in (all CeldasObstacles):
\r
481 posObstacle=i get-location.
\r
482 v = (body get-location) - (self get-location ).
\r
483 obsLoc::y=posObstacle::y.
\r
485 if (dot((i get-direction),(1,0,0))):
\r
487 obsLoc::x=((self get-location)::x + ((posObstacle::z - (self get-location)::z)*v::x/v::z)).
\r
488 obsLoc::z=posObstacle::z.
\r
492 obsLoc::z=((self get-location)::z + ((posObstacle::x - (self get-location)::x)*v::z/v::x)).
\r
493 obsLoc::x=posObstacle::x.
\r
497 if(dot((i get-direction),direction)==0):
\r
504 if(dot(direction,(1,1,1))<0):
\r
506 if((dot((self get-location),positiveDirection))>(dot(obsLoc,positiveDirection))):
\r
511 if((dot((self get-location),positiveDirection))<(dot(obsLoc,positiveDirection))):
\r
516 #Compruebo que el robot este frente a la pared
\r
517 wallCenter=dot((i get-location),(i get-direction)).
\r
518 wallBegin=wallCenter- (i get-large)/2.
\r
519 wallEnd=wallCenter + (i get-large)/2.
\r
522 yo=self get-location.
\r
523 destiny=i get-direction.
\r
527 if (dot((self get-location),(i get-direction)) > wallBegin) && (dot((self get-location),(i get-direction)) < wallEnd):
\r
535 if ((des2) && (des3)):
\r
539 dist=|obsLoc - (self get-location)|.
\r
540 if( (j==0) || (min>dist) ):
\r
545 #print "sensor: $id obstaculo: $posObstacle direP: $destiny direS: $direction yo: $yo ".
\r
556 draw set-color to (1, 0, 0).
\r
557 draw draw-line from (self get-location) to (obs).
\r