Vous êtes sur la page 1sur 12

Ain Shams University

Faculty of Engineering
Credit Hours Engineering Program
MCT 432 Hybrid Control

LAB 1
Reading Quadrature Encoder
Position

Prof. Farid A. Tolbah


Eng. Waleed A. El-Badry
Fall 2016
TABLE OF CONTENTS

1. OBJECTIVE OF CONDUCTED EXPERIMENT 1

2. HARDWARE REQUIREMENTS 1

3. SOFTWARE REQUIREMENTS 2

4. PROCEDURE 2

4.1. DISPLAY ENCODER POSITION ON AN LCD 2


4.2. PROTEUS SIMULATION 4
4.3. EXERCISE 6
4.4. COMMUNICATION WITH LABVIEW 6
4.4.1. ARDUINO CODE 6
4.4.2. LABVIEW VI 6
4.4.3. LABVIEW BLOCK DIAGRAM 7

5. RESULTS 8

6. FURTHER EXERCISE 8

7. REFERENCES 9
LIST OF FIGURES
FIG. 1 ARDUINO UNO R3 BOARD ........................................................................................................... 1
FIG. 2 SIX POLES, TWO CHANNELS MAGNETIC ENCODER ................................................................................ 1
FIG. 3 LIQUID CRYSTAL DISPLAY .............................................................................................................. 2
FIG. 4 ARDUINO USB CABLE .................................................................................................................. 2
FIG. 5 ENCODER LIBRARY BY PAUL STOFFREGAN ......................................................................................... 2
FIG. 6 PROTEUS MODEL......................................................................................................................... 5
FIG. 7 ARDUINO CODE AND PROTEUS ....................................................................................................... 5
FIG. 8 SIMULATION RESULT .................................................................................................................... 6
FIG. 9 COM PORT CONNECTIONS ............................................................................................................ 7
FIG. 10 LABVIEW BLOCK DIAGRAM......................................................................................................... 7
FIG. 11 LABVIEW - ARDUINO CO-SIMULATION FOR ENCODER READING.......................................................... 8
FIG. 12 GITHUB COURSE REPOSITORY ...................................................................................................... 9
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position

1. Objective of conducted experiment


After completing this lab, you will be able to:

Prepare the necessary hardware for reading motor position (angle) from mounted
magnetic encoder.
Understand the Arduino code for successful reading of encoder pulses and show
reading on LCD.
Transfer Arduino reading to LabVIEW.
Prevent recurring transfer and LCD update when no change in reading.
2. Hardware requirements
Arduino Uno R3 (or any board with at least two pins supporting external interrupt).

Fig. 1 Arduino UNO R3 board

Quadrature magnetic encoder. Experiment was conducted using Sha Yang Ye geared
motor with onboard encoder available in Mechatronics lab.

Fig. 2 six poles, two channels magnetic encoder

P a g e 1 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position

2x16 alphanumeric LCD.

Fig. 3 Liquid Crystal Display

USB cable for connecting Arduino to PC.

Fig. 4 Arduino USB cable

3. Software requirements
Arduino IDE for programming Arduino.
NI LabVIEW 2015 or later.
NI VISA 16 or later to allow LabVIEW to connect to available serial ports.
Proteus ISIS 7.5 or later for offline simulation.
FabulaTech Virtual serial port kit or any alternative to allow serial communication
between LabVIEW and Arduino.

4. Procedure
4.1. Display encoder position on an LCD
The code will utilize encoder library by Paul Stoffregan which can be downloaded from
library manager

Fig. 5 Encoder library by Paul Stoffregan

P a g e 2 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position

After installing the library, create a new sketch and copy the below code, then save it with
any name appropriate.
#include <Encoder.h>
#include <LiquidCrystal.h>

const int encaPin=2;


const int encbPin=3;

int oldPosition = -999;


char buffer[6];

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);


Encoder myEnc(encbPin, encaPin);

void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Encoder Count:");
}

void loop()
{
int newPosition = myEnc.read();
if (newPosition>231)
myEnc.write(0);
if (newPosition<-231)
myEnc.write(0);
if (newPosition != oldPosition) {
oldPosition = newPosition;
sprintf(buffer,"%04d", newPosition);
Serial.println(buffer);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(buffer);
delay(50);
}
}

Snippet for reading motor angle from magnetic encoder and display it on LCD

You are supposed to be acquainted with Arduino commands. However, few highlights to
comprehend the code flow would be useful.

