c     user amplitude subroutine
      Subroutine vuamp(
C          passed in for information and state variables
     *     ampName, time, ampValueOld, dt, nprops, props, nSvars, svars, 
     *     lFlagsInfo, nSensor, sensorValues, sensorNames, 
     *     jSensorLookUpTable, 
C          to be defined
     *     ampValueNew, 
     *     lFlagsDefine,
     *     AmpDerivative, AmpSecDerivative, AmpIncIntegral)
      
      include 'vaba_param.inc'

C     svars - additional state variables, similar to (V)UEL
      dimension sensorValues(nSensor), svars(nSvars)
      dimension props(nprops)
      character*80 sensorNames(nSensor)
      character*80 ampName

C     time indices
      parameter (iStepTime        = 1,
     *           iTotalTime       = 2,
     *           nTime            = 2)
C     flags passed in for information
      parameter (iInitialization   = 1,
     *           iRegularInc       = 2,
     *           nFlagsInfo        = 2)
C     optional flags to be defined
      parameter (iComputeDeriv     = 1,
     *           iComputeSecDeriv  = 2,
     *           iComputeInteg     = 3,
     *           iStopAnalysis     = 4,
     *           iConcludeStep     = 5,
     *           nFlagsDefine      = 5)

      dimension time(nTime), lFlagsInfo(nFlagsInfo),
     *          lFlagsDefine(nFlagsDefine)
      dimension jSensorLookUpTable(*)

      lFlagsDefine(iComputeDeriv)    = 1
      lFlagsDefine(iComputeSecDeriv) = 1

c     get sensor value
      vTrans_CU1  = vGetSensorValue('HORIZ_TRANSL_MOTION',
     *                              jSensorLookUpTable,
     *                              sensorValues)

      if (ampName(1:22) .eq. 'MOTOR_WITH_STOP_SENSOR' ) then
         if (lFlagsInfo(iInitialization).eq.1) then 
            ampValueNew      = ampValueOld

            svars(1) = 0.0
            svars(2) = 0.0
         else
            tim = time(iStepTime)
            tStep = props(1)
            tAccelerateMotor = props(2)
            omegaFinal = props(3)

c           ramp up the angular rot velocity  of the electric motor
c           after which hold constant
            if (tim .le. tAccelerateMotor) then 
               ampValueNew = omegaFinal*tim/tAccelerateMotor

            else
               ampValueNew = omegaFinal
            end if

c           retrieve old sensor value
            vTrans_CU1_old = svars(1)

c           detect wether the square translator connection 
c           crosses the zero value; count the number of 
c           crossings
            if (vTrans_CU1_old*vTrans_CU1 .le. 0.0 .and.
     *          tim .gt. tAccelerateMotor ) then 
               svars(2) = svars(2) + 1.0
            end if            
            nrCrossings = int(svars(2))

c           stop the motor if sensor crosses zero the second time
c           (one complete motion back and forth for the traveling head of
c           the translator)
c           Alternatively, the analysis can be stopped if needed.
            if (nrCrossings.eq.2) then 
               ampValueNew =  0.0
               lFlagsDefine(iConcludeStep)=1
            end if

c           store sensor value
            svars(1) = vTrans_CU1

         end if         
      end if 

      return
      end