Program LepZAxis ; { Example VPascal program to control a Ludl z-axis motor. Copyright (C) 1996 Digital Optics Ltd. This program creates a number of menu entries (in the Tools menu) together with its own toolbar containing five buttons for moving the motor. Menu Entry What it does ---------- ------------ "Goto position" Move the motor to an absolute position. "Set home position" Mark current motor position as "home". "Set coarse increment" Set the value used for coarse increments. "Set fine increment" Set the value used for fine increments. "Enable joystick" Enable joystick (actually a knob) control of motor. "Disable joystick" Disable joystick control of motor. "Send Ludl command" Send any high-level command string to the controller. "Shutdown controller" Halt all motor actions and close serial port. Toolbar Button What it does -------------- ------------ z-Axis Begin Move motor to home position. z-Axis Fast Reverse Decrement motor position using coarse value. z-Axis Reverse Decrement motor position using fine value. z-Axis Forward Increment motor position using fine value. z-Axis Fast Forward Increment motor position using coarse value. Notes: 1. This program assumes that the motor is auxiliary motor "b". To control a different motor, say x or y, change the Device constant below. 2. When the program starts it disables the joystick. To enable or disable the joystick choose the appropriate menu entry. When you select "Set home position" from the menu the joystick is automatically enabled to allow the user to set the home position. 3. Each toolbar button procedure is shared via DDE so that, if desired, other scripts or external programs can call the stepping procedures. The DDE share names are the same as the procedure names. } toolbar 'z-Axis Motor' ; { Toolbar caption } const Port = 2 ; { Com port for LUDL controller } Speed = 9600 ; { Baud rate } Bits = 8 ; { Data bits } Parity = NoParity ; { Parity setting } Stop = 2 ; { Data stop bits } Device = 'b' ; { Device ID. Other controllers may use x, y, r etc. } var fInc ; { Fine increment for stepping } cInc ; { Coarse increment for stepping } Ack ; { Acknowledgements from controller } procedure EnableJoystick ; forward ; procedure DisableJoystick ; forward ; procedure InitController ; { Initialise the controller } begin { Try to open serial port } OpenSerial( Port,Speed,Bits,Parity,Stop ) ; if SerialError <> 0 then Halt( 'Could not connect to LUDL controller!' ) ; { Make sure no garbage in buffers } TxFlush ; RxFlush ; { Controller expects after tx and after rx } SetTxEnd( chr(13) ) ; SetRxEnd( chr(10) ) ; { Select high-level programming format } Transmit( chr(255)+chr(65) ) ; { Set defaults } fInc := 1000 ; cInc := 10000 ; DisableJoystick ; end ; { InitController } procedure WaitWhileBusy ; dde 'WaitWhileBusy' ; { Wait while controller is busy } var Busy ; Ch ; begin Busy := true ; repeat repeat TxFlush ; RxFlush ; Transmit( 'status '+Device ) ; repeat until RxWaiting > 0 ; Ch := RxChar ; Busy := Ch = 'B' ; if Busy then WriteStatus( 'Busy' ) else WriteStatus ; until not Busy ; until Ch = 'N' ; end ; { WaitWhileBusy } procedure GotoPos ; menu '-Go to position...' ; const Msg = 'Enter the absolute position for the motor' ; var Pos ; Ch ; begin { Suggest a default } Pos := '0' ; { Prompt user } if GetString( Msg,Pos ) <> id_Cancel then begin { Send command } RxFlush ; Transmit( 'move '+Device+'='+Pos ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; WaitWhileBusy ; end ; end ; { GotoPos } procedure GotoHomePos ; button btn_zBegin,'Go to home position' ; dde 'GotoHomePos' ; begin { Send command } RxFlush ; Transmit( 'move '+Device+'=0' ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; WaitWhileBusy ; end ; { GotoHomePos } procedure StepRevCoarse ; button btn_zFRev,'Step reverse (coarse)' ; dde 'StepRevCoarse' ; begin { Send command } RxFlush ; Transmit( 'movrel '+Device+'='+Str( -cInc ) ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; WaitWhileBusy ; end ; { StepRevCoarse } procedure StepRevFine ; button btn_zRev,'Step reverse (fine)' ; dde 'StepRevFine' ; begin { Send command } RxFlush ; Transmit( 'movrel '+Device+'='+Str( -fInc ) ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; WaitWhileBusy ; end ; { StepRevFine } procedure StepFwdFine ; button btn_zFwd,'Step forward (fine)' ; dde 'StepFwdFine' ; begin { Send command } RxFlush ; Transmit( 'movrel '+Device+'='+Str( fInc ) ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; WaitWhileBusy ; end ; { StepFwdFine } procedure StepFwdCoarse ; button btn_zFFwd,'Step forward (coarse)' ; dde 'StepFwdCoarse' ; begin { Send command } RxFlush ; Transmit( 'movrel '+Device+'='+Str( cInc ) ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; WaitWhileBusy ; end ; { StepFwdCoarse } procedure SetHomePos ; menu 'Set home position...' ; const Msg1 = 'Use the joystick to set the HOME position and then press OK' ; Msg2 = 'Home position is now set' ; begin { Make sure to enable joystick for user! } EnableJoystick ; { Prompt user to move motor to (arbitrary) home position using joystick } WriteInfo( Msg1 ) ; { Disable joystick again to prevent screw ups } DisableJoystick ; { Specify current motor position as "home" } RxFlush ; Transmit( 'here '+Device+'=0' ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; { Tell user position is set } WriteInfo( Msg2 ) ; end ; { SetHomePos } procedure SetCoarseInc ; menu 'Set coarse increment...' ; const Msg = 'Enter a new value for the COARSE increment' ; begin GetNumber( Msg,cInc ) ; end ; { SetCoarseInc } procedure SetFineInc ; menu 'Set fine increment...' ; const Msg = 'Enter a new value for the FINE increment' ; begin GetNumber( Msg,fInc ) ; end ; { SetFineInc } procedure EnableJoystick ; menu '-Enable joystick' ; begin { Send command } RxFlush ; Transmit( 'joystick '+Device+'+' ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; end ; { EnableJoystick } procedure DisableJoystick ; menu 'Disable joystick' ; begin { Send command } RxFlush ; Transmit( 'joystick '+Device+'-' ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; end ; { DisableJoystick } procedure SendCmd ; menu '-Send LUDL command string...' ; const Msg1 = 'Enter a command string or press Cancel to quit' ; Msg2 = '; ;Note: The device ID is ' ; var Cmd ; { Command string to send } Quit ; { True when done sending commands } Msg ; { Prompt message } begin Cmd := '' ; Msg := Msg1 + Msg2 + Device ; repeat Quit := GetString( Msg,Cmd ) = id_Cancel ; if not Quit then begin TxFlush ; RxFlush ; Transmit( Cmd ) ; Ack := RxString ; WriteInfo( 'Tx> '+Cmd+';Rx> '+Ack ) ; WaitWhileBusy ; end ; until Quit ; end ; { SendCmd } procedure CloseController ; menu 'Shutdown controller' ; begin { Send command } RxFlush ; Transmit( 'halt' ) ; { Receive (and ignore) acknowledgement } Ack := RxString ; { Close serial port } CloseSerial( Port ) ; end ; { CloseController } begin InitController ; end