Servo Control

Embed Size (px)

DESCRIPTION

Control de servomotor

Citation preview

Servo ControlThis example shows how to use Simulink Support Package for Arduino Hardware to control a standard servo motor.Supported Hardware: Arduino Esplora Arduino Fio Arduino Leonardo Arduino Lilypad USB Arduino Mega 2560 Arduino Mega ADK Arduino Micro Arduino Mini Arduino Nano 3.0 Arduino Pro Arduino UnoAvailable versions of this example:Arduino Mega 2560 board:arduinomega2560_servocontrol_sweepThe provided model is pre-configured for Arduino Mega 2560 and can be run on any of the borad listed in the "Suppoerted Hardware" section, by changing the "Target Hardware" parameter in the configuration parameters of the model as described in Task 1 of this example.On this page

IntroductionPrerequisitesRequired HardwareTask 1 - Configure the Model for Supported Arduino HardwareTask 2 - Connect the Servo Motor to the Arduino BoardTask 3 - Set the Servo Motor Position via an Internal SourceTask 4 - Control the Servo Motor Position via a PotentiometerTask 5 - Control the Servo Motor Position via a PhotocellOther Things to TrySummary

IntroductionSimulink Support Package for Arduino Hardware enables you to create and run Simulink models on Arduino board. The target includes a library of Simulink blocks for configuring and accessing Arduino sensors, actuators and communication interfaces.In this example you will learn how to create Simulink model that controls a standard servo motor. In a standard servo motor, the shaft position can be precisely set, usually between 0 and 180 degrees. Servo motors are used in many industrial, military and consumer applications and products.PrerequisitesWe recommend completingGetting Started with Arduino Hardwareexample.Required HardwareTo run this example you will need the following hardware: Supported Arduino board USB cable Standard servo motor Potentiometer CdS photocell 10 kOhm resistor Breadboard wires Small breadboard (recommended)

Task 1 - Configure the Model for Supported Arduino HardwareYou will perform this task if your Arduino board is not Arduino Mega 2560 hardware.In this task, you will configure the model for the supported Arduino board.1.In your Simulink model, clickTools > Run on Target Hardware > Options....2.When theConfiguration Parameterspage opens up, set theTarget hardwareparameter to your required Arduino hardware. Do not change any other settings.3.ClickOK.Task 2 - Connect the Servo Motor to the Arduino BoardIn this task, you will connect your motor to the Arduino board. Servo motors have three wires: power, ground, and signal. Connect them as described below.1.Connect the power wire (usually red) to the 5V pin.2.Connect the ground wire (usually black) to the ground pin.3.Connect the signal wire (usually orange) to digital pin 4. Refer toPin Mapping on Arduino Blocksin the documentation for supported pin numbers.

Task 3 - Set the Servo Motor Position via an Internal SourceIn this task you will set the position of the servo motor shaft using an internal source. The shaft angle will vary between 0 and 180 degrees, up and down.1.Open theServo Controlmodel.2.Notice theStandard Servo Writeblock. The block sets the new angle of the servo motor shaft every 0.01 second.3.In your Simulink model, clickTools > Run on Target Hardware > Options.4.When theConfiguration Parameterspage opens up, set theTarget hardwareparameter to supported Arduino hardware which is connected to your computer. Do not change any other settings.5.In your Simulink model, click theDeploy To Hardwarebutton on the toolbar.6.When the model starts running on the Arduino board, observe the motor shaft position sweeping between 0 and 180 degrees.Task 4 - Control the Servo Motor Position via a PotentiometerIn this task you will set the position of the servo motor shaft manually, using a potentiometer. Connect the outer potentiometer terminals to 5V and GND pins on your Arduino board. Connect the middle terminal to analog input pin 0. As your rotate the potentiometer, its resistance changes. As a result, the voltage at the analog input pin changes as well. Your task is to set the servo motor angle proportional to this voltage.

1.In MATLAB, select HOME > New > Simulink Model.2.Entersimulinkat the MATLAB prompt. This opens the Simulink Library Browser.3.In the Simulink Library Browser, navigate toSimulink Support Package for Arduino Hardware > Common.4.Drag theAnalog Inputblock into the model. Double-click the block and set thePin numberto 0, and theSample timeto 0.01 second.5.Drag theStandard Servo Writeblock into the model. Double-click the block and set thePin numberto 4.6.Connect theAnalog Inputand theStandard Servo Writeblocks.7.From Simulink Math Operations library, drag theGainblock into the model and drop it on the line connecting theAnalog Inputand theStandard Servo Writeblock. Double-click theGainblock and set its value to 0.1760 (maximum servo motor displacement in degrees divided by analog input digital resolution i.e. 180/1023).8.In your Simulink model, clickTools > Run on Target Hardware > Prepare To Run....9.When theConfiguration Parameterspage opens up, set theTarget hardwareparameter to supported Arduino hardware. Do not change any other settings.10.ClickOK.11.In your Simulink model, click theDeploy To Hardwarebutton on the toolbar. The model will now be deployed to the Arduino hardware.12.When the model starts running on Arduino board, observe the motor shaft sweeping between 0 and 180 degrees.If you have trouble creating the model as described above, you can use theServo Controlmodel.Task 5 - Control the Servo Motor Position via a PhotocellIn this task you will create a model that sets the position of the servo motor according to light intensity detected by a photocell. In a dark room the motor shaft should be positioned at 0 degrees, while in a bright room it should be positioned at 180 degrees.Use a photocell and a resistor instead of the potentiometer you used in Task 4. Connect one end of the photocell to the 5V pin on the Arduino board. Next, connect one end of the resistor to the GND pin on the board. Connect the other ends of the photocell and the resistor together and then to the analog input pin 0 on the board.

