summaryrefslogtreecommitdiff
path: root/user-code/dcmotor/dcmotor-python.tex
blob: 7f454bb4d80ea617a8a7206281b0c68030c35bf1 (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
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
\section{Controlling DC motor from Python}
\subsection{Controlling the DC motor}
In this section, we discuss how to carry out the experiments of the
previous section from Python.  We will list the same three experiments,
in the same order. As mentioned earlier, the Shield must be removed from
the \arduino\ and the \arduino\ needs to be connected to the computer
with a USB cable, as shown in \figref{arduino}. The reader should go through the instructions given in
\secref{sec:python-start} before getting started.

\paragraph{Note:} The readers are advised to affix a small
(very lightweight) piece of paper at the tip of the shaft of the DC motor.
That will help them observe the direction of rotation
of the DC motor while running the experiments.

\begin{enumerate}
  \item In the first experiment, we will learn how to drive the DC motor
        from Python. The code for this experiment is
        given in  \pyref{py:dcmotor-clock}. As explained earlier in \secref{sec:light-py}, we begin with
        importing necessary modules followed by setting up the serial port.
        Next, the code has a command of the following form:
        \begin{lstlisting}[style=nonumbers]
              cmd_dcmotor_setup(1, H-Bridge type, Motor number, PWM pin 1, PWM pin 2)
        \end{lstlisting}
        As mentioned earlier, this chapter makes use of an H-Bridge circuit which
        allows direction of the current passing through the DC motor to be changed.
        We are using L293D as an H-Bridge circuit in this book. Thus, we will pass the value 3 for
        H-Bridge type. Python-Arduino toolbox, as explained in \secref{sec:python-toolbox},
        supports three types of H-Bridge circuit. \tabref{table:convention}
        provides the values to be passed for different H-Bridge circuits.
        Next argument in the command given above is Motor number. Here, we pass the value 1.
        Finally, we provide the PWM pins to which the DC motor is connected. As
        shown in \figref{fig:dcmotorconn}, pins 9 and 10 are connected to the
        input of the breakout board. As a result, the command {\tt cmd\_dcmotor\_setup} becomes
        \lstinputlisting[firstline=27,lastline=27]
        {\LocDCMpycode/dcmotor-clock.py}

        The next line of \pyref{py:dcmotor-clock} is of the following form:
        \begin{lstlisting}[style=nonumbers]
          cmd_dcmotor_run(1, Motor number, [sign] PWM value)
        \end{lstlisting}
        Here, we will pass the value 1 in Motor number.  As mentioned earlier,
        for each of the PWM pins on \arduino\ board, the input can come from 8 bits.
        Thus, these pins can supply values between $- 255$ and $+ 255$. Positive values correspond to clockwise
        rotation while negative values correspond to anti-clockwise rotation. Based on the PWM value and polarity,
        corresponding analog voltage is generated.
        We put a PWM value of 100 to make the DC motor run at an intermediate speed.
        As a result, the command {\tt cmd\_dcmotor\_run} becomes
        \lstinputlisting[firstline=28,lastline=28]
        {\LocDCMpycode/dcmotor-clock.py}

        The above-mentioned command does not say for how long the motor should run.  This is taken care of
        by the {\tt sleep} command, as given below:
        \lstinputlisting[firstline=29,lastline=29]{\LocDCMpycode/dcmotor-clock.py}
        With this, the DC motor will run for or 3 seconds. At last,
        we release the DC motor, as shown below:
        \lstinputlisting[firstline=30,lastline=30]{\LocDCMpycode/dcmotor-clock.py}
        With the execution of this command, the PWM functionality on the \arduino\ pins
        is ceased.  This has the motor number as an input
        parameter. At last, we close the serial port.


        \paragraph{Note:} If the sleep command (at line 29 of \pyref{py:dcmotor-clock})
        were not present, the DC motor will not even run: soon after putting the value 100,
        the DC motor would be released, leaving no time in between.  On the other hand, if
        the DC motor is not released (\ie\ line number 30 of \pyref{py:dcmotor-clock} being commented),
        the DC motor will go on rotating. That's why, it may be inferred that
        line number 30 of \pyref{py:dcmotor-clock} is mandatory
        for every program. We encourage the readers to run  \pyref{py:dcmotor-clock} by commenting
        any one or two of the lines numbered 29 and 30.  Go ahead and do it - you will not break
        anything.  At the most, you may have to unplug the USB cable connected to \arduino\ and
        restart the whole thing from the beginning.

  \item It is easy to make the DC motor run in the reverse direction by
        changing the sign of PWM value being written.  This is done in
        \pyref{py:dcmotor-both}.  In this code, we make the DC motor
        run in one direction for 3 seconds and then make it rotate in the
        reverse direction for 2 seconds.  The rotation in reverse direction
        is achieved by putting $- 100$ in the command {\tt cmd\_dcmotor\_run},
        as shown below:
        \lstinputlisting[firstline=28,lastline=28]
        {\LocDCMpycode/dcmotor-both.py}
        % This makes the green LED light up as well, recall the discussion in \secref{sec:led-pril}.  After
        After adding a {\tt sleep} of 2 seconds, we release the motor by issuing
        the command {\tt cmd\_dcmotor\_release}, followed by closing the serial port:
        \lstinputlisting[firstline=30,lastline=30]
        {\LocDCMpycode/dcmotor-both.py}
        With this, the motor comes to a halt.
        % This turns the green LED off as well.

  \item Next, we make the DC motor run in forward and reverse
        directions, in a loop.  This is done through
        \pyref{py:dcmotor-loop}.  We first write PWM $+100$ for 3
        seconds.  After that, halt the motor for 2 seconds by writing zero PWM value.
        Next, make the motor rotate in the reverse direction by writing PWM $-100$ for two seconds.
        Next, we make the motor stop for one second. This procedure is put in a {\tt for} loop which runs for 4 iterations.
        At last, we release the motor by issuing the command {\tt cmd\_dcmotor\_release}, followed by closing the serial port

\end{enumerate}

% Initialization: In all the experiments in this section, we need to
% initialize the DC motor first, using a Python command of the following
% type:
% \begin{lstlisting}[style=nonumbers]
%   cmd_dcmotor_setup(1,H-Bridge type,Motor number,PWM pin 1,PWM pin 2)
% \end{lstlisting}

% As mentioned earlier, number 1 in the above list refers to the Arduino Uno board.
% We now discuss how to choose values for the other parameters in this command. As
% mentioned above, Popular numbering convention for different types of DC motor breakout
% boards is given in Table 7.1. For example, L293D is type 3. Next, we have to provide
% the motor number we want to control. In our case, it is number 1. Finally we want
% to provide PWM pin numbers on Arduino Uno. As mentioned earlier, we are using
% pins 10 and 11. In Table 7.2, we list the choices that we have made. Inserting these
% parameter values in the above shown Python command, we get the following command

% self.obj\_arduino.cmd\_dcmotor\_setup(1,3,1,self.pin1,self.pin2)

% To rotate the motor,we have to provide motor number
% and the PWM value. The Python command is of the form,

% cmd\_dcmotor\_run ( 1 , Motor number , ( sign ) (PWM value ) )

% The PWM values to be given are as same as explained in Scilab code before.

% To run the motor for specified amount of time,we will use sleep command

% sleep(3) //sleep for 3 seconds

% To release the dc motor, we will use the following command

% cmd\_dcmotor\_release(1,1) //Motor 1 is release

% To run motor in loop, for loop is used in Python code 7.3.

\subsection{Python Code}
\label{sec:dcmotor-python-code}
\addtocontents{pyd}{\protect\addvspace{\codclr}}

\begin{pycode}
  \pcaption{Rotating the DC motor}
  {Rotating the DC motor.  Available at
    \LocDCMpybrief{dcmotor-clock.py}.}
  \label{py:dcmotor-clock}
  \lstinputlisting{\LocDCMpycode/dcmotor-clock.py}
\end{pycode}

\begin{pycode}
  \pcaption{Rotating the DC motor in both directions}
  {Rotating DC motor in both directions.  Available at
    \LocDCMpybrief{dcmotor-both.py}.}
  \label{py:dcmotor-both}
  \lstinputlisting{\LocDCMpycode/dcmotor-both.py}
\end{pycode}

\begin{pycode}
  \pcaption{Rotating the DC motor in both directions in a loop}{Rotating
    the DC motor in both directions in a loop.
    Available at
    \LocDCMpybrief{dcmotor-loop.py}.}
  \label{py:dcmotor-loop}
  \lstinputlisting{\LocDCMpycode/dcmotor-loop.py}
\end{pycode}