®
®
Learning Basic Mechatronics concepts using the Arduino Board and MATLAB , , The MathWorks, 400 Continental Blvd, Suite 600-028, El Segundo, CA, 90245 .
.
. c n I , s k r o W h t a M e h T 9 0 0 2 ©
®
®
A few challenges with Mechatronics projects r u no ntro uct on &
ot vat on
Moto Mo torr Co Cont ntro roll
Arduino target and Useful Links
2
®
®
A few challenges with Mechatronics projects r u no ntro uct on &
ot vat on
Moto Mo torr Co Cont ntro roll
Arduino target and Useful Links
2
®
®
, , , 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
®
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
onnect on to , , er a , re ess Programming-Analysis Environment OS / Drivers (Real Time ?) Embedded / Small Form Factor Computer 4
®
®
,
Relays
Motors
DC/Steppers/Servos gna mp ca on Power Suppliers, Batteries Forward/Reverse, H-Bridges
Displays
5
®
®
Cost Eas of Use Flexibility Non trivial process even for simple projects Some challenges are inherent to the multidisciplinary na ure o e e an ’s goo or s u en s o s rugg e with them (e.g. sensors, actuators, power, design) Some are not e. . debu in C code drivers issues hardware limitations, unreadable manuals, high costs) 6
®
®
A few challenges with Mechatronics projects r u no ntro uct on &
ot vat on
Motor Control
Arduino target and Useful Links
7
®
®
Arduino is an open-source microcontroller board, with an associated development environment.
8
®
®
The schematics and the software are released under the Creative 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.
Some form of contribution might be traded in this case 9
®
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
®
®
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 Relay Control Shields 11
®
®
A few challenges with Mechatronics projects r u no ntro uct on &
ot vat on
Motor Control
Arduino target and Useful Links
12
®
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 an adaptability (i.e. changing code and functions on the fly)
13
®
®
Basically any Mechatronics project requiring sensing and acting, provided that computational requirements are not too high (e.g. can’t 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
®
®
Arduino IO Package: Used to perform analog and digital input and output as
Arduino Target: Used to compile and download Simulink® code directly to t e r u no oar
15
®
®
16
®
®
.
MATLAB is more interactive, results from Digital/Analog I/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 b) People get the job done faster in MATLAB 17
®
®
.
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
®
®
A few challenges with Mechatronics projects r u no ntro uct on &
ot vat on
Motor Control
Arduino target and Useful Links
19
®
®
USE: Connect
Assign Pin Mode Digital Output Analog Input na og u pu
Disconnect Un lu
20
®
®
Use the command a=arduino(‘port’), with the right COM port as a string input argument, to connect MATLAB workspace: >> a=arduino(‘COM5’);
21
®
®
Use the command a.pinMode(pin,str) to get or set the mode of a specified pin: Examples: >> a.pinMode(11,'output') >> a.pinMode(10,'input') >> val=a.pinMode(10) >> a.pinMode;
22
®
®
Use the command a.digitalRead(pin) to read the digital status of a pin: Examples: >> val=a.digitalRead(4)
This returns the value (0 or 1) of the digital pin number 4
23
®
>> a.digitalRead(4) ans =
MATLAB Command Window
®
Command executed while button is released
0 >> a.digitalRead (4) ans =
Command executed while u on s presse
1
24
®
®
Use the command a.digitalWrite(pin,val) with the pin as first argument and the value (0 or 1) as
a.digitalWrite(13,1); % sets pin #13 high a.digitalWrite(13,0); % sets pin #13 low
25
®
®
Command Window >> a.digitalWrite(9,1) >> a. g ta Wr te ,
Led Off
Led On
26
®
Use the command val=a.analogRead(pin) with the pin as an integer argument: Example: val=a.analo Read 0
®
% reads analo
in # 0
The returned argument ranges from 0 to 1023 Note that 6 analog input pins (0 to 5) coincide with the g a p ns o an are oca e on e o om r g corner of the board 27
®
MATLAB Window
®
>> a.analogRead(3) ans = 285 >> a.analogRead(3) ans = 855
28
®
®
Use the command a.analogWrite(pin,val) with the pin as first argument and the value (0 to 255) as second
a.analogWrite(11,90); % sets pin #11 to 90 a.analogWrite(3,10); % sets pin #3 to 10
29
®
®
Command Window >> a.analogWrite(9,10) >> a.ana ogWr te ,
Led On (4%)
Led On (20%)
30
®
®
Use the command delete(a) to disconnect the MATLAB session from the Arduino board:
the IDE environment
31
®
®
This challenge is described in the last part of the Ladyada Arduino tutorial, http://www.ladyada.net/learn/arduino/ and (user switches mode by pushing a button):
1. All LEDs Off 2. All LEDs On 3. s n ng s mu taneous y w t var a e requency regulated by a potentiometer . variable speed regulated by a potentiometer 32
®
®
33
®
®
MATLAB OUTPUTS INPUTS
USER 34
®
% 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'); a.pinMode(7, 'INPUT');
% start loop disp('Starting main loop, push button to change state ...');
a.pinMode(8, 'INPUT'); % sets digital and analog (pwm) output pins a.pinMode(5, 'OUTPUT'); % pwm available here . ,' ' 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'); % button pin and analog pin bPin=2;aPin=2; % initialize state state=0;
®
% loop for 1 minute c while toc/60 < 1 % read analog input ain=a.analogRead(aPin); v=100*ain/1024 % 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 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); '
='
',
='
end
35
®
% toggle state all on or off , for i=9:13, a.digitalWrite(i,state); end end
% blink all leds with variable delay if (state==2), for j=0:1, % analog output pins for i=9:11, a.analogWrite(i,20*(i-8)*j); end % digital output only pins for i=12:13, . , end pause((15*v*(1-j)+4*v*j)/1000); end end
®
% wave if (state==3), or = : , a.digitalWrite(9+mod(i,5),0); a.digitalWrite(9+mod(i+1,5),1); pause(v/200); end a.digitalWrite(13,0);
% update state prev=curr;
% turn everything off for i=9:13, a.digitalWrite(i,0); end
36
®
®
The schematics was implemented using the prototyping shield:
This shield allows for an easy prototyping of projects based on the Arduino board 37
®
Pot to
®
Button to change
on/off LEDs delay
Board underneath
38
®
®
A few challenges with Mechatronics projects r u no ntro uct on &
ot vat on
Motor Control
Arduino target and Useful Links
39
®
®
Servo Ports #1&2
Motor Ports #3&4 Stepper Port #2
Motor Ports #1&2
External Power Connector
Jumper on == same power source for both Arduino and motor shield
Arduino Board underneath 40
®
®
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 . pea w erma s u own pro ec on, . o 41
®
Plug Board Connect Plug Servo
®
USE: Servo Attach Servo Write Servo Read ervo e ac
Unplug Servo Disconnect np ug oar
42
®
®
Use the command val=a.servoStatus(num) to get the status of a servo, which can be either: attac e rea y or rea or wr te detached (pin 9 or 10 can be otherwise used)
Examples: val=a.servoStatus(1); a.servoStatus(2); a.servoStatus;
43
®
®
Use the command a.servoAttach(num) to attach a servo to the corresponding pwm pin (servo #1 uses pin , .
a.servoAttach(1); % attach servo #1 a.servoAttach(2); % attach servo #2
44
®
®
Use the command val=a.servoRead(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.servoRead(1); % read angle servo #1 val=a.servoRead 2 % read an le servo 2
45
®
®
Use the command a.servoWrite(num,val) to rotate a servo of a given angle. e rst argument s t e num er o t e servo, t e secon is the angle.
Examples: a.servoWrite(1,45); % rotates 45° servo #1
46
®
®
Use the command a.servoDetach(num) to detach a servo from the corresponding pwm pin. Examples: a.servoDetach 1 % detach servo #1 a.servoDetach(2); % detach servo #2
47
®
MATLAB Command n ow:
Power Jumper on (suggested)
®
External Power: 6V Lantern Battery
>> a.servoAttach(2) a.servo r
e
,
>> a.servoWrite(2,180) >> a.servoRead(2) ans =
Servo #2
180
48
®
Plug Board Connect Wire Motor
®
USE:
et pee Run (or Step)
Unwire Disconnect np ug
49
®
®
Use the command val= val=a. a.mo moto torS rS eed( eed(nu num, m,va val) l); ; 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.motorSpeed(4,200); val=a.motorSpeed(1); a.mo or pee ; a.motorSpeed;
Note: nothing moves unless we issue a run command … 50
®
.
®
,
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.motorRun(1,'forward'); a.motorRun(3,'backward'); a.motorRun(1,'release');
51
®
>> a.mo or pee
Command Window:
®
,
>> a.motorRun(3,’forward’)
(running)
Motor #3
52
®
®
Use the command val=a.ste erS 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.stepperSpeed(2,50) val=a.stepperSpeed(1); a.s epper pee ; a.stepperSpeed;
Again, nothing moves unless we issue a step command ... 53
®
®
a.stepperStep(num,str,sty,steps); to advance a
stepper motor of a certain number of steps. ere num s t e num er o t e stepper 1 or 2 , str can be either “forward”, “backward”, “release”, sty is the style of motion and can be “sin le” ‘double’ ‘interleave’ ‘microstep’, steps is the number of steps (0 to 255) Examples: a.stepperStep(1,'forward’,‘interleave’,50); a.ste erSte (1 ‘backward’ ‘double’ 200) a.stepperStep(1,'release'); 54
®
>> a.s epper pee
Command Window:
®
,
>> a.stepperStep(1,'backward','microstep',100)
Arduino Target, Links, Conclusion
Stepper #1
Coil #1
Coil #2
55
®
®
A few challenges with Mechatronics projects r u no ntro uct on &
ot vat on
Motor Control
Arduino target and Useful Links
56
®
®
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
®
®
58
®
®
59
®
®
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
®
®
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: p: www. a ya a.ne rary procure o
ys. m 61
®
®
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] mu n r u no arge : searc or n en ra (or google for “Arduino Target”) 62