Programming With MATLAB/Simulink - NDSU - North · PDF fileBEST Robotic, Inc. MATLAB/Simulink...

37
BEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink September 20, 2014 1 BISON BEST

Transcript of Programming With MATLAB/Simulink - NDSU - North · PDF fileBEST Robotic, Inc. MATLAB/Simulink...

Page 1: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

BEST Robotic, Inc.MATLAB/Simulink Team Training

Programming With MATLAB/Simulink

September 20, 2014 1BISON BEST

Page 2: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

What You’ll Need… Minimum System Requirements

Microsoft Windows XP or Later 32-bit or 64-bit machine Administrator Access on the PC Internet connection

Software required MATLAB R2014a free version available at

http://www.mathworks.com/academia/student-competitions/best-robotics/ Registration and application required; approval expected in three days ! Installation instructions will be provided in the approval email

Associate to your license to your registered account Download and install MATLAB from the link provided (about 40 minutes) Activate the software Install the VEX Support Package and Companion App (about 15 minutes)

September 20th, 2014 2Bison BEST

Page 3: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Key Software Locations

August 1, 2010 3Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 4: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Click on the MATLAB r2014a link

Starting MATLAB

August 1, 2010 4Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 5: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Something on MATLAB

August 1, 2010 5Copyright © 2010 BEST Robotics, Inc. All rights reserved.

MATLAB Window Opens: MATLAB means Matrix Laboratory. It is textural programming environment. It can execute commands directly as typed in the command

window or run a script code from a saved file

Page 6: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Choose Apps from the main menu

For BEST Robotics tools

August 1, 2010 6Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 7: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

….Best Robotics tools

September 20th, 2014 Bison BEST 7

• Choose VEX Companion

Page 8: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Using VEX Support

September 20th, 2014 Bison BEST 8

Mathworks VEX support is embedded in Simulink, which is accessed using the following interface

Page 9: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

MATLAB Simulink Simulink is MATLAB’s graphical programming

interface. Programming is accomplished by connecting various graphical

icons in a specific order. Simulink graphical icons are collected in what are known as

Libraries. Simulink programs are known as Models

The Simulink Library collection is opened by clicking the Simulink library icon

August 1, 2010 9Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 10: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

September 20th, 2014 Copyright © 2010 BEST Robotics, Inc. All rights reserved. 10

Simulink VEX Library• To open Simulink VEX Library, • Click Open Library

NOTE: The Library window may be hidden far to the right of your screen

Page 11: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Before Creating your Program You must know what you want to achieve The connection layout of motors and servos

to the Cortex controller. The assignment of Joystick keys and their

intended control functions. How the joysticks will control the motors and

servos Any other interaction between the Cortex

controller and the hardware such as sensors

September 20th, 2014 Bison BEST 11

Page 12: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

The Typical BEST Robot Model The robot can be driven either in Tank or in Arcade

mode. The joystick has four analog channels and four digital

channels The controller can drive Up to 10 motors and servos in analog mode Up to 4 servos in digital mode Can read up to 8 analog sensors (such as potentiometers) Can read up to 8 digital sensors (such as limit switches)

Simulink library has an icon for each of the possible functions: to read the joystick, to drive motors and servos, and to read sensors.

Our example robot will run in arcade driven by two motors; there will be two actuator motors, one with a limit switch and four servos Analog joysticks to motors and digital sticks to servos

September 20th, 2014 12Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 13: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Creating a Simulink Robot Program Open a robot model window by clicking the “Create

new model” button on the VEX Support Package Companion menu

September 20th, 2014 Bison BEST 13

Page 14: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

The BEST Robot Model

August 1, 2010 14Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 15: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

+ 127

+ 127

-127

-127

+ 127

+ 127

-127

-127

The joystick and accelerometer limit return values are +127 and -127 as shown in the figure. The buttons have a return value of 0 when pressed and 1 when released.

Accelerometer rotate right limit + 127

Accelerometer rotate left limit - 127

Accelerometer rotate forward limit + 127

Accelerometer rotate back limit -127

Reading the Joystick Signals

Y Axis

X A

xis

September 20th, 2014 15Copyright © 2010 BEST Robotics, Inc. All rights reserved.

4 Analog Signals4 Digital Signals4 Accelerometer Signals

Page 16: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

September 20th, 2014 16Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 17: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Start building your robot Click and drag one Game pad button and one one

game pad joystick.

September 20th, 2014 Bison BEST 17

Page 18: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

…robot building Make four copies of gamepad joystick and four

copies of gamepad buttons

September 20th, 2014 Bison BEST 18

Page 19: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Actuating Motors and Servos

September 20th, 2014 19Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 20: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Actuator Controls

August 1, 2010 20Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 21: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

August 1, 2010 21Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Add motors and Servos

Page 22: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Other Model Functions

August 1, 2010 22Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 23: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Contents of the Utilities module

September 20th, 2014 Bison BEST 23

Page 24: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Motors Drag the Arcade module into your model. Connect two motors

and two analog joysticks as

Drag the two digital inputs (to serve as limit switches) and one Limit switch control block in the model. Connect the motor, limit switch and joystick

September 20th, 2014 Bison BEST 24

Page 25: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Bring as Many Functions asNeeded, and Connect

September 20, 2014 25Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 26: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Now, Set the Channels

August 1, 2010 26Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 27: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Channles for the Digital Input Limit Switches

September 20th, 2014 Bison BEST 27

Page 28: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Working with Digital Joysticks Analog joysticks spits numbers from -127 to +127, i.e.

while digital joysticks spits only two numbers, 0 and 1.

Therefore, digital joystick commands are ON and OFF only, there are no intermediate values. You can have slow and high speeds or stop the servo in between fully open and fully closed.

When a digital joystick is connected to the servo, we need to translate its value of 1 as 127using a multiplier

Open the Mathematical tools library, and get a GAIN

September 20th, 2014 Bison BEST 28

Page 29: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Drag the Gain icon and make as many copies as the digital joysticks that you have

September 20th, 2014 Bison BEST 29

Page 30: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Set the multiplier Double click the gain to open its properties window

Set a Gain or multiplier of 127 for all digital joysticks that drive servos (or motors)

September 20th, 2014 Bison BEST 30

Page 31: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

The final model

September 20th, 2014 Bison BEST 31

Page 32: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

After Building the Model

August 1, 2010 32Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Save the simulink model file You may want to Simulate the model before

downloading into your Vex Controller.

Open the utilities block again (if it is closed) Pay attention to the Simulation Input, and the

Simulation Output collections

Page 33: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

August 1, 2010 33Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 34: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Simulating the Arcade

August 1, 2010 34Copyright © 2010 BEST Robotics, Inc. All rights reserved.

• Pull a variable input, a the field simulator and a toggle. Make as mane copies as necessary

• Connect the variable inputs to the inputs of the joysticks• Connect the toggles to the inputs of the joysticks• Connect the field simulator to the two drive motors

Page 35: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Simulation Control

August 1, 2010 35Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 36: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

After Simulation Remove all simulation signals Save your model file Compile the model to generate C-codes and

upload into Cortex microcontroller. Make sure Cortex is connected Choose Code-> C/C++ Code -> Build Model

August 1, 2010 36Copyright © 2010 BEST Robotics, Inc. All rights reserved.

Page 37: Programming With MATLAB/Simulink - NDSU - North  · PDF fileBEST Robotic, Inc. MATLAB/Simulink Team Training Programming With MATLAB/Simulink. September 20, 2014. BISON BEST 1

Additional support http://www.mathworks.com/academia/student-competitions/best-robotics/

September 20th, 2014 Bison BEST 37