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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
|
\chapter{Interfacing a DC Motor}
\thispagestyle{empty}
\label{dcmotor}
\newcommand{\LocDCMfig}{\Origin/user-code/dcmotor/figures}
\newcommand{\LocDCMscicode}{\Origin/user-code/dcmotor/scilab}
\newcommand{\LocDCMscibrief}[1]{{\tt \seqsplit{
Origin/user-code/dcmotor/scilab/#1}},
see \fnrefp{fn:file-loc}}
\newcommand{\LocDCMardcode}{\Origin/user-code/dcmotor/arduino}
\newcommand{\LocDCMardbrief}[1]{{\tt \seqsplit{
Origin/user-code/dcmotor/arduino/#1}},
see \fnrefp{fn:file-loc}}
%%%%%%%%%%%%%python starts
\newcommand{\LocDCMpycode}{\Origin/user-code/dcmotor/python}
\newcommand{\LocDCMpybrief}[1]{{\tt \seqsplit{
Origin/user-code/dcmotor/python/#1}},
see \fnrefp{fn:file-loc}}
%%%%%%%%%%%%%python ends
%%%%%%%%%%%%%julia starts
\newcommand{\LocDCMjuliacode}{\Origin/user-code/dcmotor/julia}
\newcommand{\LocDCMjuliabrief}[1]{{\tt \seqsplit{
Origin/user-code/dcmotor/julia/#1}},
see \fnrefp{fn:file-loc}}
%%%%%%%%%%%%%julia ends
%%%%%%OpenModelica Starts
\newcommand{\LocDCMOpenModelicacode}{\Origin/user-code/dcmotor/OpenModelica} %added for OpenModelica
\newcommand{\LocDCMOpenModelicabrief}[1]{{\tt \seqsplit{%
Origin/user-code/led/OpenModelica/#1}}, see \fnrefp{fn:file-loc}} % added for OpenModelica
%%%%%OpenModelcia Ends
Motors are widely used in commercial applications.
DC motor converts electric power obtained from direct current to
mechanical motion. This chapter describes the experiments to
control DC motor with \arduino\ board. We will observe the
direction of motion of the DC motor being changed
using the microcontroller on \arduino\ board.
Control instruction will be sent to \arduino\ using Arduino IDE,
Scilab scripts, Scilab Xcos, Python, Julia, and OpenModelica.
The experiments provided in this chapter don't require the shield.
Therefore, the readers must remove the shield from the \arduino\ before
moving further in this chapter. Before removing the shield,
the readers are advised to detach \arduino\ from the computer.
\section{Preliminaries}
In order to change its direction, the sign of the voltage applied to
the DC motor is changed. For that, one needs to use external hardware
called \index{H-Bridge circuit DC motor}%
H-Bridge circuit DC motor with \arduino. \index{H-Bridge}%
H-Bridge allows direction of the current passing through the DC motor
to be changed. It avoids the sudden short that may happen while
changing the direction of current passing through the motor. It is
one of the essential circuits for the smooth operation of a DC
motor. There are many manufacturers of H-bridge circuit viz.
\index{L293D,L298}%
L293D, L298, etc. Often they provide small \index{PCB breakout
board}%
PCB breakout boards. These modules also provide an extra supply that
is needed to drive the DC motor. \figref{fig:motordriverboard} shows
the diagram of a typical breakout board containing IC L293D, which will
be used in this book. One may note that the toolboxes presented in this book supports three types of H-Bridge circuit. \tabref{table:convention} provides the values to be passed for different H-Bridge circuits. \par
\begin{figure}
\centering
\includegraphics[width=\lgfig]{\LocDCMfig/dcmotor_board.png}
\caption{L293D motor driver board}
\label{fig:motordriverboard}
\end{figure}
\begin{table}
\centering
\caption{Values in the \scilab\ command for different H-Bridge circuits}
\label{table:convention}
\begin{tabular}{llc}\hline
Type of H-Bridge circuit & Value \\ \hline
MotorShield Rev3 & 1 \\ \hline
PMODHB5/L298 & 2 \\ \hline
L293D & 3 \\ \hline
\end{tabular}
\end{table}
Input from \arduino\ to H-bridge IC is in \index{pulse width
modulation, PWM}%
pulse width modulation (PWM) form. PWM is a technique to generate
analog voltages using digital pins. We know that \arduino\ has digital
input-output pins. When these pins are configured as an output, they
provide High (5V) or Low (0V) voltage. With PWM technique, these pins
are switched on and off iteratively and fast enough so that the
voltage is averaged out to some analog value in between 0-5V. This
analog value depends on ''switch-on'' time and ''switch-off''
time. For example, if both ''switch-on'' time and ''switch-off'' time
are equal, average voltage on PWM pin will be 2.5V. To enable fast
switching of digital pin, a special hardware is provided in
microcontrollers. PWM is considered as an important resource of
the microcontroller system. \arduino\ board has 6 PWM pins (3, 5, 6, 9, 10, 11) \cite{arduino-pwm}.
On an original \arduino\ board, these pins are marked with a tilde sign next to the pin number,
as shown in \figref{fig:uno-pwm}. For each of these pins, the input can come from 8 bits.
Thus we can generate $2^8 = 256$ different analog values (from 0 to 255)
in between 0-5V with these pins.
\begin{figure}
\centering
\includegraphics[width=\lgfig]{\LocDCMfig/uno-pwm.jpg}
\caption{PWM pins on an \arduino\ board}
\label{fig:uno-pwm}
\end{figure}
We now carry out the following connections:
\begin{enumerate}
\item Connect input of L293D (M1\_IN) pins to two of the PWM pins
available on \arduino. We have used pins 9 and 10 of the \arduino\
board.
\item Connect the output of the L293D (M1\_OUT) pins directly to the 2
wires of the DC motor. As the direction is changed during the
operation, the polarity of the connection does not matter.
\item Connect supply (Vcc) and ground (Gnd) pins of L293D to 5V and
Gnd pins of the \arduino\ board, respectively.
\end{enumerate}
A schematic of these connections is given in
\figref{fig:dcm-schematic}. The actual connections can be seen in
\figref{fig:dcmotorconn}.
\begin{figure}
\centering
\includegraphics[width=\smfig]{\LocDCMfig/schematic.png}
\caption{A schematic of DC motor connections}
\label{fig:dcm-schematic}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=\lgfig]{\LocDCMfig/dc_motor_description.jpg}
\caption{How to connect the DC motor to the \arduino\ board}
\label{fig:dcmotorconn}
\end{figure}
\section{Controlling the DC motor from Arduino}
\subsection{Controlling the DC motor}
\label{sec:dcm-ard}
In this section, we will describe some experiments that will help
drive the DC motor from the Arduino IDE. We will also give the
necessary code. We will present three experiments in this section.
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:ard-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 We now demonstrate how to drive the DC motor from the Arduino
IDE. \ardref{ard:dcmotor-clock} has the required code for this. It
starts the serial port at a baud rate of 115200. Pins 9 and 10 are
declared as output pins and hence values can be written on to them. The following
lines are used to declare these two pins as output pins:
\lstinputlisting[firstline=3,lastline=4]
{\LocDCMardcode/dcmotor-clock/dcmotor-clock.ino}
Next, we write PWM 100 on pin 9 and PWM 0 on pin 10, as shown below:
\lstinputlisting[firstline=5,lastline=6]
{\LocDCMardcode/dcmotor-clock/dcmotor-clock.ino}
Recall from \figref{fig:dcmotorconn} that pins 9 and 10 are connected to the
input of the breakout board, which in turn makes the DC motor run at
an intermediate speed. Some of the breakout boards may not have enough current driving
capability and hence tend to heat up. To avoid these difficulties,
the DC motor is run at an intermediate value of PWM 100. Remember, we can
increase this value upto 255.
% As mentioned in \secref{sec:led-pril}, a high on pin 9 also makes the blue LED turn on.
The line containing {\tt delay} makes the previous command execute
for 3 seconds. As a result, the DC motor continues to rotate for 3
seconds. After this, we put a 0 in both pins 9 and 10, as shown below:
\lstinputlisting[firstline=8,lastline=9]
{\LocDCMardcode/dcmotor-clock/dcmotor-clock.ino}
With this, the motor comes to a halt.
% The blue LED is also turned off.
\item It is easy to make the DC motor run in the reverse direction by
interchanging the values put on pins 9 and 10. This is done in
\ardref{ard: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 pin 10, as shown below:
\lstinputlisting[firstline=8,lastline=9]
{\LocDCMardcode/dcmotor-both/dcmotor-both.ino}
% This makes the green LED light up as well, recall the discussion in \secref{sec:led-pril}. After
Next, we release the motor by writing 0 in both pins 9 and 10, as shown below:
\lstinputlisting[firstline=11,lastline=12]
{\LocDCMardcode/dcmotor-both/dcmotor-both.ino}
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
\ardref{ard:dcmotor-loop}. We first write PWM 100 in pin 9 for 3
seconds. After that, make the motor stop for 2 seconds. Finally,
make the motor rotate in the reverse direction by writing PWM 100 in pin 10
for two seconds. Finally, we make the motor stop for one second.
The entire thing is put in a {\tt for} loop which runs for 5 iterations.
\end{enumerate}
\begin{exercise}
Carry out the following exercise:
\begin{enumerate}
\item Try out some of the suggestions given above, \ie\ removing
certain numbers from the code
\item See if the DC motor runs if you put 1 instead of 100 as the PWM
value. Explain why it does not run. Find out the smallest value at
which it will start running.
\end{enumerate}
\end{exercise}
\subsection{Arduino Code}
\label{sec:dcmotor-arduino-code}
\lstset{style=mystyle}
\addtocontents{ard}{\protect\addvspace{\codclr}}
\begin{ardcode}
\acaption{Rotating the DC motor}
{Rotating the DC motor. Available at
\LocDCMardbrief{dcmotor-clock/dcmotor-clock.ino}.}
\label{ard:dcmotor-clock}
\lstinputlisting{\LocDCMardcode/dcmotor-clock/dcmotor-clock.ino}
\end{ardcode}
\begin{ardcode}
\acaption{Rotating the DC motor in both directions}{Rotating the DC
motor in both directions.
Available at
\LocDCMardbrief{dcmotor-both/dcmotor-both.ino}.}
\label{ard:dcmotor-both}
\lstinputlisting{\LocDCMardcode/dcmotor-both/dcmotor-both.ino}
\end{ardcode}
\begin{ardcode}
\acaption{Rotating the DC motor in both directions in a loop}{Rotating
the DC motor in both directions in a loop.
Available at
\LocDCMardbrief{dcmotor-loop/dcmotor-loop.ino}.}
\label{ard:dcmotor-loop}
\lstinputlisting{\LocDCMardcode/dcmotor-loop/dcmotor-loop.ino}
\end{ardcode}
\section{Controlling the DC motor from Scilab}
\label{sec:dcm-sci}
\subsection{Controlling the DC motor}
In this section, we discuss how to carry out the experiments of the
previous section from Scilab. 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:sci-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 Scilab. The code for this experiment is
given in \sciref{sci:dcmotor-clock}. As explained earlier in \secref{sec:light-sci},
we begin with serial port initialization.
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. The Scilab-Arduino toolbox, as explained in \secref{sec:sci-ard-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=2,lastline=2]
{\LocDCMscicode/dcmotor-clock.sce}
% \begin{table}
% \centering
% \caption{Values in the \scilab\ command for different H-Bridge circuits}
% \label{table:convention}
% \begin{tabular}{llc}\hline
% Type of H-Bridge circuit & Value \\ \hline
% MotorShield Rev3 & 1 \\ \hline
% PMODHB5/L298 & 2 \\ \hline
% L293D & 3 \\ \hline
% \end{tabular}
% \end{table}
The next line of \sciref{sci: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=3,lastline=3]
{\LocDCMscicode/dcmotor-clock.sce}
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=4,lastline=4]{\LocDCMscicode/dcmotor-clock.sce}
With this, the DC motor will run for 3000 milliseconds or 3 seconds. At last,
we release the DC motor, as shown below:
\lstinputlisting[firstline=5,lastline=5]{\LocDCMscicode/dcmotor-clock.sce}
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 4 of \sciref{sci: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 5 of \sciref{sci:dcmotor-clock} being commented),
the DC motor will go on rotating. That's why, it may be inferred that
line number 5 of \sciref{sci:dcmotor-clock} is mandatory
for every program. We encourage the readers to run \sciref{sci:dcmotor-clock} by commenting
any one or two of the lines numbered 4, 5 or 6. 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
\sciref{sci: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=5,lastline=5]
{\LocDCMscicode/dcmotor-both.sce}
% 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=7,lastline=8]
{\LocDCMscicode/dcmotor-both.sce}
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
\sciref{sci: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}
% \subsection{Initialization}
% In all the experiments in this section, we need to initialize the DC
% motor first, using a \scilab\ 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\ board. We now discuss how to choose values for the other
% parameters in this command. As mentioned above, there are many
% H-bridge IC manufacturers. The inbuilt function {\tt
% cmd\_dcmotor\_setup} can work with most of the widely used ICs,
% through a suitable input parameter. Users have to provide the type
% number of the breakout board they have. Popular numbering convention
% for different types of DC motor breakout boards is given in
% \tabref{table:convention}. 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.
% As mentioned earlier, we are using pins 10 and 11. In
% \tabref{tab:dcmotor-init}, we list the choices that we have made.
% Inserting these parameter values in the above shown \scilab\ command,
% we get the following command \\
% \lstinputlisting[firstline=2,lastline=2]
% {\LocDCMscicode/dcmotor-clock.sce}
% which is line number 2 in \sciref{sci:dcmotor-clock}. We have already
% seen
% the first two lines of this code and hence will not explain here. We
% will add more lines to this code as we go along.
% % \begin{table}
% % \centering
% % \caption{Parameters for DC motor initialization}
% % \label{tab:dcmotor-init}
% % \begin{tabular}{|l|c|} \hline
% % Parameter & Value \\ \hline
% % H-Bridge type & 3 \\
% % Motor number & 1 \\
% % PWM 1 pin & 9 \\
% % PWM 2 pin & 10 \\ \hline
% % \end{tabular}
% % \end{table}
% \subsection{Rotation for a specified time}
% \label{sec:dc-both}
% We will now explain how to run the DC motor. We have to provide motor
% number and the PWM value. The \scilab\ command is of the form,
% \begin{lstlisting}[style=nonumbers]
% cmd_dcmotor_run(1,Motor number,(sign)(PWM value))
% \end{lstlisting}
% Motor number is 1, as mentioned earlier. Considering that the input
% to a PWM pin comes from two 8 digital pins, we can provide 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 to
% run at an intermediate speed. Assigning these values, we get the
% following command:
% \lstinputlisting[firstline=3,lastline=3]{\LocDCMscicode/dcmotor-clock.sce}
% This is line number 3 in \sciref{sci:dcmotor-clock}. This command
% does not say for how long the motor should run. This is taken care of
% by the {\tt sleep} statement. The units of sleep are milliseconds.
% For example, line number 4 of \sciref{sci:dcmotor-clock}, given next,
% says that \scilab\ should go to sleep for three seconds.
% \lstinputlisting[firstline=4,lastline=4]{\LocDCMscicode/dcmotor-clock.sce}
% Line number 5 of \sciref{sci:dcmotor-clock}, shown below, is mandatory
% for every program.
% \lstinputlisting[firstline=5,lastline=5]{\LocDCMscicode/dcmotor-clock.sce}
% It releases the DC motor. The PWM functionality on the \arduino\ pins
% is ceased using this command. This has the motor number as an input
% parameter.
% If the sleep command discussed above 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. If on the other hand,
% the DC motor is not released (\ie\ line number 6 being absent), the DC
% motor will go on rotating. Line number 6 of \sciref{sci:dcmotor-clock}
% closes the serial port.
% We encourage you to run the above code without either line numbers 4,
% 5 or 6 or all combinations. Go ahead and do it - you will not break
% anything. At the most, you may have to unplug the USB cable and
% restart the whole thing from the beginning.
% \sciref{sci:dcmotor-clock} can easily be extended to make the DC motor
% run in both directions. The modified code is available in
% \sciref{sci:dcmotor-both}.
\begin{exercise}
Carry out the following exercise:
\begin{enumerate}
\item Try out some of the suggestions given above, \ie\ removing
certain numbers from the code.
\item See if the DC motor runs if you put 1 instead of 100 as the PWM
value. Explain why it does not run. Find out the smallest value at
which it will start running.
\end{enumerate}
\end{exercise}
% \subsection{Using the capabilities of \scilab}
% Given that Scilab has a powerful programming syntax, a lot of
% different experiments can be tried out. We illustrate a few in this
% section. We begin with a {\tt for loop}.
% In the previous section, we presented \sciref{sci:dcmotor-both}, where
% we made the motor run in both directions, five seconds in the
% clockwise direction and two seconds in reverse. This code can be
% embedded in a loop and the motor be made to repeat a certain number of
% times. This idea is implemented through \sciref{sci:dcmotor-loop}.
% Through the {\tt for loop} in between line numbers 3 and 8, we make
% the DC motor repeat four times the cycle containing one rotation in
% each direction.
% \figref{fig:dcmotorfc} explains the entire operation
% through a flowchart.
% \begin{figure}
% \centering
% \includegraphics[width=\lgfig]{\LocDCMfig/dcmotorflowchart.png}
% \caption{Flowchart}
% \label{fig:dcmotorfc}
% \end{figure}
% It is not difficult to see how some of the other features of the
% \scilab\ programming language can be used along with this DC motor.
% For example, it is possible to read a temperature value and based on
% its value, start or stop the motor. For real world applications, one
% has to provide extra current carrying capabilities through external
% hardware.
\subsection{Scilab Code}
\label{sec:dcmotor-scilab-code}
\addtocontents{cod}{\protect\addvspace{\codclr}}
\begin{scicode}
\ccaption{Rotating the DC motor}
{Rotating the DC motor. Available at
\LocDCMscibrief{dcmotor-clock.sce}.}
\label{sci:dcmotor-clock}
\lstinputlisting{\LocDCMscicode/dcmotor-clock.sce}
\end{scicode}
\begin{scicode}
\ccaption{Rotating the DC motor in both directions}
{Rotating DC motor in both directions. Available at
\LocDCMscibrief{dcmotor-both.sce}.}
\label{sci:dcmotor-both}
\lstinputlisting{\LocDCMscicode/dcmotor-both.sce}
\end{scicode}
\begin{scicode}
\ccaption{Rotating the DC motor in both directions in a loop}{Rotating
the DC motor in both directions in a loop.
Available at
\LocDCMscibrief{dcmotor-loop.sce}.}
\label{sci:dcmotor-loop}
\lstinputlisting{\LocDCMscicode/dcmotor-loop.sce}
\end{scicode}
\section{Controlling the DC motor from Xcos}
In this section, we will see how to drive the DC motor from Scilab Xcos.
We will carry out the same three experiments as in the previous
sections. For each experiment, we will give the location of the zcos file and the
parameters to set. The reader should go through the instructions
given in \secref{sec:xcos-start} before getting started.
% If the
% rotation of the DC motor is blocked by any obstacle in any of the
% experiments given below, you may want to hold it in your hand and let
% it run unhindered.
\begin{enumerate}
\item First we will see a simple code that drives the DC motor for a
specified time. When the file required for this experiment is
invoked, one gets the GUI as in \figref{fig:dcmotor-clock}. In
the caption of this figure, one can see where to locate the file.
\begin{figure}
\centering
\includegraphics[width=\smfig]{\LocDCMfig/dcmotor-clock.png}
\caption[Control of DC motor for a specified time from Xcos]
{Control of DC motor for a specified time from Xcos. This is what
one sees when \LocDCMscibrief{dcmotor-clock.zcos}, is
invoked.}
\label{fig:dcmotor-clock}
\end{figure}
We will next explain how to set the parameters for this simulation.
To set value on any block, one needs to right click and open the
{\tt Block Parameters} or double click. The values for each block
is tabulated in \tabref{tab:dcmotor-clock}. In case of {\tt
DCMOTOR\_SB}, enter 3 to indicate for L293D board. After clicking
on OK, another dialog box will pop up. In that, enter the PWM pin numbers
as 9 and 10 and click OK.
All other parameters are to be left
unchanged.
\begin{table}
\centering
\caption{Xcos parameters to drive the DC motor for a specified time}
\label{tab:dcmotor-clock}
\begin{tabular}{llc} \hline
Name of the block & Parameter name & Value \\ \hline
ARDUINO\_SETUP & Identifier of Arduino Card & 1 \\
& Serial com port number & 2\portcmd \\ \hline
TIME\_SAMPLE & Duration of acquisition(s) & 10 \\
& Sampling period(s) & 0.1 \\ \hline
DCMOTOR\_SB & Type of Shield & 3 \\
& Arduino card number & 1 \\
& PWM pin numbers & 9 10 \\
& Motor number & 1 \\ \hline
STEP\_FUNCTION & Step time & 5 \\
& Initial Value & 100 \\
& Final Value & 0 \\ \hline
\end{tabular}
\end{table}
% Can you find out for how long the DC motor will run when this program
% is executed? In which block do we provide this information? The
% answer is that the DC motor will stop only when we terminate the
% program. As in the previous experiments, we can terminate the Xcos
% program by pressing the stop button. The DC motor gets released when
% the stop button is pressed.
\item Next, we will describe the Xcos code that drives the DC motor in
both forward and reverse directions. When the file required for
this experiment is invoked, one gets the GUI as in
\figref{fig:dcmotor-both}. In the caption of this figure, one can
see where to locate the file.
\begin{figure}
\centering
\includegraphics[width=\smfig]{\LocDCMfig/dcmotor-both.png}
\caption[Xcos control of the DC motor in forward and reverse
directions]{Xcos control of the DC motor in forward and reverse
directions. This is what one sees when
\LocDCMscibrief{dcmotor-both.zcos}, is invoked.}
\label{fig:dcmotor-both}
\end{figure}
We will next explain how to set the parameters for this simulation.
To set value on any block, one needs to right click and open the
{\tt Block Parameters} or double click. The values for each block
is tabulated in \tabref{tab:dcmotor-both}. All other parameters are
to be left unchanged.
\begin{table}
\centering
\caption{Xcos parameters to drive the DC motor in forward and
reverse directions}
\label{tab:dcmotor-both}
\begin{tabular}{llc} \hline
Name of the block & Parameter name & Value \\ \hline
ARDUINO\_SETUP & Identifier of Arduino Card & 1 \\
& Serial com port number & 2\portcmd \\ \hline
TIME\_SAMPLE & Duration of acquisition(s) & 10 \\
& Sampling period(s) & 0.1 \\ \hline
DCMOTOR\_SB & Type of Shield & 3 \\
& Arduino card number & 1 \\
& PWM pin numbers & 9 10 \\
& Motor number & 1 \\ \hline
STEP\_FUNCTION & Step time & 5 \\
& Initial Value & 100 \\
& final value & 0 \\ \hline
CLOCK\_c & Period & 1 \\
& Initialisation Time & 0.1 \\ \hline
\end{tabular}
\end{table}
\item Next, we will describe the Xcos code that drives the DC motor in
a loop. When the file required for
this experiment is invoked, one gets the GUI as in
\figref{fig:dcmotor-loop}. In the caption of this figure, one can
see where to locate the file.
\begin{figure}
\centering
\includegraphics[width=\smfig]{\LocDCMfig/dcmotor-loop.png}
\caption[Xcos control of the DC motor in both directions in a loop]
{Xcos control of the DC motor in both directions in a loop. This is what one sees when
\LocDCMscibrief{dcmotor-loop.zcos}, is invoked.}
\label{fig:dcmotor-loop}
\end{figure}
We will next explain how to set the parameters for this simulation.
To set value on any block, one needs to right click and open the
{\tt Block Parameters} or double click. The values for each block
is tabulated in \tabref{tab:dcmotor-loop}. All other parameters are
to be left unchanged.
\begin{table}
\centering
\caption{Xcos parameters to drive the DC motor in a loop}
\label{tab:dcmotor-loop}
\begin{tabular}{llc} \hline
Name of the block & Parameter name & Value \\ \hline
ARDUINO\_SETUP & Identifier of Arduino Card & 1 \\
& Serial com port number & 2\portcmd \\ \hline
TIME\_SAMPLE & Duration of acquisition(s) & 10 \\
& Sampling period(s) & 0.1 \\ \hline
DCMOTOR\_SB & Type of Shield & 3 \\
& Arduino card number & 1 \\
& PWM pin numbers & 9 10 \\
& Motor number & 1 \\ \hline
STEP\_FUNCTION 1 & Step time & 3 \\
& Initial Value & 100 \\
& Final Value & 0 \\ \hline
STEP\_FUNCTION 2 & Step time & 5 \\
& Initial Value & 0 \\
& Final Value & 100 \\ \hline
STEP\_FUNCTION 3 & Step time & 7 \\
& Initial Value & 0 \\
& Final Value & 100 \\ \hline
BIGSOM\_f & Inputs ports signs/gain & [1;-1;1] \\ \hline
\end{tabular}
\end{table}
\end{enumerate}
%\section{Do we need any of these? \redcolor{Manas, please answer}}
% \begin{figure}
% \centering
% \includegraphics[width=\smfig]{\LocDCMfig/dc-motor-simple.png}
% \caption[Control of DC motor from Xcos]{Control of DC motor from
% Xcos. This is what one sees when {\tt
% \LocDCMscibrief/dc-motor-simple.zcos} is invoked.}
% \label{fig:dcm-xcos-simple}
% \end{figure}
% \begin{enumerate}
% \item Card 1 on com 5 block: Right-click and open the block properties
% or double click on this block. In the resulting dialog window,
% enter the com port number of your system.
% \item Typeshield 1 on card 1: Following the procedure mentioned above,
% make sure that the entry for this block is 3, which corresponds to
% L293D breakout board, as explained in \tabref{table:convention}.
% \end{enumerate}
% Leave the other blocks unchanged.
% This Xcos program is used to put a specific PWM value to the DC motor.
% We can right click (or double click) on any block and see what
% parameter values are present in it. By doing this, we can see that
% this program is used to put in a PWM value of 255. Start executing
% the program by pressing the right arrow key.
\begin{exercise}
Carry out the following exercise:
\begin{enumerate}
\item Keep reducing the PWM value and find out the minimum value
required to run the DC motor. Is this value in agreement with what
we found in the previous section?
\item Change the PWM value to $-100$ and check if the DC motor rotates
in the opposite direction.
\item Find out the smallest PWM value required to make the motor run
in the opposite direction. That is, find the least count for both
directions.
\item Come up with a method to rotate the motor in two directions for
different time periods.
\end{enumerate}
\end{exercise}
\section{Controlling the 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. The 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{sci: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}
\section{Controlling the DC motor from Julia}
\subsection{Controlling the DC motor}
In this section, we discuss how to carry out the experiments of the
previous section from Julia. 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:julia-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 Julia. The code for this experiment is
given in \juliaref{julia:dcmotor-clock}.
As explained earlier in \secref{sec:light-julia}, 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]
DCMotorSetup(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. The Julia-Arduino toolbox, as explained in \secref{sec:julia-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 DCMotorSetup} becomes
\lstinputlisting[firstline=5,lastline=5]
{\LocDCMjuliacode/dcmotor-clock.jl}
The next line of \juliaref{julia:dcmotor-clock} is of the following form:
\begin{lstlisting}[style=nonumbers]
DCMotorRun(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 DCMotorRun} becomes
\lstinputlisting[firstline=6,lastline=6]
{\LocDCMjuliacode/dcmotor-clock.jl}
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=7,lastline=7]{\LocDCMjuliacode/dcmotor-clock.jl}
With this, the DC motor will run for or 3 seconds. At last,
we release the DC motor, as shown below:
\lstinputlisting[firstline=8,lastline=8]{\LocDCMjuliacode/dcmotor-clock.jl}
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 7 of \juliaref{julia: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 8 of \juliaref{julia:dcmotor-clock} being commented),
the DC motor will go on rotating. That's why, it may be inferred that
line number 8 of \juliaref{julia:dcmotor-clock} is mandatory
for every program. We encourage the readers to run \juliaref{julia:dcmotor-clock} by commenting
any one or two of the lines numbered 7 and 8. 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
\juliaref{julia: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 DCMotorRun},
as shown below:
\lstinputlisting[firstline=8,lastline=8]
{\LocDCMjuliacode/dcmotor-both.jl}
% 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 DCMotorRelease}, followed by closing the serial port:
\lstinputlisting[firstline=10,lastline=11]
{\LocDCMjuliacode/dcmotor-both.jl}
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
\juliaref{julia: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 DCMotorRelease}, followed by closing the serial port.
\end{enumerate}
\subsection{Julia Code}
\label{sec:dcmotor-julia-code}
\addtocontents{juliad}{\protect\addvspace{\codclr}}
\begin{juliacode}
\jcaption{Rotating the DC motor}
{Rotating the DC motor. Available at
\LocDCMjuliabrief{dcmotor-clock.jl}.}
\label{julia:dcmotor-clock}
\lstinputlisting{\LocDCMjuliacode/dcmotor-clock.jl}
\end{juliacode}
\begin{juliacode}
\jcaption{Rotating the DC motor in both directions}
{Rotating DC motor in both directions. Available at
\LocDCMjuliabrief{dcmotor-both.jl}.}
\label{julia:dcmotor-both}
\lstinputlisting{\LocDCMjuliacode/dcmotor-both.jl}
\end{juliacode}
\begin{juliacode}
\jcaption{Rotating the DC motor in both directions in a loop}{Rotating
the DC motor in both directions in a loop.
Available at
\LocDCMjuliabrief{dcmotor-loop.jl}.}
\label{julia:dcmotor-loop}
\lstinputlisting{\LocDCMjuliacode/dcmotor-loop.jl}
\end{juliacode}
\section{Controlling the DC motor from OpenModelica}
\subsection{Controlling the DC motor}
In this section, we discuss how to carry out the experiments of the
previous section from OpenModelica. 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:OpenModelica-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 OpenModelica. The code for this experiment is
given in \OpenModelicaref{OpenModelica:dcmotor-clock}.
As explained earlier in \secref{sec:light-OpenModelica},
we begin with importing the two packages: Streams and SerialCommunication 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. The OpenModelica-Arduino toolbox, as explained in
\secref{sec:load-om-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=15,lastline=15]
{\LocDCMOpenModelicacode/dcmotor-clock.mo}
The next line of \OpenModelicaref{OpenModelica: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=16,lastline=16]
{\LocDCMOpenModelicacode/dcmotor-clock.mo}
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=17,lastline=17]{\LocDCMOpenModelicacode/dcmotor-clock.mo}
With this, the DC motor will run for 3000 milliseconds or 3 seconds. At last,
we release the DC motor, as shown below:
\lstinputlisting[firstline=18,lastline=18]{\LocDCMOpenModelicacode/dcmotor-clock.mo}
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 17 of \OpenModelicaref{OpenModelica: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 18 of \OpenModelicaref{OpenModelica:dcmotor-clock} being commented),
the DC motor will go on rotating. That's why, it may be inferred that
line number 18 of \OpenModelicaref{OpenModelica:dcmotor-clock} is mandatory
for every program. We encourage the readers to run \OpenModelicaref{OpenModelica:dcmotor-clock} by commenting
any one or two of the lines numbered 17 and 18. 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
\OpenModelicaref{OpenModelica: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=17,lastline=17]
{\LocDCMOpenModelicacode/dcmotor-both.mo}
% 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=19,lastline=19]
{\LocDCMOpenModelicacode/dcmotor-both.mo}
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
\OpenModelicaref{OpenModelica: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}
%%%%%%%OpenModelica description ends
% \subsection{Using the Xcos features in DC motor control}
% Xcos can be used to control the DC motor in many different ways. In
% this section, we will see a few approaches. First, we will make the
% DC motor start and stop. For this, we will open the program given in \figref{fig:dcm-xcos-start-stop}.
% \begin{figure}
% \centering
% \includegraphics[width=\smfig]{\LocDCMfig/dc-motor-start-stop.png}
% \caption[Xcos program to make the DC motor to rotate, pause and
% repeat]{Xcos program to make the DC motor to rotate, pause and
% repeat. This is what one sees when {\tt
% \LocDCMscibrief/dc-motor-start-stop.zcos} is invoked.}
% \label{fig:dcm-xcos-start-stop}
% \end{figure}
% The input is a train of pulses of positive values. The amplitude of
% these pulses is chosen to be 255. The period is chosen to be 1s.
% \redcolor{these values have to be checked - this section to be
% rewritten.} On executing this Xcos code, the DC motor rotates for
% 1s, pauses for 1s, and repeats. This continues until the Xcos program
% is terminated.
% \begin{exercise}
% Carry out the following exercise:
% \begin{enumerate}
% \item The user may repeat this exercise with different amplitude and
% periods.
% \item The above may be repeated with negative PWM values.
% \item One may find the least count for all experiments.
% \end{enumerate}
% \end{exercise}
% We will next see how to give positive and negative values of PWM in
% the same experiment. For this, we will use the Xcos program given in
% \figref{fig:dcm-xcos-both}. This code is similar to the previous one,
% but for a few minor changes. First of all, we have introduced a gain
% block, and assigned a value of 255. This block is excited by pulses
% of $+1$ and $-1$ alternately. This simple approach, however, results
% in the DC motor running in both directions for identical durations.
% \begin{figure}
% \centering
% \includegraphics[width=\lgfig]{\LocDCMfig/dc-motor-both.png}
% \caption[Xcos program to make the DC motor to rotate, pause and to
% rotate in the opposite direction]{Xcos program to make the DC motor
% to rotate, pause and to rotate in the opposite direction. This is
% what one sees when {\tt \LocDCMscibrief/dc-motor-both.zcos} is
% invoked.}
% \label{fig:dcm-xcos-both}
% \end{figure}
% \begin{exercise}
% Carry out the following exercise:
% \begin{enumerate}
% \item Repeat this experiment for different amplitudes and periods.
% \end{enumerate}
% \end{exercise}
% \subsection{Troubleshooting \redcolor{Do we need this? - Manas, please answer}}
% \begin{enumerate}
% \item If we want to connect external supply, Ground (Gnd) pin of L293D board, external supply and \arduino\ board should be shorted. This creates common ground voltage for entire set-up.
% \item We can connect more than one motor simultaneously using H-bridge like L293D. Typically break-out boards support 2 motors. However we are limited by number of PWM pins available on the board. For each motor cable of bidirectional motion, we need two PWM pins
% \end{enumerate}
\subsection{OpenModelica Code}
\label{sec:dcmotor-OpenModelica-code}
Unlike other code files, the code/ model for running experiments using OpenModelica are
available inside the OpenModelica-Arduino toolbox, as explained in \secref{sec:load-om-toolbox}.
Please refer to \figref{om-examples-toolbox} to know how to locate the experiments.
\addtocontents{OpenModelicad}{\protect\addvspace{\codclr}}
\begin{OpenModelicacode}
\mcaption{Rotating the DC motor}
{Rotating the DC motor.
Available at Arduino -> SerialCommunication -> Examples -> dcmotor
-> dcmotor\_clock.}
\label{OpenModelica:dcmotor-clock}
\lstinputlisting{\LocDCMOpenModelicacode/dcmotor-clock.mo}
\end{OpenModelicacode}
\begin{OpenModelicacode}
\mcaption{Rotating the DC motor in both directions}
{Rotating DC motor in both directions. Available at Arduino -> SerialCommunication -> Examples -> dcmotor
-> dcmotor\_both.}
\label{OpenModelica:dcmotor-both}
\lstinputlisting{\LocDCMOpenModelicacode/dcmotor-both.mo}
\end{OpenModelicacode}
\begin{OpenModelicacode}
\mcaption{Rotating the DC motor in both directions in a loop}{Rotating
the DC motor in both directions in a loop.
Available at Arduino -> SerialCommunication -> Examples -> dcmotor
-> dcmotor\_loop.}
\label{OpenModelica:dcmotor-loop}
\lstinputlisting{\LocDCMOpenModelicacode/dcmotor-loop.mo}
\end{OpenModelicacode}
%%%%%%%%%%OpenModelica code ends
|