Friday, June 24, 2016

Viper Single Motor Simulink Control

Please Check Click Here for the Latest Release of the VIPER Blog

Day 19 June 16th, 2016

This entry will show the basic setup to run the Viper Telescope motors using Simulink, a microcontroller and the power supply units. Below is the end code for one of the motors. By the end of this blog one should be able to create a running model that allows complete operation of the DC Motors that are installed onto the telescope.

Completed Model: This is the simulink block to control one of the brake and motor combinations of the Viper Telescope. This Simulink code ran on an Arduino Mega microcontroller which facilitated the communication between the power supplies and the operator (Simulink user). 

1. Arduino Support Simulink INSTALL

2. Motor Control Overview, Simulink/Microcontroller




Step 1: Installation of Arduino Support to MATLAB SIMULINK

Follow the links to these sites to install the latest support packages for the Arduino Microcontrollers if they are yet not installed. If these are installed scroll to Step 2.

Method 1: Download and Install Outside of Matlab Environment




Method 2: Download Using Matlab Environment 


1. Open MATLAB and select the Add-Ons arrow and then select Get Add-Ons tab. 

Inline image 2


2. This screen will appear after clicking. In the search bar type arduino then search should be selected.

Inline image 4

3. After searching for arduino a page similar to this will appear.

Inline image 5


Download and Install using the MATLAB software. Restart MATLAB completely.




Step 2: Creating the Simulink Block to Operate the Microcontroller and Power Supply

Follow these steps to connect the Power Supply (motor and brake control) to the User/Operator (Simulink) via a Microcontroller (Arduino Mega 2560).

1. Open Simulink in MATLAB

Arduino support package must be installed at this point, if not go to Step 1. Open MATLAB and enter the Simulink environment by either typing 'Simulink' into the Matlab Command Window or by selecting the Simulink Block in the Simulink Module within the HOME tab.


Method 1: MATLAB Command Window

Using MATLAB's Command Window to open Simulink. To open Simulink type 'simulink' into the command window, omit the apostrophes.


After this command is entered the Simulink Initialization box will appear.

Continue to the end of Method 2.

Method 2: Simulink Block

Using MATLAB's HOME Tab to Open Simulink by clicking the Simulink Block. Select the HOME tab on the top right of your MATLAB environment, then select the Simulink Block to the right of the CODE module. As shown in the photo above at the far right. This is the same action as typing 'simulink' into the Command Window.
After clicking the Simulink Block the Simulink Initialization box will appear.





End of Method 2.

Either way is acceptable as long as you can open the Simulink environment.

Simulink Main Menu

Simulink is now open, Go Bananas. Go B.A-N.A-NA.S.

2. Creating an Arduino Environment in Simulink

This is a very important set of steps to ensure that running into problems using any Arduino becomes a thing of the past. Open Simulink to the Main Menu to begin.

1. From the Simulink Main Menu double click the Blank Model block. It is located within the Simulink Drop Down Menu, it will be the top right block.
Selecting Blank Model to start using Simulink.
2. Once the Blank Model has loaded and opened fully save as 'Arduino_Blank' or as to what you prefer so that it will be saved as an Arduino starting block. This will be helpful for the future to that you do not have to go through these steps each time you want to use an Arduino. Also make the window larger so that it will become easier to use the tools and place the blocks that are utilized.

After opening a Blank Model this window will appear, this the Blank Model.
Help yourself out by making the Window Larger as it will expand the tool boxes.


3. Change the running time to infinite by typing 'inf' where the '10.0' is in the top tool bar. This will allow the program you create to run until the operator quits. Next to that running time input is a deployment drop down menu with Normal as the default selection, change it to External so that the arduino will be connected to.


Default Set Parameters for Running Time and Deployment type. Default Running Time is set to 10.0 and default Deployment type is Normal. These need to be changed for the purposes of the VIPER motor control.  
Showing Location of where to change 10.0 to inf. Next to that is the Normal/External selection in a drop down menu. 
To complete this step your window settings for Running Time and Deployment type should mirror the above example.

4. Changing from No Equipment Employment to Arduino Hardware Employed.

Start by clicking the Gear  on the toolbar called Model Configuration Parameters
Entering Model Configuration Parameters Menu.


Once opened, this may take a minute, select the Hardware Implementation section underneath the Select: Section. Select the board you are using, allow for it to load the libraries.
Selecting Hardware Implementation within the Model Configuration Parameters Menu.
Loading Libraries after selecting the Arduino Mega 2560 board, your board may differ, that is okay. Select the exact board you're implementing here to avoid issues. 
Underneath the Hardware board settings > Target Hardware Resources select to Build, Load and run your program in the Build Action: drop down menu.
Select Build, Load and Run from Build Action. After this is clicked a loading screen will pop up for a few seconds. 
Select OK  at the bottom of the menu. Save this file again so you can access it later on and not have to go through these steps.

3. Creating a Simulink Block to Command the Microcontroller

Open your arduino template if it is not already opened. Select 'save as' so you can keep the original template as is and you can freely edit this "new" model. 

1. Using the Library Browser to select Blocks to use.

Select the Library Browser icon  to open the library browser to search for tools to use.
Opening the Library Browser
Library Browser Opened, with the Simulink selected. 
Select the Simulink Support Package for Arduino Hardware towards the bottom of the directories. Then, select the blue Common box. 
Common Arduino Hardware is opened, you can select any of these blocks now by clicking and dragging them onto the workspace. To adjust parameters for any block simply double click and its menu will appear. 
Follow the above instructions for the first directory Simulink to also find useful tools.
If you cannot find what you're looking for search for it on the top of the window. This feature will sift through all of the directories for you and display them in a neat layout in the result display. Click the Magnifying Glass A  to search, hitting enter does not always respond.



Below is the current working model of Micro Controlled VIPER Motor Control, I am not going to display step by step on how to make it as the tools are above and it is not tricky. I will, however, go over the attributes of each tool block and why it has been selected.
Working Model

2. The Brakes -- Technical
The motors that adjust the Elevation angle of the VIPER Radio Telescope have brakes that are engaged when no voltage is applied across them. To spin the motor the brakes need to be disengaged.
To do this we must turn a power supply that will provide 90 VDC to the + and - leads of the brake terminals. This whole process will be explained in another blog altogether, and will be linked as soon as it is published. This blog will focus on the Microcontroller side of things.

a. Microcontroller Wiring
Below is a diagram of the Microcontroller Relay Wiring that will connect the Arduino Mega 2560 to the Sorensen DCR-150-6B2 Power Supply.
Note: The arduino selected in the drawing is an Arduino Uno, but the pin locations are correct. The Arduino's Digital PIN4 is connected to an end of the relay's inductor with the other end of the inductor connected to the ground of the arduino. This allows the arduino to enable the selected pin to turn the relay on or off depending upon desired state. On the other end of the relay on the Common (C) input is Terminal 1 of the TB3 rail on the back of the Sorensen Power Supply. On the Normally Closed (NC) input connect Terminal 11 of the TB3 rail. When the power supply is turned on and these wires are connected together it goes into standby mode. To turn off standby mode, disengaging the brakes, you may move the motors. The power supply should be set up before hand so that the brakes will be sent a 90 VDC potential.

b. Simulink
Above is a snapshot of the Brake section of the working model. 

     First, a Non Pulse Width Modulation (PWM) pin must be selected on the microcontroller. In the case of the Arduino series, a PWM pin is marked with a ~ sign or it is clearly labeled PWM. PIN4 of both the Arduino Uno and Mega 2560 are standard Digital In/Out (Digital I/O) pins, so we will use them. Use up all strict I/O pins first, so that the PWM pins are available for processes that require a PWM pin and the user does not have to reconfigure later. If you run out of Digital I/O pins then tap into the PWM pins for the Digital I/O purpose.

     To employ the Digital Output block go into the Library Browser and either search for Digital Output (selecting the Arduino version) or select from the Common box of the Simulink Support Package Arduino Hardware, then click and pull the Digital Output block onto the model.
     This needs to have either a value of 1 (True/On) or 0 (False/Off) going into its input. Therefore, a Constant block must be used as the input of the Digital Output block. Connect together by dragging your cursor, left click held, from the output of the Constant block to the input of the Digital Output block.

The Video above will go through the steps to complete this process. Note: The ENGAGE and DISENGAGE values are WRONG in the VIDEO. But, it will show how to do everything that needs to be done. The video was trimmed with Laptop Screens in mind recorded at 720p, the initial image is blurry, don't worry it gets clear.  Enjoy.
     In order to flip the brake on or off a switch must be used. This can be found in the Simulink section of the Library Browser or you can search for one. After the switch has been placed onto the screen there will be grey text saying to click it to activate the parameters. Above is a snapshot of what that will look like. When a window similar to this pops up you can select the constant block that is connected to the Digital Output of the Arduino. Assign Values of 1 and 0 to the Top/Bottom selections. It does not matter where they go, as long as Disengage is Labeled with 1 and Engaged is labeled with 0. This comes from the relay arrangement selected. If the relay was set up to be the other way, using the Normally Open (NO) input would reverse the relations above.
--------------------------------------------------------------------------------------------------------------------------
The REASON the NC has been selected is so that the operator MUST ALWAYS disengage the brake. This ensures that the operator has full control of every power supply from start up, since the default set point of the Switch is 0.
--------------------------------------------------------------------------------------------------------------------------
      You can now flip the switch after selecting 'OK' at the bottom of the switch menu. Sometimes the program will move the switch. If this happens select the switch so that there is a blue box around it, and gently click the top or the bottom, depending what isn't selected, to see the switch work. If set up correctly the value in the constant box will also change between 0 and 1.

3. Motor Direction -- Technical


Flipping the direction in which the motors spin is one of the most important tasks of the motors. If we can not correct for the wanted angle by going the other way then our time has been wasted.

a. Microcontroller Wiring and Simulink
To flip the direction of the motors we must flip the polarity of the motor leads. This will be explored further in the blog for the hardware end of things. To put it simple, we are using two (2) relays in order to flip the polarity of the motors, in what is known as a H-Bridge.
H-Bridge Basic Design. The Red is the closed circuit and the black shows the open leads. Instead of switches, the model uses relays to act as switches. Forward motion would be set as Normally Closed (NC) and Reverse would be set as Normally Open (NO). figure from: http://blog.solutions-cubed.com/wp-content/uploads/2012/06/h-bridge.png

Motor Direction Simulink Switch Set Up.

With all of the above being left for another blog to explain the ins and outs I will begin by stating this blog only touches the Microcontroller (arduino) side of the project. To begin grounding both relays' inductors to the arduino's common ground. Now, for the other end of the inductors (of the relays, also called coils) will go to two (2) separate Digital I/O (Digital I/O) pins. The above image uses PIN 2 and PIN 3. They are connected together (in SIMULINK ONLY) so that one switch is used to turn them both on or off depending upon the direction that is desired. The video in the previous section shows the steps for this process without the added Digital Output block, for the additional block just connect the second input to the wire as seen above.

The relay used in the working model is a JZC-11F. Where the diagram shows the coil is where
the inductor is; the polarity of this circuit does not matter, as it will work in either direction. 
4. Speed Control -- Technical -- Technical -- Technical -- Technical -- Technical

Read the operator's manual pages of the Agilent 5784A Power Supply for Voltage Programming of OUTPUT VOLTAGE (pages 43 to 44) before thinking about touching this section. It is not as simple to use as the power supply for the brakes.

As you may have read in the owner's manual the Voltage Programming has two ranges, they are 0-5V and 0-10V. Since the arduino has a maximum of 5V for a digital output the appropriate choice is the 0-5V range, but this is information for the Motor Speed Control Blog.

a. Microcontroller Wiring and Simulink
Motor Speed control using Pulse Width Modulation (PWM). The Arduino's PWM range is from 0-255 where 0 correlates to 0V and 255 to 5V (or the maximum of the arduino).  The Manual Switch in between the PWM Block and the Value Blocks is used to ensure that when DIRECTION is changed that the motor is not spinning. 
PIN 5 (PWM) of the arduino is connected directly to the analog input of the Agilent 5748A Power Supply. The analog pin that the arduino output (PIN 5) is connected to is J9 and the arduino's ground is connected to J22 of the same analog input.

No comments:

Post a Comment