summaryrefslogtreecommitdiffstats
path: root/Kod/bilbana/IOFunctions/set_car_speed.m
blob: 3aab1db21fc9e17e5529e25bd61956293142b66a (plain) (blame)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function [] = set_car_speed(track,speed)
%SET_CAR_SPEED Sets the specified car (track) to a specified speed.
%   Changes the duty cycle of counter 0 and 4 to change speed of car on
%   track 1 and track 2 respectively. Valid values of the duty cycle are
%   between 0 and 1, the input speed of the cars are defined as percent.
%
%   Tobias Lindell 2013-02-12.

global mytaskh
global lib

switch nargin
    case 2
        % Check if _any_ counters been initilized, stop program if not
        if isempty(mytaskh)
            disp(['User needs to initialize counters for track ',num2str(track),' before setting car speed!'])
            clearvars -global mytaskh lib
            return
        end
        
        % Setting duty cycle, with limits
        speed = min(99, speed);
        speed = max(0.1,speed);
        duty_cycle = speed / 100;
        
        switch track
            case 1
                % Check if track 1 counter has been initialized, stop program if not
                if isfield(mytaskh,'ctr_0')
                    % Stop task (necessary to change duty cycle)
                    calllib(lib,'DAQmxStopTask',mytaskh.ctr_0);
                    % Set new duty cycle
                    calllib(lib,'DAQmxSetCOPulseDutyCyc',mytaskh.ctr_0,'Dev1/ctr0',duty_cycle);
                    % Restart task
                    calllib(lib,'DAQmxStartTask',mytaskh.ctr_0);
                else
                    disp(['User needs to initialize counters for track ',num2str(track),' before setting car speed!'])
                    return
                end
            case 2
                if isfield(mytaskh,'ctr_4')
                    calllib(lib,'DAQmxStopTask',mytaskh.ctr_4);
                    calllib(lib,'DAQmxSetCOPulseDutyCyc',mytaskh.ctr_4,'Dev1/ctr4',duty_cycle);
                    calllib(lib,'DAQmxStartTask',mytaskh.ctr_4);
                else
                    disp(['User needs to initialize counters for track ',num2str(track),' before setting car speed!'])
                    return
                end
            otherwise
                disp('Wrong track number sent to set_car_speed!')
                return
        end
    otherwise
        disp('Wrong number of arguments sent to set_car_speed(track,speed)! Should be 2!')
end
end