www.pudn.com > truetime-1.2-compiled.zip > actuator_init.cpp
// Distributed control system: actuator node
//
// Receives messages from the controller and actuates
// the plant.
#define S_FUNCTION_NAME actuator_init
#include "ttkernel.cpp"
// code function
double act_code(int seg, void *data) {
static double *y, *u, t;
switch (seg) {
case 1:
ttWait("packet");
return 0.0;
case 2:
return 0.0005;
case 3:
u = (double *)ttGetMsg(); // Receive message
if (u != NULL) {
ttAnalogOut(1, *u);
delete u; // delete message
}
ttSetNextSegment(1); // loop and wait for new packet
return 0.0;
}
}
double msgRcvhandler(int seg, void *data)
{
ttNotifyAll("packet");
return FINISHED;
}
void init()
{
// Initialize TrueTime kernel
ttInitKernel(0, 1, FP); // nbrOfInputs, nbrOfOutputs, fixed priority
// Actuator task
double deadline = 100.0;
double prio = 1.0;
ttCreateTask("act_task", deadline, prio, act_code);
ttCreateJob("act_task");
// Initialize network
ttCreateInterruptHandler("msgRcv", prio, msgRcvhandler);
ttInitNetwork(2, "msgRcv"); // node #2 in the network
ttCreateEvent("packet");
}
void cleanup() {
}