matlab not recognizing variable defined in switch/case (Undefined function or variable)

19 visualizaciones (últimos 30 días)
Hi!
I have an issue with a variable defined within a if/else statement, which is triggerede by a case and used outside the if/else statement. Have have multiple other variables which are also defined/triggered in the cases and they work fine. I can tell that the case is recognized and the command is used but for some reason it seem like the action of defining the variable in that case is ignored..
This is my first question, so please let me know if the information in the snippets are insufficient and I will provide more info.
Thanks!
Codesnippet:
switch cmd_idx
case 1 % DETECT
disp('Detecting controllers ...')
probe_flags = 1;% + 4; % ENUMERATE_PROBE and ENUMERATE_NETWORK flags used
%enum_hints = 'addr=192.168.1.1,172.16.2.3';
enum_hints = ''; % no probing the network // BGTO
% enum_hints = 'addr='; % Use this hint string for broadcast enumeration
%device_names = ximc_enumerate_devices_wrap(probe_flags, enum_hints)
device_names = {'xi-emu:///C:\Users\Chr_0\AppData\Roaming\XILab.conf/V_1?serial=1', 'xi-emu:///C:\Users\Chr_0\AppData\Roaming\XILab.conf/V_2?serial=2', 'xi-emu:///C:\Users\Chr_0\AppData\Roaming\XILab.conf/V_3?serial=3'} %simuline
devices_count = size(device_names,2);
if devices_count == 0
error('No STANDA devices found. Try re-plugging the USB cable.')
end
% There must be 3 devices (axis controllers)
if devices_count < 3
error('Not all STANDA devices are detected. Try re-plugging the USB cable.')
end
disp('3 controllers found, as expected.')
device_names_standa=device_names;
% check that all dev. names are there. have been found
ids_found=zeros(1,3);
for dev_idx=1:3
if ~isempty(strfind(device_names_standa{dev_idx},'V_1?serial=1'))
ids_found(3)=1;
end
if ~isempty(strfind(device_names_standa{dev_idx},'V_2?serial=2'))
ids_found(1)=1;
end
if ~isempty(strfind(device_names_standa{dev_idx},'V_3?serial=3'))
ids_found(2)=1;
end
end
if sum(ids_found) < 3
ids_found
error('Not all devices were detected !')
end
ret_val=devices_count;
do_movement=0;
wait_for_movement_end=0;
case 2 % MOVE_X
standa_axis = 1;
do_movement = 1;
move_relative=0;
case 3 % MOVE_Y
standa_axis = 2;
do_movement = 1;
move_relative=0;
case 4 % MOVE_Z
standa_axis = 3;
do_movement = 1;
move_relative=0;
case 5 % MOVE_X_REL
standa_axis = 1;
do_movement = 1;
move_relative=1;
case 6 % MOVE_Y_REL
standa_axis = 2;
do_movement = 1;
move_relative=1;
case 7 % MOVE_Z_REL
standa_axis = 3;
do_movement = 1;
move_relative=1;
case 8 % GET_X
standa_axis = 1;
do_movement = 0;
wait_for_movement_end=0;
case 9 % GET_Y
standa_axis = 2;
do_movement = 0;
wait_for_movement_end=0;
case 10 % GET_Z
standa_axis = 3;
do_movement = 0;
wait_for_movement_end=0;
case 11 % MOVE_X_WAIT
standa_axis = 1;
do_movement = 0;
wait_for_movement_end=1;
case 12 % MOVE_Y_WAIT
standa_axis = 2;
do_movement = 0;
wait_for_movement_end=1;
case 13 % MOVE_Z_WAIT
standa_axis = 3;
do_movement = 0;
wait_for_movement_end=1;
case 14 % GO_HOME_X
standa_axis = 1;
do_movement = 0;
go_home = 1;
case 15 % GO_HOME_Y
standa_axis = 2;
do_movement = 0;
go_home = 1;
case 16 % GO_HOME_Z
standa_axis = 3;
do_movement = 0;
go_home = 1;
otherwise
error('Unsupported command code! Rewrite the function!')
end % to switch
% If this is an axis command, check the speed and open a device (axis controller)
if exist('standa_axis','var')
% Check the speed. Must be non-negative and below 5mm/s
if (speed_si > max_speed_si(standa_axis)) || (speed_si <0)
error(['The movement speed must be non-negative and up to ' num2str(max_speed_si(standa_axis)) ' ' units_si{standa_axis} '!'])
end
% open the device
device_name = device_names_standa_fixed{standa_axis};
device_id = calllib('libximc','open_device', device_name);
if device_id==-1
error('Failed to open device!')
end
end
% Calculate the next position, check if it is safe
if do_movement
% Calculate the next absolute position or relative step
tgt_pos = round(position*pos_scaling(standa_axis));
if standa_axis~=4 % for the translation stage, limit the motion within -50 mm to 50 mm
state_s = ximc_get_status(device_id);
if move_relative==1
end_pos=state_s.CurPosition + tgt_pos;
else
end_pos=tgt_pos + range_midpoint(standa_axis); % absolute input, add the 0-point offset
end
if (end_pos < movement_limits_hw(standa_axis,1))||(end_pos > movement_limits_hw(standa_axis,2))
disp('The target position is outside of the safe movement range !')
do_movement=0;
wait_for_movement_end=0;
end
end
end
if do_movement
% Calculate the HW speed from the user-supplied (or default) speed
if speed_si==0
speed_si=default_speed_si(standa_axis);
end
speed_hw=round(speed_si*speed_scaling_si2hw(standa_axis)); % only integer numbers!
uspeed_hw= 0;
% Check if it is the same as in HW, and set it if needed
try
[cur_speed, cur_uspeed] = ximc_get_speed(device_id);
catch
[cur_speed, cur_uspeed] = ximc_get_speed(device_id); % try second time, rotation controller fails here once after power-up
end
if (cur_speed~=speed_hw)||(cur_uspeed~=uspeed_hw)
% trying to enable more precise speed setting, DOES NOT WORK,
% microstep precision is not observed (seen in the rotation/translation times)
% if standa_axis~=2 % the rotation stage rotates left instead of right after these commands
% calb = struct();
% calb.A = 1; % arbitrary choice for example, set by user in real scenarios
% calb.MicrostepMode = 9; % == MICROSTEP_MODE_FRAC_256
% state_calb_s = ximc_get_status_calb(device_id, calb);
% %disp('Status calb:'); disp(state_calb_s);
%
% ximc_set_microstep_256(device_id); % set maximum precision (256 sub-steps)
% end
disp(['Setting speed to ' num2str(speed_si) ' ' units_si{standa_axis}])
ximc_set_speed(device_id, speed_hw, uspeed_hw);
else
disp(['The speed is ' num2str(speed_si) ' ' units_si{standa_axis}])
end
% Call the move command (absolute-MOVE or relative-MOVR)
if move_relative==1
result = calllib('libximc','command_movr', device_id, tgt_pos, 0 ); % microsteps (partial) = 0
else
tgt_pos=tgt_pos + range_midpoint(standa_axis); % for absolute target, add the 0 point step offset
result = calllib('libximc','command_move', device_id, tgt_pos, 0 ); % microsteps (partial) = 0
end
% tic % timing tests - T_start
if result ~= 0
disp(['Command MOVE failed with code ', num2str(result)]);
else
disp('Moving...')
end
end
if go_home
disp('Homing...')
result = calllib('libximc', 'command_home', device_id);
if result ~= 0
disp(['Command failed with code', num2str(result)]);
end
disp('Waiting for stop...');
result = calllib('libximc','command_wait_for_stop', device_id, 100);
if result ~= 0
disp(['Command failed with code', num2str(result)]);
end
end
The error:
Detecting controllers ...
device_names =
1×3 cell array
{'xi-emu:///C:\Users\Chr_0\Ap…'} {'xi-emu:///C:\Users\Chr_0\Ap…'} {'xi-emu:///C:\Users\Chr_0\Ap…'}
3 controllers found, as expected.
Unrecognized function or variable 'go_home'.
Error in standa_move (line 372)
if go_home
Error in motion_stages_init (line 14)
standa_move('detect');
Error in Stagetest (line 3)
motion_stages_init()

Respuesta aceptada

Stephen23
Stephen23 el 1 de Mzo. de 2021
The variable go_home is only defined in cases 14, 15, and 16. In all of the other cases it is undefined.
You can probably resolve this by defining it with a default value before the switch:
go_home = false;
switch ...
...
end
  4 comentarios
Egor Kovalev
Egor Kovalev el 18 de Nov. de 2022
I also work with standa device and i have a question. Do u have any materials of work on the controller 8SCMC5 in Matlab? Where can i find the description of the commands in test programm?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Kernel Creation from MATLAB Code en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by