00001
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <errno.h>
00036 #include <math.h>
00037
00038 #include "plc.h"
00039
00040 CLASS *plc::oclass = NULL;
00041 plc *plc::defaults = NULL;
00042
00044 plc::plc(MODULE *mod)
00045 {
00046
00047 if (oclass==NULL)
00048 {
00049
00050 oclass = gl_register_class(mod,"plc",sizeof(plc),PC_BOTTOMUP);
00051
00052
00053 if (gl_publish_variable(oclass,
00054 PT_char1024,"source",PADDR(source),
00055 PT_object,"network",PADDR(network),
00056 NULL)<1) GL_THROW("unable to publish properties in %s",__FILE__);
00057
00058
00059 memset(this,0,sizeof(plc));
00060 strcpy(source,"");
00061 controller = NULL;
00062 network = NULL;
00063 defaults = this;
00064 }
00065 }
00066
00068 int plc::create()
00069 {
00070
00071 memcpy(this,defaults,sizeof(plc));
00072
00073
00074 controller = new machine;
00075 return 1;
00076 }
00077
00079 int plc::init(OBJECT *parent)
00080 {
00081 if (strcmp(source,"")==0 && parent!=NULL)
00082 sprintf(source,"%s.plc",parent->oclass->name);
00083 if (controller->compile(source)<0)
00084 GL_THROW("%s: PLC compile failed", source);
00085 if (controller->init(parent)<0)
00086 GL_THROW("%s: PLC init failed", source);
00087 parent->flags |= OF_HASPLC;
00088
00089 if (network)
00090 {
00091 controller->connect(OBJECTDATA(network,comm));
00092 gl_verbose("machine %x has connected to network '%s'", controller, network->name?network->name:"anonymous");
00093 }
00094
00095 return 1;
00096 }
00097
00099 TIMESTAMP plc::sync(TIMESTAMP t0, TIMESTAMP t1)
00100 {
00101 if (t0>0)
00102 {
00103 int dt = controller->run((double)(t1-t0)/TS_SECOND);
00104 if (dt<0)
00105 return TS_INVALID;
00106 else if (dt==0x7fffffff)
00107 return TS_NEVER;
00108 else
00109 return t1+(TIMESTAMP)(dt*TS_SECOND);
00110 }
00111 else
00112 return t1;
00113 }
00114
00116
00118 EXPORT int create_plc(OBJECT **obj, OBJECT *parent)
00119 {
00120 *obj = gl_create_object(plc::oclass);
00121 if (*obj!=NULL)
00122 {
00123 plc *my = OBJECTDATA(*obj,plc);
00124 gl_set_parent(*obj,parent);
00125 my->create();
00126 return 1;
00127 }
00128 return 0;
00129 }
00130
00131 EXPORT int init_plc(OBJECT *obj)
00132 {
00133 if (obj!=NULL)
00134 {
00135 plc *my = OBJECTDATA(obj,plc);
00136 return my->init(obj->parent);
00137 }
00138 return 0;
00139 }
00140
00141 EXPORT TIMESTAMP sync_plc(OBJECT *obj, TIMESTAMP t0)
00142 {
00143 TIMESTAMP t1 = OBJECTDATA(obj,plc)->sync(obj->clock,t0);
00144 obj->clock = t0;
00145 return t1;
00146 }
00147