itaMotorNanotec_Slayer.m 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
classdef itaMotorNanotec_Slayer < itaMotorNanotec
    %ITAMOTORCONTROL Summary of this class goes here
    %   Detailed explanation goes here
    
    properties(Access = protected, Hidden = true)
        mSerialObj; % the serial connection
        
        mMotorControl; % parent controlling this class
        sArgs_motor;
        
    end
    
    properties 

    end
    
    properties(Constant, Hidden = true)
        % used to be sArgs_default_slayer
        sArgs_default_motor    = struct( ...
            'wait',         true,       ...
            'speed',        2,          ...
            'VST',          'adaptiv',  ...
            'acceleration_ramp', 2000,  ...
            'absolut',      true,       ...
            'gear_ratio',   200,        ...
            'current',      80,         ...
27
            'ramp_mode',    2           ); 
28 29 30 31 32 33 34 35 36 37 38
    end
    
    methods
        function this = itaMotorNanotec_Arm(varargin)
            options =   struct('motorControl', []);
            options    =   ita_parse_arguments(options, varargin);
            this.mMotorControl = options.motorControl;
            this.mSerialObj = itaSerialDeviceInterface.getInstance();
            
            this.motorID = 5;
            this.motorName = 'Slayer';
39 40
            
            this.motorLimits = [-82 190];
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
        end
        
        function this = init(this)
            
        end
        
        function stop(this)
            % DO NOT ASK - JUST STOP ALL MOTORS!
            for i = 1:5 % repeat several times to ensure that every motor stops!
                this.mSerialObj.sendAsynch(sprintf('#%dS\r'        , this.motorID));
