summaryrefslogtreecommitdiff
path: root/Origin/tools/scilab/linux/macros/ARDUINO_DCMOTOR.sci
blob: 054b4ef98e50dd214b2cc1d898127188eeb38c09 (plain)
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
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
//
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
//
//

function [x, y, typ]=ARDUINO_DCMOTOR(job, arg1, arg2)
    x=[];
    y=[];
    typ=[];
    select job
    case 'plot' then
        // deprecated
    case 'getinputs' then
        // deprecater
    case 'getoutputs' then
        // deprecated
    case 'getorigin' then
        // deprecated
    case 'set' then
        x=arg1;
        graphics=arg1.graphics;
        exprs=graphics.exprs
        model=arg1.model;

        while %t do
            [ok1,type_shield,num_arduino,exprs1]=scicos_getvalue('Arduino DC MOTOR parameters',..
            [gettext('Type of Shield (1 : Adafruit, 2: MotorShield Rev3, 3: PMODHB5 (L298), 4: L293 (2 PWM))');...
             gettext('Arduino card number')],list('vec',1,'vec',1), exprs(1:2))
            mess=[];

            if ~ok1 then break; end //cancel

            if num_arduino<>1 then
                mess=[mess ;gettext("Only card 1 can be used with this toolbox version ")];
                ok1=%f;
            end
            if type_shield~=1 & type_shield~=2 & type_shield~=3 & type_shield~=4
                mess=[mess ;_("Type shield must be 1, 2, 3 or 4")]
                ok1 = %f
            end

            if ok1 then
                num_pin_1=0; num_pin_2=0;  
                if type_shield==1 then //get number of motor (between 1 et 4)
//                    if evstr(exprs(3))>2 then
//                        exprs(3)=string(1)
//                    end
                    [ok,motor_number,exprs2]=scicos_getvalue('Motorshield Rev 3 parameters',..
                    [gettext('Number of DC motor : 1 or 2')],list('vec',1), exprs(5))
                    if ~ok then break;  end //cancel
                    if (motor_number < 1 | motor_number>2)
                        mess=[mess ;_("Motor number must be 1 or 2 for Motorshield Rev 3 card")]
                        ok = %f
                    end      
                    if motor_number==1 then
                        num_pin_1=12;
                        num_pin_2=3;
                    else
                        num_pin_1=13;
                        num_pin_2=11;
                    end
                elseif  type_shield==2 then
                    [ok,num_pin_1,num_pin_2,motor_number,exprs2]=scicos_getvalue('PMODHB5 or L298 driver parameters',..
                    [gettext('Direction pin ');gettext('Enable (speed) Pin');gettext('Motor number (between 1 and 4)')],list('vec',1,'vec',1,'vec',1), exprs(3:5))
                    if ~ok then break;  end //cancel  
                    if (motor_number < 1 | motor_number>4)
                        mess=[mess ;_("Motor number must be between 1 and 4")]
                        ok = %f
                    end                      
                elseif  type_shield==3 then
                    [ok,num_pin_1,num_pin_2,motor_number,exprs2]=scicos_getvalue('L293 driver parameters (control 2 PWM)',..
                    [gettext('PWM 1 Pin');gettext('PWM 2 Pin');gettext('Motor number (between 1 and 3)')],list('vec',1,'vec',1,'vec',1), exprs(3:5))
                    if ~ok then break;  end //cancel  
                    if (motor_number < 1 | motor_number>4)
                        mess=[mess ;_("Motor number must be between 1 and 4")]
                        ok = %f
                    end   
                end

                if ok  then
                    exprs2=string([num_pin_1;num_pin_2;motor_number])
                    // Everything's ok
                    model.rpar=[type_shield;num_arduino;num_pin_1;num_pin_2;motor_number];
                    graphics.exprs = string(model.rpar)

                    x.model=model;
                    x.graphics = graphics;
                    break
                else
                    message(mess);
                end 

            else 
                message(mess);
            end


        end


    case 'define' then
        model=scicos_model();
        model.sim=list("ARDUINO_DCMOTOR_sim", 5)
        model.blocktype='d';
        model.dep_ut=[%f %f];
        model.in=[1];
        model.evtin=[1]
        model.evtout=[1]
        model.firing=[0;-1]
        num_arduino=1; type_shield=1; num_pin_1=13; num_pin_2=11; motor_number=1;
        model.rpar=[type_shield;num_arduino;num_pin_1;num_pin_2;motor_number]; //Default Pin number and Arduino card number
        x=standard_define([2 2],model,[],[]);
        x.graphics.in_implicit=['E'];
        x.graphics.style=["blockWithLabel;verticalLabelPosition=bottom;verticalAlign=top;spacing=-2;displayedLabel=Typeshield %s<br>on card %s"]
        x.graphics.exprs=string([type_shield;num_arduino;num_pin_1;num_pin_2;motor_number]);
    end
endfunction