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 lWheel,rWheel (object).
\r
165 tleft,tright (int).
\r
172 datos-finales (hash).
\r
179 - to get-wheel-width:
\r
182 - to get-wheel-radius:
\r
185 + to set-global-velocity to velocity=5 (float):
\r
186 rWheel set-velocity to velocity.
\r
187 lWheel set-velocity to velocity.
\r
189 + to get-global-velocity:
\r
190 return ((rWheel get-velocity) + (lWheel 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
212 sa = new SistemaAutonomo.
\r
215 fSensor = (self add-sensor at (2.0, .4, 0)).
\r
216 fSensor set-direction to (1,0,0).
\r
217 #fSensor set-direction to (0,0,1).
\r
218 fSensor set-id at 1.
\r
219 fSensor set-body at self.
\r
220 bSensor = (self add-sensor at (-2.0, .4, 0)).
\r
221 bSensor set-direction to (-1,0,0).
\r
222 #bSensor set-direction to (0,0,1).
\r
223 bSensor set-id at 2.
\r
224 bSensor set-body at self.
\r
225 lSensor = (self add-sensor at (0, .4, 1.5)).
\r
226 lSensor set-direction to (0,0,1).
\r
227 #lSensor set-direction to (1,0,0).
\r
228 lSensor set-id at 3.
\r
229 lSensor set-body at self.
\r
232 rSensor = (self add-sensor at (0, .4, -1.5)).
\r
233 rSensor set-direction to (0,0,-1).
\r
234 #rSensor set-direction to (-1,0,0).
\r
235 rSensor set-id at 4.
\r
236 rSensor set-body at self.
\r
238 lWheel = (self add-wheel at (0, 0, -1.5)).
\r
239 rWheel = (self add-wheel at (0, 0, 1.5)).
\r
241 tleft=tright=0. #Debe ser inicializado en 0 esta asi para probar!!!!!!!!!!!!!!!!!!!!!!!!
\r
243 entorno{"sensor_f"} = 0.
\r
244 entorno{"sensor_b"} = 0.
\r
245 entorno{"sensor_r"} = 0.
\r
246 entorno{"sensor_l"} = 0.
\r
248 entorno{"movido"} = 0.
\r
249 sa update-entorno entorno entorno.
\r
252 teorias = 2 new Teorias.
\r
253 teorias{0} init named "avanzar" with-action "avanza".
\r
254 (teorias{0}) set-dato-inicial name "sensor_f" value ANY.
\r
255 (teorias{0}) set-dato-inicial name "sensor_b" value ANY.
\r
256 (teorias{0}) set-dato-inicial name "sensor_r" value ANY.
\r
257 (teorias{0}) set-dato-inicial name "sensor_l" value ANY.
\r
258 (teorias{0}) set-dato-inicial name "movido" value ANY.
\r
259 (teorias{0}) set-dato-final name "sensor_f" value ANY.
\r
260 (teorias{0}) set-dato-final name "sensor_b" value ANY.
\r
261 (teorias{0}) set-dato-final name "sensor_r" value ANY.
\r
262 (teorias{0}) set-dato-final name "sensor_l" value ANY.
\r
263 (teorias{0}) set-dato-final name "movido" value ANY.
\r
265 teorias{1} init named "avanzar2" with-action "avanza".
\r
266 (teorias{1}) set-dato-inicial name "sensor_f" value ANY.
\r
267 (teorias{1}) set-dato-inicial name "sensor_b" value ANY.
\r
268 (teorias{1}) set-dato-inicial name "sensor_r" value ANY.
\r
269 (teorias{1}) set-dato-inicial name "sensor_l" value ANY.
\r
270 (teorias{1}) set-dato-inicial name "movido" value ANY.
\r
271 (teorias{1}) set-dato-final name "sensor_f" value ANY.
\r
272 (teorias{1}) set-dato-final name "sensor_b" value ANY.
\r
273 (teorias{1}) set-dato-final name "sensor_r" value ANY.
\r
274 (teorias{1}) set-dato-final name "sensor_l" value ANY.
\r
275 (teorias{1}) set-dato-final name "movido" value ANY.
\r
278 sa add teoria (teorias{0}).
\r
279 sa add teoria (teorias{1}).
\r
281 datos-finales{"movido"} = 1.
\r
283 sa update-datos-finales datos-finales datos-finales.
\r
288 #+ to post-iterate:
\r
289 valuef,valueb,valuer,valuel (float).
\r
295 print "iteracion 0".
\r
296 if (sa has-next-theory):
\r
299 print "hay teoria".
\r
300 teoria = sa get-next-theory.
\r
301 if ((teoria get-accion) == "avanza"): {
\r
302 #entorno{"sensor"} = 1.
\r
303 #entorno{"movido"} = 1.
\r
304 self set-global-velocity.
\r
307 if ((teoria get-accion) == "retrocede"): {
\r
308 self set-global-velocity to -5.
\r
314 if (iterate==100) && (teoria):
\r
317 entorno{"sensor_f"} = (fSensor get-data).
\r
318 entorno{"sensor_b"} = (bSensor get-data).
\r
319 entorno{"sensor_r"} = (rSensor get-data).
\r
320 entorno{"sensor_l"} = (lSensor get-data).
\r
322 sa update-entorno entorno entorno.
\r
323 if (sa validate theory teoria): {
\r
327 print "Teoria no valida, salimos".
\r
338 Stationary : CeldasObstacle (aka CeldasObstacles) {
\r
339 % A CeldasObstacle is used in conjunction with OBJECT(CeldasControl)
\r
340 % and OBJECT(CeldasVehicle). It is what the OBJECT(CeldasSensor)
\r
341 % objects on the CeldasVehicle detect.
\r
343 % There are no special behaviors associated with the walls--they're
\r
344 % basically just plain OBJECT(Stationary) objects.
\r
348 direction (vector).
\r
351 + 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
352 self init-with-shape shape (new Shape init-with-cube size theSize) color theColor at-location theLocation with-rotation theRotation.
\r
355 + 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
356 self register with-shape theShape at-location theLocation with-rotation theRotation.
\r
357 self set-color to theColor.
\r
362 + to set-direction at theDirection (vector):
\r
363 direction=theDirection.
\r
365 + to get-direction:
\r
369 Link : CeldasWheel (aka CeldasWheels) {
\r
370 % A CeldasWheel is used in conjunction with OBJECT(CeldasVehicle)
\r
371 % to build Celdas vehicles. This class is typically not instantiated
\r
372 % manually, since OBJECT(CeldasVehicle) creates one for you when you
\r
373 % add a wheel to the vehicle.
\r
382 - to set-joint to j (object):
\r
387 + section "Configuring the Wheel's Velocity"
\r
389 + to set-velocity to n (float):
\r
390 % Sets the velocity of this wheel.
\r
392 if n > CELDAS_MAX_VELOCITY: n = CELDAS_MAX_VELOCITY.
\r
395 joint set-joint-velocity to velocity.
\r
398 % Gets the velocity of this wheel.
\r
404 Link : CeldasSensor (aka CeldasSensors) {
\r
405 % A CeldasSensor is used in conjunction with OBJECT(CeldasVehicle)
\r
406 % to build Celdas vehicles. This class is typically not instantiated
\r
407 % manually, since OBJECT(CeldasVehicle) creates one for you when you
\r
408 % add a sensor to the vehicle.
\r
411 direction (vector).
\r
412 positiveDirection(vector).
\r
413 sensorAngle (float).
\r
420 direction = (1,0,1).
\r
421 positiveDirection= (1,0,1).
\r
424 draw = new Drawing.
\r
427 + section "Configuring the Sensor Values"
\r
428 + to set-id at n (int):
\r
431 + to set-body at robotBody(object):
\r
434 + to set-sensor-angle to n (float):
\r
435 % Sets the angle in which this sensor can detect obstacles. The default
\r
436 % value of 1.6 means that the sensor can see most of everything in
\r
437 % front of it. Setting the value to be any higher leads to general
\r
438 % wackiness, so I don't suggest it.
\r
442 + to set-direction to n (vector):
\r
444 positiveDirection::x=|n::x|.
\r
445 positiveDirection::y=|n::y|.
\r
446 positiveDirection::z=|n::z|.
\r
448 + section "Getting the Sensor Values"
\r
450 + to get-sensor-value:
\r
451 % Gets the sensor value. This should be used from post-iterate,
\r
452 % if not, the sensor reading correspond to the previous
\r
464 wallBegin,wallEnd,wallCenter (float).
\r
466 toObstacle(vector).
\r
471 posObstacle,destiny,yo(vector).
\r
477 foreach i in (all CeldasObstacles):
\r
479 posObstacle=i get-location.
\r
480 v = (body get-location) - (self get-location ).
\r
481 obsLoc::y=posObstacle::y.
\r
483 if (dot((i get-direction),(1,0,0))):
\r
485 obsLoc::x=((self get-location)::x + ((posObstacle::z - (self get-location)::z)*v::x/v::z)).
\r
486 obsLoc::z=posObstacle::z.
\r
490 obsLoc::z=((self get-location)::z + ((posObstacle::x - (self get-location)::x)*v::z/v::x)).
\r
491 obsLoc::x=posObstacle::x.
\r
495 if(dot((i get-direction),direction)==0):
\r
502 if(dot(direction,(1,1,1))<0):
\r
504 if((dot((self get-location),positiveDirection))>(dot(obsLoc,positiveDirection))):
\r
509 if((dot((self get-location),positiveDirection))<(dot(obsLoc,positiveDirection))):
\r
514 #Compruebo que el robot este frente a la pared
\r
515 wallCenter=dot((i get-location),(i get-direction)).
\r
516 wallBegin=wallCenter- (i get-large)/2.
\r
517 wallEnd=wallCenter + (i get-large)/2.
\r
520 yo=self get-location.
\r
521 destiny=i get-direction.
\r
525 if (dot((self get-location),(i get-direction)) > wallBegin) && (dot((self get-location),(i get-direction)) < wallEnd):
\r
533 #aux1=dot((self get-location),(i get-direction)).
\r
535 #print "sensor: $id obstaculo: $posObstacle direP: $destiny direS: $direction yo: $yo ".
\r
536 #print "begin: $wallBegin end: $wallEnd ".
\r
537 #print "begin: $wallBegin end: $wallEnd".
\r
541 #print "sensor: $id , des1: $des1, des2: $des2, des3: $des3".
\r
542 if ((des2) && (des3)):
\r
545 #print " posObstacle: $posObstacle".
\r
547 print "sensor: $id obstaculo: $posObstacle direP: $destiny direS: $direction yo: $yo ".
\r
548 print "begin: $wallBegin end: $wallEnd ".
\r
551 dist=|obsLoc - (self get-location)|.
\r
552 if( (j==0) || (min>dist) ):
\r
557 #print "sensor: $id obstaculo: $posObstacle direP: $destiny direS: $direction yo: $yo ".
\r
568 draw set-color to (1, 0, 0).
\r
569 draw draw-line from (self get-location) to (obs).
\r