%                 pause(this.waitForSerialPort);
            end
            while this.mSerialObj.BytesAvailable
                ret = this.mSerialObj.recvAsynch;
            end    
        end
        
        
        function getStatus(this)
            motorControl.add_to_commandlist(sprintf('#%d$\r'    , this.motorID));
        end
        
        function status = isActive(this)
            if this.mIsInit == false
                this.mSerialObj.sendAsynch(sprintf('#%d$\r'    , this.motorID));
            end
            status = this.mIsInit;
        end
        
        function setActive(this,value)
           this.mIsInit = value;
        end
        
        function id = getMotorID(this)
            id = this.motorID;
        end
        
        function name = getMotorName(this)
           name = this.motorName; 
        end
        
        function sendConfiguration(this)
            motorControl = this.mMotorControl;
            % Set Input 3 as external Referenceswitch *****Important!!!
            motorControl.add_to_commandlist(sprintf('#%d:port_in_b7\r'  , this.motorID)); % Added swtich 2
            motorControl.add_to_commandlist(sprintf('#%d:port_in_c7\r'  , this.motorID));
            motorControl.add_to_commandlist(sprintf('#%d:port_out_a1\r' , this.motorID));
            motorControl.add_to_commandlist(sprintf('#%d:port_out_a2\r' , this.motorID));
            % Define switch behavior
            % DO NOT USE THE FOLLOWING TWO LINES! THEY DON'T WORK
            % AS THEY ARE SUPPOSED TO. CHECK SETTING VIA NANOPRO!
            % Free back for external switch disabled during normal
            %this.add_to_commandlist(sprintf('#%dl=%d\r'         , this.motorID, bin2dec('0100010000101000')));
            % Free back for external switch enabled during normal
            %this.add_to_commandlist(sprintf('#%dl=%d\r'         , this.motorID, bin2dec('001010000100010')));
            motorControl.add_to_commandlist(sprintf('#%dl%d\r'         , this.motorID, 5154));
            motorControl.add_to_commandlist(sprintf('#%dJ=1\r'          , this.motorID));
            motorControl.add_to_commandlist(sprintf('#%dz=0\r'          , this.motorID));

        end
        
        function this = moveToReferencePosition(this)
            
            % Prepare reference move (arm)
            if ~this.mIsInit
                ita_verbose_info('Not initialized - This should not happen',0);
            end
            
            % Call Reference-Mode:
            motorControl.add_to_commandlist(sprintf('#%dp=4\r', this.motorID));
            %**********Important!!*********************
            % Set direction:
            motorControl.add_to_commandlist(sprintf('#%dd=0\r'          , this.motorID));
            %******************************************
            % Calculate and set upper speed:
            stepspersecond      =   (this.sArgs_default_motor.speed/8/0.9*this.sArgs_default_motor.gear_ratio);
            motorControl.add_to_commandlist(sprintf('#%du=%.2f\r'       , this.motorID, stepspersecond));
            % Calculate and set lower speed:
            stepspersecond      =   (this.sArgs_default_motor.speed/2/0.9*this.sArgs_default_motor.gear_ratio);
            motorControl.add_to_commandlist(sprintf('#%do=%.2f\r'       , this.motorID, stepspersecond));
            % Start reference move:
            motorControl.add_to_commandlist(sprintf('#%dA\r'            , this.motorID));

            
            this.old_position = itaCoordinates(1);
            this.mIsReferenced = true;
            ita_verbose_info('Slayer referenced...',1);
        end
        
        function this = startMoveToPosition(this)
             this.mMotorControl.add_to_commandlist(sprintf('#%dA\r'        , this.motorID));
        end
        
        
        function started = prepareMove(this,position,varargin)
            
           if ~this.mIsInit
               ita_verbose_info('Slayer: No initialized! This should not happen!',0)
               started = false;
               return;
           end
           
           if ~this.mIsReferenced
              ita_verbose_info('Slayer: No reference move done! Not moving!',0)
              started = false;
              return;
           end
            
           sArgs.continuous = false;
           sArgs.speed = this.sArgs_default_motor.speed;
           if ~isempty(varargin)
               sArgs = ita_parse_arguments(sArgs,varargin);
           end
           if sArgs.continuous
                ret = this.prepare_move(position, 'speed', sArgs.speed,'continuous', true); 
                started = true;
           else
               % if only the theta angle is given
               if ~isa(position,'itaCoordinates')
                  tmpPosition = this.old_position;
                  if ~isnan(position)
                      tmpPosition.theta_deg = position;
                  end
                  position = tmpPosition;
               end

               if this.old_position.theta ~= position.theta

                    angle = position.theta_deg;
                    ret = this.prepare_move(angle, 'wait', true); 
                    this.old_position = position;
                    started = ret;
               else
                    started = false;
               end
           end
        end

    end
    
    methods(Hidden = true)
        function ret = prepare_move(this, angle, varargin)
%              if ~this.isInitialized
%                 ita_verbose_info('Not initialized - I will do that for you...!',0);
%                 this.initialize
%             end
%             if ~this.isReferenced
%                 ita_verbose_info('Arm: You are not allowed to move the arm until you made a reference move!', 0)
%                 ret         =   false;
%                 this.wait   =   false;
%                 return;
%             end

            if isnan(angle)
                ret = false;
                return;
            end

            if (angle < this.SLAYER_limit(1)) || (angle > this.SLAYER_limit(2))
                ita_verbose_info(['Slayer: Only values between ' num2str(this.SLAYER_limit(1)) ' and ' num2str(this.SLAYER_limit(2)) ' are allowed!'], 0)
                ret         =   false;
%                 this.wait   =   false;
                return;
            end
            
            angle           =   angle + 84.34; % Reference at -84.34 degree!
            
            
            motorControl = this.mMotorControl;
            
            sArgs_slayer  =   this.sArgs_default_motor;
            % -------------------------------------------------------------
            % Meaning:
            %
            % Wait              =   Stop matlab until motor reaches final position!
            % Speed             =   Grad/sec of the arm
            % VST               =   Microstep divider. Values: 1, 2, 4, 5, 8, 10, 16, 32,
            %                       64. 254="Vorschubkonstantenmodus", 255=Adaptive Stepdivider
            % Closed_loop       =   Turn on the closed loop regulation
            % Acceleration_ramp =   Value in Hz/ms
            % Gear_ratio        =   Getriebe�bersetzung
            % Current           =   Maximum current in percent
            % Ramp_mode         =   0=trapez, 1=sinus-ramp, 2=jerkfree-ramp
            % -------------------------------------------------------------
            sArgs_slayer  =   ita_parse_arguments(sArgs_slayer,varargin);
