plc/plc.cpp

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

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy