None Lab_1

Challenge

Since you will be using a Zumo for your projects, you will eventually need to be able to model and control the Zumo's motors. However, it is difficult to learn how to do that when the Zumo keeps driving away! So you will use the same motors that are used in the Zumo to run the robot traffic gates that you are working with in the Week 1 Monday Exercise.

Thus the current challenge is to operate a physical motor-driven gate so that by pressing a button the motor will turn and raise the gate until it contacts the "up" limit switch, stopping the motor. When the button is pressed again, the motor will need to turn on and drive the gate down until the "down" limit switch is pressed, stopping the motor again.

Disciplined Process

There is no expectation that you know enough at this moment to meet the challenge described above, but approaching it in managable pieces will get you there. As was discussed in the Week 01 Monday notebook, we will follow the same high-level Disciplined Process here.

image-4.png

We won't always have a lot of detail in each of these steps, and we may take detours and backtracks along the way as we discover that we need to modify our model scope or change our approach to controller design, but Lab 1 follows this disciplined process all the way through (check out the table of contents!!)

If we begin with modeling, we'll need to decide on something that we need to model. We will make the choice to scope the motor. This is not the only choice we could make to begin, but because we have to make a choice and the motor seems to be an important component we'll start there.

To start, we need the simplest model we could develop just to get through all the steps and then we can assess if more fidelity is needed. A simple model doesn't need to be a mathematical model, it just needs to give you an expectation of how the motor will behave so you can make some kind of prediction. Let's give it a try!

Model Development: DC Motor

A direct current motor uses windings of wire to generate a magnetic field which causes the rotor to turn when placed in a magnetic field. As the rotor aligns with the external magnetic field, the commutator switches the current to a subsequent coil of wire which causes it to again rotate. The process is repeated for every segment of the commutator causing the rotor to turn continuously. It has been found experimentally that the torque is proportional to the current and the angular velocity is proportional to the voltage (something you discussed in Systems).

The video below explains the process in an animation:

In [1]:
%%html
<iframe width="900" height="540" src="https://www.youtube.com/embed/LAtPHANEfQo?start=0&end=148" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Model Development: Relay

The ME480 Workstation includes a relay (see the Hardware Resource: ME 480 Portable Workstation)

to see where it is located) which allows you to "turn on" power to a device, like your motor, without physially moving a switch. On the ME480 Workstation, for safety, no power can be applied to the DC motor without first activating the relay, so we need to include the relay in our motor model.

A relay is essentially an electrically actuated switch that allows you to switch a high-power circuit (such as a motor drive) using a low-power circuit (such as an Arduino).

The basic information about how a relay works is provided in the video below:

In [2]:
%%html
<iframe width="900" height="540" src="https://www.youtube.com/embed/n594CkrP6xE?start=107&end=229" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Model Development: DC Motor & Relay

On the ME480 workstation, the relay's contact is normally open, and breaks the connection between the MOTOR1 (+) pin to the left of the breadboard and the motor's positive terminal. The coil of the relay, which will close this connection and allow current to flow through the motor, is attached to pin 37 of the Arduino Mega allowing you to open and close the connector using Arduino code. A circuit diagram explaining this piece of the workstation's circuit is shown below. Note some of these connections are pins you can connector wire to, while others are built-in to the printed circuit board (PCB) as indicated on the diagram.

image-5.png

**CONFIRM YOUR MOTOR IS CONNECTED TO THE MOTOR 1 PLUG ON THE PCB

Model Validation: DC Motor & Relay

Although we don't have a mathematical model (such as a differential equation or transfer function) for either the relay or the motor, we do have a low fidelity model of the behavior of each. The relay acts as a switch and the motor will move with voltage. Although simple, it's enough to validate.

WORKSTATION SETUP

Before you begin, you must configure your ME480 workstation

image.png

**DO NOT INSTALL THE FULL GATE**, we'll just use the hub as a way to visualize what the motor is doing.

**DO NOT PROCEED UNTIL YOU HAVE MADE THESE CHANGES**

