# Cut To Length (Punching) Application/zh-hans

 语言: English  • 中文（简体）‎

# 程序

```common shared c1 as cam

dim shared toolong as error "The given cut length is too long for the given slave accel."
dim shared toofast as error "Slave axis can not move that fast"

' Shared profile parameters:..............................................
dim shared lprof        as double  ' total length
dim shared aprof        as double  ' acceleration used
dim shared vprof        as double  ' cruise velocity
dim shared t_acc_prof   as double  ' acceleration duration
dim shared t_dec_prof   as double  ' end of cruise phase
dim shared t_tot_prof   as double  ' total velocity trapeze duration
'.........................................................................

program
' Process data
dim sangle      as double = 10  ' start to cut at this angle
dim eangle      as double = 60  ' finish the cut at this angle
dim rev         as double = 360 ' full turn of the wheel
dim Vwheel      as double = 60  ' RPM - velocity of the wheel

dim CutLength   as double = 20  ' mm of the slave motion
dim CutAngle    as double
dim CutTime     as double
dim i           as long

' Variables used in cam computation
dim t           as double
dim t_from_end  as double

' Creating 1000 points in the cam
createcamdata 1000 c1

' Computing the next motion
CutAngle = rev - (eangle-sangle)    ' The wheel passes this angel while it moves
CutTime  = CutAngle/(Vwheel*360/60) ' Time needed to move to another cut (sec)
with A1

call ProfileLTA(CutLength,CutTime,Acc)

' Start computing the cam table
' t is relative time of velocity trapeze profile
for i = 1 to c1.size
c1.MasterData[i] = (i-1)*rev/c1.size ' Defining a uniform master axis

select case (c1.MasterData[i])
case is < sangle     ' it is a rotary axis in cases when master < start
' we need to compute the relative time from the end
t_from_end = (sangle - c1.MasterData[i])/(Vwheel*360/60)
t = CutTime - t_from_end
c1.SlaveData[i] = VTProfile(t)  - CutLength

case is <= eangle    ' master is between start and end - no motion of slave:
c1.SlaveData[i] = 0.0

case is > eangle     ' Time passed after exit [start,end] master segment
t = (c1.MasterData[i]-eangle)/(Vwheel*360/60)
c1.SlaveData[i] = VTProfile(t)

end select
next
end with

with A2
' Setup the cam table
c1.Cycle            = -1 ' endless camming
Attach
FirstCam         = c1
MasterSource     = A1.Pcmd   'For example with 2 axes
'           MasterSource    = pext
GearRatio        = 1
Slave            = CAM
SlaveDisconnect  = 0
En               = ON
Detach

end with

Print "Cut to length Ready"

end program

' Velocity Trapeze pre-calculation routine
' Input: Length, Total time, max Accleration
' Global used: Vmax

sub ProfileLTA(byval L as double ,byval T as double, byval A as double)
dim sqr as double

with A1
aprof = A
sqr = T^2 - 4*L/A
select case (sqr)

case is < -0.004 ' less then 4ms
throw toolong

case is < 0
' In cases of numeric computation errors (high accel. values) this could be a small
' negative number. In order to avoid math error the value is cut to zero.
sqr = 0

case else
sqr = sqrt(sqr)
end select

vprof = 0.5*A*(T - sqr)
if vprof > vmax then
throw toofast
end if

' Compute fixed points in time (end of acceleration, begin of deceleration)
t_acc_prof = vprof/A
t_dec_prof = T - t_acc_prof
t_tot_prof = T

end with
end sub

'.................................................................................
' Velocity Trapeze gernal purpose profile function
' uses shared variables:
' t_acc - time where acceleration ends (cruise starts)
' t_dec - time where deceleration starts (cruise ends)
' t_tot - end of movement
' aprof - accleration/deceleration value
' vprof - cruise (max) velocity
' lprof - total movement length
function VTProfile(byval t as double) as double
select case(t)
case is <=0
VTProfile = 0
case is <= t_acc_prof
VTProfile = 0.5*aprof*t^2
case is <= t_dec_prof
VTProfile = 0.5*vprof^2/aprof + (t-t_acc_prof)*vprof
case is < t_tot_prof
VTProfile = lprof - 0.5*aprof*(t_tot_prof-t)^2
case else
VTProfile = lprof
end select
end function
```