The model will be similar to the one used in Task 4.Other Things to TryExperiment with other blocks in the Arduino block library. For example: UseDigital Inputblock to adjust the motor shaft position depending on the external control signal or signals. UseStandard Servo Readblock to read the motor shaft position and compare it with the desired position.SummaryThis example showed you how to use Simulink Support Package for Arduino Hardware to control a standard servo motor. In this example you learned that: Standard Servo Write block allows you to set the servo motor shaft position, usually in the range from 0 to 180 degrees. Analog Input block measures the voltage applied to an Arduino analog input pin. Variety of sensors can be attached to analog pins.

Getting Started with Arduino HardwareThis example shows how to use Simulink Support Package for Arduino Hardware to run a Simulink model on Arduino board.Supported Hardware: Arduino Esplora Arduino Fio Arduino Leonardo Arduino Lilypad USB Arduino Mega 2560 Arduino Mega ADK Arduino Micro Arduino Mini Arduino Nano 3.0 Arduino Pro Arduino UnoAvailable versions of this example:Arduino Mega 2560 board:arduinomega2560_gettingstartedThe provided model is pre-configured for Arduino Mega 2560 and can be run on any of the borad listed in the "Suppoerted Hardware" section, by changing the "Target Hardware" parameter in the configuration parameters of the model as described in Task 4 of this example.On this page

IntroductionPrerequisitesRequired HardwareModelTask 1 - Connect an LED to an Arduino Output PinTask 2 - Review Arduino Block LibraryTask 3 - Create a Model for Arduino HardwareTask 4 - Configure and Run the Model on Supported Arduino HardwareOther Things to TrySummary

IntroductionSimulink Support Package for Arduino Hardware enables you to create and run Simulink models on Arduino board. The target includes a library of Simulink blocks for configuring and accessing Arduino sensors, actuators and communication interfaces. Additionally, the target enables you to monitor and tune algorithms running on Arduino board from the same Simulink models from which you developed the algorithms.In this example you will learn how to create and run a simple Simulink model on Arduino board. See other examples for Arduino board to learn how to use External mode and to learn how to implement more complex algorithms.PrerequisitesIf you are new to Simulink, we recommend completingInteractive Simulink Tutorial, reading the Getting Started section of theSimulink documentationand runningSimulink Getting Started example.Required HardwareTo run this example you will need the following hardware: Supported Arduino board USB cable LED 220 Ohm resistor Breadboard wires Small breadboard (recommended)ModelThe following figure shows the example model:

Task 1 - Connect an LED to an Arduino Output PinIn this task, you will connect an LED to an Arduino output pin so you can see changes in the logical state of the pin.1.Attach one end of the 220 Ohm resistor to output pin 9 on the Arduino board. Use the recommended breadboard and the breadboard wires.2.Attach the long leg (positive) of the LED to the resistor. Attach the short leg (negative) to the ground pin on the Arduino board.

Task 2 - Review Arduino Block LibrarySimulink Support Package for Arduino Hardware provides an easy way to create algorithms that use Arduino sensors and actuators by using the blocks that can be added to your Simulink model. The blocks are used to configure the associated sensors and actuators, as well as to read and write data to them.1.Entersimulinkat the MATLAB prompt. This opens the Simulink Library Browser.2.In the Simulink Library Browser, navigate toSimulink Support Package for Arduino Hardware > Common.3.Double-click theDigital Outputblock. Review the block mask, which contains a description of the block and parameters for configuring the associated Arduino digital output pin.

Task 3 - Create a Model for Arduino HardwareIn this task, you will create a simple Simulink model that changes the state of the Arduino digital output pin.1.In MATLAB, select HOME > New > Simulink Model.2.Drag thePulse Generatorblock from the SimulinkSourceslibrary to your model.3.Double-click thePulse Generatorblock. Set thePulse typeto parameter toSample basedand set theSample timeparameter to 0.1 second.4.Drag theDigital Outputblock to the model. Use the default block settings.5.Connect thePulse Generatorblock to theDigital Outputblock.Task 4 - Configure and Run the Model on Supported Arduino HardwareIn this task, you will configure and run your model on the supported Arduino board.1.Connect the Arduino board to your computer with a USB cable.2.In your Simulink model, clickTools > Run on Target Hardware > Prepare To Run....3.When theConfiguration Parameterspage opens up, set theTarget hardwareparameter to supported Arduino hardware which is connected to your computer. Do not change any other settings.4.ClickOK.5.In your Simulink model, click theDeploy To Hardwarebutton on the toolbar. The model will now be deployed to the connected Arduino hardware.6.Look at the LED attached to pin 9. The LED should blink one time every second.7.Save your model.Apre-configured modelis included for your convenience.Other Things to TryExperiment with other blocks in the Arduino block library. For example: Create and run a model that turns the LED on if a signal is applied to a digital input pin. Create and run a model that repeatedly brightens and dims an LED. Hint: use the PWM block.SummaryThis example introduced the workflow for creating an algorithm from a Simulink model and then running it on the supported Arduino board. In this example you learned that: Simulink Support Package for Arduino Hardware provides blocks for configuring, reading from and writing to Arduino sensors and actuators. You can use theDeploy To Hardwarebutton to configure and run the model on supported Arduino board.

MATLAB CommandYou clicked a link that corresponds to this MATLAB command:arduinomega2560_gettingstartedRun the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.