Unit 03 Section 02 Lesson 03



Integrated System Three Automatic Pedestrian Gate

Assigned activities

The purpose of the following activity is to develop capability with building an integrated system that combines input and output under software control.  This specific activity is to prototype an automatic pedestrian gate which can safely allow access and give warning of gate activation.

Activity 1 Part 1 The Basic Gate

Required Resources

  • Phidget motor controller
  • Phidget servo controller
  • Servo motor
  • Passive Infrared sensor (PIR)
  • Phototransistor
  • Infrared LED
  • Red LED (with resistor)
  • Piezo alarm

Gate Fabrication

The fabrication of the gate is straight forward. Small 4-40 bolts and nuts are used to attach a short piece of wood or plastic to a servo motor. If you use wood,  colour it with a marker to provide a visual warning. Construct your gate on a piece of cardboard or paper and use double sided tape to hold the componets and phidgets. The next two links show how the parts are fabricated and assemblied.

Arm Template

Servo Wheel and Gate Attachment Template

Sensor Fabrication

The Passive Infrared sensor (PIR) needs to be shielded so that the gate is not accidentally activated. The PIR can be taped to the end of an enclosure.

  • Fabricate a small box (see link below) or use a shortened paper towel tube as shown in the next figure.  It needs to have the end open on the side towards the path as indicated by the striped walkway in the next figure.

Figure tube shielding the PIR

The following link will give you more information on fabricating the enclosure

The photogate consists of a phototransistor and infrared LED pair.  They need to be held directly opposite each other to detect the person passing between them.  One method of fabricating for this prototype is illustrated in the next figure.


Figure Phototransistor–Infrared LED pair

Circuit Configuration

