Você está na página 1de 26

2012

Brian Greiner
Professor Peter Han ECET365 Embedded
Microprocessor Systems
12/17/2012
Course Project - Robot
1

Contents
Summary ....................................................................................................................................................... 2
Introduction .................................................................................................................................................. 2
Purpose and scope .................................................................................................................................... 2
Sources and methods................................................................................................................................ 3
Report organization .................................................................................................................................. 3

Hardware implementation ........................................................................................................................... 3
Transmitter ............................................................................................................................................... 4
Receiver..................................................................................................................................................... 6
Dragon12 development board and HCS12 ............................................................................................... 9
Continuous rotation servo motors .......................................................................................................... 10
Chassis ..................................................................................................................................................... 11
Hardware issues ...................................................................................................................................... 11

Software implementation ........................................................................................................................... 12
Pulse width modulation .......................................................................................................................... 12
Finite state machine ................................................................................................................................ 13
Software polling ...................................................................................................................................... 14
Phase locked loop ................................................................................................................................... 14
Software issues ....................................................................................................................................... 14

Conclusion ................................................................................................................................................... 15
Project Recommendations ...................................................................................................................... 15
Technical Results ..................................................................................................................................... 15
Critical Thinking Results .......................................................................................................................... 16

Appendix A References ............................................................................................................................ 17
Appendix B Source code .......................................................................................................................... 18
Appendix C PWM State Outputs .............................................................................................................. 21

2

Summary
This report covers the course project for ECET 365. The goal of the course project is to build a remote
controlled PWM operated moving robot. The robot built for this project is detailed in this report. The
hardware is made up of a remote control transmitter built on a bread board, a receiver, also on a bread
board, a development board with microcontroller, four servo motors and a chassis. Four keys on the
remote send data to the receiver. The receiver then sends these key presses to the microcontroller,
which uses this information to control the speed and direction of the servo motors mounted on the
chassis.
The microcontroller is programmed to vary the duty cycle of a PWM signal to the servo motors to
control their speed and direction. The specifics of this PWM duty cycle are determined by logic
implemented in transitioning between the states of a finite state machine. The program polls a valid
transmission pin on the receiver decoder and when this becomes active, uses the new button press to
determine which state to transition to. As timing is important for this implementation, a phase locked
loop is used to maintain the system clock while the robot runs in standalone mode.
Video of the functioning robot may be found at the following link:
http://www.youtube.com/watch?v=Rn6wQefUG_U
Introduction
The course project involved the building and testing of a microcontroller controlled remote activated
robot. The robot receives inputs from a transmitter module and uses these inputs to determine a
direction of travel.
Purpose and scope
The purpose of this project is to demonstrate knowledge of the curriculum learned throughout a series
of electronics and computer engineering courses over the past several semesters at DeVry University.
3

The functioning robot demonstrates knowledge in hardware design, software design and embedded
controller interfacing. Furthermore, creating the robot required skills in both hardware and software
troubleshooting using tools such as an oscilloscope and digital voltage meter to test the hardware and
the Codewarrior IDE to debug the program.
Sources and methods
Several sources were used in the design and implementation of the robot. Primarily the course texts
from the current course and previous courses were used as a basis to plan the software
implementations. These texts are, Embedded Microcomputer Systems Real Time interfacing by
Valvano and, HCS12/9S12 An introduction to software and hardware interfacing by Huang. Also
sourced heavily were the datasheet for the HCS12 microcontroller and the datasheets for the encoder
IC, transmitter module, receiver module and decoder IC used in the project.
Report organization
This report is organized by the individual parts involved in the planning and construction of the robot. It
starts with information on the hardware used to create the robot. This is followed by the details about
the software implementation and finally there is a technical and critical thinking conclusion. The
references, source code and some project documentation are included in the appendices.
Hardware implementation
There are five main components that make the robot work. The first component is a user interface
which encodes and transmits button presses. Secondly, there is a receiver module that receives and
decodes these button presses. Central to these components is the dragon 12 development board
including an HCS12 microcontroller. The microcontroller takes inputs from the receiver and outputs
control to four continuous rotation servo motors. These motors and the development board are
mounted to a chassis.
4

