Emergency Stop based on external signal

C, C++, and Visual C++
Post Reply
SashaLag
Fresh meat
Posts: 2
Joined: Sun Jul 01, 2018 3:30 am
Contact:

Emergency Stop based on external signal

Post by SashaLag »

Hi,

I would like to implement an Emergency Stop routine based on a signal received from an external device through USB. I know there are delays involved in this problem (because of the operating system, because of the USB and because of different controllers utilized) but I want to do it anyway.

Now, my program has to move a stepper motor with precise position ending so I suppose, CONTROL_MODE_RUN and sslep(certain amount) may not the best solution (am I wrong?). So be it CONTROL_MODE_STEP.

It moves correctly with these instructions:

Code: Select all

for (int j = 0; j < NumAcquisition1; ++j) {
   RotSteps = (TotalSteps1 / (NumAcquisition1 - 1)) * j;
   printf("\nSegment: %d\n", j);
   PhidgetStepper_setTargetPosition(BedMotor, BedSteps);
   ssleep(5);
}
but I really don't know how to implement this Emergency Stop in Phidget Environment. I have a background on microcontrollers, so there I would implement an Interrupt routine, but here I don't know which route to follow. Does this instruction

Code: Select all

PhidgetReturnCodePhidgetStepper_setTargetPosition_async(
PhidgetStepperHandle ch,
double targetPosition,
void (CCONV *asyncHandler)(PhidgetHandle phid, void *ctx, PhidgetReturnCode res),
void *ctx
) 
fullfill my needs? If so, can you explain to me what to insert instead of void (CCONV *asyncHandler)(PhidgetHandle phid, void *ctx, PhidgetReturnCode res)?

Thank you in advance!
User avatar
mparadis
Site Admin
Posts: 959
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Emergency Stop based on external signal

Post by mparadis »

You can tell the motor to stop by setting the targetPosition to the currentPosition.

The difference between setTargetPositon and setTargetPosition_async is that they async version is non-blocking, which is only useful if there are timing-critical things you want to do immediately after stopping.

In order to implement a stop, you'll need to use events. For example, if you have a Phidget with a digital input that reads your stop signal, you could set up an OnStateChange event for the digital input, and then inside that handler put code that sets targetPosition equal to currentPosition. You'll need to pass a pointer to the Stepper handle into the event handler for this. Passing the pointer in is explained here.
SashaLag
Fresh meat
Posts: 2
Joined: Sun Jul 01, 2018 3:30 am
Contact:

Re: Emergency Stop based on external signal

Post by SashaLag »

ah, it's clear to me now! Thank you for the explanation, really appreciated!
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests