-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArduino.mo
More file actions
1792 lines (1669 loc) · 106 KB
/
Arduino.mo
File metadata and controls
1792 lines (1669 loc) · 106 KB
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
package Arduino
extends Arduino.SerialCommunication.Icons.GenericICPackage;
package SerialCommunication "Serial Communication Package for Arduino"
extends Arduino.SerialCommunication.Icons.FunctionLayerIcon;
import ModelicaReference.Operators;
package Functions
extends Modelica.Icons.Package;
import Modelica;
function open_serial "Command to initialize the serial port which is connected to Arduino"
extends Modelica.Icons.Function;
input Integer handle, port, baudrate;
output Integer OK;
external OK = open_serial(handle, port, baudrate) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>open_serial</b>(handle,port,baudrate);
</pre></blockquote>
<h4>Description</h4>
<p>
Establishes a serial communication using port number \"port\".To connect to the Arduino board, check the port number from the device manager or check it from the Arduino software.
</p>
</html>"));
end open_serial;
function read_serial "read characters from serial port"
extends Modelica.Icons.Function;
input Integer handle;
input Integer size;
output Integer r_OK;
protected
Integer buf[size + 1];
//String buf;
external r_OK = read_serial(handle, buf, size) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>read_serial</b>(handle,size);
</pre></blockquote>
<h4>Description</h4>
<p>The translation setting of openserial can come in the way, affecting the number of characters effectively read. If that is the case, omitting the parameter n might be a bad idea, as scilab can hang. This would happen if the input stream contains CR or LF characters which are filtered out: in such cases serialstatus counts them, but readserial does not see them and keeps wating (if the blocking mode of openserial was true) until the total number is received.
</p>
</html>"));
end read_serial;
//algorithm
//Modelica.Utilities.Streams.print(String(buf));
function write_serial "write to the serial port"
extends Modelica.Icons.Function;
input Integer handle;
input String str;
input Integer size;
output Integer w_OK;
external w_OK = write_serial(handle, str, size) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>write_serial</b>(handle,str,size);
</pre></blockquote>
<h4>Description</h4>
<p>No end of line character is appended to the string; the user might have to add it if the device being talked to requires it. The Tcl command puts -nonewline is used. In addition, the translation mode of openserial can come into way.
</p>
</html>"));
end write_serial;
function close_serial "Command to close the serial port which is connected to Arduino"
extends Modelica.Icons.Function;
input Integer handle;
output Integer c_OK;
external c_OK = close_serial(handle) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>close_serial</b>(handle);
</pre></blockquote>
<h4>Description</h4>
<p>Closes the port for serial communication specified by \"handle\".It is important to close the serial port after use, else the port would be busy and restart of Scilab might required to connect to it again.
</p>
</html>"));
end close_serial;
function status_serial "get status of the serial port"
extends Modelica.Icons.Function;
input Integer handle;
output Integer stat_OK;
protected
Integer bytes[2];
external stat_OK = status_serial(handle, bytes) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>status_serial</b>(handle);
</pre></blockquote>
<h4>Description</h4>
<p>Provides status of serial communication channel specified by \"handle\".Get some information about the number of characters present in the input and output buffers of the serial port, and about the status lines (DTS, CTS, etc.).The translation setting of openserial can come in the way. If the input stream contains CR or LF characters which are filtered out, openserial counts them but readserial does not see them.
</p>
</html>"));
end status_serial;
function cmd_digital_out "Command to sent out digital signal to a connected Arduino board"
extends Modelica.Icons.Function;
input Integer h, pin_no, val;
output Integer digital_w_OK;
external digital_w_OK = cmd_digital_out(h, pin_no, val) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_digital_out</b>(handle,pin_no,value);
</pre></blockquote>
<h4>Description</h4>
<p>The Arduino board has a set of logical ports (digital) that are used for writing or reading data from a component.
To map a UNO, ports 2-13 are available (0 and 1 are used for serial transmission). For MEGA board, ports 2-53 are available. The port takes the low logic level (0) or logic high (1) which corresponds to the reference voltage.
</p>
</html>"));
end cmd_digital_out;
function cmd_digital_in "Command to read in digital signal from a connected Arduino board"
extends Modelica.Icons.Function;
input Integer h, pin_no;
output Integer digital_in;
external digital_in = cmd_digital_in(h, pin_no) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_digital_in</b>(handle,pin_no);
</pre></blockquote>
<h4>Description</h4>
<p>The Arduino board has a set of logical ports (digital) that are used for writing or reading data from a component.
To map a UNO, ports 2-13 are available (0 and 1 are used for serial transmission). For MEGA board, ports 2-53 are available. The port takes the low logic level (0) or logic high (1) which corresponds to the reference voltage.
</p>
</html>"));
end cmd_digital_in;
function delay "Provides delay.Suspends Openmodelica."
extends Modelica.Icons.Function;
input Integer t;
external delay(t) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>delay</b>(milliseconds);
</pre></blockquote>
<h4>Description</h4>
<p>Delay process for specified number of miliseconds specified by the argument. The actual suspension time may be longer because of other activities in the system, or because of the time spent in processing the call.
</p>
</html>"));
end delay;
function cmd_analog_in "Command to read in analog signal from a connected Arduino board"
extends Modelica.Icons.Function;
input Integer h, pin_no;
output Integer val;
external val = cmd_analog_in(h, pin_no) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_analog_in</b>(handle,pin_no);
</pre></blockquote>
<h4>Description</h4>
<p>Arduino UNO board has 6 analog input ports (A0 to A5), the Arduino Mega board has 16 analog input ports (A0 to A15). The 10 bits channels convert the analog input from 0 to 5 volts, to a digital value between 0 and 1023.
</p>
</html>"));
end cmd_analog_in;
function cmd_analog_out "Command to sent out analog signal to a connected Arduino board"
extends Modelica.Icons.Function;
input Integer h, pin_no;
input Real val;
output Integer analog_w_OK;
external analog_w_OK = cmd_analog_out(h, pin_no, val) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_analog_out</b>(handle,pin_no,val);
</pre></blockquote>
<h4>Description</h4>
<p>The analog outputs of the Arduino Uno is available at the pins 3,5,6,9,10 and 11, while on the Mega board, the outputs are on pins 1-13 and 44-46. It is a bit misleading to use the term 'analog output', because in order to generate this output while minimizing energy losses, the Arduino uses PWM (Pulse Width Modulation) available on these ports. By varying the duty cycle of the PWM is altered the average voltage across the component connected to this port, which has the effect of having a analog output voltage.
The input port accepts the value from 0 to 255 which is correspoding to the duty cycle of 0 to 100%. In other words, sending 0 to the block will generate 0 V output at the port, 127 generates 2.5V and 255 generates 5V. (the port is 8 bits, so the resolutions of output would be 2^8 =256).
</p>
</html>"));
end cmd_analog_out;
function cmd_analog_in_volt "Command to read in analog signal from a connected Arduino board"
extends Modelica.Icons.Function;
input Integer h, pin_no;
output Integer val;
external val = cmd_analog_in_volt(h, pin_no) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_analog_in_volt</b>(handle,pin_no);
</pre></blockquote>
<h4>Description</h4>
<p>Arduino UNO board has 6 analog input ports (A0 to A5), the Arduino Mega board has 16 analog input ports (A0 to A15). The 10 bits channels convert the analog input from 0 to 5 volts, to a digital value between 0 and 1023. This function scale the reading to 0-5 so the user could get the measured voltage directly.
</p>
</html>"));
end cmd_analog_in_volt;
function cmd_analog_out_volt "Command to sent out analog signal to a connected Arduino board"
extends Modelica.Icons.Function;
input Integer h, pin_no;
input Real val;
output Integer analog_v_wOK;
external analog_v_wOK = cmd_analog_out_volt(h, pin_no, val) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_analog_out_volt</b>(handle,pin_no,val);
</pre></blockquote>
<h4>Description</h4>
<p>The analog outputs of the Arduino Uno is available at the pins 3,5,6,9,10 and 11, while on the Mega board, the outputs are on pins 1-13 and 44-46. It is a bit misleading to use the term 'analog output', because in order to generate this output while minimizing energy losses, the Arduino uses PWM (Pulse Width Modulation) available on these ports. By varying the duty cycle of the PWM is altered the average voltage across the component connected to this port, which has the effect of having a analog output voltage.
</p>
</html>"));
end cmd_analog_out_volt;
function cmd_dcmotor_setup "Command to setup pins to control DC motor"
extends Modelica.Icons.Function;
input Integer handle, driver_type, motor_no, pin1, pin2;
external cmd_dcmotor_setup(handle, driver_type, motor_no, pin1, pin2) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_dcmotor_setup</b>(handle,driver_type,motor_no,pin_no1,pin_no2);
</pre></blockquote>
<h4>Description</h4>
<p>Arduino board does not deliver enough power, so it is necessary to use a H-bridge circuit/IC to control the motor. There are several types of H-bridge IC that do not all operate on the same principle. For example, the L298 requires the use of a PWM signal with current sense. The L293 uses two PWM to set the speed and direction. Ready-to-use Shields are also available.
Remember that the PWM is 8-bit (0 to 255). The input of the block could accept any value, but it would saturate at +- 255.
</p>
</html>"));
end cmd_dcmotor_setup;
function cmd_dcmotor_run "Command to run DC motor after setting up"
extends Modelica.Icons.Function;
input Integer handle, motor_no, val;
external cmd_dcmotor_run(handle, motor_no, val) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_dcmotor_run</b>(handle,motor_no,value);
</pre></blockquote>
<h4>Description</h4>
<p>Arduino board does not deliver enough power, so it is necessary to use a H-bridge circuit/IC to control the motor. There are several types of H-bridge IC that do not all operate on the same principle. For example, the L298 requires the use of a PWM signal with current sense. The L293 uses two PWM to set the speed and direction. Ready-to-use Shields are also available.
Remember that the PWM is 8-bit (0 to 255). The input of the block could accept any value, but it would saturate at +- 255.
</p>
</html>"));
end cmd_dcmotor_run;
function cmd_dcmotor_release "Command to release pins which have setup for DC motor"
extends Modelica.Icons.Function;
input Integer handle, motor_no;
external cmd_dcmotor_release(handle, motor_no) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_dcmotor_release</b>(handle,motor_no);
</pre></blockquote>
<h4>Description</h4>
<p>Arduino board does not deliver enough power, so it is necessary to use a H-bridge circuit/IC to control the motor. There are several types of H-bridge IC that do not all operate on the same principle. For example, the L298 requires the use of a PWM signal with current sense. The L293 uses two PWM to set the speed and direction. Ready-to-use Shields are also available.
Remember that the PWM is 8-bit (0 to 255). The input of the block could accept any value, but it would saturate at +- 255.
</p>
</html>"));
end cmd_dcmotor_release;
function cmd_servo_attach "Command to attach servo motor to Arduino"
extends Modelica.Icons.Function;
input Integer handle, servo_no;
external cmd_servo_attach(handle, servo_no) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_servo_attach</b>(handle,servo_no);
</pre></blockquote>
<h4>Description</h4>
<p>A servomotor is an rotary actuator consist of an electric motor, gears, a potentiometer and an analogue or digital electronics for control. The servomotor usualy used for a position control application (or speed for continuous rotation servos).
The user must give the command of the position setpoint or desired speed. This command is sent to the actuator in pulses spaced by 10 to 20 ms. The coding of these pulses is made such that a pulse of 1.5 ms corresponding to the centered position (rest), a pulse of 1 ms corresponds to an angle of 90° in the anticlockwise direction, and a pulse 2 ms corresponds to an angle of 90° clockwise. All other pulse widths give intermediate values.
A servomotor for continuous rotation, the pulse width control the rotational speed and the direction. It is recommended to use a voltage regulator to power the servomotor instead of using the Arduino board power. For simplicity, the function takes an input commnad in degrees from 0 to 180. Two actuators can be controlled with this toolbox. (modified version of 3 motors available)
</p>
</html>"));
end cmd_servo_attach;
function cmd_servo_move "Command to run servo motor which has been setup"
extends Modelica.Icons.Function;
input Integer handle, servo_no, val;
external cmd_servo_move(handle, servo_no, val) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_servo_move</b>(handle,servo_no,value);
</pre></blockquote>
<h4>Description</h4>
<p>A servomotor is an rotary actuator consist of an electric motor, gears, a potentiometer and an analogue or digital electronics for control. The servomotor usualy used for a position control application (or speed for continuous rotation servos).
The user must give the command of the position setpoint or desired speed. This command is sent to the actuator in pulses spaced by 10 to 20 ms. The coding of these pulses is made such that a pulse of 1.5 ms corresponding to the centered position (rest), a pulse of 1 ms corresponds to an angle of 90° in the anticlockwise direction, and a pulse 2 ms corresponds to an angle of 90° clockwise. All other pulse widths give intermediate values.
A servomotor for continuous rotation, the pulse width control the rotational speed and the direction. It is recommended to use a voltage regulator to power the servomotor instead of using the Arduino board power. For simplicity, the function takes an input commnad in degrees from 0 to 180. Two actuators can be controlled with this toolbox. (modified version of 3 motors available)
</p>
</html>"));
end cmd_servo_move;
function cmd_servo_detach "Command to release the pin which has been setup for servo motor"
extends Modelica.Icons.Function;
input Integer handle, servo_no;
external cmd_servo_attach(handle, servo_no) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>cmd_servo_detach</b>(handle,servo_no);
</pre></blockquote>
<h4>Description</h4>
<p>A servomotor is an rotary actuator consist of an electric motor, gears, a potentiometer and an analogue or digital electronics for control. The servomotor usualy used for a position control application (or speed for continuous rotation servos).
The user must give the command of the position setpoint or desired speed. This command is sent to the actuator in pulses spaced by 10 to 20 ms. The coding of these pulses is made such that a pulse of 1.5 ms corresponding to the centered position (rest), a pulse of 1 ms corresponds to an angle of 90° in the anticlockwise direction, and a pulse 2 ms corresponds to an angle of 90° clockwise. All other pulse widths give intermediate values.
A servomotor for continuous rotation, the pulse width control the rotational speed and the direction. It is recommended to use a voltage regulator to power the servomotor instead of using the Arduino board power. For simplicity, the function takes an input commnad in degrees from 0 to 180. Two actuators can be controlled with this toolbox. (modified version of 3 motors available)
</p>
</html>"));
end cmd_servo_detach;
function ieeesingle2num "ieee-745 floating point converter"
extends Modelica.Icons.Function;
input String hexa;
output Real y;
external y = ieeesingle2num(hexa) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>ieeesingle2num</b>(hexa);
</pre></blockquote>
<h4>Description</h4>
<p>Converts an hexadecimal integer to ieee single precision format
</p>
</html>"));
end ieeesingle2num;
function math_floor "Floor function"
extends Modelica.Icons.Function;
input Real x;
output Integer y;
external y = mfloor(x) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.Functions.<b>math_floor</b>(x);
</pre></blockquote>
<h4>Description</h4>
<p>Returns the greatest integer less than or equal to x
</p>
</html>"));
end math_floor;
function getArduinoVersion "Returns the Arduino version used"
extends Modelica.Icons.Function;
output String arduinoVersion(fixed = true);
algorithm
arduinoVersion := "1.1";
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.<b>getArduinoVersion</b>();
</pre></blockquote>
<h4>Description</h4>
<p>Gives the version of the arduino used.
</p>
</html>"));
end getArduinoVersion;
function cmd_encoder_init "Initiates the encoder"
extends Modelica.Icons.Function;
input Integer h, encoder_mode, pin_no1, pin_no2;
external cmd_encoder_init(h, encoder_mode, pin_no1, pin_no2) annotation(
Library = "SerialComm");
annotation(
Documentation(info = "<html>
<h4>Syntax</h4>
<blockquote><pre>
Arduino.SerialCommunication.<b>cmd_encoder_init</b>(handle,encoder_mode,pin_no1,pin_no2);
</pre></blockquote>
<h4>Description</h4>
<p>Used to read encoder signal from one or more channels
</p>
</html>"));
end cmd_encoder_init;
//Incomplete
end Functions;
package Examples
extends Modelica.Icons.ExamplesPackage;
package led
extends Modelica.Icons.ExamplesPackage;
model led_blue "Turn on Blue LED"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
sComm.delay(2000);
if ok <> 0 then
strm.print("Check the serial port and try again");
else
digital_out := sComm.cmd_digital_out(1, 9, 1) "This will turn ON the blue LED";
end if;
strm.print(String(time));
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 10));
end led_blue;
model led_blue_delay "Turn on Blue LED for a period of 2 seconds"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
sComm.delay(2000);
if ok <> 0 then
strm.print("Check the serial port and try again");
else
digital_out := sComm.cmd_digital_out(1, 9, 1) "This will turn the blue LED";
sComm.delay(2000) "let the blue LED be on for two seconds";
digital_out := sComm.cmd_digital_out(1, 9, 0) "turn off blue LED";
sComm.delay(2000) "let the blue LED be off for two seconds";
end if;
strm.print(String(time));
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 10));
end led_blue_delay;
model led_blue_red "Turn on Red & Blue LED"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
if ok <> 0 then
strm.print("Check the serial port and try again");
else
sComm.delay(2000);
digital_out := sComm.cmd_digital_out(1, 9, 1) "This will turn the blue LED";
digital_out := sComm.cmd_digital_out(1, 11, 1) "This will turn the red LED";
sComm.delay(5000) "Delay for 5 seconds";
digital_out := sComm.cmd_digital_out(1, 9, 0) "This turns off the blue Led";
sComm.delay(3000) "Delay for 3 seconds";
digital_out := sComm.cmd_digital_out(1, 11, 0) "This turns off the red Led";
end if;
strm.print(String(time));
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 10));
end led_blue_red;
model led_blink "This will turn on and turn off the user LED for every second for 10 times"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
sComm.delay(2000);
if ok <> 0 then
strm.print("Check the serial port and try again");
else
for i in 1:10 loop
digital_out := sComm.cmd_digital_out(1, 13, 0) "This will turn off the LED";
sComm.delay(500) "Delay for 0.5 seconds";
digital_out := sComm.cmd_digital_out(1, 13, 1) "This turns the Led";
sComm.delay(500) "Delay for 0.5 seconds";
end for;
end if;
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 10));
end led_blink;
model led_green_blink "This will turn on and turn off the green LED for every second for 5 times"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
sComm.delay(1000);
if ok <> 0 then
strm.print("Check the serial port and try again");
else
for i in 1:10 loop
digital_out := sComm.cmd_digital_out(1, 10, 0) "This will turn off the green LED";
sComm.delay(1000) "Delay for 1 second";
digital_out := sComm.cmd_digital_out(1, 10, 1) "This turns the green Led";
sComm.delay(1000) "Delay for 1 second";
end for;
end if;
strm.print(String(time));
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 10));
end led_green_blink;
end led;
package push
extends Modelica.Icons.ExamplesPackage;
model led_push_button "Conrolling LED with PushButton"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer digital_in(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
end when;
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
digital_in := sComm.cmd_digital_in(1, 12) "Read from digital pin 12";
if digital_in == 0 then
digital_out := sComm.cmd_digital_out(1, 9, 0) "This will turn OFF the blue LED";
sComm.delay(200);
else
digital_out := sComm.cmd_digital_out(1, 9, 1) "This will turn ON the blue LED";
sComm.delay(200);
end if;
end if;
//for i in 1:1000 loop
//end for;
//strm.print(String(time));
when terminal() then
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 1, Tolerance = 1e-6, Interval = 0.01));
end led_push_button;
model push_button_status "Checking Status of PushButton"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer digital_in(fixed = false);
Integer digital_out(start = 0, fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
end when;
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
digital_in := sComm.cmd_digital_in(1, 12);
if digital_in == 0 then
digital_out := sComm.cmd_digital_out(1, 9, 0) "This will turn OFF the blue LED";
strm.print("LOW");
sComm.delay(200);
else
digital_out := sComm.cmd_digital_out(1, 9, 1) "This will turn ON the blue LED";
strm.print("HIGH");
sComm.delay(200);
end if;
end if;
//for i in 1:1000 loop
//end for;
when terminal() then
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
//sComm.cmd_arduino_meter(digital_in);
annotation(
experiment(StartTime = 0, StopTime = 1, Tolerance = 1e-6, Interval = 0.01));
end push_button_status;
end push;
package ldr
extends Modelica.Icons.ExamplesPackage;
model ldr_led "LED indicating light sensor readings"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer analog_in(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
sComm.delay(2000);
end when;
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
analog_in := sComm.cmd_analog_in(1, 5) "read analog pin 5 (ldr)";
if analog_in < 300 then
digital_out := sComm.cmd_digital_out(1, 9, 1) "Turn ON LED";
else
digital_out := sComm.cmd_digital_out(1, 9, 0) "Turn OFF LED";
end if;
sComm.delay(200);
end if;
//for i in 1:500 loop
//end for;
//strm.print(String(time));
when terminal() then
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
//Run for 500 iterations
//Setting Threshold value of 300
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 0.02));
end ldr_led;
model ldr_read "Reading light intensity using ldr"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer analog_in(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
sComm.delay(2000);
end when;
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
analog_in := sComm.cmd_analog_in(1, 5) "read analog pin 5 (ldr)";
strm.print("LDR Readings at time " + String(time) + " : " + String(analog_in));
sComm.delay(500);
end if;
//for i in 1:10 loop
//end for;
when terminal() then
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
//Run for 10 iterations
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 1));
end ldr_read;
end ldr;
package pot
extends Modelica.Icons.ExamplesPackage;
model pot_threshold
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer analog_in(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
sComm.delay(1000);
end when;
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
analog_in := sComm.cmd_analog_in(1, 2) "read analog pin 2";
strm.print("Potentiometer Readings:" + String(analog_in));
if analog_in >= 0 and analog_in < 320 then
digital_out := sComm.cmd_digital_out(1, 11, 1) "Turn ON LED";
sComm.delay(1000);
digital_out := sComm.cmd_digital_out(1, 11, 0) "Turn OFF LED";
elseif analog_in >= 320 and analog_in <= 900 then
digital_out := sComm.cmd_digital_out(1, 10, 1) "Turn ON LED";
sComm.delay(1000);
digital_out := sComm.cmd_digital_out(1, 10, 0) "Turn OFF LED";
elseif analog_in > 900 and analog_in <= 1023 then
digital_out := sComm.cmd_digital_out(1, 9, 1) "Turn ON LED";
sComm.delay(1000);
digital_out := sComm.cmd_digital_out(1, 9, 0) "Turn OFF LED";
end if;
end if;
//for i in 1:10 loop
//end for;
when terminal() then
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
//Run for 10 iterations
//Threshold 1
//Threshold 2
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 1));
end pot_threshold;
end pot;
package thermistor
extends Modelica.Icons.ExamplesPackage;
model therm_buzzer "Sound buzzer depending on thermistor readings"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer analog_in(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
sComm.delay(2000);
end when;
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
analog_in := sComm.cmd_analog_in(1, 4) "read analog pin 4";
if analog_in > 500 then
digital_out := sComm.cmd_digital_out(1, 3, 1) "Turn ON Buzzer";
else
digital_out := sComm.cmd_digital_out(1, 3, 0) "Turn OFF Buzzer";
end if;
sComm.delay(200);
end if;
//for i in 1:500 loop
//end for;
//Run for 500 iterations
//Setting Threshold value of 500
when terminal() then
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 0.02));
end therm_buzzer;
model therm_read "Thermistor Readings"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer analog_in(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "At port 0 with baudrate of 115200";
sComm.delay(2000);
end when;
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
analog_in := sComm.cmd_analog_in(1, 4) "read analog pin 5 (ldr)";
strm.print("Thermistor Readings " + " : " + String(analog_in));
sComm.delay(500);
end if;
//for i in 1:20 loop
//end for;
when terminal() then
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
//Run for 20 iterations
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 0.5));
end therm_read;
end thermistor;
package dcmotor
extends Modelica.Icons.ExamplesPackage;
model dcmotor_clock "Rotate DC Motor clockwise"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 1, 115200) "COM port is 0 and baud rate is 115200";
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
sComm.delay(2000);
sComm.cmd_dcmotor_setup(1, 3, 1, 9, 10) "Setup DC motor of type 3 (L293D), motor 1, pin 9 and 10";
sComm.cmd_dcmotor_run(1, 1, 100) "Motor 1 runs at PWM 100";
sComm.delay(3000) "This is allowed to continue for 3 seconds";
sComm.cmd_dcmotor_release(1, 1) "Motor 1 is released";
end if;
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 10));
end dcmotor_clock;
model dcmotor_both "Rotate DC Motor in both directions"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "COM port is 0 and baud rate is 115200";
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
sComm.delay(2000);
sComm.cmd_dcmotor_setup(1, 3, 1, 9, 10) "Setup DC motor of type 3 (L293D), motor 1, pin 9 and 10";
sComm.cmd_dcmotor_run(1, 1, 255) "Motor 1 runs at PWM 100";
sComm.delay(3000) "for 3 seconds";
sComm.cmd_dcmotor_run(1, 1, -255) "Motor 1 runs at PWM -100 in reverse direction";
sComm.delay(2000) "for 2 seconds";
sComm.cmd_dcmotor_release(1, 1) "Motor 1 is released";
end if;
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 10));
end dcmotor_both;
model dcmotor_loop "Rotate DC Motor in both directions in a loop"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "COM port is 0 and baud rate is 115200";
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
for i in 1:4 loop
sComm.cmd_dcmotor_setup(1, 3, 1, 9, 10) "Setup DC motor of type 3 (L293D), motor 1, pins 9 and 10";
sComm.cmd_dcmotor_run(1, 1, 100) "Motor 1 runs at PWM 100";
sComm.delay(2000) "for 3 seconds";
sComm.cmd_dcmotor_run(1, 1, 0) "Halt the motor";
sComm.delay(2000) "for 2 seconds";
sComm.cmd_dcmotor_run(1, 1, -100) "Run it at PWM 100 in reverse direction";
sComm.delay(2000) "for 2 seconds";
sComm.cmd_dcmotor_release(1, 1) "Motor 1 is released";
end for;
end if;
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 10));
end dcmotor_loop;
end dcmotor;
package servo
extends Modelica.Icons.ExamplesPackage;
model servo_init "Rotate Servo Motor "
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "COM port is 0 and baud rate is 115200";
if ok <> 0 then
strm.print("Check the serial port and try again");
else
sComm.delay(2000);
sComm.cmd_servo_attach(1, 1) "To attach the motor to pin 9 of servo1";
sComm.cmd_servo_move(1, 1, 0) "tell servo to rotate by 30 degrees";
sComm.delay(3000);
end if;
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
annotation(
experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5));
end servo_init;
model servo_loop "Rotate servo motor by 20 degrees 10 times"
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer c_ok(fixed = false);
Integer angle(fixed = true);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "COM port is 0 and baud rate is 115200";
if ok <> 0 then
strm.print("Check the serial port and try again");
else
sComm.delay(2000);
sComm.cmd_servo_attach(1, 1) "Attach motor to pin 9. 1 means pin 9.";
sComm.delay(2000);
angle := 20 "Angle by which it has to move";
for i in 1:10 loop
sComm.cmd_servo_move(1, 1, angle * i) "tell servo to rotate by 20 degrees";
sComm.delay(1000) "waits for a sec";
end for;
end if;
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
//sComm.cmd_servo_detach(1, 1) "Detach the motor";
annotation(
experiment(StartTime = 0, StopTime = 5, Tolerance = 1e-6, Interval = 5));
end servo_loop;
model servo_reverse
extends Modelica.Icons.Example;
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
Integer c_ok(fixed = false);
algorithm
when initial() then
ok := sComm.open_serial(1, 0, 115200) "COM port is 0 and baud rate is 115200";
if ok <> 0 then
strm.print("Check the serial port and try again");
else
sComm.delay(2000);
sComm.cmd_servo_attach(1, 1) "Attach the motor to pin 9. 1 means 9";
sComm.cmd_servo_move(1, 1, 90) "Move the servo to 90 degree";
sComm.delay(1000) "be there for one second";