Arduino Matlab
Arduino Matlab
Giampiero Campa Campa, PhD PhD, Technical Evangelist The MathWorks, 400 Continental Blvd, Suite 600-028, El Segundo, CA, 90245 giampiero campa@mathworks com [email protected]
2009 The MathWorks, Inc.
Outline
A few challenges with Mechatronics projects Arduino Ad i I Introduction t d ti & M Motivation ti ti Analog and Digital IO Motor Control Arduino target and Useful Links
Computer
Connection C ti t to DAQ (USB (USB, PCI PCI, S Serial, i l Wi Wireless) l ) Programming-Analysis Environment OS / Drivers (Real Time ?) Embedded / Small Form Factor Computer
4
Displays
Outline
A few challenges with Mechatronics projects Arduino Ad i I Introduction t d ti & Motivation M ti ti Analog and Digital IO Motor Control Arduino target and Useful Links
Arduino
Arduino is an open-source microcontroller board, with an associated development environment.
Philosophy
The schematics and the software are released under the Creative Commons License Manufacturing and Distributing an official Arduino product is subject to a few restrictions (basically the authors need to be contacted) to make sure that:
Things work properly. The Th product d t fits fit i into t th the overall ll project. j t It is manufactured under reasonably fair labor conditions.
10
Shields
Shields are boards to be mounted on top of the Arduino They extend its functionality to control different devices, acquire data, and so on Examples:
Motor/Stepper/Servo M t /St /S Shields Shi ld (Motor (M t Control) C t l) Multichannel Analog and Digital IO Shields Prototyping Shields Ethernet and Wireless communication Shields Wave Shields (Audio) GPS Logging and Accelerometer Shields Relay Control Shields
11
Outline
A few challenges with Mechatronics projects Arduino Ad i I Introduction t d ti & Motivation M ti ti Analog and Digital IO Motor Control Arduino target and Useful Links
12
13
MathWorks Solutions
Arduino IO Package: Used to perform analog and digital input and output as well as motor control from the MATLAB command line
Arduino Target: Used to compile and download Simulink code directly to th A the Arduino d i b board d
15
Arduino IO : Concept
16
Outline
A few challenges with Mechatronics projects Arduino Ad i I Introduction t d ti & M Motivation ti ti Analog and Digital IO Motor Control Arduino target and Useful Links
19
USE: Assign Pin Mode Digital Input Digital Output Analog Input A l O Analog Output t t
Disconnect Unplug p g
20
Connect
Use the command a=arduino(port), with the right COM port as a string input argument, to connect MATLAB with the board and create an arduino object in the workspace: >> a=arduino(COM5);
21
22
23
Command executed while button is released Command executed while b tt is button i pressed d
25
Led Off
Led On
26
28
29
Led On (4%)
Led On (20%)
30
Disconnect
Use the command delete(a) to disconnect the MATLAB session from the Arduino board: >> delete(a); This renders the serial port available for other sessions or the IDE environment
31
Schematics
33
MATLAB
INPUTS
Information Flow
OUTPUTS
USER
34
MATLAB Code
% initialize pins disp('Initializing p( g Pins ...'); ) % sets digital input pins a.pinMode(2, 'INPUT'); a.pinMode(3, 'INPUT'); a.pinMode(4, 'INPUT'); a.pinMode(7, 'INPUT'); a.pinMode(8, 'INPUT'); % sets digital and analog (pwm) output pins a.pinMode(5, 'OUTPUT'); % pwm available here a pinMode(6 'OUTPUT'); a.pinMode(6, 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; t t 0 % get previous state prev=a.digitalRead(bPin); % start loop disp('Starting main loop, push button to change state ...'); % loop for 1 minute tic 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); disp(['state disp([ state = ' num2str(state) ', delay = ' num2str(v/200)]); end
35
MATLAB Code
% toggle state all on or off if (state<2), (state<2) for i=9:13, a.digitalWrite(i,state); end end % wave if (state==3), f i=4:8, for i 48 a.digitalWrite(9+mod(i,5),0); a.digitalWrite(9+mod(i+1,5),1); pause(v/200); end a.digitalWrite(13,0); end
% blink all leds with variable delay if (state==2), for j=0:1, % analog output pins f i=9:11, for i 9 11 a.analogWrite(i,20*(i-8)*j); end % digital output only pins for i=12:13, a digitalWrite(i j); a.digitalWrite(i,j); end pause((15*v*(1-j)+4*v*j)/1000); end end
36
Prototyping Shield
The schematics was implemented using the prototyping shield:
This shield allows for an easy prototyping of projects based on the Arduino board
37
Implementation
38
Outline
A few challenges with Mechatronics projects Arduino Ad i I Introduction t d ti & M Motivation ti ti Analog and Digital IO Motor Control Arduino target and Useful Links
39
Motor Shield
Servo Ports #1&2
Jumper on == same power source for both Arduino and motor shield
USE: Servo Attach Servo Status Servo Write Servo Read S Servo D t h Detach
42
43
Servo Attach
Use the command a.servoAttach(num) to attach a servo to the corresponding pwm pin (servo #1 uses pin #10 servo #2 uses pin #9) #10, #9). Examples: a.servoAttach(1); % attach servo #1 a.servoAttach(2); % attach servo #2
44
Servo Read
Use the command val=a.servoRead(num) to read the angle from a servo. The argument is the number of the servo. servo The returned value is the angle in degrees, yp y from 0 to 180. typically
Examples: val=a.servoRead(1); % read angle servo #1 val=a.servoRead(2); val a.servoRead(2); % read angle servo #2
45
Servo Write
Use the command a.servoWrite(num,val) to rotate a servo of a given angle. The Th first fi t argument t is i the th number b of f the th servo, the th second d is the angle.
46
Servo Detach
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
Servo Example
MATLAB Command Wi d Window:
>> a.servoAttach(2) >> a a.servoWrite(2,5) servoWrite(2 5) >> a.servoWrite(2,180) >> a.servoRead(2) ans =
Servo #2
180
48
USE:
49
DC Motor Speed
Use the command val=a.motorSpeed(num,val); p , to get or set the speed of a DC motor. The first argument, num, is the number of the motor (1 to 4) the th second d argument ti is th the speed d (0 t to 255) Examples: a.motorSpeed(4,200); val=a.motorSpeed(1); a.motorSpeed(3); t S d(3) a.motorSpeed; Note: nothing moves unless we issue a run command
50
DC Motor Run
Use the command a.motorRun(num,str); a motorRun(num str); to run a given DC 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
DC Motor: Example
MATLAB Command Window:
>> a.motorSpeed(3,100) t S d(3 100) >> a.motorRun(3,forward)
(running)
Motor #3
52
Stepper #1
Coil #1
Coil #2
55
Outline
A few challenges with Mechatronics projects Arduino Ad i I Introduction t d ti & M Motivation ti ti Analog and Digital IO Motor Control Arduino target and Useful Links
56
Arduino Target
Arduino IO: Used to perform analog and digital input and output from the MATLAB command line Arduino Target: Used to compile and download Simulink code directly to the Arduino board
57
58
Arduino Examples
59
60
Conclusion
Arduino is an inexpensive open-source microcontroller board, well suited for a wide range of projects Arduino + ArduinoIO package + MATLAB = inexpensive and interactive Analog and Digital IO from the MATLAB command line Arduino + Motor Shield + ArduinoIO package + MATLAB = inexpensive and interactive Motor Control from the MATLAB command line
63