%             this.wait       =   sArgs_arm.wait;
            if (sArgs_slayer.speed == 0)
                % This means: STOP!
                motorControl.add_to_commandlist(sprintf('#%dS\r'        , this.motorID));
                return
            end
            
            if (this.sArgs_slayer.speed > 20) || (this.sArgs_slayer.speed < 0)
                ita_verbose_info('Slayer: Speed must be between >0 and 20!', 0)
                ret             =   false;
                return
            end
         % Set microstep-divider:
            if strcmpi(this.sArgs_slayer.VST, 'adaptiv')
                motorControl.add_to_commandlist(sprintf('#%dg=255\r'         , this.motorID));
            else
                motorControl.add_to_commandlist(sprintf(['#%dg=' this.sArgs_slayer.VST '\r'] , this.motorID));
            end
            % Set maximum current:
            motorControl.add_to_commandlist(sprintf('#%di=%.0f\r'           , this.motorID, this.sArgs_slayer.current));
            % Choose ramp mode: (0=trapez, 1=sinus-ramp, 2=jerkfree-ramp):
            motorControl.add_to_commandlist(sprintf('#%d:ramp_mode=%d\r'    , this.motorID, this.sArgs_slayer.ramp_mode));
            % Set maximum acceleration jerk:
            motorControl.add_to_commandlist(sprintf('#%d:b=100\r'           , this.motorID));
            % Use acceleration jerk as braking jerk:
            motorControl.add_to_commandlist(sprintf('#%d:B=0\r'             , this.motorID));
            % Use motor as classic step motor: (No closed loop supported!)
            motorControl.add_to_commandlist(sprintf('#%d:CL_enable=0\r'     , this.motorID));
            % Correction of the sinus-commutierung: (Should be on!)
            motorControl.add_to_commandlist(sprintf('#%d:cal_elangle_enable=1\r'    , this.motorID));
            % Set the speed:
            % Divide by 0.9 because each (half)-step is equal to 0.9 degree
            % and multiply by the gear_ratio because the given speed value
            % is for the arm and not for the motor:
            stepspersecond      =   (this.sArgs_slayer.speed/0.9*this.sArgs_slayer.gear_ratio);
            motorControl.add_to_commandlist(sprintf('#%do=%.2f\r'           , this.motorID, stepspersecond));
            % Calculate the number of steps:
            % Divide by 0.9 because each (half)-step is equal to 0.9 degree
            % and multiply by the gear_ratio because the given angle value
            % is for the arm and not for the motor:
            steps               =   (angle/0.9*this.sArgs_slayer.gear_ratio);
            % Only absolut position mode!:
            motorControl.add_to_commandlist(sprintf('#%dp=2\r'              , this.motorID));
            % Set position (positive/negaive relative to the
            % reference:
            motorControl.add_to_commandlist(sprintf('#%ds=%.2f\r'           , this.motorID, steps));
            % Set acceleration ramp:
            % This formula is given by the programming handbook of Nanotec!
            value               =   round((3000/(this.sArgs_slayer.acceleration_ramp + 11.7))^2);
            motorControl.add_to_commandlist(sprintf('#%db=%.0f\r'           , this.motorID, value));
            % Brake ramp:
            motorControl.add_to_commandlist(sprintf('#%dB=0\r'              , this.motorID));
            % Zero menas equal to acceleration ramp!
            ret = true;
        end       
    end
    
end