Experimental Validation

In this lab we'll power the workstation and motor with the USB cable connected to your workstation and a computer. See the Hardware Resource: ME 480 Portable Worstation for more details). The green power LED on the workstation's circuit board will be illuminated when it is powered on.

You will be using the breadboard to power the motor, so you will need to connect the power source to the breadboard by referring to the Hardware Resource: Breadboard.

You will be writing an Arduino program to allow the voltage to pass through the relay in order for the motor to spin! In particular, you should create a program that energizes the relay by setting the arduino pin to ouput 5V (HIGH) in order to close the ciruit. You can use the digitalWrite() command described in the Arduino Resource: Common Functions to change the voltage on an Arduino output pin.

Using the Arduino software, setup your board to be the "Arduino Mega 2560" (it's under your green circuit board!) and try to program the Arduino to close (energize) and open (denergize) the relay. Note, that in the diagram above, the relay will complete the connection between Motor positive terminal to the breadboard pin MOTOR1 (+), but that will not connect the motor to a power source! You will need to create a complete circuit by connecting the MOTOR1 (+) and MOTOR1 (-) pins to 5V and ground buses on your breadboard using your jumper wires so when the relay closes the motor will be energized and you can observe it's behavior. The gate hub will help you see the rotation when you get your motor running. * JUST BE CAREFUL THE GATE HUB DOESN'T CATCH THE MOTOR WIRES WHEN IT SPINS

Once you have run and tested your code, enter it in the cell below. Reproduce or copy/past the backtick mark notation below and place your code between the marks so your code is properly formatted

```c++

[put code here]

```

You can find the ` character on the upper left key on the keyboard under the tilde (~).

YOUR ANSWER HERE

In the cell below, describe the motor behavior when it is connected to a complete 5V circuit. Also explain what happens when you change the polarity of the motor wires by changing how 5V and ground are connected to Motor1 (-) and Motor1 (+).

YOUR ANSWER HERE

Controller Design: DC Motor Speed and Direction

If you were to change the voltage connected to the DC motor you would see that as voltage increases the speed increases (you learned this relationship in Systems!). However, we can only supply a fixed 5V to our motor with our workstation. This is not an uncommon problem for digital systems and means we need a way to change the voltage the motor "sees" even though our voltage supply remains fixed. Quite a conundrum!

Model Development: Pulse Width Modulation

Since digital systems are not well suited to vary a voltage, we need to find something they CAN do well. A digital signal CAN modulate a on or off very easily. So we will modulate the signal on and off over time to adjust the AVERAGE voltage the motor recieves.

This technique is called Pulse width modulation. In this technique the frequency of a signal is CONSTANT, and just the DUTY CYCLE (the % time the signal is on vs off over one period) is varied. The figure below shows that by changing the duty cycle (and NOT the fequency) the average voltage of a signal changes.

image.png

If the pulse width modulated frequency is chosen to be high enough above the time constant of a system, it will "see" the PWM signal as a constant DC signal.


Model Validation Pulse Width Modulation

As it turns out, the arduino has the ability to produce a Pulse Width Modulated (PWM) signal! In the Hardware Resource you will find the list of pins you have available to you via the female header above the LCD screen on the Workstation circuit board. Of those pins, pins 44 and 46 are capable of PWM.

So lets try programming the Arduino to generate different duty cycles. First load the Arduino software (making sure you are setup for the Arduino Mega 2560 board)

Once the software is open and properly connected, use the example code below and modify to work with your selected PWM pin.

void setup() {
  // put your setup code here, to run once.  This currently is setup to set pin 7 to output
  pinMode(7, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

    analogWrite(7,255); //this sets the PWM signal on pin 7 to 255 (the maximum amount) 
}

You should look at the details of the analogWrite function by checking out the common functions list in the Arduino Resource Notebook.


To examine the PWM signal generated by the Arduino, connect your PWM output pin to your oscilloscope. You can review oscilloscope operation in the !!!Reference Manual!!!. Don't forget to review how to properly connect the probes.

Change the value of the of the analogWrite command to different values from 0-255. Using the oscilloscope MEASURE capability, observe the change in output of the Arduino by displaying the amplitude, frequency, and average voltage. Review the Hardware Resource for the details of the Measure Menu for your model oscilloscope.

Attach an image of oscilloscope output in each of the cells below for an average voltage of approximately 2V and 4V. Make sure to list the following for each image

  • Amplitude
  • Frequency
  • Duty Cycle
  • Analog Write value

**NOTE IMAGE FILES CANNOT BE MORE THAN 1 MB IN SIZE**

YOUR ANSWER HERE

YOUR ANSWER HERE

In the cell below, conceptually describe the relationship between the waveform and the analogWrite value.

YOUR ANSWER HERE

In the cell below, write an equation relating the PWM "counts" to the average voltage supplied by the PWM pin.

YOUR ANSWER HERE

If the Arduino can output a PWM signal then it should be possible to use the signal to run the motor at different speeds... right? Unfortunately the Arduino does not deliver enough power to drive the motor directly from its digital pins. The Arduino outputs "signals" meaning they are very low current (40 mA) and are not intended to power anything.

Try it! Connect pin 44 directly to the MOTOR 1 (+) pin and connect the MOTOR 1 (-) pin to ground.

This means in order to drive the motor, WE NEED A WAY TO AMPLIFY THE ARDUINO's PWM SIGNAL!

Model Development: Transistor Model

Bipolar junction transistors (BJT) can be used as amplifiers. They can take in a small amount of current and turn it into a larger amount of current. BJTs are catagorized by how their semi-conductor material is assembled. They are constructed of $n$ material that is "doped" to have a (n)egative charge and $p$ material is doped to have a (p)ositive charge. Arranging them in different order yields different behavior. Here are the electrical schematic symbols for the two types of BJTs:

Here is a short video on how they work:

In [3]:
%%html
<iframe id="kaltura_player" src="https://cdnapisec.kaltura.com/p/721212/sp/72121200/embedIframeJs/uiconf_id/48425603/partner_id/721212?iframeembed=true&playerId=kaltura_player&entry_id=1_7vap80ur&flashvars[streamerType]=auto&amp;flashvars[localizationCode]=en&amp;flashvars[leadWithHTML5]=true&amp;flashvars[sideBarContainer.plugin]=true&amp;flashvars[sideBarContainer.position]=left&amp;flashvars[sideBarContainer.clickToClose]=true&amp;flashvars[chapters.plugin]=true&amp;flashvars[chapters.layout]=vertical&amp;flashvars[chapters.thumbnailRotator]=false&amp;flashvars[streamSelector.plugin]=true&amp;flashvars[EmbedPlayer.SpinnerTarget]=videoHolder&amp;flashvars[dualScreen.plugin]=true&amp;flashvars[hotspots.plugin]=1&amp;flashvars[Kaltura.addCrossoriginToIframe]=true&amp;&wid=1_nzy6pudg" width="900" height="550" allowfullscreen webkitallowfullscreen mozAllowFullScreen allow="autoplay *; fullscreen *; encrypted-media *" sandbox="allow-forms allow-same-origin allow-scripts allow-top-navigation allow-pointer-lock allow-popups allow-modals allow-orientation-lock allow-popups-to-escape-sandbox allow-presentation allow-top-navigation-by-user-activation" frameborder="0" title="Kaltura Player"></iframe>

We're going to use TIP41C (npn) and TIP42C (pnp) transistors. The links to the datasheets are below.

https://www.onsemi.com/pub/Collateral/TIP41C-D.PDF

https://www.onsemi.com/pub/Collateral/TIP42C-D.PDF

Find the pinout diagram that describes which pins are the base, collector and emmiter on the datasheets. Cut and paste an image of the pin out (indicating which diagram is to which chip) in the two cells below so you have them for easy reference.

YOUR ANSWER HERE

YOUR ANSWER HERE

Model Validation: npn Transistor

To understand the behavior of an npn transitor, connect a (TIP41C) to an the motor-relay circuit on your workstation as shown below. Use your pinout above to make sure you connect the wires to the right pins.

image.png

Read, understand and code the following program. Modify the code to activate the relay as you did earlier. Save the program and run it.

void setup() {
  pinMode(44, OUTPUT);            //initialize digital pin 22 as output
  Serial.begin(9600);          //initialize serial port
}

void loop() {
    digitalWrite(44, HIGH);           //set the digitial pin 22 to HIGH (output 5V)
    Serial.println("Pin 44 is high"); //print to the serial monitor
    delay(1000);                      //wait 1 second
    digitalWrite(44, LOW);             //set the digitial pin 22 to LOW (output 0V)
    Serial.println("Pin 44 is low"); //print to the serial monitor
    delay(1000);
}

Use the serial monitor to confirm your program is working. Then, in the Markdown cell below, describe the behavior of the motor when the base voltage is 0V and when the base voltage is +5V. Is this what you expect based on your model of how a transitor works?

IF YOU AREN'T SURE WHAT TO EXPECT, CHAT WITH YOUR INSTRUCTOR. YOU CAN'T VALIDATE A MODEL WITHOUT AN EXPECTATION!

YOUR ANSWER HERE

Model Validation: pnp Transisitor

Now, we will drive the motor using a pnp transistor circuit.

image-10.png

Save your existing Arduino code and then modify only if necessary to provide a 5V and 0V signal changing every 1 second to the pnp circuit. Be sure to enable the relay so that the motor can be powered.

Use the serial monitor to confirm your program is working. Then, **in the Markdown cell below, describe the behavior of the motor when the base voltage is 0V and when the base voltage is +5V. Explain the differences, if any, from the operation of the npn transitor.

YOUR ANSWER HERE

Controller Design: H-Bridge Circuit

With the ability to turn your motor on and off from a low power "signal", we still need more capability. You will need to change the motor's direction! To meet your challenge of operating the gate the motor will need to both open and close the gate! So you will also need a circuit that can change the motor direction WITHOUT requiring you to manually disconnect and reverse the wires.

The H-Bridge circuit can do this job by using 2 npn and 2 pnp transistors arranged in an "H" shape. Because transistors can act as an open a circuit when no current is provided to the base, they can be thought of as a type of switch. By alternating the switches that are closed and open, the direction of the current passing though the motor can be changed and thus the direction of the motor can be changed as shown below. image-2.png

Controller Validation: DC Motor Direction

If we use pairs of pnp and npn BJTs we can achieve this behavior with just 2 control signals! Save your existing Arduino code, and make a copy to control an H-bridge circuit.

On your breadboard, and using the provided jumper wires, construct the circuit below. USE YOUR PINOUT DIAGRAMS TO MAKE SURE YOU CONNECT THE WIRES TO THE RIGHT PINS! You might even want to sketch the wiring before you connect it.

image-4.png

Taking deliberate risk is extremely important here. If you try and build the entire circuit at once you will face a complex debugging task to find problems! Consider buidling the circuit in steps, adding one transistor at a time and testing the functionality at each step. For example, your PNP and NPN circuits above are 1/4 of this bridge! A half bridge would be the following:

image-3.png

After you construct and validate your full bridge circuit, Run the code with both pins LOW, and alternate LOW/HIGH between the two pins.

Describe the motor behavior for each pin configuration in the Markdown Cell below

YOUR ANSWER HERE

Controller Validation: DC Motor Direction and Speed

The H-bridge circuit above should allow you to run the motor in both directions, but it doesn't allow for speed control. For this we'll make use of the PWM output of the Arduino by using the analogWrite command in place of the digitalWrite command since both transistor bases are connected to PWM capable pins on the Arduino!

Save your existing Arduino code and modify it to control both direction and speed.

*Here is a tip!* You are dealing with a lot of pins now. Every time you want to change a pin number you have to change it in every single line where it appears in the code. It would save you A LOT of time to use a variable instead of a number for the pin numbers. See the example below and implement in your own code.

// the line below "declares" the name "leftPin" as an integer and a constant.  
// it is set to the value of 2 
// it is declared a constant because it is known that the value will not change when the program runs
const int leftPin  = 2;   

void setup() {
  pinMode(leftPin, OUTPUT); // initialize leftPin as an output
}

void loop() {
  analoglWrite(leftPin, 100);
}

Model Development: Switches

Having a way to control direction and speed of a motor gives you the ability to raise and lower a gate, but meet all of the requirements of the challenge you will need to know when the gate is up and when it is down. We'll need sensors for that job. The switches on your gate module will act as your sensors.

This means we need to have some model of how switches work to guide our implementation. The switches you will need to model for this challenge on the "limit" switches installed in the gate base to detect the gate is "up" and "down". The "momentary" buttons next to the reset button on the workstation will be used to allow the user to command the gate when to open and close.

For both of these components, the electrical operation is a simple switch that closes a circuit. On the ME480 workstation, the gate limit switches and the user input buttons are configured with one side connected to ground and the other connected to an Arduino digital pin. This corresponds to the circuit below:

image.png

In this case, the arduino pin should be "nominally" at 5V when the switch is open and 0V when the switch is closed, But watch the video below... that is not always the case!

In [4]:
%%html
<iframe width="900" height="540" src="https://www.youtube.com/embed/wxjerCHCEMg?start=124&end=187&version=3" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Having seen the need for a pullup resistor, you'll be happy to know the Arduino has the ability to configure a digital pin to use an internal pull-up resistor, so you don't have to add one yourself! Configuring the pins this way is as simple as one line of code.

Use the code below to give this a try. We'll use the serial monitor to indicate if the switch is closed or if the switch is open.

const int upLimitSwitch  = 10;   

void setup() {
  pinMode(upLimitSwitch, INPUT_PULLUP); // initialize the pin connected to the "up" limit switch as an input
  Serial.begin(9600); //initialize serial communication
}

void loop() {
  int sensorVal = digitalRead(upLimitSwitch); //read the current value of the pin connected to the limit switch 
  //sensorVal is declared as an integer
  //in this case, it can be 1 (if the pin is at 5V) or 0 (if the pin is connected to ground)  

  //print sensorVal to the serial monitor so you can see it.
  Serial.println(sensorVal)    
}

Model Validation: Switches

Try it out. Does the Serial monitor output correspond to closing and opening the switch as you expect?

Controller Design: Parking Gate

Using the understanding from above, design and implement a controller that achieves the following:

  • Raises and lowers the gate.
  • Stops the motor when either limit switch is pressed (THIS IS IMPORTANT! OTHERWISE YOU WILL CAUSE HIGH CURRENT HEATING AND "BURN OUT" THE MOTOR BY OVERHEATING AND DAMAGING IT!)
  • Uses the momentary switch *BTN 1 provided on the workstation to activate the motor.
  • If the gate-DOWN limit switch is pressed, when the momentary switch is pressed the motor will raise the gate.
  • If the gate-UP limit switch is pressed, when the momentary switch is pressed the motor will lower the gate.
  • For safety, the relay will be opened if the E-STOP button is depressed, stopping the motor no matter what other commands are provided. Therefore the relay will be closed (allowing the motor to run) only if the E-STOP button is unlatched.

Before beginning, install your gate module and complete gate as shown in the Hardware Resource: Installing the gate module

Include in the cell below a copy of your completed Arduino code in the cell below. Copy-paste the code block below into the mardown cell so as to properly format your code so it appears as in the Arduino software

```c++

[put code here]

```

YOUR ANSWER HERE

Controller Validation: Parking Gate

Upload a video of your gate working and provide a link to the video in the cell below. You can use media.lafayette.edu, youtube, vimeo, etc.

YOUR ANSWER HERE