Upload
doankhue
View
222
Download
2
Embed Size (px)
Citation preview
Embedded System Design - Servo Control of DC motors The design of real-time control software to give PD positional control of a mobile robot
2017
Zeeshan Mustafa Latif Ansari BEng (Hons) Electronic Engineering, Birmingham City University
1/20/2017
s09466807 Page 1
Contents Page Numbers
Introduction…………………………………………………………………….2
Section 1: Hardware Design…………………………………………………3
Block Diagram of DC-Motor Servo Controller……………………….3
Functional Specification of the Block Diagram……………………...3
Rationale for the choice of processor………………………………..5
Schematic for Single Channel DC-Motor Servo Controller………..6
Algorithm – Flowchart………………………………………………….7
Pulse Width Modulation………………………………………………..8
H-Bridge – Rotation…………………………………………………….9
DC-Motor Characterisation……………………………………………9
Mouse Position and Speed Measurement…………………………..9
Pulse Counter…………………………………………………………10
Data Acquisition……………………………………………………….10
RS232…………………………………………………………...10
Section 2: Software Design…………………………………………………11
Proportional and PD Controller (Track)…………………………….11
Speed Profiler…………………………………………………………15
A 4 Cell Profiled straight……………………………………………..15
A Profiled 90 degree rotation………………………………………..16
Summary……………………………………………………………………...17
References……………………………………………………………………17
Appendix……………………………………………………………………...18
s09466807 Page 2
Introduction
The report aims at gaining clear understanding of architecture and then building the latest
design (using Servo control of DC motors) of “MYTEE-Mouse” which is a successor to the
previously made robots - “Heretic” and “Robotic” at Birmingham City University“. To put it
more precisely, it is the design of real-time control software to give PD positional control of a
mobile robot (MYTEE-Mouse). This assessment mainly involves the position encoder of
mouse, motor drive, real-time system and closed loop control theory. Design consists of two
major parts which are given below:
Hardware Design
This part was initiated by functional specification with block diagram for a single channel DC-
motor servo controller, using the minimum number of components and an appropriate
processor of PIC 8-bit range which was chosen to be PIC18F4523. It then included a
rationale for the choice of processor, a schematic of the design and finally the development
of algorithms and description to clearly show that the design could function. Design will be
undertaken by keeping in mind the important factors i.e. Forward and Rotational profile to
drive mouse from start to finish, application of PWM to the DC motors, use of encoders to
measure the speed and position of mouse, measurements of distance by using IR sensors
and how to control the position by using the PID controller and Speed profiler. Following this,
the software design begins.
Software Design
The Software design for servo control of DC motors began with the development of program
codes in order to demonstrate how the MYTEE-Mouse can be implemented to prove its
functionality into the maze. This part, in detail, included an introduction to servo control on
MYTEE-Mouse, lab reports with data acquired and analysed (using graphs) on “PID
controller” and “Speed Profiler“ and description of the final software design to accomplish
three milestones (specified in assessment brief) which are stated below:
(a) a 4-cell profiled straight;
(b) a profiled 90 degree rotation;
(c) Sequencing of a, b in order to complete a 4x4 cell rectangle and return to the start
point.
s09466807 Page 3
Section 1: Hardware Design
Block Diagram of DC-Motor Servo Controller
Functional Specification of the Block Diagram
MYTEE mouse can be defined as a mobile device that with applied codes to its
microcontroller (being PIC18F4523 for this time), it should demonstrates some operations
such as following the wall with specific dimensions, on a unknown route, to start on a
specific point and while following the path on maze by sensing the walls on sides and in front
it reaches its pre-defined destination.
In past, two micro mouse have been built by BCU; first one was called Robotic which had
numerous interconnections which were prone to faults i.e. motor control used a MOSFET
chopper transistor with relay direction change, motor assemblies were sourced externally,
wall-sensors were top-of-the-wall devices that added greatly to the mass and turning
moment of the mouse. It was a poor designs but it performed the job required of it (Dr Wilcox,
2017
The second one was called Heretic or SMA (Student Mouse A) which was proved to be a
very successful design; it had fully symmetric array of 9 IR-sensors, an RF-link and a wire-
link for RS232 communications with a host PC, H-Bridge motor control, low-inertia DC
motors with spur an pinion gearing and high resolution encoders for wheel position
measurement. It also had reduced battery size and came out to be a much more compact
low-level design. Its applications included its use in maze-solving and in an open-
environment for co-operative and swarm research activities (Dr Wilcox, 2017).
Then another mouse was developed based on SMA which had its name changed from SMB
(Student Mouse B) to MyTEE-Mouse due to the formation of the new faculty (TEE). The
DC-Motor Servo Controller
(Microcontroller - PIC18F4523)
Motor
Encoder
H-Bridge
Power
Power Supply
8 AAA
Rechargeable Batteries
Memory
EEPROM
FRAM
Communication
ICD
Connector
RS232 Cable
PIC
PICKIT2
Debugger
s09466807 Page 4
microcontroller being used for this design is PIC18F4523 which operates at frequency of
64MHz is responsible for providing commands (that specify their operations) to its
peripherals.
Since the micro mouse operates at 5V and batteries come with supply voltage of 9.5V, a
potential divider network is required to keep voltage stabilised at 5V. Micro mouse is
connected with 8 AAA rechargeable batteries for power supply which can be charged with
the voltage supply that is set at 14.4V.
A high current half-H Driver motor (named L293D) was used to drive the left and right
motors. L293D supports bi-directional drive of two DC motors with PWM speed control
whereas in this exercise only single channel DC servo is required. When driver input controls
the direction of the motor, the PWM for the motor is applied to the enable input of the H-
bridge (Dr Wilcox, 2017). It offers the benefits of bi-directional (forward and backward) drive
currents up to 600mA per driver at voltages between 4.5V to 36V (Texas, 2017).
The mouse is connected with three pushbuttons and two LEDs. Green LED and Red LED
are connected with PB2 and PB2 respectively. When PB1 is pressed, the red LED lights up
and the mouse stars calibrating and when PB2 is pressed the green LED lights up and
mouse starts moving whereas the third pushbutton is used to stop the mouse. The mouse
can also store information about sensor readings which is achieved by connecting mouse to
“Tera Term” and when Reset pushbutton is pressed - following which the PB2.. The micro
mouse can also be turned “ON” and “OFF” by the use of power-ON switch that is connected
to it.
In order for the microcontroller to be programmed or debugged an insulation-displacement
contact (14-way IDC) connector is also used (while connected to the microcontroller) to
download the code from the MPLAB software and then to store it into the memory (EEPROM
and FRAM). EEPROM which stands for Electrically Erasable Programmable Read-Only
Memory is a non-volatile memory which is used in embedded systems to store small amount
of information while offering options to erase and reprogram individual byte of information
(tech, 2017).
FRAM (ferroelectric RAM) is a random access memory that combines the fast read and write
access of dynamic RAM (DRAM) with its ability to retain data when power is turned off.
Although, it cannot store as much data as DRAM and SRAM does but because it’s a fast
memory with very low power requirements it replaces EEPROM and SRAM for many
applications (what, 2017). Therefore, because of their unique features, both EEPROM and
FRAM are used to erase, reprogram or store code required for mouse operations. As
previously said information can be viewed through Tera Term when mouse is connected with
a RS232 and ICD connector.
It is worth mentioning that MyTEE-Mouse also has 6 wall sensors which are 3 pairs of IR
phototransistors and photo-emitters. Sensors indicate the position of the mouse by
detecting the light from the IR emitters that is reflected from the walls of the maze. The first
forward-looking sensors’ pair detects the distance to the front wall, the second angled
sensors’ pair tracks the side wall and the third side looking sensors’ pair is used to detect the
wall openings for re-calibration purposes. In this exercise IR sensors are disabled (not in
use), instead simple Proportional- derivative Controller and Speed Profiler is being used for
s09466807 Page 5
a 4 cell profiled straight, a profiled 90 degree rotation and for sequencing so that mouse can
complete 4 × 4 cell rectangle and return to it start point (Dr Wilcox, 2017).
Out of the many advantages of MyTee-Mouse, the one that must be stated is that it is cheap
to design and build and it offers great functionality as a wall follower and maze solver for UK
level competition that takes place every year at BCU.
Rationale for the choice of processor
A processor can be defined as a logic circuitry that responds to and processes the basic
instructions that drive an electronic system (MyTEE-Mouse). The four primary functions of a
processor would include fetch, decode, execute and writeback (what, 2017).
The processor was chosen based on its peripherals, size, cost and other important features
i.e. FLASH, SRAM, EEPROM, USB interface, I/O pins, number of servos support, analogue
inputs, PWM, serial communication, external interrupt pins and boot-loader etc.
PIC stands for “Peripheral Interface Controller”, it’s a specialised family of microcontrollers
which are compact microcomputers designed to control the operations of embedded
systems in robots and most of the electrical/electronic devices. A microcontroller normally
includes processor, memory and peripherals. PIC microcontrollers are in fact quite cheap
and they can be bought as kits that can be assembled by the user or as pre-built circuits.
Microcontrollers are programmed and simulated using computers and “Circuit Wizard”
software. In this project the microcontroller being used is PIC18F4523 and software to
program it using “C code” is MPLAB IDE. When the program is simulated and it works, the
program is downloaded and saved into the memory of PIC18F4523 using a USB lead which
can then be run independently without using the USB cable.
Figure 1 PIC18F4523, (micro, 2017)
PIC18F4523 is a 40-pin device with flash memory of 32 kilobytes and RAM of 1536 bytes.
According to the datasheet developed by the company “MICROCHIP TECHNOLOGY” the
actual microprocessor “PIC18F4523” has the following features:
Its CPU has up to 10 MIPS (Million Instructions per Second) performance; it also includes a
C-compiler optimized RISC (Reduced Instruction Set Computer) architecture and an eight
times eight (8x8) Single Cycle Hardware Multiply.
Its system includes an internal oscillator which supports 31 KHz to 8MHz. It also has a
property that allows a safe shutdown if clock fails. This function is called Fail-Safe Clock
s09466807 Page 6
Monitor. Furthermore it has a watchdog timer with separate RC Oscillator and it operates at
voltage ranges from 2.0V to 5.5V.
Its “nanoWatt Power Managed Modes” includes Run, Idle and SLEEP modes. Idle mode
currents down to 5.8uA typical and Sleep mode currents down to 0.1uA typical.
Its analogue features include 12-bit ADC (Analogue to Digital Converter), 13 channels, 50K
samples per second and two Analogue Comparators multiplexing. It can also be
programmed to Low Voltage Detection Module and Brown-out-Reset Module.
Its peripherals include Master Synchronous Serial Port supports SPI and IC2 Master and
slave mode. It also has EUSART (Enhanced Universal Synchronous Asynchronous
Receiver Transmitter) module with a LIN (Local Interconnect Network) bus support. Finally it
consists of Four Timer modules, 5 PWM (Pulse Width Modulation) outputs and up to 2
Capture/Compare.
Development tools include Demo and Eval Boards, Emulation & Debuggers and
Programmers (micro, 2017).
Price: The chosen microcontroller is priced at £ 4.12 which is very economical for MyTEE-
Mouse.
Schematic of Single Channel DC-motor Servo Controller
s09466807 Page 7
Algorithm – Flowchart
s09466807 Page 8
Pulse Width Modulation The PWM was implemented using the CCP Modules to reduce the loading on the processor
and to leave two timers free for pulse counting, it is shown below:
Void OpenTimers2 (unsigned char config);
Void OpenPWM1 (char period); // PWM period = (PR2 register +1) ×4×Tosc×TMP2 Pre-
scalar Value
Void SetDCPWM1 (unsigned int dutycycle); // PWM Duty period = PWM Duty Value × Tosc
× TMR2 Pre-scalar Value
// PWM Duty range = (PR2 register + 1) × 4
These two PWM channels used timer2 as their clock. PR2 register or the Timer2 prescalar
can be altered to change the PWM frequency (where prescale can be either 1.4 or 16). The
speed of a DC motor is directly proportional to the applied voltage. In order to achieve the
full 10 bit range for duty cycle, the PR2 resistor is set to 225. Using low frequencies keeps
the motor on for longer whereas using high frequencies results in higher current applied to
the motor at the start, hence less current remains which keeps the motor on for short time.
High torque produced by this current at the start allows the motor to overcome the effect of
stiction thus allowing it to accelerate quickly.
In order to fulfil all the requirements given above, the PWM frequency of 2KHz is achieved
by setting the PR2 resistor and pre-scale value to 249 and 16 respectively which gives duty
gain of 0 to 1000 (the duty gain necessary to achieve 2KHz of PWM frequency).
The PWM frequency and period is calculated as shown below:
The frequency of MyTEE-Mouse is 32 MHz
Frequency
Timer2 (input clock) = 32MHz / 4 = 8 MHz per instruction cycle
Now divide 8 MHz by the Pre-Scale value of 16 = 8 MHz / 16 = 0.5 MHz
Again divide by 249 (TIMER 2 = 8 bit value) = 0.5 MHz / (249+1) = 2 KHz
So Frequency is 2 KHz
Period
Periods = 1 / frequency
So Period = 1 / 2 kHz = 500 us this is the PWM period time.
The PWM makes good use of duty percentage i.e. acceleration, deceleration and desired
speed can be produced by increasing or decreasing duty percentage where the duty cycles
of 0 and 1000 would indicate that the output is always low and high respectively.
s09466807 Page 9
H-Bridge - Rotation The rotation of the MyTEE-Mouse was controlled by using the H-Bridge method. The motor
driver L293D was used to drive a single channel DC-motors which has the maximum
operation frequency of 5 KHz – suitable for chosen PWM frequency of 2 KHz. As the name
states that the digital amplifier which is used produces digital output such as on/off or 1/0.
Whatever the input is loaded i.e. 40% duty cycle square wave would produce 40% in output,
thus restricting the power to the motor to a defined level.
DC-Motor Characterisation A single channel RF500-TB-12560 DC-motor Iis used for MyTEE-Mouse. The motor has
maximum speed of 3100rpm when no load applied on it. Its speed is achieved by voltage
and direction is determined by polarity – speed is proportional to the voltage and direction to
the polarity.
Figure given below helps us understand the relationship between the “ideal” and “actual”
response of the motor. The “Ideal response” line shows that the “Motor Speed” and “Applied
Voltage” are directly proportional to each other. Stiction must be eliminated for the motor to
reach its maximum speed. It can also be seen that once the ideal response settles down the
motor also slows down.
Figure 2 Motor drive, (Dr Wilcox, 2017)
In order to drive the motor at constant speed the coefficient factor (Kd) which is calculated
from the “Speed vs. PWM” graph, is used to convert the speed value to PWM. The motor
was driven by setting the PWM of the DC motor to duty of 50% for 4030 pulses. Tera Term
along with RS232 line and ICD connector was used to capture the data so that graphs can
be plotted which are given below:
Mouse Position and Speed Measurement Optical encoders will be used to obtain measurements of speed and position. MyTEE-Mouse
uses quadrature encoder HLC2705 for a single channels DC-motor servo controller. The 12-
tooth pinion gear which is fitted with 60-slot encoder disc. Encoder disc rotates an IR emitter
and a times-2 quadrature (HLC2705 – provides 120 pulses/revolution) giving 2*60*80/12 =
800 pulses per revolution of the wheel. As the wheel diameter is 45mm which gives a
circumference of pi*45mm = 141.37 and when divided by pulses per revolution of the wheel
i.e. 141.37/800 gives 0.18mm per pulse.
s09466807 Page 10
Figure 3 Motor-Gear- Encoder Assembly & HLC2705 Quadrature Detector Output, (Dr Wilcox, 2017).
Pulse counters One method that could have been used to drive the microcontroller (PIC18F46K42) timers
was by enabling their timers to count external pulses by using external inputs. As this
method only counts upwards that’s why the problems was resolved by using an alternative
method where combinations of interrupts were employed which can decide whether to
decrement (down) or increment (up) the count variable by changing the values of the counter
timers.
So the pulse counting was finally achieved by configuring the timer0 to produce an interrupt
by an external input. In MCC18 compiler “Timer libraries” and header file “Timer.h” were
used, and because the timer can only count 1 pulse that’s why timer mode was set to 8 bit.
Both the wheels were monitored by adding timer1 to their opposite wheels.
Data Acquisition The data acquisition is important for MyTEE-Mouse because it is possible to gain feedback
from sensor readings which can further help provide data for analysis and understanding of
the mouse operations. The data acquisition can be achieved from the mouse by using
RS232 cable when mouse is not moving or FRAM can be used to take data when mouse is
moving in the maze.
RS232
“A USART (Universal Synchronous/Asynchronous Receiver/Transmitter) is a microchip that
facilitates communication through a computer's serial port using the RS-232C protocol”
(what, 2017). The USART function baud rate was set to 115200 so that the mouse can be
connected to the serial port (RS232) for data to be acquired using the “Tera Term”. Two
parameters “config” (configuration byte) and “spbrg” (value written to the serial port baud
rate generator) were passed to “OpenUSART” function. Then the MyTee-Mouse is
connected to the serial port by using options from the Tera Term which is selecting “Set up”
> “Serial port” along with baud rate of 115200. The code and algorithm given below explains
how the “Data Acquisition” setup is done using the USART and RS232.
s09466807 Page 11
Wait for PB1 or PB2
If PB2 upload the FRAM
Else run main program
Section 2: Software Design
Proportional and PD Controller (Track)
This part talks about the movements of the micro mouse with respect to the speed and
distance of the wheels so that the position control (servo control) can be investigated.
As it usually happens, the measured position comes out to be different than the desired
position. The “steady-state error” is calculated by subtracting the measured position from the
desired position. This error is reduced (or corrected) by taking the feedback from the output
and putting it back into the input. This process of feeding back output into the input is known
as “closed loop gain”. The “steady-state error” is then multiplied with the “gain factor (KP)” to
turn the “position error” into PWM. The output here is actually this PWM which is fed back
into the motor driver as input.
Steady-state error = desired position – measured position
For the configuration of KP values of wheels, the left wheel was chosen and KP value was
first set to 5 with no forward move, after which the mouse was programmed. Then the left
wheel was moved (rotated) with hand. Rotation was observed with two effects: firstly, the
wheels turned easily and secondly they became harder to rotate because of the controller
which tried to return the wheels back to the zero position. As soon as the wheel was
released after rotating it a little error occurred between the two positions (desired and
measured). After which the PD controller was used to correct the error by increasing the
duty which eventually made wheel return back very much closer to its original position.
Wait for PB1 or PB2
PB2
RUN
UPLOAD
Initial Position Rotated by 112 pulses. Duty rises to
560 to attempt to correct the error.
Released… error = 20. PWM drive
of 100 is insufficient to overcome
stiction.
RESET
s09466807 Page 12
After this the measured position is increased (made higher than the desired position) which
makes the duty go down and that compensates the overshoot in measured position. After
some time, they start stabilising, resulting which they compensate the effect of each other.
This leads in conducting a couple of experiments so that KP value can be attained as shown
below:
While the mouse on the bench (stand), using results given above the higher KP i.e. 20
causes the wheels to start oscillating whereas very low KP i.e. 4 gives an error in the “settle
down position”. The appropriate value for KP was chosen to be 5.
In order to convert a simple “Proportional Controller” to a “Proportional-Derivative Controller”,
the KD derivative gain factor is used where a constant amount of time is added to the
-1000
-500
0
500
1000
1
28
55
82
10
9
13
6
16
3
19
0
21
7
24
4
27
1
29
8
32
5
35
2
37
9
40
6
43
3
46
0
48
7
51
4
54
1
Calculating Steady-state Error
dpl mpl el duty
s09466807 Page 13
desired position at regular intervals (delta t). The two main advantages that “Proportional-
Derivative Controller” offers are that it damps the oscillatory response and improves the
rising time. The different KD and KP values were chosen to conduct a couple of experiments
to decide the best values. Mouse was placed both on the stand and on the Track (Bench):
It can be seen in the graph above that the duty starts to go down so that it can compensate
the overshoot of the measured position and also after some time they begin to stabilize so
that they can compensate the effect of each other. Higher overshoot occurs due to increased
value of KD and wheels start to oscillate because of increased values of KD. Hence in order
to reduce the steady-state error and overshoot, the values of KD and KP were increased
respectively. The best results achieved, as shown above, were made possible by changing
the values of KD, KP and BIAS. This stiction problem is resolved using an offset (bias value)
which is added to the motor drive.
The different derivative gains KD and proportional KP and bias value were tuned so that
oscillation and overshoot can be reduced to minimum. Different KD, KP and a fixed value of
Bias (Bias = 250) was used to draw graphs as shown below:
Overshoot
On the Stand
s09466807 Page 14
The derivative gains KP = 50, KD = 60 and BIAS = 250 were found to be the best for
Forward and Rotational Profile; therefore these values were used for demonstration in
laboratory.
Overshoot
Overshoot Oscillation
s09466807 Page 15
Speed Profiler
The speed profiler is a technique or method to see how the speed varied over a period of
time. Stepper motors are driven with open-loop profilers where “Stepper Profiler" sets the
time period for the next step. For this project DC servos are being used where “Servo
Profiler” tells the “Position Controller” what position the motor is required to be at by the next
timer interval (δt). The DC servo system uses a scheduler that runs at a specified time
period (δt). Speed is measured as the number of encoder pulses needed in that period
(pulses/ δt) and the acceleration is measured as the rate of change of speed (pulses/ δt/ δt).
Therefore it controls the speed and acceleration.
NextSpeed = CurrentSpeed + Acceleration
NextPosition = CurrentPosition + NextSpeed
For the measurement of acceleration, the profiler performs this calculation at each δt and
passes the “NextPosition” value to the controller. NextPosition is the “Desired Position”
which is calculated using the SUVAT equations that use distance, initial speed, final speed,
acceleration and time. The SUVAT equation being used here is v2 = u2 + 2*a*s which leads
to the calculation of the deceleration required to get to the end-point by allowing it to
determine when to start deceleration.
Figure 4 Speed Profiler Diagram, (Dr Wilcox, 2017)
A 4-cell profiled straight
The micro mouse has a differential-drive configuration that works by the use of two motors
which drive both the left and right wheels. Both the motors are operated by a power driver
and controller. Motor speed plays a vital role in determining the direction of the mouse. If the
speed of both the motors is not the same then mouse moves in an arc or takes a turn;
increasing speed of one motor can make the mouse move in an arc and increasing speed of
one motor while decreasing speed of the opposite motor at the same time makes the mouse
take a turn.
When decelerating
Final velocity (v) < Initial velocity (u). This gives negative acceleration i.e. deceleration.
Required deceleration >= specified deceleration
s09466807 Page 16
The graphs below show the speed of both wheels as they execute forward profile (on the
stand) with “acceleration” of 60 pulses/ δt/ δt and “maxspeed” of 30 pulses/ δt. The left
position and right position are slightly different.
The graphs given below show the speed of both wheels as they execute forward profile (on
the track) with “acceleration” of 60 pulses/ δt/ δt and “maxspeed” of 30 pulses/ δt.
Both the results (on “stand” and on the “track”) were taken after tuning the mouse with the
same parameters of KPL, KPD, BIAS forward distance and rotation factor. Both the graphs
are quite similar except a little difference in oscillation which is negligible.
A profiled 90 degree rotation
For 90 degree rotation, the “RotationalProfile” was developed which is shown by code in the
appendix. As already stated above, for rotation to take place one wheel’s maximum speed is
increased while the other wheel’s maximum speed is decreased. As the distance between
the two wheels is 76mm. This leads to a mathematical calculation of profiled 90 degree
rotation which was performed based on the calculation done in the section given above
“Mouse Position and Speed Measurement”.
Rotation
Deceleration
Acceleration
s09466807 Page 17
The circumference can be calculated by 2d (d=diameter) and applying this will give a
circumference of 76*pi = 238.8mm. For quarter of the circle as it’s a 90 degree rotation, it will
be 238.8/4 = 59.7. As for 0.18mm per pulse of the encoder, it will be divided by 0.18 so
59.7/0.18 gives 331.65.
Two graphs are taken, one while the mouse is on the stand and the other when it’s on the
maze track. The graph below illustrates the left and right positions and left and right speeds
and the average left and right speed. It is obvious from the graphs that the left (lpos) and
right (rpos) positions are in opposite direction (negative and positive) to each other that
makes the wheel perform a profiled 90 degree rotation.
Summary/Conclusion
The project as it was based on MyTEEmouse robotic platform was very interesting. The real-
time control software has been developed to give PD positional control of a mouse and it has
fulfilled all the major requirements of the assessment brief. The mouse ran very smoothly in
the maze and demonstrated accurate 90 degree rotations. Author has acquired Great
knowledge and exceptional understanding was gained regarding all parts of the mouse.
Skills and knowledge gained can be used to work on similar (embedded system design)
projects in future. Although it’s only a beginning and there is still a lot to learn in this vast
world of embedded robotics.
References
[1 – 3] Dr Wilcox, T. (2017). Embedded System Design. [online] icity.bcu.ac.uk. Available at:
http://moodle.bcu.ac.uk/pluginfile.php/996054/mod_resource/content/2/MyTEEmouseUserGuideV
1_4.pdf [Accessed 1 Jan. 2017].
[4] Texas, I. (2017). L293x Quadruple Half-H Drivers. [online] Texas Instruments. Available at:
http://www.ti.com/lit/ds/symlink/l293.pdf [Accessed 1 Jan. 2017].
[5] tech, t. (2017). What is EEPROM (electrically erasable programmable read-only memory)? -
Definition from WhatIs.com. [online] WhatIs.com. Available at:
s09466807 Page 18
http://whatis.techtarget.com/definition/EEPROM-electrically-erasable-programmable-read-only-
memory [Accessed 1 Jan. 2017].
[6] what, i. (2017). What is FRAM (ferroelectric RAM)? - Definition from WhatIs.com. [online]
SearchStorage. Available at: http://searchstorage.techtarget.com/definition/FRAM [Accessed 1 Jan.
2017].
[7] Dr Wilcox, T. (2017). Embedded System Design. [online] icity.bcu.ac.uk. Available at:
http://moodle.bcu.ac.uk/pluginfile.php/996054/mod_resource/content/2/MyTEEmouseUserGuideV
1_4.pdf [Accessed 1 Jan. 2017].
[8] what, i. (2017). What is processor (CPU)? - Definition from WhatIs.com. [online] WhatIs.com.
Available at: http://whatis.techtarget.com/definition/processor [Accessed 1 Jan. 2017].
[9] micro, c. (2017). PIC18F4523 - 8-bit PIC Microcontrollers. [online] Microchip.com. Available at:
http://www.microchip.com/wwwproducts/en/PIC18F4523 [Accessed 1 Jan. 2017].
[10] Dr Wilcox, T. (2017). Embedded System Design. [online] icity.bcu.ac.uk. Available at:
http://moodle.bcu.ac.uk/pluginfile.php/996054/mod_resource/content/2/MyTEEmouseUserGuideV
1_4.pdf [Accessed 1 Jan. 2017]
[11] what, i. (2017). What is USART (Universal Synchronous/Asynchronous Receiver/Transmitter)? -
Definition from WhatIs.com. [online] WhatIs.com. Available at:
http://whatis.techtarget.com/definition/USART-Universal-Synchronous-Asynchronous-Receiver-
Transmitter [Accessed 6 Jan. 2017].
Appendix
/* AJW 28/11/16
/* Zeeshan Ansari 20/12/2016
left and right pwm drive
left and right position measurement
10ms scheduler
PD Servo control on left and right motor
BIAS: correction for stiction
Data acquisition to FRAM if FRAM macro is TRUE
Exit from cyclic exec and close FRAM if PB2 pressed during run
Profilers added
*/
s09466807 Page 19
#pragma config
OSC=INTIO67,PWRT=ON,WDT=OFF,BOREN=OFF,MCLRE=ON,PBADEN=OFF
#pragma config CCP2MX=PORTC,STVREN=ON,LVP=OFF,XINST=OFF,DEBUG=ON
// System header files
#include <p18f4520.h>
#include <delays.h>
#include <timers.h>
#include <usart.h>
#include <stdio.h>
#include <pwm.h>
/************************** User header files ***************************/
#include "myteemouse.h"
#include "globals.h"
#include "initcore.h"
#include "controller.h"
#include "ledpb.h"
#include "FRAMfileIO.h"
#include "profiler.h"
/************************** Macro definitions ***************************/
#define FRAM TRUE
/************************** Function Prototypes **************************/
void DelaySeconds (unsigned char del);
/*************************************************************************/
/* Cyclic executive - MAIN program
/*************************************************************************/
void main (void)
{
unsigned char end_of_move,i;
s09466807 Page 20
InitCore(); // Setup Myteemouse
InitLEDPB(); // initialise I/O
RedLED(ON); // indicate ready
if (FRAM)
OpenFRAMwrite(); // redirect stdout if FRAM true
while (!PB1() && !PB2()); // wait for either PB1 or PB2
if (PB2()) { // PB2: calibrate then run
RedLED(ON);
if (FRAM) PrintFRAM(); // Upload stored data
GreenLED(OFF);
RedLED(OFF);
} else {
GreenLED(ON); // indicate running
DelaySeconds(1);
ZeroCounters();
RedLED(OFF);
printf("lpos rpos\r\n"); // Print header for data
// START RECTANGLE LOOP
for (i=0; i<4; i++) {
// MOVE 1: MOVE FORWARD 4 CELLS
SetFwdProfile(4030,FWD,60,30,0); // move forward 4 cells
do { // Do ... while not done
if (tick) { // 10ms timeout?
tick = FALSE; // clear scheduler flag
end_of_move = ExecMove();
printf("%5ld %5ld\r\n",left.current_position,
s09466807 Page 21
right.current_position);
}
} while (!end_of_move);
// END OF MOVE 1
end_of_move = FALSE;
// MOVE 2: ROTATE 90 DEGREES
SetRotProfile(328,CW,60,30); // rotate right (90 degrees)
do {
if (tick) { // 10ms timeout?
tick = FALSE; // clear scheduler flag
end_of_move = ExecMove();
printf("%5ld %5ld\r\n",left.current_position,
right.current_position);
}
} while (!end_of_move);
// END OF MOVE 2
end_of_move = FALSE;
}
// END RECTANGLE LOOP
CloseFRAMwrite(); // write EOF
SetDCPWM1(0); // pwm off
SetDCPWM2(0); //
RedLED(ON); // indicate HALT
}
while(TRUE); // loop
}
s09466807 Page 22
/*************************************************************************/
void DelaySeconds (unsigned char del)
{
unsigned int i;
for (i=0; i<10*del; i++) {
Delay10KTCYx(80); // 100ms delay
}
}
/*************************************************************************/