Upload
junaidtps1
View
219
Download
3
Embed Size (px)
DESCRIPTION
arduino matlab
Citation preview
5/28/2018 Arduino Matlab
1/63
Learning Basic Mechatronics concepts
using the Arduino Board and MATLAB
rks,
Inc.
009T
heMathWo, ,
The MathWorks, 400 Continental Blvd, Suite 600-028,
El Segundo, CA, 90245
2. .
5/28/2018 Arduino Matlab
2/63
A few challenges with Mechatronics projects
r u no ntro uct on ot vat on
Motor Control
Arduino target and Useful Links
2
5/28/2018 Arduino Matlab
3/63
, , ,
Touch, Orientation, Motion/Speed, Environment, Optical,Chemical, Flow, Voltage/Current, sensor selection is
largely application related but also related to:
Number of sensors
Ranges
A/D conversion issues as Resolution and Sampling Rate
Signal Conditioning
3
5/28/2018 Arduino Matlab
4/63
Development-Prototyping Environment
Low-Level Programming Language, Compiler/Assembler
Connection to computer (or LCD) for visualization
DAQ typically already there
Stand Alone Issues ower re uirements
Computer
onnec on o , , er a , re ess Programming-Analysis Environment
OS / Drivers (Real Time ?)
4
Embedded / Small Form Factor Computer
5/28/2018 Arduino Matlab
5/63
,
Relays
Motors
DC/Steppers/Servos
gna mp ca on
Power Suppliers, Batteries
Forward/Reverse, H-Bridges
Displays
5
5/28/2018 Arduino Matlab
6/63
Cost
Eas of Use
Flexibility
Non trivial process even for simple projects
Some challenges are inherent to the multidisciplinary
u u uwith them (e.g. sensors, actuators, power, design)
Some are not e. . debu in C code drivers issues
6
hardware limitations, unreadable manuals, high costs)
5/28/2018 Arduino Matlab
7/63
A few challenges with Mechatronics projects
r u no ntro uct on ot vat on
Motor Control
Arduino target and Useful Links
7
5/28/2018 Arduino Matlab
8/63
Arduino is an open-source microcontroller board, with anassociated development environment.
8
5/28/2018 Arduino Matlab
9/63
The schematics and the software are released under theCreative Commons License
Manufacturing and Distributing an official Arduino product
to be contacted) to make sure that:
Things work properly.
e pro uc s n o e overa pro ec . It is manufactured under reasonably fair labor conditions.
9
Some form of contribution might be traded in this case
5/28/2018 Arduino Matlab
10/63
ATme a328 microcontroller
16 MHz, 32 KB FLASH, 2KB SRAM, 1K EEPROM 19 DIO pins (6 can be 8-bits 500Hz PWM outputs)
6 analog inputs (10 bits over 0-5V range, 15kSPS)
5V operating voltage, 40 mA DC Current per IO Pin
I2C (TWI) fully supported and SPI partially supported
USB connection (FTDI chip converts USB to Serial)
Power jack and optional 9V power supplier
10
5/28/2018 Arduino Matlab
11/63
They extend its functionality to control different devices,acquire data, and so on
Examples: o or epper ervo e s o or on ro
Multichannel Analog and Digital IO Shields
Prototyping Shields
Ethernet and Wireless communication Shields Wave Shields (Audio)
GPS Lo in and Accelerometer Shields
11
Relay Control Shields
5/28/2018 Arduino Matlab
12/63
A few challenges with Mechatronics projects
r u no ntro uct on ot vat on
Motor Control
Arduino target and Useful Links
12
5/28/2018 Arduino Matlab
13/63
Projects requiring Analog and Digital IO
Mechatronics Projects using Servo, DC or Stepper Motors
Projects with volume/size and/or budget constraints
ro ec s requ r ng some amoun o ex y anadaptability (i.e. changing code and functions on the fly)
13
5/28/2018 Arduino Matlab
14/63
Basically any Mechatronics project requiring sensing andacting, provided that computational requirements are not
too high (e.g. cant do image processing with it)
Projects
There is a very large community of people using it for all
kind of projects, and a very lively forum where it is
14
5/28/2018 Arduino Matlab
15/63
Arduino IO Package:Used to perform analog and digital input and output as
Arduino Target:
Used to compile and download Simulink code directly to
e r u no oar
15
5/28/2018 Arduino Matlab
16/63
16
5/28/2018 Arduino Matlab
17/63
.
MATLAB is more interactive, results from Digital/AnalogI/O instructions can be seen immediately without needing
.
It is a good idea even just for algorithm prototyping
MATLAB code is generally more compact and easier to
understand than C (higher-abstraction data types,
, ,of code) which means:
a MATLAB scales better with ro ect com lexit
17
b) People get the job done faster in MATLAB
5/28/2018 Arduino Matlab
18/63
.
For wider-breadth ro ects that mi ht include data
analysis, signal processing, calculations, simulation,statistics, control design ) MATLAB is better suited
Engineering Departments typically need to introduce
MATLAB durin the first ears so this acka e will allow
professors to keep the same environment and students to
practice more MATLAB
People might already be more familiar with MATLAB than
18
5/28/2018 Arduino Matlab
19/63
A few challenges with Mechatronics projects
r u no ntro uct on ot vat on
Motor Control
Arduino target and Useful Links
19
5/28/2018 Arduino Matlab
20/63
USE:
ConnectAssign Pin Mode
Digital Output
Analog InputDisconnect
Un lu
na og u pu
20
5/28/2018 Arduino Matlab
21/63
Use the command a=ar dui no( por t ) , with the rightCOM port as a string input argument, to connect MATLAB
workspace:
>> a=ar dui no( COM5 ) ;
21
5/28/2018 Arduino Matlab
22/63
Use the command a. pi nMode( pi n, st r )to get or set the mode of a specified pin:
Examples:
>> a. pi nMode( 11, ' out put ' )>> a. pi nMode( 10, ' i nput ' )>> val =a. pi nMode( 10)
.
>> a. pi nMode;
22
5/28/2018 Arduino Matlab
23/63
Use the command a. di gi t al Read( pi n) to read thedigital status of a pin:
Examples:
>> val =a. di gi t al Read( 4)
This returns the value (0 or 1) of the digital pin number 4
23
5/28/2018 Arduino Matlab
24/63
>> a. di gi t al Read( 4)
ans =
Command
executed while
button is released
0
>> a. di gi t al Read ( 4) Command
executed while
MATLABCommand
Window
24
ans =
1
u on s presse
5/28/2018 Arduino Matlab
25/63
Use the command a. di gi t al Wr i t e( pi n, val )with the pin as first argument and the value (0 or 1) as
a. di gi t al Wr i t e( 13, 1) ; % set s pi n #13 hi gha. di gi t al Wr i t e( 13, 0) ; % set s pi n #13 l ow
25
5/28/2018 Arduino Matlab
26/63
>> a. di gi t al Wr i t e( 9, 1)
Command
Window
a. g a r e ,
Led OnLed Off
26
5/28/2018 Arduino Matlab
27/63
Use the command val =a. anal ogRead( pi n)with the pin as an integer argument:
Example:
val =a. anal o Read 0 % r eads anal o i n # 0
The returned argument ranges from 0 to 1023
Note that 6 analog input pins (0 to 5) coincide with the
27
g a p ns o an are oca e on e o om r g
corner of the board
5/28/2018 Arduino Matlab
28/63
>> a. anal ogRead( 3)MATLAB
ans =
285
Window
>> a. anal ogRead( 3)
ans =
28
5/28/2018 Arduino Matlab
29/63
Use the command a. anal ogWr i t e( pi n, val ) with thepin as first argument and the value (0 to 255) as second
a. anal ogWr i t e( 11, 90) ; % set s pi n #11 t o 90a. anal ogWr i t e( 3, 10) ; % set s pi n #3 t o 10
29
5/28/2018 Arduino Matlab
30/63
>> a. anal ogWr i t e( 9, 10)
Command
Window
a. ana og r e ,
Led On (20%)Led On (4%)
30
5/28/2018 Arduino Matlab
31/63
Use the command del et e( a) to disconnect the
MATLAB session from the Arduino board:
the IDE environment
31
5/28/2018 Arduino Matlab
32/63
This challenge is described in the last part of the LadyadaArduino tutorial, http://www.ladyada.net/learn/arduino/ and
(user switches mode by pushing a button):
1. All LEDs Off
2. All LEDs On
. s n ng s mu aneous y w var a e requencyregulated by a potentiometer
32
.
variable speed regulated by a potentiometer
5/28/2018 Arduino Matlab
33/63
33
5/28/2018 Arduino Matlab
34/63
INPUTSOUTPUTS
MATLAB
34
USER
5/28/2018 Arduino Matlab
35/63
% initialize pins
disp('Initializing Pins ...');
% get previous state
prev=a.digitalRead(bPin);
% sets digital input pins
a.pinMode(2, 'INPUT');
a.pinMode(3, 'INPUT');
a.pinMode(4, 'INPUT');
% start loop
disp('Starting main loop, push button to change state ...');
% loop for 1 minute
a.pinMode(7, 'INPUT');
a.pinMode(8, 'INPUT');
% sets digital and analog (pwm) output pins
a.pinMode(5, 'OUTPUT'); % pwm available here' '
c
while toc/60 < 1
% read analog input
ain=a.analogRead(aPin);v=100*ain/1024. ,
a.pinMode(9, 'OUTPUT'); % pwm available here
a.pinMode(10,'OUTPUT'); % pwm available here
a.pinMode(11,'OUTPUT'); % pwm available here
a.pinMode(12,'OUTPUT');
a.pinMode(13,'OUTPUT');
% read current button value
% note that button has to be kept pressed a few seconds to make sure
% the program reaches this point and changes the current button value
curr=a.digitalRead(bPin);
% button pin and analog pin
bPin=2;aPin=2;
% initialize state
% button is being released, change state
% delay corresponds to the "on" time of each led in state 3 (wave)
if (curr==1 && prev==0),
state=mod(state+1,4);
' ' ' '
35
state= ; ,
end
5/28/2018 Arduino Matlab
36/63
% toggle state all on or off% wave
if (state==3), ,
for i=9:13,
a.digitalWrite(i,state);
end
end
or = : ,
a.digitalWrite(9+mod(i,5),0);
a.digitalWrite(9+mod(i+1,5),1);
pause(v/200);
end
a.digitalWrite(13,0);
% blink all leds with variable delay
if (state==2),
forj=0:1,
% analog output pins
% update state
prev=curr;
or = : ,
a.analogWrite(i,20*(i-8)*j);
end
% digital output only pins
for i=12:13, % turn everything off
fori=9:13, a.digitalWrite(i,0); end. ,
endpause((15*v*(1-j)+4*v*j)/1000);
end
end
36
5/28/2018 Arduino Matlab
37/63
The schematics was implemented using the prototyping
shield:
This shield allows for an easy prototyping of projects
37
based on the Arduino board
5/28/2018 Arduino Matlab
38/63
Pot toButton tochange
on/off
LEDs
delay
Boardunderneath
38
5/28/2018 Arduino Matlab
39/63
A few challenges with Mechatronics projects
r u no ntro uct on ot vat on
Motor Control
Arduino target and Useful Links
39
5/28/2018 Arduino Matlab
40/63
Servo Ports
# 1 & 2
Motor Ports
# 3 & 4
Stepper Port #2
Motor Ports
# 1 & 2
Arduino
40
Board
underneath
External
Power
Connector
Jumper on == same
power source for both
Arduino and motor shield
5/28/2018 Arduino Matlab
41/63
2 connections for 5V 'hobb ' servos connected to the
Arduino's high-resolution dedicated timer
Up to 4 bi-directional DC motors with individual 8-bit
speed selection
Up to 2 stepper motors (unipolar or bipolar) with single
coil, double coil, interleaved or micro-stepping.
4 H-Bridges: L293D chipset provides 0.6A per bridge
41
. pea w erma s u own pro ec on, . o
5/28/2018 Arduino Matlab
42/63
Plug Board USE:
Connect
Plug Servo Servo Attach
Servo Write
Servo Read
Unplug Servo
Disconnect
ervo e ac np ug oar
42
5/28/2018 Arduino Matlab
43/63
Use the command val =a. ser voSt at us( num) to get
the status of a servo, which can be either:
a ac e rea y or rea or wr e
detached (pin 9 or 10 can be otherwise used)
Examples:val =a. ser voSt at us( 1) ;
a. ser voSt at us( 2) ;a. ser voSt at us;
43
5/28/2018 Arduino Matlab
44/63
Use the command a. ser voAt t ach( num) to attach a
servo to the corresponding pwm pin (servo #1 uses pin
, .
a. ser voAt t ach( 1) ; % at t ach ser vo #1a. ser voAt t ach( 2) ; % at t ach ser vo #2
44
5/28/2018 Arduino Matlab
45/63
Use the command val =a. ser voRead( num) to read the
angle from a servo. The argument is the number of the
.
The returned value is the angle in degrees,
typically from 0 to 180.
Examples:val =a. ser voRead( 1) ; % r ead angl e ser vo #1val =a. ser voRead 2 % r ead an l e ser vo #2
45
5/28/2018 Arduino Matlab
46/63
Use the command a. ser voWr i t e( num, val ) to rotate
a servo of a given angle.
e rs argumen s e num er o e servo, e secon
is the angle.
Examples:
a. ser voWr i t e( 1, 45) ; % r ot at es 45 ser vo #1
46
5/28/2018 Arduino Matlab
47/63
Use the command a. ser voDet ach( num) to detach a
servo from the corresponding pwm pin.
Examples:
a. ser voDet ach 1 % det ach ser vo #1a. ser voDet ach( 2) ; % det ach ser vo #2
47
5/28/2018 Arduino Matlab
48/63
>> a. ser voAt t ach( 2)MATLAB
Command
External Power:6V Lantern Battery
Power Jumperon (suggested)
. ,
>> a. ser voWr i t e(2, 180)
>> a. ser voRead( 2)
n ow:
ans =
180Servo #2
48
5/28/2018 Arduino Matlab
49/63
Plug Board USE:
Connect
Wire Motor
e pee
Run (or Step)
Unwire
Disconnect
np ug
49
5/28/2018 Arduino Matlab
50/63
Use the command val =a. mot or S eed( num, val ) ; to
get or set the speed of a DC motor.
The first argument, num, is the number of the motor (1 to
e secon argumen s e spee o
a. mot or Speed( 4, 200) ;val =a. mot or Speed( 1) ;a. mo or pee ;a. mot or Speed;
50
Note: nothing moves unless we issue a run command
5/28/2018 Arduino Matlab
51/63
. ,
motor.
The first argument, num, is the number of the motor (1 to 4)
the second argument is a string that can be either
forward, backward, release
Examples:a. mot or Run( 1, ' f or war d' ) ;
a. mot or Run( 3, ' backwar d' ) ;a. mot or Run( 1, ' r el ease' ) ;
51
5/28/2018 Arduino Matlab
52/63
a. mo or pee ,
>> a. mot or Run( 3, f orward )
Command
Window:
(running)
Motor #3
52
5/28/2018 Arduino Matlab
53/63
Use the command val =a. st e er S eed( num, val ) ;
to get or set the speed of a stepper motor.
The first argument, num, is the number of the motor (1 to
e secon argumen s e spee n o
a. st epper Speed( 2, 50)val =a. st epper Speed( 1) ;a. s epper pee ;a. st epper Speed;
53
Again, nothing moves unless we issue a step command ...
5/28/2018 Arduino Matlab
54/63
a. st epper St ep( num, st r , st y, st eps) ; to advance a
stepper motor of a certain number of steps.
ere nums t e num er o t e stepper or , st r canbe either forward, backward, release, st y is the style
of motion and can be sin le double interleavemicrostep, st eps is the number of steps (0 to 255)
Examples:a. st epper St ep( 1, ' f or war d , i nt er l eave , 50) ;a. st e er St e ( 1, backwar d , doubl e , 200) ;
54
a. st epper St ep( 1, ' r el ease' ) ;
5/28/2018 Arduino Matlab
55/63
a. s epper pee ,
>> a. st epper St ep( 1, ' backwar d' , ' mi cr ost ep' , 100)
Command
Window:
Arduino Target, Links, Conclusion
Stepper #1 Coil #1
Coil #2
55
5/28/2018 Arduino Matlab
56/63
A few challenges with Mechatronics projects
r u no ntro uct on ot vat on
Motor Control
Arduino target and Useful Links
56
5/28/2018 Arduino Matlab
57/63
Arduino IO:
Used to perform analog and digital input and output from
Used to compile and download Simulink code directly to
the Arduino board
57
5/28/2018 Arduino Matlab
58/63
58
5/28/2018 Arduino Matlab
59/63
59
5/28/2018 Arduino Matlab
60/63
An extensive list of sites where you can buy
an Arduino is here:. .
http://www.adafruit.com/
provides a starter pack that includes pretty much
.
60
5/28/2018 Arduino Matlab
61/63
RC Servos from Pololu:
http://www.pololu.com/catalog/category/23
DC Motors from Pololu:
http://www.pololu.com/catalog/category/51
Jameco for Stepper Motors and pretty much everything:
http://www.jameco.com/
List of Hobbyist and Surplus Stores:
61
p: www. a ya a.ne rary procure o y s . m
5/28/2018 Arduino Matlab
62/63
Official Arduino web site: http://arduino.cc/en/
Knowledge base: http://www.freeduino.org/
Official Getting Started guide:
http://arduino.cc/en/Guide/HomePage
http://www.ladyada.net/learn/arduino/
MATLAB ArduinoIO package, send me an email here:[email protected]
62
mu n r u no arge : searc or n en ra
(or google for Arduino Target)
5/28/2018 Arduino Matlab
63/63
Arduino is an inexpensive open-source microcontroller
board, well suited for a wide range of projects
Arduino + ArduinoIO package + MATLAB =
the MATLAB command line
Arduino + Motor Shield + ArduinoIO package + MATLAB= inexpensive and interactive Motor Control from the
63