Transmitter
The transmitter is made up primarily of a voltage regulator circuit, four pushbutton switches, a Holtek
HT6014 encoder and an Easy-Link wireless Ultra small Transmitter. Each of the buttons on the
transmitter is wired so that a press of the button creates a path to ground. This is interpreted by the
encoder as a logic low. There are four inputs to the encoder that will trigger the data output to send
the encoded data to the transmitter. The push button switches are each wired to one of these four
inputs. The encoder also has eight lines for addressing, all of which are wired to ground. Furthermore
the encoder has an oscillator input for varying the oscillation using a resistor and the data output lines
to the transmitter. The resistor chosen for the oscillator inputs has a resistance of 1M. According to
the data sheet, with a five volt supply this should create an oscillator frequency of 3kHz, the
recommended frequency for the oscillator. The data output of the encoder is fed into the Ultra Small
Transmitter, which transmits the data using Amplitude shift keying (ASK) at 433.92MHz.

The oscillator frequency resistor is calculated from this chart.
5


The voltage regulator circuitry provides a clean 5V to power the transmitter.
The encoder also has an LED to indicate a transmission.

Wires on the breadboard are color coded for ease of understanding and later troubleshooting. Non-
colored silver jumpers provide the grounding for the address pins. There are four pushbuttons that are
associated with forward, reverse, left and right.
6


For R1, a 1M resistor was chosen. All of the address pins are grounded. Two filter capacitors of 4.7F
and 0.1F are used with the voltage regulator to provide clean power.
Receiver
The receiver is made up of similar components to the transmitter. It also requires a clean 5V power
supply, but instead of constructing one on the bread board, power was sourced from the dragon12
development board, which already has a filtered and regulated 5V supply. The receiver module receives
the ASK signal at 433.92MHz sent from the transmitter and converts to a data stream that will be
interpreted by a decoder IC. The decoder IC, a Holtek HT6034 has a data input line and receives serial
data from the receiver module. Successfully decoded data first causes a valid transmission (VT) pin to
transition from low to high briefly. Following, one of four output lines normally in a logic high state
will latch to a logic low state until different code is received. All address lines on the decoder IC were
grounded to match the encoder. An appropriate resistor, 100k, was chosen to regulate the oscillator to
approximately 91kHz as per the suggestion of approximately 33 times that of the encoder made by the
OSC2
Dout
SW4
SW2
SW1 1 2
SW2 1 2
AD9
AD12
SW3 1 2
1
AD10
AD11
4
SW1 SW5 1 2
TLP434A
3
SW0 SW4 1 2
1
2
SW3 SW7 1 2
7805
1 3
VIN VOUT
C1
TE
OSC1
Vdd
RF Out
SW6 1 2
R1
7

datasheet. The four output lines and the VT pin are wired to the pins associated with port B, bits 0
through 4 on the dragon board.

With a 5V supply a 91k resistor is ideal, but a 100k is available and worked perfectly.

Wires are color coded for ease of troubleshooting. The green wires are the outputs or the oscillator, the
yellow are data inputs, the white is the VT bit and the red and black are power and ground respectively.
8


Here can be seen the impulse on VT (Channel 2) and the corresponding latch to logic low on one output
line (Channel 1) of the receiver. This impulse and latch are used by the microcontroller to decide how to
regulate the PWM on port P.

Here a 100k resistor is used for R1. Clean voltage for the receiver is supplied by the dragon board.
D9
PB0
PB1
1 2
1 2
P
O
R
T
P
6
1 2
1 2
A2
P
O
R
T
P
3
DIN
A6 PB3
OSC1
P
O
R
T
P
0
1 2
RF in
O
S
C
1
GRD
P
O
R
T
P
4
MC9S12DG256
C1
A0
A3
P
O
R
T
P
2
PB7
Vcc
D10
Vdd
1 2
Vss
GRD
A5
C1
P
O
R
T
P
5
Vcc
HT12D
GRD
A7
Linear Out
1 2
Digital Out
PB4
PB6
RLP434
D8
A1
1
A - +
MG1
MOTOR SERVO
1 2
OSC2
O
S
C
2
P
O
R
T
P
1
PB2 D11
PB5
A4
1 2
A - +
MG2
MOTOR SERVO
1 2
7805
1 3
VIN VOUT
R1
VT
9

