Changeset 130

Show
Ignore:
Timestamp:
07/10/08 03:13:18 (5 years ago)
Author:
atorf
Message:

Adapted GUI_WatchAnalogSensor to work with new toolbox (and with ultrasonic)

Location:
branches/atorf/RWTHMindstormsNXT
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • branches/atorf/RWTHMindstormsNXT/COM_CloseNXT.m

    r128 r130  
    8787        end%try 
    8888         
    89         textOut(sprintf('Closing handle (USB) with MAC = %s (handle was %.1f minutes old)\n', h.NXTMAC, etime(clock, h.CreationTime) / 60)); 
    9089         
    9190        if h.ConnectionTypeValue == 1 % USB 
     91            textOut(sprintf('Closing handle (USB) with MAC = %s (handle was %.1f minutes old)\n', h.NXTMAC, etime(clock, h.CreationTime) / 60)); 
    9292            if h.OSValue == 1 % win 
    9393                USB_CloseHandle_Windows(h); 
     
    9696            end%if 
    9797        else % BT 
     98            textOut(sprintf('Closing handle (Bluetooth) on port %s (handle was %.1f minutes old)... ', h.ComPort, etime(clock, h.CreationTime) / 60)); 
    9899            BT_CloseHandle(h); 
    99100        end%if 
     
    171172%% Close given bluetooth connection 
    172173 
    173     textOut(sprintf('Closing Bluetooth connection on port %s ... ', h.ComPort)); 
     174    %textOut(sprintf('Closing Bluetooth connection on port %s ... ', h.ComPort)); 
    174175 
    175176 
     
    232233%% Close all serial ports 
    233234    if nargin < 1 
    234         textOut(sprintf('Closing all open serial ports... ')); 
     235        textOut(sprintf('Closing all serial ports... ')); 
    235236        if ispc % WINDOWS VERSION ------------- 
    236237            try 
     
    271272        ComPort = readFromIniFile(inisection, 'SerialPort', inifilename); 
    272273 
    273         textOut(sprintf('Closing open serial port %s... ', ComPort)); 
     274        textOut(sprintf('Closing serial port %s... ', ComPort)); 
    274275 
    275276        if ispc % WINDOWS VERSION ------------- 
  • branches/atorf/RWTHMindstormsNXT/COM_CollectPacket.m

    r128 r130  
    116116            textOut(sprintf('Packet contains error message %d: "%s"\n', statusbyte, errmsg)); 
    117117            DebugMode(oldstate); 
     118            % increase handle's error-count by 1 
     119            handle.TransmissionErrors(1); 
    118120        end%if 
    119121    end%if 
  • branches/atorf/RWTHMindstormsNXT/demos

    • Property svn:ignore set to
      *.asv
  • branches/atorf/RWTHMindstormsNXT/demos/GUI_WatchAnalogSensor.m

    r3 r130  
    2323% Edit the above text to modify the response to help GUI_WatchAnalogSensor 
    2424 
    25 % Last Modified by GUIDE v2.5 26-Jul-2007 14:15:55 
     25% Last Modified by GUIDE v2.5 10-Jul-2008 03:03:01 
    2626 
    2727% Begin initialization code - DO NOT EDIT 
     
    108108global DisableScreenOut; 
    109109global StopSensorWatchLoop; 
     110global USmode; 
    110111StopSensorWatchLoop = false; 
    111112 
    112113 
    113 port = str2num(get(handles.txtPort, 'String')); 
     114%tmp = get(handles.lstPort, 'String'); 
     115port = get(handles.lstPort, 'Value') - 1; % - 1 because 1st sensor is 0 
    114116tmp = get(handles.lstSensorType, 'String'); 
    115117sensortype = tmp{get(handles.lstSensorType, 'Value')}; 
    116 %sensortype  
    117  
    118 h = BT_GetDefaultHandle; 
     118 
     119PlotData = get(handles.chkPlotData, 'Value'); 
     120 
     121USmode = false; 
     122if strcmpi(sensortype, 'Ultrasonic') 
     123    USmode = true;   
     124end%if 
     125 
     126h = COM_GetDefaultNXT; 
    119127 
    120128OldScreenOutMode = DisableScreenOut; 
     
    124132 
    125133try 
    126     if NXT_SetInputMode(port, sensortype, 'PERIODCOUNTERMODE', 'reply', h) ~= 0 
    127         errordlg('Invalid sensor type, or the specified sensor is not connected to the specified port') 
    128         return 
     134    if USmode 
     135        CloseSensor(port); 
     136        OpenUltrasonic(port); 
     137    else 
     138        if NXT_SetInputMode(port, sensortype, 'PERIODCOUNTERMODE', 'reply', h) ~= 0 
     139            errordlg('Invalid sensor type, or the specified sensor is not connected to the specified port') 
     140            return 
     141        end%if 
    129142    end%if 
    130      
    131143catch 
    132144    errordlg('Invalid sensor type, or the specified sensor is not connected to the specified port') 
     
    135147 
    136148 
     149MaxYVal = 1024; 
     150if USmode; MaxYVal = 256; end%if 
    137151 
    138152axes(handles.axsSensorData); 
    139 axis([0 DisplayPeriod 0 1024]); 
     153axis([0 DisplayPeriod 0 MaxYVal]); 
    140154hold on 
    141155cla 
    142 plot([0 DisplayPeriod], [1024 * 0.45 1024 * 0.45], 'k--') 
    143 plot([0 DisplayPeriod], [1024 * 0.55 1024 * 0.55], 'k--') 
    144 cmap = colormap(jet(1024)); 
     156 
     157if ~USmode 
     158    plot([0 DisplayPeriod], [MaxYVal * 0.45 MaxYVal * 0.45], 'k--') 
     159    plot([0 DisplayPeriod], [MaxYVal * 0.55 MaxYVal * 0.55], 'k--') 
     160else 
     161    plot([0 DisplayPeriod], [150 150], 'k--') 
     162end%if 
     163cmap = colormap(jet(MaxYVal)); 
    145164 
    146165 
    147166tictic(1); 
    148167tictic(3); 
    149 % we've got this handy bytecounter only when using a serial port handle, 
    150 % i.e. under windows... 
    151 if ispc 
    152     StartBytesReceived = h.ValuesReceived; 
    153 else 
    154     StartBytesReceived = 0; 
    155 end%if 
     168 
     169StartBytesReceived = h.BytesReceived(); 
     170StartErrorCount = h.TransmissionErrors(); 
     171 
    156172ReplyCount = 0; 
    157173MaxLatency = 0; 
    158174LatencySum = 0; 
     175 
     176if h.ConnectionTypeValue == 1 
     177    LineWidth = 2; % USB 
     178else 
     179    LineWidth = 3; 
     180end%if 
     181     
     182 
    159183while(~StopSensorWatchLoop) 
    160      
     184 
    161185    if toctoc(1) >= 1 
    162186         
    163         if ispc % WINDOWS version for serial port handle only... 
    164             set(handles.txtReceivedBytes, 'String',  h.ValuesReceived - StartBytesReceived); 
    165             StartBytesReceived = h.ValuesReceived; 
    166         end%if 
    167             
     187        PlotData = get(handles.chkPlotData, 'Value'); 
     188         
     189        set(handles.txtReceivedBytes, 'String',  h.BytesReceived() - StartBytesReceived); 
     190        StartBytesReceived = h.BytesReceived(); 
     191 
    168192        set(handles.txtAvgLatency, 'String',  [num2str(ceil((LatencySum / ReplyCount) * 1000)) ' ms'] ); 
    169193        LatencySum = 0; 
     
    175199        ReplyCount = 0; 
    176200         
     201        set(handles.txtTransmissionErrors, 'String', h.TransmissionErrors() - StartErrorCount); 
     202        StartErrorCount = h.TransmissionErrors(); 
     203 
     204        % update current values so there's at least some kind of info 
     205        if ~USmode             
     206            set(handles.txtPeriodCount, 'String', out.ScaledVal); 
     207            set(handles.txtCurVal, 'String', out.NormalizedADVal); 
     208        else 
     209            set(handles.txtCurVal, 'String', out); 
     210        end%if 
     211 
     212         
     213        drawnow % need this for MATLAB to not freeze if drawing is disabled 
     214         
    177215        tictic(1); 
    178216    end%if 
     
    181219    tictic(2); 
    182220     
    183     out = NXT_GetInputValues(port, h); 
     221    if ~USmode 
     222        out = NXT_GetInputValues(port, h); 
     223    else 
     224        out = GetUltrasonic(port); 
     225        if out < 1 
     226            % work around for color map to not crash 
     227            out = 1; 
     228        end%if 
     229    end%if 
    184230    ReplyCount = ReplyCount + 1; 
    185231     
     
    187233    LatencySum = LatencySum + tmp; 
    188234    if tmp > MaxLatency, MaxLatency = tmp; end 
     235  
    189236    
    190      
    191     set(handles.txtPeriodCount, 'String', out.ScaledVal); 
    192     set(handles.txtCurVal, 'String', out.NormalizedADVal); 
    193     
    194     plot([toctoc(3) toctoc(3)], [0 out.NormalizedADVal], 'LineWidth', 3, 'Color', cmap(out.NormalizedADVal + 1, :)) 
    195      
    196      
    197237    if toctoc(3) > DisplayPeriod 
    198238        tictic(3); 
    199239        cla 
    200         plot([0 DisplayPeriod], [1024 * 0.45 1024 * 0.45], 'k--') 
    201         plot([0 DisplayPeriod], [1024 * 0.55 1024 * 0.55], 'k--') 
     240        if ~USmode 
     241            plot([0 DisplayPeriod], [MaxYVal * 0.45 MaxYVal * 0.45], 'k--') 
     242            plot([0 DisplayPeriod], [MaxYVal * 0.55 MaxYVal * 0.55], 'k--') 
     243        else 
     244            plot([0 DisplayPeriod], [150 150], 'k--') 
     245        end%if 
    202246    end%if 
    203      
    204      
    205     drawnow 
     247 
     248     
     249    if PlotData 
     250        if ~USmode             
     251            set(handles.txtPeriodCount, 'String', out.ScaledVal); 
     252            set(handles.txtCurVal, 'String', out.NormalizedADVal); 
     253            plot([toctoc(3) toctoc(3)], [0 out.NormalizedADVal], 'LineWidth', LineWidth, 'Color', cmap(out.NormalizedADVal + 1, :)) 
     254        else 
     255            set(handles.txtCurVal, 'String', out); 
     256            plot([toctoc(3) toctoc(3)], [0 out], 'LineWidth', LineWidth, 'Color', cmap(out + 1, :)) 
     257        end%if 
     258 
     259        drawnow 
     260    end%if 
    206261     
    207262     
    208263end%while 
     264 
     265try 
     266    CloseSensor(SENSOR_1); 
     267    CloseSensor(SENSOR_2); 
     268    CloseSensor(SENSOR_3); 
     269    CloseSensor(SENSOR_4); 
     270catch 
     271    % nothing 
     272end%try 
     273     
    209274DisableScreenOut = OldScreenOutMode; 
    210275 
     
    243308 
    244309 
    245 function edit3_Callback(hObject, eventdata, handles) 
    246 % hObject    handle to edit3 (see GCBO) 
    247 % eventdata  reserved - to be defined in a future version of MATLAB 
    248 % handles    structure with handles and user data (see GUIDATA) 
    249  
    250 % Hints: get(hObject,'String') returns contents of edit3 as text 
    251 %        str2double(get(hObject,'String')) returns contents of edit3 as a double 
    252  
    253310 
    254311% --- Executes during object creation, after setting all properties. 
     
    266323 
    267324 
    268 function edit4_Callback(hObject, eventdata, handles) 
    269 % hObject    handle to edit4 (see GCBO) 
    270 % eventdata  reserved - to be defined in a future version of MATLAB 
    271 % handles    structure with handles and user data (see GUIDATA) 
    272  
    273 % Hints: get(hObject,'String') returns contents of edit4 as text 
    274 %        str2double(get(hObject,'String')) returns contents of edit4 as a double 
    275  
    276  
    277325% --- Executes during object creation, after setting all properties. 
    278326function edit4_CreateFcn(hObject, eventdata, handles) 
     
    358406 
    359407 
    360 function edit8_Callback(hObject, eventdata, handles) 
    361 % hObject    handle to edit8 (see GCBO) 
    362 % eventdata  reserved - to be defined in a future version of MATLAB 
    363 % handles    structure with handles and user data (see GUIDATA) 
    364  
    365 % Hints: get(hObject,'String') returns contents of edit8 as text 
    366 %        str2double(get(hObject,'String')) returns contents of edit8 as a double 
    367  
    368  
    369 % --- Executes during object creation, after setting all properties. 
    370 function edit8_CreateFcn(hObject, eventdata, handles) 
    371 % hObject    handle to edit8 (see GCBO) 
    372 % eventdata  reserved - to be defined in a future version of MATLAB 
    373 % handles    empty - handles not created until after all CreateFcns called 
    374  
    375 % Hint: edit controls usually have a white background on Windows. 
    376 %       See ISPC and COMPUTER. 
    377 if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
    378     set(hObject,'BackgroundColor','white'); 
    379 end 
    380  
    381  
    382 % --- Executes on button press in checkbox1. 
    383 function checkbox1_Callback(hObject, eventdata, handles) 
    384 % hObject    handle to checkbox1 (see GCBO) 
    385 % eventdata  reserved - to be defined in a future version of MATLAB 
    386 % handles    structure with handles and user data (see GUIDATA) 
    387  
    388 % Hint: get(hObject,'Value') returns toggle state of checkbox1 
    389  
    390  
    391 % --- Executes on button press in checkbox2. 
    392 function checkbox2_Callback(hObject, eventdata, handles) 
    393 % hObject    handle to checkbox2 (see GCBO) 
    394 % eventdata  reserved - to be defined in a future version of MATLAB 
    395 % handles    structure with handles and user data (see GUIDATA) 
    396  
    397 % Hint: get(hObject,'Value') returns toggle state of checkbox2 
    398  
    399  
    400 % --- Executes on button press in checkbox3. 
    401 function checkbox3_Callback(hObject, eventdata, handles) 
    402 % hObject    handle to checkbox3 (see GCBO) 
    403 % eventdata  reserved - to be defined in a future version of MATLAB 
    404 % handles    structure with handles and user data (see GUIDATA) 
    405  
    406 % Hint: get(hObject,'Value') returns toggle state of checkbox3 
    407  
    408  
    409  
    410 function edit9_Callback(hObject, eventdata, handles) 
    411 % hObject    handle to edit9 (see GCBO) 
    412 % eventdata  reserved - to be defined in a future version of MATLAB 
    413 % handles    structure with handles and user data (see GUIDATA) 
    414  
    415 % Hints: get(hObject,'String') returns contents of edit9 as text 
    416 %        str2double(get(hObject,'String')) returns contents of edit9 as a double 
    417408 
    418409 
     
    554545 
    555546% Hint: get(hObject,'Value') returns toggle state of cmdResetCount 
    556  
    557 h = BT_GetDefaultHandle; 
    558 port = str2num(get(handles.txtPort, 'String')); 
    559 NXT_ResetInputScaledValue(port, h) 
     547global USmode 
     548 
     549if ~USmode 
     550    %h = COM_GetDefaultNXT; 
     551    port = get(handles.lstPort, 'Value') - 1; 
     552    NXT_ResetInputScaledValue(port) 
     553end%if 
    560554 
    561555 
     
    572566% if default handle set, ok, if not, create one... 
    573567try 
    574     h = BT_GetDefaultHandle(); 
     568    h = COM_GetDefaultNXT(); 
    575569catch 
    576     BT_CloseAllHandles('bluetooth.ini'); 
    577     BT_SetDefaultHandle(BT_OpenHandle('bluetooth.ini', 'check')) 
     570    COM_CloseNXT('all', 'bluetooth.ini'); 
     571    COM_SetDefaultNXT(COM_OpenNXT('bluetooth.ini', 'check')); 
    578572end%try 
    579573 
     
    602596 
    603597 
     598 
     599 
     600 
     601function txtTransmissionErrors_Callback(hObject, eventdata, handles) 
     602% hObject    handle to txtTransmissionErrors (see GCBO) 
     603% eventdata  reserved - to be defined in a future version of MATLAB 
     604% handles    structure with handles and user data (see GUIDATA) 
     605 
     606% Hints: get(hObject,'String') returns contents of txtTransmissionErrors as text 
     607%        str2double(get(hObject,'String')) returns contents of txtTransmissionErrors as a double 
     608 
     609 
     610% --- Executes during object creation, after setting all properties. 
     611function txtTransmissionErrors_CreateFcn(hObject, eventdata, handles) 
     612% hObject    handle to txtTransmissionErrors (see GCBO) 
     613% eventdata  reserved - to be defined in a future version of MATLAB 
     614% handles    empty - handles not created until after all CreateFcns called 
     615 
     616% Hint: edit controls usually have a white background on Windows. 
     617%       See ISPC and COMPUTER. 
     618if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
     619    set(hObject,'BackgroundColor','white'); 
     620end 
     621 
     622 
     623 
     624 
     625% --- Executes on selection change in lstPort. 
     626function lstPort_Callback(hObject, eventdata, handles) 
     627% hObject    handle to lstPort (see GCBO) 
     628% eventdata  reserved - to be defined in a future version of MATLAB 
     629% handles    structure with handles and user data (see GUIDATA) 
     630 
     631% Hints: contents = get(hObject,'String') returns lstPort contents as cell array 
     632%        contents{get(hObject,'Value')} returns selected item from lstPort 
     633 
     634 
     635% --- Executes during object creation, after setting all properties. 
     636function lstPort_CreateFcn(hObject, eventdata, handles) 
     637% hObject    handle to lstPort (see GCBO) 
     638% eventdata  reserved - to be defined in a future version of MATLAB 
     639% handles    empty - handles not created until after all CreateFcns called 
     640 
     641% Hint: popupmenu controls usually have a white background on Windows. 
     642%       See ISPC and COMPUTER. 
     643if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
     644    set(hObject,'BackgroundColor','white'); 
     645end 
     646 
     647 
     648 
     649 
     650% --- Executes when user attempts to close figure1. 
     651function figure1_CloseRequestFcn(hObject, eventdata, handles) 
     652% hObject    handle to figure1 (see GCBO) 
     653% eventdata  reserved - to be defined in a future version of MATLAB 
     654% handles    structure with handles and user data (see GUIDATA) 
     655 
     656% Hint: delete(hObject) closes the figure 
     657delete(hObject); 
     658 
     659 
     660 
     661 
     662% --- Executes on button press in chkPlotData. 
     663function chkPlotData_Callback(hObject, eventdata, handles) 
     664% hObject    handle to chkPlotData (see GCBO) 
     665% eventdata  reserved - to be defined in a future version of MATLAB 
     666% handles    structure with handles and user data (see GUIDATA) 
     667 
     668% Hint: get(hObject,'Value') returns toggle state of chkPlotData 
     669 
     670