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

C     svars - additional state variables, similar to (V)UEL
      dimension sensorValues(nSensor), svars(nSvars)
      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,
     *           iComputeDoubleInteg = 4,
     *           iStopAnalysis       = 5,
     *           iConcludeStep       = 6,
     *           nFlagsDefine        = 6)

      parameter(tStep=0.18d0, tAccelerateMotor = .00375d0, 
     *     omegaFinal=23.26d0, zero=0.0d0, one=1.0d0)
      dimension time(nTime), lFlagsInfo(nFlagsInfo),
     *          lFlagsDefine(nFlagsDefine)
      dimension jSensorLookUpTable(*)

c     get sensor value
      vTrans_CU1  = GetSensorValue('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) = zero
            svars(2) = zero
         else
            tim = time(iStepTime)

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. zero .and.
     *          tim .gt. tAccelerateMotor ) then 
               svars(2) = svars(2) + one
            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 
               lFlagsDefine(iConcludeStep)=1
            end if

c           store sensor value
            svars(1) = vTrans_CU1

         end if         
      end if 

      return
      end