Make sure the Phidget motor and servo controllers are NOT plugged in!

  • Connect the orange wire of the Passive Infrared Sensor (PIR) to input line 0 of the Phidget motor controller's input terminal block. Connect the black wire to one of the grounds (gnd) on the same block. See the illustration below.
     
  • Connect the red wire to the +5 volt and the black wire to  on the Phidget motor controller.

     
    Figure PIR wired to motor controller input
  • Connect the red wire of the Phototransistor (dark colored LED type device) to input line 1 of the Phidget motor controller and the black wire to ground. Note: use the other ground on the input terminal strip.


    Figure Phototransistor wired to input
  • Connect the red wire of the Infrared LED (clear device) to +5volts of the Phidget motor controller (make sure the PIR connection doesn't fall out). Connect the black wire to the  ground (gnd) on the power terminal strip . See the following illustration.

Figure Infrared LED wired to power terminal
  • Finally connect the servo motor with gate attached to servo motor connection 0. Using connection 0 means you don't have to use a power supply on the Phidget servo controller as your computer supplies the power to this connection. See the next illustration.

Figure Completed System

Create the Graphical User Interface

Note: Save your project at frequent intervals!

  • Run Visual Basic 6.  Select a Standard Exe.
     
  • Draw a Frame on the form.
     
  • Draw the following controls on the frame. A suggested layout is shown in the next figure below.
    • 2 timers
    • 2 command buttons
    • a text box

Change the properties for the following:

  • Frame1
    • Caption - Gate Control
    •  Font - Arial, 14pt, bold
    • Backcolor - something appropriate!
       
  • Timer1
    • Interval - 500
    • Enabled - False
       
  • Timer2:
    • Interval - 500
    • Enabled - False
       
  • Command1 button:
    • Caption - Enable Gate
    • Font - Arial, Bold, 12
    • Style - Graphical
    • Backcolor - some color to contrast with the Frame color
       
  • Command2 button:
    • Caption - Disable Gate
    • Font -  Arial, Bold, 12
    • Style - Graphical
    • Backcolor - some color to contrast with the Frame color
       
  • Text1 box:
    • Text - Delete the default text (Text1)
    • Backcolor -  green
    • Font - Arial, Bold, 14

Figure Gate Control Layout

Code the Interface

  • You will be using Phidgets therefore the Phidget library must be referenced. Open the Project menu and select References.

    Scroll down and check Phidget Library 2.1. Click OK.
     
  • Open the code window. Start by selecting General...Declarations in the small windows. Enter the following code:

Dim WithEvents Motor As PhidgetMotorControl, WithEvents Servo As PhidgetAdvancedServo, FlagPIR as Boolean,FlagPhoto as Boolean

  • Code the Form_Load procedure:

Set Motor = New PhidgetMotorControl
Call Motor.Open

Set Servo = New PhidgetAdvancedServo
Call Servo.Open

FlagPIR = False
FlagPhoto = True

Servo.Position(0) = 0

  • Next select Servo....OnAttach from the small windows at the top of the code form (Servo in the left window, OnAttach in the right) and code the following:
  • Servo.ServoType(0) = PHIDGETCOM_SERVO_HITEC_HS322HD
    Servo.Engaged(0) = True
    Servo.VelocityLimit(0) = 40  

  • Code the Command1 (Enable Gate) button with the following:

Timer1.Enabled = True
Text1 = "Approach"

  • Code the Command2 (Disable Gate) button with the following:

    Timer1.Enabled = False
    Timer2.Enabled = False

Next you need to add five (5) sub procedures.

  • Open the Tools menu and select Add Procedure. Name the first procedure RaiseGate.

    Repeat this step and name the other four procedures LowerGate, Delay, ScanPIR, and ScanPhotoGate
     
  • Code the RaiseGate Procedure:

' This code turns the servo one degree at a time through 90 degrees.

For I = 1 to 90
   Servo.Position(0) =I
   Delay
Next I

  • Code the LowerGate procedure with the following:

' This code turns the servo in reverse

For I = 90 to 1 step -1
   Servo.Position(0) =I
   Delay
Next I

  • Code the Delay procedure with the following:

For T = 1 to 1000000 : Next T

  • Code the ScanPIR procedure with the following:

'Standard code for input on the Phidget motor controller

if (motor.InputState(0)=False) And (FlagPIR = False) Then
   FlagPIR = True
   Timer1.Enabled = False
   Text1.BackColor = vbRed
   Text1 = "GateUP"
   Text1.Refresh

   RaiseGate
  
   Timer2.Enabled = True
   FlagPhoto = False
   Text1.BackColor = vbGreen
   Text1 = "Walk"
   Text1.Refresh
End If

  • Code the ScanPhotoGate procedure with the following:

if (motor.InputState(1)=False) And (FlagPhoto = False) Then
   FlagPhoto = True

   Timer2.Enabled = False
   Text1.BackColor = vbRed
   Text1 = "GateDOWN"
   Text1.Refresh

   LowerGate

   FlagPIR = False
   Text1.BackColor = vbGreen
   Text1 = "Wait"
   Text1.Refresh
   For T = 1 to 100000000 : Next T
   Timer1.Enabled = True
   Text1 = "Approach"
   Text1.Refresh
End If

  • Code Timer1 with the following:

ScanPIR

  • Code Timer2 with the following:

ScanPhotoGate

  • Save the program!!

Test the System

Connect the USB cables to both Phidgets and to the computer. Connect the 6 volt power adapter to the Phidget motor controller. Note: the PIR needs a minute to warm up and become stable.

  • Run the program.
     
  • Click the Enable Gate button.

    This will start the computer scanning the PIR.
     
  • Place your hand in front of the PIR. The gate should raise and stay in the open position. Place your hand through the photogate. The gate should close.

Activity 1 Part 2 Modify the Project - Custom Controls

You can add a custom angular gauge control to display the position of the gate as it opens and closes.

  • Start by enlarging the form and frame to make room for the control.
     
  • Next add the control to the toolbar. Open the Project window and select Components.
     
  • In the Components window, scroll down and check GMS Angular Gauge ActiveX Control. Click OK.
     
  • Select the control on the toolbar (it should be the last control) and draw on the frame to the right of the textbox. As you change the properties you may have to stretch and enlarge the AGauge control to see the scales etc.!
     
  • Modify the properties for AGauge1:

ScaleMaxValue 90
ScaleMinValue 0
ScaleOriginX 1
ScaleOriginY -1 (minus 1)
ScaleStartAngle 270
ScaleStopAngle 360

Tic Delta 10
TicInnerRadius 1.6
TicLabelRadius 2
TicOuterRadius 1.8

NeedleLength 1.5

Hubs 1
HubColor Red

Font Arial, Bold, 12

Captions 1
Caption Gate Position
CaptionX -0.6 (minus 0.6)
CaptionY 1


Figure Sample Visual Design

Modify the Code

You need to add the similar lines of code to two procedures.

  • Open the code window and scroll to the RaiseGate Procedure and add the code shown in bold (the whole procedure is shown).

For I = 1 to 90
   Servo.Position(0) =I
   Delay
   AGauge1.NeedleValue = I
   AGauge1.Redraw

Next I  

  • Next move to the LowerGate procedure and add the following lines in the position shown.

For I = 90 to 1 step -1
   Servo.Position(0) =I
   Delay
   AGauge1.NeedleValue = I
   AGauge1.Redraw

Next I  

Test the System

  • Run the modified program.

    The angular gauge should display the gate's position not in degrees but in steps.

Activity 1 Part 3 Gate Motion Alarm

When your gate moves, you need to alert the pedestrian. You can add both visual and audible indicators to your project. You can add a red LED and a Piezo crystal to meet this criteria.

The motor controller has two motor outputs that are not being used. Connect a LED to output 0 (make sure it has a resistor attached) and a piezo alarm to output 1.

Add the following line of code, shown in bold, to the RaiseGate and LowerGate procedures.

  • RaiseGate:

For I = 1 to 90
   Servo.Position(0) =I
   Delay
  
AGauge1.NeedleValue = I
   AGauge1.Redraw
   Motor.Velocity(0) = 100
   Motor.Velocity(1) = 100

Next I

Motor.Velocity(0) = 0
Motor.Velocity(1) = 0 

  • LowerGate:

For I = 90 to 0
   Servo.Position(0) =I
   Delay
  
AGauge1.NeedleValue = I
   AGauge1.Redraw
   Motor.Velocity(0) = 100
   Motor.Velocity(1) = 100

Next I

Motor.Velocity(0) = 0
Motor.Velocity(1) = 0 

Test the System

The LED should come on when the gate moves and an alarm should sound just before it moves.

Your Turn - Additional Challenges

This project can be expanded by adding several new components. If you have time and your instructor agrees, you might try the following.

  1. Add two more buttons to manually raise and lower the gate. These could be used to test the gate. An additional challenge would be to make sure that once the gate is raised the "raise gate" button is disabled. Of course, when the gate is in its lowered position, the "lower gate" button should also be disabled.

    Hint: Check out the Enabled property for the command buttons.
     
  2. How can you make the gate safe when it lowers.  That is, if it encounters an obstacle can you get it to stop.

    Hint: Add the microswitch to the bottom of the gate and when it is pressed interrupt the lower gate code  Careful :)

 

©2016 CDLI