Dragon12 development board and HCS12
The dragon development board has as a central component, the MC9S12DG256 microcontroller from
Freescale Semiconductors. For this robot the microcontroller takes inputs from port B and outputs
pulse-width modulation (PWM) signals to port P pins 4 through 7. Port P from the microcontroller is also
wired through jumpers to an RGB LED and a speaker on the development board. These jumpers were
removed to prevent loading on the PWM channels. The dragon board also includes an LCD which has a
backlight controlled by a jumper. Since the robot is battery powered and this LCD is not utilized, the
jumper was disconnected. Power for the board was sourced from a 9 volt battery. The servos were
powered by and independent battery connected to terminal block T7. There are four connectors
specifically designed to interface to servo control on the dragon board. These were utilized to control
the four servo motors on the robot.

10

Power is wired from the voltage regulator circuit on the dragon board (terminal block T4) to the
prototyping board where the receiver is constructed. The separate power for the servo motors is
implemented by positioning Jumper J35. Grounds for the servo and development board are connected
together.
Continuous rotation servo motors
Four continuous rotation servo motors designated #900-00008 by Parallax were used as the primary
motion for the robot. These servos have a cable with three lines; they are power (red), ground (black)
and a line for control using PWM (white). According to the data sheet for the servo, a pulse should have
a positive width of between 1ms and 2ms and a negative width of 20ms. A positive width of 1.5ms to
2ms will cause the servo to rotate counter-clockwise with increased speed correlated to the pulse width
while a positive width of 1ms to 1.5ms will cause the servo to rotate clockwise with speed inversely
correlated to pulse-width. The output shaft of these servo motors each has a wheel, which provides the
motion of the robot.

The servos are connected to servo control outputs specifically built into the dragon board for interfacing
with servo motors. The motors are mounted to the chassis, which is a project box.
11

Chassis
The chassis chosen for this project is a simple project enclosure. The size of the chassis needed to be
large enough to accommodate the dragon board and four servo motors. It also needed to be light
enough that it could be carried by the torque specified by the servo motors and modifiable to allow
fitment of the dragon board and servos.

The dragon board is mounted to the underside of the lid of the project box with the antenna from the
receiver fed through a hole in the lid. The servos are mounted to the outside of the chassis with screws.
Hardware issues
The one hardware issue that required the most troubleshooting was simply getting the receiver and
transmitter to work together. Initially the transmitter was wired up incorrectly due to some confusion in
a diagram on the datasheet. Several hours were spent finding this issue that involved taking readings
from many locations in both receiver and transmitter. Finally, communication was established. The
initial design for the chassis had the motors mounted lengthwise. This created poor performance in
12

turning. The wider and shorter wheelbase created by mounting the tires widthwise on the chassis solved
this.
Software implementation
The goal of the program used for this robot was to take inputs from port B, provided by the receiver
module and output PWM control signals to port P, which is connected to the servo motors. The HCS12
already has PWM channels in place that, when configured properly, output to port P. While reading this
section it is helpful to reference the source code in Appendix B.
Pulse width modulation
The pulse width modulation on the HCS12 is controlled by several registers. For this robot a PWM signal
with a period of 20ms and a pulse width of between 1ms and 2ms needed to be created to control the
servos. Four servo motors were driven using the PWM control signals from port P pins 4-7. In order to
control four servo motors using the built in servo interfaces on the dragon board, 8-bit PWM is required,
as concatenating the PWM channels would only allow for two of the interfaces to be utilized. The
function PWMinit() initializes the PWM channel to the required parameters. The 24MHz system clock is
first pre-scaled to 1.5MHz using the value 0x44 on the PWMPRCLK register. Both clocks A and B are then
scaled further to 10kHz by values of 75 on PWMSCLA and PWMSCLB. This gives a period of 0.1ms. The
registers PWMCTL and PWMPOL are set to not concatenate the PWM channels with an initial high output.
To gain a 20ms period for PWM a value of 200 is written to each of PWMPER4, PWMPER5, PWMPER6 and
PWMPER7. The registers for duty cycle, PWMDTY, are initialized to zero. PWME turns on the PWM cannels.
After PWM is initialized the program can regulate the speed of the servo motors simply by changing the
values written to the four PWMDTY registers. The results of these different duty values based on the
current state of the machine can be found in appendix C.
13

Finite state machine
The program is designed using an abstraction known as the finite state machine (FSM). An FSM has
certain states with parameters and may transition to different states based on a time period or external
input. This is a perfect model for this robot as the robot will be traveling in one direction at any given
time and will transition to a different direction based on the inputs from the remote control transmitter.
There are nine states described for this robot. Each of the nine states has a different value for PWM duty
for the left and right wheels of the robot. Each state may transition to up to four other states based on
the inputs to port B.

