Licencia de Creative Commons
Este tutorial se acoge a la licencia de Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

 

SC PASCAL: TRABAJANDO CON SERVOMOTORES

Vamos a usar servomotores para mover las agujas de un instrumento analógico. En este ejemplo vamos a usar el indicador de flaps. Las variables IOCP son:

  • Aguja Flap izquierdo: 327
  • Aguja Flap derecho: 328

como se ve en las siguientes imágenes:

 

El script SC Pascal es el siguiente:


PROGRAM FLAPS;

CONST
//ponemos nombres a las variables IOCP
//we named IOCP offsets
   offset_needle_flapl=327;
   offset_needle_flapr=328;

VAR
//ponemos nombres a las variables calculadas
//we named calculated variables
   pos_needle_flapl:integer;
   pos_needle_flapr:integer;


//IOCP EVENTs
//definimos que variables IOCP nos interesan chequear
//we define which IOCP offsets must to be checked
procedure OnIOCPConnect(socket:byte);
begin
   RegIOCPOffset(1,offset_needle_flapl);
   RegIOCPOffset(1,offset_needle_flapr);
end;

procedure OnIOCPChange(socket:byte;offset,value:integer);
BEGIN
IF (offset=offset_needle_flapl) THEN BEGIN
   IF (value=0) THEN BEGIN // flaps 0
      pos_needle_flapl:=231;
      WriteServo('SIMIO300011',1,pos_needle_flapl);
   END ELSE BEGIN
   IF (value<=410) THEN BEGIN // flaps 1
      pos_needle_flapl:=round(231-(value*0.060975610));
      WriteServo('SIMIO300011',1,pos_needle_flapl);
   END ELSE BEGIN
   IF (value<=819) THEN BEGIN // flaps 2
      pos_needle_flapl:=round(206-((value-410)*0.068459658));
      WriteServo('SIMIO300011',1,pos_needle_flapl);
   END ELSE BEGIN
   IF (value<=2048) THEN BEGIN // flaps 5
      pos_needle_flapl:=round(178-((value-819)*0.028478438));
      WriteServo('SIMIO300011',1,pos_needle_flapl);
   END ELSE BEGIN
   IF (value<=4096) THEN BEGIN // flaps 10
      pos_needle_flapl:=round(143-((value-2048)*0.013671875));
      WriteServo('SIMIO300011',1,pos_needle_flapl);
   END ELSE BEGIN
   IF (value<=6144) THEN BEGIN // flaps 15
      pos_needle_flapl:=round(115-((value-4096)*0.008789063));
      WriteServo('SIMIO300011',1,pos_needle_flapl);
   END ELSE BEGIN
   IF (value<=10240) THEN BEGIN // flaps 25
      pos_needle_flapl:=round(97-((value-6144)*0.004638672));
      WriteServo('SIMIO300011',1,pos_needle_flapl);
   END ELSE BEGIN
   IF (value<=12288) THEN BEGIN // flaps 30
      pos_needle_flapl:=round(78-((value-10240)*0.013671875));
      WriteServo('SIMIO300011',1,pos_needle_flapl);
   END ELSE BEGIN // flaps 40
      pos_needle_flapl:=round(50-((value-12288)*0.004882813));
      WriteServo('SIMIO300011',1,pos_needle_flapl);
   END;
END;
END;
END;
END;
END;
END;
END;
end;

IF (offset=offset_needle_flapr) THEN BEGIN
   IF (value=0) THEN BEGIN // flaps 0
      pos_needle_flapr:=231;
      WriteServo('SIMIO300011',2,pos_needle_flapr);
   END ELSE BEGIN
   IF (value<=410) THEN BEGIN // flaps 1
      pos_needle_flapr:=round(231-(value*0.065853659));
      WriteServo('SIMIO300011',2,pos_needle_flapr);
   END ELSE BEGIN
   IF (value<=819) THEN BEGIN // flaps 2
      pos_needle_flapr:=round(204-((value-410)*0.078239609));
      WriteServo('SIMIO300011',2,pos_needle_flapr);
   END ELSE BEGIN
   IF (value<=2048) THEN BEGIN // flaps 5
      pos_needle_flapr:=round(172-((value-819)*0.021969081));
      WriteServo('SIMIO300011',2,pos_needle_flapr);
   END ELSE BEGIN
   IF (value<=4096) THEN BEGIN // flaps 10
      pos_needle_flapr:=round(145-((value-2048)*0.015136719));
      WriteServo('SIMIO300011',2,pos_needle_flapr);
   END ELSE BEGIN
   IF (value<=6144) THEN BEGIN // flaps 15
      pos_needle_flapr:=round(114-((value-4096)*0.01171875));
      WriteServo('SIMIO300011',2,pos_needle_flapr);
   END ELSE BEGIN
   IF (value<=10240) THEN BEGIN // flaps 25
      pos_needle_flapr:=round(90-((value-6144)*0.005126953));
      WriteServo('SIMIO300011',2,pos_needle_flapr);
   END ELSE BEGIN
   IF (value<=12288) THEN BEGIN // flaps 30
      pos_needle_flapr:=round(69-((value-10240)*0.012695313));
      WriteServo('SIMIO300011',2,pos_needle_flapr);
   END ELSE BEGIN // flaps 40
      pos_needle_flapr:=round(43-((value-12288)*0.00390625));
      WriteServo('SIMIO300011',2,pos_needle_flapr);
   END;
END;
END;
END;
END;
END;
END;
END;
END;
END;




//MAIN
Begin
//WRITE HERE YOUR CODE
//Activamos los servos 1 y 2 de nuestra tarjeta de servos
//We activate servos 1 and 2 in our servo card
   EnableServo('SIMIO300011',1,1,127);
   EnableServo('SIMIO300011',2,1,127);
End.

 

En este vídeo se muestra el script funcionando:

 

 

 

Ultima edición: 31.01.2018

 

Versión española

English version