Importing both encoder and LCD libraries.


#include <Encoder.h>
#include <LiquidCrystal.h>

P a g e 3 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position

Define the encoder pins (channel A and Channel B). It is recommended that both pins
are interrupt pins for better performance of the library [1]
const int encaPin=2;
const int encbPin=3;
Read the encoder pulses.
int newPosition = myEnc.read();
The used encoder has 6 poles and mounted on 19:1 gearbox. So encoder resolution is
calculated as:
ResolutionPPR = (No. of poles)x(No. of channels)x(Grear Ratio) Eq. 1

= (6)(2)(19.25) = 231
Where PPR indicates Pulse Per Revolution.

Since we measure position not number of revolustions, we override encoder reading


if exceeded one revolution in any direction.
if (newPosition>231)
myEnc.write(0);
if (newPosition<-231)
myEnc.write(0);
Also LCD is updated only if new reading arrived. This saves the LCD from recurring
refresh while encoder (motor ) is at standstill.
if (newPosition != oldPosition) {
oldPosition = newPosition;
sprintf(buffer,"%04d", newPosition);
Serial.println(buffer);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(buffer);
delay(50);
}

4.2. Proteus simulation


Open the file Encoder Reading Model.pdsprj accompanied with your lab files. The model
comprises five components:

Arduino UNO model (based on ATMEGA328P).


Scope (to monitor encoder output and the 90o phase shift.
Encoder circuit that is composed of LM298 motor shield and DC motor with encoder.
Again we are interested in encoder only. This can be however used as a base for closed
loop control.
LCD module connected to Arduino as instructed in code.

P a g e 4 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position

Virtual COM (for later use with LabVIEW).

Fig. 6 Proteus model

Before you start the simulation, make sure your code is compiled into hex file. Double click
on Arduino chip in simulation and point to the generated hex file. It is recommended that you
place the hex file alongside your Proteus
model.

Fig. 7 Arduino code and Proteus

Run the simulation and watch results

P a g e 5 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position

a) Scope output showing both channels output signal


b) LCD display
Fig. 8 Simulation result

4.3. Exercise
Modify the Arduino code to display the actual position in radian and degree
considering that a 1 = 231 .
4.4. Communication with labVIEW
The second part of lab activities is to communicate LabVIEW with Proteus for reading
encoder position and plot it on a chart.
4.4.1. Arduino Code

The code discussed earlier has the command Serial.println(buffer)which sends


encoder reading as 6 characters to LabVIEW.

4.4.2. LabVIEW VI
As mentioned in earlier, your PC must have NI VISA [2] installed in addition to
LabVIEW to allow LabVIEW to detect available serial port. Since there is no
connected physical Arduino so far, we will also use virtual com port emulator
which virtually connect two com ports together. One will be used on Arduino
(inside Proteus) and the other with LabVIEW.

P a g e 6 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position

Fig. 9 COM port connections

4.4.3. LabVIEW block diagram


The VI was coded by instructors to provide minimum effort to successfully receive
encoder data.

Fig. 10 LabVIEW block diagram

VISA resource name : to enumerate available com ports. You either select the virtual
com port or in reality the Arduino com port.
Configure serial port : after selecting the com port, we may edit the baudrate and
reading timeout

P a g e 7 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position

VISA read : Reading six characters from Arduino (you can change that in Arduino code
buffer variable to suit your needs).
String to Number conversion : Since Arduino passes data as encoded string, this block
converts the transmitted characters into single decimal value.
VISA close : This is to close connection and unlock com port so other applications can
use it if needed.

5. Results
Start Proteus simulation and then LabVIEW.

Fig. 11 LabVIEW - Arduino co-simulation for encoder reading

6. Further exercise
Modify the code to calculate velocity in PPR.

7. File repository
You can get all files of lab 1 from GitHub repository of the course [3].

P a g e 8 | 12
MCT 432 Hybrid Control Lab 1: Reading Quadrature Encoder Position

Fig. 12 GitHub course repository

8. References
[1] Encoder Library for Arduino. Available:
https://www.pjrc.com/teensy/td_libs_Encoder.html
[2] N. Instruments. NI VISA. Available: http://www.ni.com/download/ni-visa-
15.0.1/5693/en/
[3] F. Tolbah and W. El-Badry. MCT 432 Hybrid Control Repository. Available:
https://github.com/wbadry/MCT432-Hybrid-Control

P a g e 9 | 12

Vous aimerez peut-être aussi