14

Here is the state diagram that describes the transitions that may occur as a result of the inputs received
from the remote control.
Software polling
For the design of the software, there was a choice between using interrupts and using polling to
determine when the robot may change states. The decoder circuit provides a valid transmission (VT)
output which is fed into port B pin 4. This output pulses when a new button press is received by the
receiver. Since at this time, the only thing the robot may do is change states, it was decided to poll this
pin on port B by using a bit mask. The polling solution keeps the software shorter and easier to
understand than an interrupt driven solution. The software polls port B pin 4 where VT is connected and
when a pulse is detected scans Port B pins 0 through 3. The results of the value read from port B along
with the current state information is used to determine the state to transition to next.
PORT B
Pin 0 Pin 1 Pin 2 Pin 3 Pin 4
LFT FWD RVS RGT VT
Phase locked loop
The final piece in the software design is the phase locked loop (PLL). In order to have the microcontroller
run in standalone mode, a PLL must be used to create and stabilize the 24MHz system clock. The
programming for the PLL can be found in the function init_PLL( ).
Software issues
There were many software issues that occurred. The process for troubleshooting these software issues
was to step through the code one line at a time, watch the results of the changes in the software
variables and while using the Serial monitor, in Codewarrior, observe the output from the PWM
channels on Port P. One particular issue that had to be remedied was an issue related to transitions to
the stopped state. When the robot was in the forward state, a press of the RVS button would
transition through stopped directly to reverse. The problem was found to be that VT stays high for
15

longer than a debounce period and causes a double transition. This was resolved by creating a software
delay of a full half second whenever the robot transitions to a stopped state.
Conclusion
The hardware for the transmitter was assembled on a breadboard with a 9 volt battery connected to the
voltage regulator. The receiver was assembled on the breadboard that is part of the dragon
development board. The dragon board and four servo motors were mounted to the project box chassis.
The software was uploaded using a serial USB cable to the microcontroller. Extensive testing and
troubleshooting took place, but finally the robot was able to drive, and all directions were achieved.
Project Recommendations
The project was well written and allowed for open ended interpretation by students. The ability to add
additional components to the robot such as distance detectors or to use a joystick for control instead of
buttons might make the project more engaging.
Technical Results
There are four 8-bit PWM channels used to control the four servo motors. Through simple programming
the microcontroller was able to take inputs from the decoder module and make decisions as to what to
output to the PWM channels controlling the wheels. There were limitations imposed by the hardware
that could have potentially been designed differently. There are only four purpose built servo controller
interfaces on the dragon board. These are hard wired to port p pins 4 through 7. Because four servos
needed to be driven by these four ports, 16 bit concatenation was not possible. With the 8 bit PWM,
resolution allowed steps in size of the duty cycle of only 0.1ms. These are large steps when considering
the range of the PWM duty cycle only varied by 1ms.
16

Critical Thinking Results
The tools and knowledge gained on both hardware and software in this course and during the
construction of this project create a foundation for a vast array of skills that are applicable in many fields
where technology is implemented today. These skills, including programming embedded controllers and
interfacing with microcontrollers have applications in many industries from computer hardware design,
automotive, pharmaceutical, moving boxes to beer production. Automation, human interfacing and
wireless communication are all elements of the future of technology.

17

Appendix A References

Huang, H. W. (2009). The hcs12 / 9s12: An introduction to software and hardware interfacing. (2 ed.).
Clifton Park, NY: Delmar.
Valvano, J. (2006). Embedded microcomputer system: real time interfacing. (2nd ed.). Mason, Ohio:
Cengage Learning.

18

Appendix B Source code
/******************************************************
*Written and tested by Brian Greiner 12/15/2012
*Week 6 Lab, Final project ECET 365 DeVry University
*Professor Peter Han
*This Program is used with the dragon12 development
*board and the MC9S12DG256 microcontroller
*to controll a robot using PWM
*A receiver module inputs to port B and the
*microcontoller outputs PWM to port p to drive
*servo motors
******************************************************/

#include <hidef.h> /* common defines and macros */
#include <mc9s12dg256.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg256b"


