Please take a minute to review and accept our Terms of Use.
Welcome to the PLECS User Forum, where you can ask questions and receive answers from other members of the community.

Many technical questions regarding PLECS are answered on the Technical Solutions page of our website. Tutorial videos, specific application examples, and pre-recorded webinars are available on our YouTube page. Please follow us on LinkedIn for the latest Plexim news.

Create Delay with C-script

0 votes
1,671 views
Dear Oliver,

I am a new PLECS user. Attached is the model to create a delay that you created before.

Could you please explain the below commands in detail? I am very new to PLECS C-script, so I don't understand your command.

Your help is greatly appreciated!

Regards,

Alex

if(IsMajorStep)

{

if(IsSampleHit(0))

{

if(Input_pre == 0 && InputSignal(0, 0) == 1)

{

double nextHit = CurrentTime + InputSignal(1, 0);

if (nextHit > CurrentTime)

NextSampleHit = CurrentTime + InputSignal(1, 0);

else

OutputSignal(0, 0) = 1;

}

if(Input_pre == 1 && InputSignal(0, 0) == 0)

{

OutputSignal(0, 0) = 0;

}

}

if(IsSampleHit(1))

{

NextSampleHit = NEVER;

OutputSignal(0, 0) = 1;

}

}
asked Nov 10, 2020 by Alex Ng (15 points)

1 Answer

0 votes

You should read the chapters "How PLECS works" and "Using C-Scripts" in the PLECS manual. I put some comments in the code (and made some additions) below to make it more understandable.

 
// We are not worried about integration and such, so only consider major steps
if(IsMajorStep)
{
   // Continuous task: Rising edge detection and start of delay
   if(IsSampleHit(0))
   {      
      // Detect edge of incoming signal
      if(Input_pre == 0 && InputSignal(0, 0) == 1)
      {
          // Calculate time or next execution of task 1
          double nextHit = CurrentTime + InputSignal(1, 0);
          if (nextHit > CurrentTime)
             NextSampleHit = CurrentTime + InputSignal(1, 0);
          else
             OutputSignal(0, 0) = 1; // no delay, output 1 immediately
      }
      // Detect falling edge
      if(Input_pre == 1 && InputSignal(0, 0) == 0)
      {
         // Cancel delay
         NextSampleHit = NEVER;
         // Output 0 immediately
         OutputSignal(0, 0) = 0;
      }
   }
   // Task 1: executed after specified delay after edge detection
   if(IsSampleHit(1))
   {
      // Safety measure: check if input signal is still 1
      if (InputSignal(0, 0) == 1)
         OutputSignal(0, 0) = 1; // turn-on delay over, turn on signal
   }
}

Kind regards,

Oliver

 

answered Nov 11, 2020 by Oliver Schwartz (622 points)
edited Nov 11, 2020 by Oliver Schwartz
Thank you, Oliver. I really appreciate your help!
...