const struct State {

int leftCtl; //left PWM duty
int rightCtl;//right PWM duty
const struct State *goTo[4];

};



typedef const struct State sType;

//buttons
#define RGT 0x01 //0001
#define FWD 0x02 //0010
#define RVS 0x04 //0100
#define LFT 0x08 //1000

//state pointers
#define ST &FSM[0] //stopped
#define CL &FSM[1] //circle left
#define CR &FSM[2] //circle right
#define FW &FSM[3] //forward
#define FR &FSM[4] //forward right
#define FL &FSM[5] //forward left
#define RV &FSM[6] //reverse
#define RR &FSM[7] //reverse right
#define RL &FSM[8] //reverse left



sType FSM[9]={ //states
{0,0,{CL,RV,FW,CR}}, //stopped
{20,20,{CL,RV,FW,ST}}, //circle left
{10,10,{ST,RV,FW,CR}}, //circle right
{20,10,{FL,ST,FW,FR}}, //forward
{20,15,{FL,ST,FW,FR}}, //forward right
{15,10,{FL,ST,FW,FR}}, //forward left
{10,20,{RL,RV,ST,RR}}, //reverse
{10,15,{RL,RV,ST,RR}}, //reverse right
{15,20,{RL,RV,ST,RR}}, //reverse left
};

char VT = 0; //valid transmission
19

char foo = 0; //holding port value
char NS = 0; //next state

sType *ptr;

void delay(int);
void PWMinit(void);
void init_PLL(void);
void runWheels(int, int);

void main(void){

DDRB = 0x00; // Port B inputs
PWMinit();
init_PLL();
ptr = ST;

while(1){

runWheels(ptr->leftCtl,ptr->rightCtl);

VT = 0;
while (VT == 0){ //wait for valid transmission
if ((PORTB & 0x10)!=0) {
delay(20); //debounce
VT = PORTB & 0x10;
}
}


foo = ~PORTB & 0x0f; //check which button was pressed
switch (foo){
case RGT: NS = 0;break;
case FWD: NS = 1;break;
case RVS: NS = 2;break;
case LFT: NS = 3;break;
default: break;
}

ptr=ptr->goTo[NS];

if (ptr->leftCtl == 0) delay(500); //pause if stopped for .5 sec

}
}


void delay(int del){ // delay for 'del' msecs
int i,j;
for(i=0; i<del; i++)
for(j=0; j<4000; j++);
}


void runWheels(int leftCtl, int rightCtl){

PWMDTY4 = rightCtl; //Front Right
PWMDTY6 = rightCtl; //Rear Right
PWMDTY5 = leftCtl; //Front Left
PWMDTY7 = leftCtl; //Rear Left

}

20

void PWMinit() {

PWMSCLA = 75; //scale aclock /(75 * 2)
PWMSCLB = 75; //scale bclock /(75 * 2)
PWMCLK = 0xf0; //Ch. 4,5,6,7 source: clock SA,SB
PWMPRCLK = 0x44; //Prescale A & B 2^4
PWMPOL = 0xF0; //Initial HIGH output (left-align) on ch. 4,5,6,7
PWMCTL = 0x00; //8-bit PWM counter, period and duty cycle registers
PWMPER4 = 200; //Channel 4 period value = 20 ms 1/((24MHz/2^4)/(75*2)) *
200
PWMDTY4 = 0; //duty initailly 0;
PWMPER5 = 200; //Channel 5
PWMDTY5 = 0;
PWMPER6 = 200; //Channel 6
PWMDTY6 = 0;
PWMPER7 = 200; //Channel 7
PWMDTY7 = 0;
PWME |= 0xf0; // turn-on PWM ch. 4,5,6,7

}

void init_PLL( ){ // initializes phase-locked loop (hardware)for 24Mhz CPU clock
asm(sei); // for running board standalone w/out Codewarrior
CLKSEL &= ~0x80;
PLLCTL |= 0x40;
SYNR = 0x05;
REFDV = 0x01;
while((CRGFLG & 0x08) == 0){ // wait here for lock
CRGFLG |= 0x08;
}
CLKSEL |= 0x80;
asm(cli);
} // use this function as is


21

Appendix C PWM State Outputs


Forward

Forward Left
22


Forward Right

Stopped
23


Reverse

Reverse Left
24


Reverse Right

Circle Left
25


Circle Right

Você também pode gostar