Professional Documents
Culture Documents
Team 3 B
Supervisor : Prof Lu Li
Andrew Lim Hock Khim
A0066195X
Lin Shaodun
A0066078X
A0066104R
Lin Mingkun
A0066174A
Maung Guang Xi
U0800858R
U0800867B
Li Qiming
A0066164E
Faculty of Engineering
Department of Mechanical Engineering
Table of Contents
Page
Acknowledgements
Abstract
1.0
Introduction
1.1
Aim
1.2
Background
1.3
Methodology
1.4
Scope
2.0
10
3.0
2.1
Group Timelines
11
2.2
13
14
of an Autonomous Robots
3.1
3.2
4.0
Chassis Design
15
3.1.1
Design 1
15
3.1.2
Design 2
16
3.1.3
Design 3
17
Final Selection
18
of an Autonomous Robots
4.1
4.2
5.0
Arm Design
18
4.1.1
Selection of Designs
19
4.1.2
21
Gripper Design
22
4.2.1
Selection of Designs
22
4.2.2
25
26
5.1
26
5.1.1
28
5.2
PCB Assembly
30
5.3
32
Faculty of Engineering
Department of Mechanical Engineering
5.4
6.0
7.0
9.0
10.0
34
35
6.1
35
6.2
37
Troubleshooting
39
7.1
Mechanical Parts
39
7.1.1
40
7.1.2
40
7.1.3
40
7.1.4
40
7.1.5
41
7.2
8.0
Gripper Assembly
Electrical Parts
42
7.2.1
42
7.2.2
42
7.2.3
42
7.2.4
42
42
8.1
Gripper Motor
42
8.2
Arm Motor
43
8.3
Theta Motor
44
8.4
DC Drive Motor
45
46
9.1
46
Control Circuit
Testing of Program
57
10.1
57
10.2
57
10.3
11.0
Recommendation
58
12.0
Conclusion
59
13.0
References
59
Faculty of Engineering
Department of Mechanical Engineering
14.0
Appendices
60
Appendix A
Research
Appendix B
Costing Details
Appendix C
Other Concepts
Appendix D
Detailed Drawings
Appendix E
Program Codes
Faculty of Engineering
Department of Mechanical Engineering
Acknowledgements
A/Prof Y F Zhang
Course Lecturer & Project Coordinator
Prof Lu Li
Project Mentor
We would like to express our most sincere thanks to the above mentioned and also
those who had helped us in one way or another in the successful completion of our
project.
Faculty of Engineering
Department of Mechanical Engineering
Abstract
This project aims to design and build an autonomous robot to move along a
designated track on an arena with utmost accuracy with the aid of sensors being
correctly positioned. This autonomous robot is programmed to pick an empty coke
from an elevated platform after having passed through a gantry and place this empty
can on another platform with different height at the most efficient timing of 3 to 5 sec.
This machine would demonstrate its capability to maneuver through a 300 mm
wide and 450 mm high gantry to pick up an empty Coke can from a designated
elevated platform on the left and transport it to a platform on the right in the shortest
possible time. This is made possible through a detailed program to activate the driving
motor of the chassis, motor and belt bringing the long arm downwards, sensors and
other devices to react at the precise timing.
Using the design methodology, our group was able to successfully design and
build a functioning robot, sophisticated in appearance and with multiple operations.
Though our group did not manage to get the robot to complete the task, we have
managed to impress the judge through our own unique design and the different
functional movements.
We have shown that we were able to display and showcase the many different
methods and technique of pick and place operations. This is important as it provides
us with a framework to hone our mechanical and electrical design in whatever
situation the industries have to offer, instead of remaining stagnated to current
ideology. Our team members have learned to work collectively and holistically
throughout the project cycle, this has not only created a deep bonding among us but
also enhanced our teambuilding skill.
Faculty of Engineering
Department of Mechanical Engineering
1.0
Introduction
1.1
Aim
The aim of this project is to design and build an autonomous robot to navigate
through a rectangular gate, pick up an empty drink can from a raised platform in the
left and place the can onto a raised platform in the right in the shortest possible time.
The objective of this project not only build upon our fundamental in the way to
connect every moving parts together but also how to inject the auto-capability
software to the hardware through integration use programming. This is achieved by
many session of brainstorming to get the best mechanical approach and also the most
effective and efficient way to meet our objective. This project also enabled us to
explore deeper into new methods instead of using the existing ones in the design of
our autonomous robot. This is especially crucial in our developmental stage as we
further embark on our remaining journey in this Bachelor of Technology programme.
1.2
Background
The machine must be self-contained with no form of remote control for all the
functional tasks. The machine must fit inside the 200 x 150 mm start area with no
height limit. The machine must not weight more than 5 kg.
1.3
Methodology
wheels to track.
1.4
b.
c.
d.
Scope
Faculty of Engineering
Department of Mechanical Engineering
b.
c.
The lines shown in the arena are retro-reflective tape (50 mm in width)
for guiding.
.
Faculty of Engineering
Department of Mechanical Engineering
Coke Can
A typical empty Coke Can is made of aluminum, with shape that is cylindrical and
with an overall dimension, dia 50mm x height 80mm.
Elevated Platforms
Both raised platform will be of the same dimension of 100 by 100 mm. The height of
the platform can be different, between 20 mm to 50 mm, in steps of 10 mm.
Gates
The gate is 300 mm wide. The exact location of the gate will not be made known until
the competition, but shall be constrained between 450 mm and 750 mm from the
starting base line, in steps of 50 mm. The lower 50 mm of the gate shall be made retro
reflective for sensing purposes.
Machine
For the machine dimension, there is no specific dimension that we must adhere to.
But our machine must strictly obey the following conditions stated;
The machine must be self-contained.
a.
b.
The machine must fit inside the 200 x 150 (mm) starting box.
c.
d.
The main focus is to ensure speed, stability and accuracy of the machine during
operation.
Faculty of Engineering
Department of Mechanical Engineering
2.0
Autonomous Robot
The discussion on the arms and grippers and the mechanism to operate both of them
had given us a good brain storming session, coupled with the advice given by our
teacher had certainly provided us with some good decision making. To continue with
the momentum with our project, we have decided to meet every Saturday from 1400
to 1700 hrs and discuss the best ways to connect all the moving parts more
effectively.
2.1
Project Timeline
Before a project timeline was charted for this project, our team shared their areas of
expertise and how each can effectively contribute to the successful completion of this
project.
10
Faculty of Engineering
Department of Mechanical Engineering
Name
Role
Task Allocated
Team Lead
LIN SHAODUN
Mechatronics
Designer
LEONG KANG
SIANG
Mechanical
Designer
1.
2.
3.
4.
5.
1.
LIN MINGKUN
Manufacturing
1. Fabricate Parts,
2. Prepare alternative design concepts for
Gripper and Chassis ,
3. Support Robot Assembly.
MAUNG GUANG
XI
Designer
MOHAMAD
YAZID BIN
ISMAIL
Designer
1. Fabricate Parts,
2. Prepare alternative design concepts for
Gripper and Chassis ,
3. Support Robot Assembly.
LI QIMING
Software
Engineer
11
Faculty of Engineering
Department of Mechanical Engineering
12
Faculty of Engineering
Department of Mechanical Engineering
2.2
Other report
stuffs
Project
management
Resource
planning
Group
Report
Design drawings,
screen captures
Programs
LIN SHAODUN
LI QIMING
Dummy
Arena
Hand sketch
ideas
Other report
stuffs
Decision
matrix
Design
philosophy
Other report
stuffs
Flow Chart
MAUNG GUANG
XI
Group
Report
Design
calculations,
Motor torque
requirement,
Comparison
time of
alternatives
LIN MINGKUN
MOHAMAD
YAZID BIN
ISMAIL
LEONG KANG
SIANG
LIN MINGKUN
Design
calculations,
Motor torque
requirement,
time
Chassis design
alternatives
Sensor alternatives
Comparison of
alternatives
Arm design
alternatives
Robot Sequence
alternatives
Gripper design
alternatives
Control design
alternatives
Resource
planning
Human resources
Initial study
External resources
Project planning
LEONG KANG
SIANG
MAUNG
GUANG XI
MOHAMAD
YAZID BIN
ISMAIL
13
Faculty of Engineering
Department of Mechanical Engineering
A project task allocation was agreed upon as an interim measure to get everyone
moving forward.
S/No
1
Task Needed
Calculation base on different loads for the
following:
Task Allocation
LIN MINGKUN
LEONG KANG SIANG
Gripper design
Sensor
Robot Sequence
Control design
3
ANDREW LIM
Human resources
External resources
Initial study
Project planning
4
LI QIMING
LIN SHAODUN
14
Faculty of Engineering
Department of Mechanical Engineering
3.0
3.1
Chassis Design
Chassis Design is the most vital and significant aspect in this project. In order to
make the robot move, execute and complete the task within the fastest time in a
stable condition, selection of materials, size, width, length, height are
quintessential from an engineering standpoint. This accounts for 60% of the
entire design and every factor has to carefully consider.
3.1.1
Design 1
Pros:
o
Highest maneuverability.
Higher reliability.
Fastest speed.
Cons:
o
Highest cost.
Complicate software.
15
Faculty of Engineering
Department of Mechanical Engineering
3.1.2
Design 2
Pros:
o
Fastest speed.
Cons:
o
Highest cost.
Complicate software.
Less stable.
3.1.3
Design 3
16
Faculty of Engineering
Department of Mechanical Engineering
3.2
Pros:
o
Creative design.
More stability.
Cons:
o
Average speed.
Final Selection
This year, our group has decided to come out a different, unique and creative design
concept compared with others team. We knew that a four wheels drive concept as
well a six wheel drive concept will work since these have been proven from the past
competition. In order to benefit more from this learning process, we have decided not
to adopt similar design concept.
17
Faculty of Engineering
Department of Mechanical Engineering
4.0
Design of the Arms and Grippers for the working functions of an
Autonomous Robots
Initially we were thinking of using the flip over type instead of rotating after hearing
from our lecturer's comments so as to minimise complexity and keep the mechanism
as simple as possible as last year, more than 50% failed to accomplish the task. As
there was only one group who had used the rotating type due to longer time in the
motion. Beside timing and completion of mandatory task, the overall outlook of the
robot will also be taken into consideration; hence detailed discussions were carried
out on the gripper, arm and turntable and also the flip-over design as well as the
moving mechanism design. More designs were proposed for brainstorming, such as
the below diagram. Fine-tuning was also proposed on the design for the arm, theta
and gripper to achieve better results. Some sketches and simple diagrams were drawn
to show how to improve the design
4.1 Arm Design
Arm design is like the human body in this project, it acts as a coordinator to link the
chassis and gripper together. Hence it is very vital and every factors need to be
examine carefully to ensure that the arm design is fully functional and compatible
with the other parts.
4.1.1 Selection of Designs
Though there were many designs being researched and discussed upon (Appendix A),
but several sessions of brainstorming, our team had narrowed down to the following
three arm designs:
Design A:
Gear
Servo Motor
Base
Gripper
Arm
18
Faculty of Engineering
Department of Mechanical Engineering
This arm design uses a one piece concept which comprises arm, gripper and base to
be linked together as shown above. However the materials used in this arm design
must be light-weight in order to support the various parts weight.
Design B:
Servo
Motor
Arm
Base
Gripper
Timing Belt
This arm design comprises a unique appearance and using the mechanism of timing
belt to operate the arm motion.
Design C:
Servo
Motor
Arm
Gripper
Base
19
Faculty of Engineering
Department of Mechanical Engineering
This arm design uses simple mechanism with motor attached to the gear to move up
or down of the arm.
Aesthetics
Producibility
Creativity
Use of Standard Parts
Reliability
Cost
After serious investigation and consideration, our team had come out with the above
ARROW showing the six criteria in the Decision Matrix. The purpose of the
decision matrix is to allow us to analyze and grade them according to the various
designs.
Below is the table of Decision Matrix:
Criteria
Weight (%)
Design A
Design B
Design C
Cost
10
Reliability
25
18
20
18
15
12
10
Creativity
15
12
10
Reproducibility
25
17
20
18
Aesthetics
10
Total
100%
65
80
71
20
Faculty of Engineering
Department of Mechanical Engineering
Overall
Design A
65%
3rd
Design B
80%
1st
Design C
71%
2nd
Design 2 had emerged champion in the table of Decision Matrix, with the
uniqueness and innovative mechanism making use of timing belt to tilt the
gripper parallel to the floor at which angle the arm make, our team had
unanimously agreed to use this design for the arm.
4.2 Gripper Design:
Gripper design is extremely important in this project as it perform the most crucial
execution action to pick and place the can from one platform to another. In order to
make the robot to perform the desired action, all factors need to be taken seriously
into consideration. Similar to the arm design, thorough research were done and many
21
Faculty of Engineering
Department of Mechanical Engineering
mechanisms of this nature were discovered and discussed (Appendix A), however
only 3 sets of designs are discussed here.
Design A:
Worm Gear
Base Plate
Gripper
Servo
Motor
Design A uses a 4 bar linkage mechanism and by attaching a motor to the worm gear,
the gripper is able to open/close easily.
Design B:
Actuator
Gripper
Base
Plate
22
Faculty of Engineering
Department of Mechanical Engineering
Design B is a little rigid as it has more parts as compared to the other designs and it
takes slightly more time to operate the gripper motion.
Design C:
Servo
Motor
Gripper
Gear
Design C had an innovative outlook and with the help of the servo motor attaching to
the gear, the gearing mechanism can easily operate the motion of the gripper.
Below is the table of Decision Matrix:
Criteria
Weight(%)
Design A
Design B
Design C
Cost
10
Reliability
25
18
15
23
15
10
12
Creativity
15
10
10
12
Producibility
25
18
18
22
Aesthetics
10
Total
100%
71
65
86
Overall
Design A
71%
2nd
Design B
65%
3rd
Design C
86%
1st
23
Faculty of Engineering
Department of Mechanical Engineering
Design C had emerged champion in the table of Decision Matrix, with its
innovative appearance and yet simple gearing mechanism, our team had also
unanimously agreed to use this design.
Any finalized design were discussed and decided by majority of the team members,
hence all the designs were finalized with group consent so as to prevent any conflict
in the event of the robot failing to complete mandatory task. Many consensuses were
put up and agreed upon.
24
Faculty of Engineering
Department of Mechanical Engineering
5.0
5.1
The robot chassis design is based on Robo-PICA 2.0 robot kit purchased from
Innovative Experiment Co. Ltd. The Robo-PICA 2.0 robot kit consist the following
Mechanical components:
Originally the DC motor and track wheel set is mounted on a plastic universal plate,
but this kind of structure is lack of rigidity also cannot ensure each component are
accurately aligned and installed, which definitely will affect the performance of the
robot.
In order to overcome this problem, the base plate of the robot chassis must be
redesigned, featured with locating pins to precisely locate all the components. (Figure
5.1)
25
Faculty of Engineering
Department of Mechanical Engineering
Original Chassis
Improved Chassis
Figure 5.1
For instance, locating pins are added onto base plate to locate the two DC motors,
which is to ensure the drive sprockets mounts on DC motor shaft are parallel to
moving direction, so that the robot will move in a straight line hence reduce the needs
of line tracking correction. (Figure 5.2)
Locating Pins
Locating Pins
Original Mounting
Improved Mounting
Figure 5.2
The same locating pin concept is applied to mounting of long angled shaft base as
well: (Figure 5.3).
With these locating pins, it is not only easing the alignment of components when
mounts onto base plate, but also shortens the total assembly time. When there is a
need to remove the components from the assembly during debugging, we do not need
to concern about whether it can be put back to the original position, as the parts will
be in the same position with simply align it against the locating pins.
26
Faculty of Engineering
Department of Mechanical Engineering
Locating Pins
Locating Pins
Original Mounting
Figure 5.3
Improved Mounting
Figure 5.4
The overall chassis design looks like this in exploded view: (Figure 5.5)
27
Faculty of Engineering
Department of Mechanical Engineering
Figure 5.5
Functionality of each component is briefly illustrated as follow: (Figure 5.6)
PCB mounting plate
DC Motor
Track
Theta Shaft
Counter Weight
Guiding wheels
Theta motor
Base Plate
Main Sprocket
Side IR Sensor
Bottom IR Sensor
Figure 5.6
28
Faculty of Engineering
Department of Mechanical Engineering
Figure 5.7
The main microcontroller is the PIC16F887. Figure 5.8 shows the operating diagram
of RBX-877 board.
Figure 5.8
29
Faculty of Engineering
Department of Mechanical Engineering
RBX-877 Board
Figure 5.9
30
Faculty of Engineering
Department of Mechanical Engineering
Elongation at Break 12 %
The robot arm assembly comes with a unique feature is : it use a pair of timing gear
and a timing belt to maintain the gripper always at horizontal position , this is useful
as according to TM3101 design specification the Coke can might be placed in
different height, with this feature, the height different can be compensated easily.
(Figure 5.10)
Timing belt
Figure 5.10
31
Faculty of Engineering
Department of Mechanical Engineering
The drive servo motor use a pair of bevel gear to transmit power, the reason of not
using direct drive is because of space constraint when theta axis rotates. Since the
gear ratio is 1:1, the torque is only affected by mechanical efficiency of bevel gear.
(Figure 5.11)
Bevel Gears
Figure 5.11
Another end of timing gear is mounted on an adjustable block, so that the timing belt
tension can be adjusted properly. (Figure 5.12)
Figure 5.12
32
Faculty of Engineering
Department of Mechanical Engineering
Figure 5.13
Distance sensor
Touch sensor
Figure 5.14
33
Faculty of Engineering
Department of Mechanical Engineering
6.0
displacement profile is
Cycloidal motion.
Here is the screen capture when of Motion Simulation interface. (Figure 6.1)
34
Faculty of Engineering
Department of Mechanical Engineering
Figure 6.1
After Motion study finished, we can plot the displacement curve, angular velocity and
peak torque required, etc. (Figure 6.2)
Angular Velocity
Angular Displacement
Torque Required
Figure 6.2
By varying the total displacement time, we can define what is the suitable
acceleration which will not exceed the torque output from Theta servo motor. (Figure
6.3)
35
Faculty of Engineering
Department of Mechanical Engineering
12
10
Torque Required
Torque Supplied
8
6
4
2
0.50
0.75
1.00
1.25
1.50
1.75
2.00
Figure 6.3
Above graph shows the torque required increases exponentially when the
displacement time become shorter, since the torque supplied is 2.5kg from Tahmazo
TS-1033MG motor, we can see the feasible displacement time is 1.50 second , after
consider mechanical efficiency, actual motion acceleration condition, and resistance
force created by cable routing.
6.1.2 Required Torque for Arm motor
Apply the same principle to Arm motor, the torque curve looks like follow: (Figure
6.4)
Angular Velocity
Angular Displacement
Torque Required
Figure 6.4
By varying the total displacement time, we can define what is the suitable
acceleration which will not exceed the torque output from Arm servo motor. (Figure
6.5)
36
Faculty of Engineering
Department of Mechanical Engineering
30
25
20
Torque Required
Torque Supplied
15
10
5
0.25
0.50
0.75
1.00
1.25
1.50
Figure 6.5
Above graph shows the total displacement time should be larger than 0.50 second,
otherwise the arm motor might stall when lift up from zero degree position. Of course
this data just a rough guide line as it does not consider the factors like bevel gear
mechanical efficiency and cable routing tension. And the actual movement of arm
cannot be smooth which creates higher acceleration, hence the torque require will be
even higher.
Torque applied on robot arm need to be counter balanced by the frictional force
produced by the robot weight, if the theta moving too fast, the whole robot might
shift position, which will affect the accuracy of robot arm movement. Below
graph show some consideration of this issue:
Torque applied
40
35
Reaction Force
Track to Floor u=0.6
Track to Floor u=0.8
Track to Floor u=1.0
30
25
Reaction force
available
20
15
10
5
0
0.50
0.75
1.00
1.25
1.50
1.75
2.00
From the graph we can see if we set the theta motion time to 1.5 seconds and the
servo motor can move smoothly, the torque to drive robot arm should not cause whole
robot shift position at worst case scenario.
37
Faculty of Engineering
Department of Mechanical Engineering
7.1 Troubleshooting
After completed assembly and wiring, many issues encountered during
debugging, below are some of the mechanical and electrical problems
investigated and resolved.
7.1.1 Robot Arm timing gear issue.
Issue: Gripper cannot maintain at horizontal position when arm rotates.
Root cause: Mechanism design failure.
Solution: Change design as follow. (Figure 7.1)
Initially the timing gear is fixed onto the arm plate, this incorrect design causes
gripper attached at the end of arm not stays in horizontal position when arm
rotates. After found out this root cause, change the mounting of timing gear from
arm plate to bearing holder plate so that the timing gear will not move together
with the arm and the timing belt mechanism always maintain the gripper in
horizontal position.
Timing Gear
Arm Plate
Figure 7.1
7.1.2 Low voltage supply to DC motors.
Issue: Robot moving very slowly as the rpm of DC motor is slow.
Root cause: The output voltage from H-bridge is low.
Solution: After studied the circuit diagram and the specification of some
components, we know the Voltage regulator KIA7042 can withstand input
voltage up to 12V, and the H-bridge driver L293D can have input up to 30V,
(Figure 7.2) so we decided to increase supply voltage from 6.0V to 7.5V, by
change the battery holder from AAx4 to AAx6, and fabricated one dummy
battery to obtain 1.5x5=7.5V supply. With this modification, the robot can move
50% faster.
38
Faculty of Engineering
Department of Mechanical Engineering
5.5 V
Voltage regulator
4.3 V
Figure 7.2
H-bridge driver
The maximum voltage for DC motor is 9V, but we did not use 9V as supply as the
current-limiting resistors become very hot with higher voltage supplied, this has
been confirmed by thermal image. (Figure 7.3) In order not to damage the PIC
board, we decide only go for 7.5V as maximum allowable input voltage.
Figure 7.3
7.1.3 Low current supply to RC Servo motors.
Issue: The built in servo motor port only supply 200mA each.
Root cause: Circuit design limitation.
Solution: The servo motor used in the robot takes 300~600mA, so we have to
use external power supply to provide higher current to servo motor otherwise it
will not produce enough torque. We choses 2800mAH 12V Li-ion Polymer
rechargeable battery, and made a 12V to 6V converter board to step down the
supply for 3 servo motors.
39
Faculty of Engineering
Department of Mechanical Engineering
Figure 7.4
7.1.4 Wrong wiring of servo motor
Issue: Unable to command the servo motors.
Root cause: Forgot to connect the GND pin of servo ports when using external
power supply.
Solution: Re-connect the GND pin as follow: (Figure 7.5)
Figure 7.5
7.1.5 Arm motor torque is insufficient.
Issue: The torque of arm motor (Tower pro SG5010) is insufficient.
Root cause: Supplier put wrong specification on the website. This motor is
purchased from www.dealextreme.com , on that website, it states the torque is
15kg.cm at 6V, but in fact, on motor manufacturers website, the SG501o torque
is actually 6.5kg.cm, so definitely it will not able to lift up the robot arm.
Solution: Change Tower pro SG5010 to Hitec HSR-5980SG motor. (24kg.cm @
6V)
TM3101 Mechanical Engineering Design
40
Faculty of Engineering
Department of Mechanical Engineering
Before
After
Figure 7.6
Additional
distance sensor
Figure 7.7
7.1.8 High load acting on theta motor directly.
Issue: High force directly acting on the theta motor shaft when robot arm
rotates.
Root cause: PCB plate mounts on DC motor directly and it is in over-hang
condition which is not rigid enough when robot arm moves.
Solution: Use the old arm motor mounting bracket to improve the PCB plate
mounting rigidity. (Figure 7.8)
41
Faculty of Engineering
Department of Mechanical Engineering
Support bracket
Before
After
Figure 7.8
Weakest link
Figure 7.9
42
Faculty of Engineering
Department of Mechanical Engineering
Figure 7.10
43
Faculty of Engineering
Department of Mechanical Engineering
8.0
8.1
Gripper Motor
Gripper Motor Calculation
FG = Required gripping force [N] per gripper jaw
MG = Gripping torque = FG x r
r = Distance between gripper zero point and gripping point
m = Work piece mass [kg]
Assuming friction at bearing is negligible.
g = Acceleration due to gravity (9.81m/s2) is required if
acting against the acceleration, a
= Angle of V-gripper finger
= Coefficient of friction between gripper finger and work
piece
Assume a = 3 m/s2
Mass of can, m = 35 g
Coefficient of Friction, = 0.5 (Aluminum to Aluminum
contact)
2 = 120o , = 60o
Distance, r = 75mm
44
Faculty of Engineering
Department of Mechanical Engineering
8.2
Arm Motor
Assume a = 3 m/s2
Mass of arm assembly, m = 350 g
)
(
45
Faculty of Engineering
Department of Mechanical Engineering
8.3
Theta Motor
46
Faculty of Engineering
Department of Mechanical Engineering
8.4
DC Drive Motor
Drive Wheel
g
r
1.25
2.5
Time (s)
v=
= 0.52 m/s
a=
Mass of robot, m = 2 kg
= 0.416 m/s
(
47
Faculty of Engineering
Department of Mechanical Engineering
9.0
Popular programming language for PIC could be assembly and C code, but before
coming to real programming stage, we need to understand the schematic diagram of
the PIC, registers, functions and I/Os of each pin of the micro controller. After many
tries on the PIC with LED lights, we moved on to explore the controlling of servo
motor, we faced the challenge when we made use of interrupts and PWM of PIC
programming. In this area we have limited resources and professionals and also
constrains in terms of mechanical design, budget and functionalities of PIC. Despite
all these limitations we have eventually come up with a set of programming codes
that meet the robot operational requirement (See Appendix E Program codes)
9.1
Control Circuit
The control circuit of the various sequences to activate the devices installed onto our
autonomous robot was collectively discussed and a few flow charts were mapped up
depicting the stages of operation and movement.
48
Faculty of Engineering
Department of Mechanical Engineering
Power On
Arm Hoisting
Sequence
Port Initialization
Sequence
Place Sequence
Is the Start
Button Pressed?
Standby Mode
Arm Lowering
Sequence for
Place
Line Tracking
Sequence
Gripper Open
Sequence
Pick Up
Sequence
STOP, End of
Operation
Arm Lowering
Sequence for
Pickup
49
Faculty of Engineering
Department of Mechanical Engineering
START
Beep for finish
Port Initialization
Delay
500mS
Power On
Initialize Port
RA0~5 as Input
Initialize Port
RE0 as Input
Initialize Port
RB0~5 as Output
Initialize Port
RC0~5 as Output
Delay
500mS
Delay
500mS
Delay
100mS
Display Ready
Message on LCD
Initialize RD2~7
for LCD
Initialize Buzzer
Finish
50
Faculty of Engineering
Department of Mechanical Engineering
Chassis move
straight
forward
Y
N
Y
N
N
N
Is side IR Sensor
output high?
Y
Robot moved past gate,
send pulse to Port RB5 to
lower arm to 45o
Robot continues
to move forward
Y
Y
Is rear IR sensor
output high?
STOP, proceed to
Pick Up Sequence
N
TM3101 Mechanical Engineering Design
51
Faculty of Engineering
Department of Mechanical Engineering
Pick Up Sequence
START
STOP, proceed to
Arm Lowering
Sequence for Pickup
52
Faculty of Engineering
Department of Mechanical Engineering
N
IR distance sensor
feedback height
above platform is 5cm
Y
Send pulse to RC0
to beep (x1) buzzer
STOP, proceed to
Gripper Close
Sequence
Y
Send pulse to RC0
to beep
(continuous) buzzer
STOP, rectification
required
53
Faculty of Engineering
Department of Mechanical Engineering
N
Touch sensor
contact close
STOP, proceed to
Arm Hoisting
Sequence
STOP, rectification
required
54
Faculty of Engineering
Department of Mechanical Engineering
STOP, proceed to
Place Sequence
START
STOP, proceed to
Arm Lowering
Sequence for Place
55
Faculty of Engineering
Department of Mechanical Engineering
N
IR distance sensor
feedback height
above platform is 5cm
Y
Send pulse to RC0
to beep (x1) buzzer
STOP, proceed to
Gripper Open
Sequence
Y
Send pulse to RC0
to beep
(continuous) buzzer
STOP, rectification
required
56
Faculty of Engineering
Department of Mechanical Engineering
Touch sensor
contact open
Y
Send pulse to RC0
to beep (x3) buzzer
STOP, End of
Operation
57
Faculty of Engineering
Department of Mechanical Engineering
10.0
Testing of Program
Programmer has tested out all the major electronic components to be used for the
project using sample robotic machine provided by the lecturer and proceed to do
optimization of the programming part. The main objective for this testing on the
components is to ascertain whether controlling of multiple actions is possible and at
the same time maintaining the stability of the entire system.
10.1
58
Faculty of Engineering
Department of Mechanical Engineering
10.2
The real test of program after assembly of our robot take days and nights on refining
programming code, numerous times of loading and running our robot on our self
made dummy arena and actual arena. Thinking optimization from software point of
view based on the fixed design is very challenging, because electrical, mechanical and
even software issues would come up at the same time while observation did not help
in most of the time if issues happened internally.
Problem Arises
Remedial Actions
(1) Weak mechanism lock inside the timing (1) Tighten screw on the shaft inside the
belt.
timing belt by 1st dismantling the arm from
the chassis.
(2) Insufficient torque of the servo motor to (2) Replace servo motor with higher torque
lift up the arm due to incorrect specification which increased our overall cost.
from vendor.
(3) Servo motor failure half way during the (3) Replace with new servo motors.
testing progress.
(4) DC motor running in low speed and the
tracks are not moving straight which caused
our robot unable to reach the right position
for pick & place.
59
Faculty of Engineering
Department of Mechanical Engineering
10.3
In the programming approach to this project, we created Line & Object tracking
function for correctness of the moving path of the robot, Smart auto-sensing &
comparing system for the sensors to automatically compare the black and white color
on the stage with changing environment meaning to avoid incorrect reading of the
sensor due to different lighting setup, Auto compensation sensing and gripping
system for incorrect positioning of our robot before pick & place the can.
60
Faculty of Engineering
Department of Mechanical Engineering
x_L
x_R
R
x_S
Object Detected by
Side sensor x_S and
Line tracking by
sensor x_L & x_R
61
Faculty of Engineering
Department of Mechanical Engineering
x_THETA
Stage
/****Smart auto-sensing & comparing system****/
x_THETA=ARM_MOTION_DOWN();//ARM LOWER DOWN
Lcd_Cmd(_LCD_CLEAR);
while(1)
Dynamic variable
{
x_THETA to avoid
Auto
ADCON0=0b11001001; //side sensor
Sensing
the influence of
ADCON0.GO=1;
Indicator
variant Height of
while(ADCON0.GO);
the stage and
x_S=(ADRESH*4)+(ADRESL/64);
lighting conditions
if(x_THETA==0)
ARM_MOTION_DOWN_HOLD();
if((x_S<100) & (x_THETA==0))
......................................................
{
x_THETA=1;
ARM_MOTION_DOWN_HOLD();
ADCON0=0b11000001; //distance sensor1
ADCON0.GO=1;
while(ADCON0.GO);
62
Faculty of Engineering
Department of Mechanical Engineering
Optimizations by IR sensors:
63
Faculty of Engineering
Department of Mechanical Engineering
Sensor
To
Detect
CAN
Auto
Compensation
Angle
64
Faculty of Engineering
Department of Mechanical Engineering
11.0
Recommendation
There were many problem areas that we did not anticipate initially, though on
hindsight there were areas that could be improved, especially on the selection of
motors as regards to quality compared with lower price.
There are also pros and cons of sensors that can be good indicators by forming a close
loop system to play a role in feedback control, but sensors are too sensitive due to
reflection, lightings, humidity, hence in our case need programming code to handle
medium range reflecting due to crowded people around the arena
12.0
Conclusion
Although it was hard work spending many hours each week on the project, we have
gained significantly through the intensive research and detailed brainstorming session
that we have embarked on from start to find the most unique and elegant way to
design and build our machine. We have learned many new things that were not
covered in our lectures, like organizational and interpersonal skill.
Finally we have managed to acquire the many new skills and in depth knowledge
from numerous testing and troubleshooting to effectively integrate both hardware and
software.
65
Faculty of Engineering
Department of Mechanical Engineering
13.0
References
External Resources
http://www.sparkfun.com
http://www.sgbotic.com/
http://www.amainobj.com/
http://shop.rotor.com.sg/
http://www.sparkfun.com/datasheets/Robotics/Robo-PICA2007.pdf
http://www.streetdirectory.com/asia_travel/travel/travel_id_2810/travel_site_1706
A-Main Objectives Pte Ltd
66
Faculty of Engineering
Department of Mechanical Engineering
14.0
Appendices
Appendix A
Research
(A) CHASSIS
Design A
Design B
Design C
Design D
Design E
Design F
Design B
Design C
GRIP HANDLE
GRIP STATIC
FRAME
BOLT & NUT
DELRIN MOUNTING
GRIP LINKAGE
MOUNTING BRACKET
(L-SHAPE)
SERVO MOTOR
67
Faculty of Engineering
Department of Mechanical Engineering
Design D
Design E
Design F
Design A
Design B
Design C
Design D
Design E
Design F
68
Faculty of Engineering
Department of Mechanical Engineering
(D) Sensors
Design A
Design B
Design C
Design D
Design E
Design F
69
Faculty of Engineering
Department of Mechanical Engineering
Description
Paid to
Paid by
Amount
Receipt
Lin Shaodun
SGD 1059.8
Page 3-8
Sparkfun Electronics
Lin Shaodun
SGD 247
Page 9
Continental Electronics
Enterprise
Lin Shaodun
SGD 70.1
Page 10
IR sensor x 2
James Yong
Lin Shaodun
SGD 20
Page 11
Lin Shaodun
SGD 21.72
Page 11
DealExtreme.com
Lin Shaodun
USD 20.82
Page 12
Continental Electronics
Enterprise
Lin Shaodun
SGD 4.90
Page 13
5V Voltage Regulator
Li QiMing
SGD 10
Page 14
Li QiMing
SGD 39.5
Page 15
10
Li QiMing
SGD 21.72
Receipt
Missing
11
Lin Shaodun
SGD 32.96
Page 16
12
Lin Shaodun
SGD 53.5
Page 17
70
Faculty of Engineering
Department of Mechanical Engineering
Appendix C Concepts
Concepts Comparison
Sensor to
detect the
stage height
Gap
Sensor to detect
the stage height
A 3-DOF Arm
71
Faculty of Engineering
Department of Mechanical Engineering
Arm Shafts
72
Faculty of Engineering
Department of Mechanical Engineering
Arm
73
Faculty of Engineering
Department of Mechanical Engineering
Base Plates
74
Faculty of Engineering
Department of Mechanical Engineering
Battery Holder
75
Faculty of Engineering
Department of Mechanical Engineering
Bearing Plates - A
76
Faculty of Engineering
Department of Mechanical Engineering
Bearing Plates - B
Bearing Plate - C
77
Faculty of Engineering
Department of Mechanical Engineering
78
Faculty of Engineering
Department of Mechanical Engineering
Bearing Shaft C
Collar
79
Faculty of Engineering
Department of Mechanical Engineering
Jaw Flat
80
Faculty of Engineering
Department of Mechanical Engineering
81
Faculty of Engineering
Department of Mechanical Engineering
82
Faculty of Engineering
Department of Mechanical Engineering
83
Faculty of Engineering
Department of Mechanical Engineering
84
Faculty of Engineering
Department of Mechanical Engineering
85
Faculty of Engineering
Department of Mechanical Engineering
86
Faculty of Engineering
Department of Mechanical Engineering
87
Faculty of Engineering
Department of Mechanical Engineering
88
Faculty of Engineering
Department of Mechanical Engineering
89
Faculty of Engineering
Department of Mechanical Engineering
SG 90 Bracket
90
Faculty of Engineering
Department of Mechanical Engineering
91
Faculty of Engineering
Department of Mechanical Engineering
92
Faculty of Engineering
Department of Mechanical Engineering
93
Faculty of Engineering
Department of Mechanical Engineering
94
Faculty of Engineering
Department of Mechanical Engineering
Weight
95
Faculty of Engineering
Department of Mechanical Engineering
Robot moves straight (arm and gripper are ready to pick after the pole) about 1~2 secs.
Grip the can once the robot stop moving about 0.5-1 sec.
Command the arm lift up and rotate at the same time to the right platform about 1-2secs.
Release the can about 0.5 sec or lower down the arm with distance sensor to detect the
height and place the can nicely about 1-2 secs.
at
at
at
at
at
at
TRISD2_bit;
TRISD3_bit;
TRISD7_bit;
TRISD6_bit;
TRISD5_bit;
TRISD4_bit;
char i;
int Adc;
#include<mfunction.h>
unsigned int j;
int i_t;
char txt_THETA[4];
char txt_L_R[4];
char txt_DIST[4];
int x;
int x_L;
int x_R;
int x_B;
int x_S;
int x_D1,x_D2;
int x_SERVO;
int x_L_R;
int x_THETA;
int x_REPOS;
int x_DELAYC;
TM3101 Mechanical Engineering Design
96
Faculty of Engineering
Department of Mechanical Engineering
int x_NHEIGHT;
int x_DIFF;
int k;
char data_[6];
void THETA_LEFT()
{
for(j=42;j<65;j++)
//qiming 61
{
for(x_SERVO=2;x_SERVO>0;x_SERVO--)
{
PORTB.F4=1;
PORTB.F5=1;
PORTC.F5=1;
Delay_us(700);
PORTB.F5=0;
SERVO_P_NEW(j-28);
PORTC.F5=0;
SERVO_P_NEW(80-j);
//80-j
PORTB.F4=0;
Delay_ms(19); //20
Delay_us(950);
}
ADCON0=0b11010001; //left sensor
ADCON0.GO=1;
while(ADCON0.GO);
x_L= (ADRESH*4)+(ADRESL/64);
ADCON0=0b11010101; //right sensor
ADCON0.GO=1;
while(ADCON0.GO);
x_R= (ADRESH*4)+(ADRESL/64);
ADCON0=0b11001101; //behind sensor
ADCON0.GO=1;
while(ADCON0.GO);
x_B= (ADRESH*4)+(ADRESL/64);
if((x_L>700) & (x_R>700) & (x_L_R!=5))
{
x_L_R=4;
}
if(x_L_R==4)//((x_B<80) & (x_L_R==4))
{
Delay_ms(80);
Motor_Brake_New();
Motor_Stop();
x_L_R=5;
}
}
}
void main()
{
ANSELH.F3=0;
ANSEL.F1=1;
TRISB.F4=0;
TRISB.F5=0;
TRISC.F5=0;
TRISA.F5=1;
TRISE.F0=1;
TRISA.F3=1;
TRISA.F2=1;
TM3101 Mechanical Engineering Design
97
Faculty of Engineering
Department of Mechanical Engineering
TRISA.F0=1;
TRISA.F1=1;
/**********testing**********/
Lcd_Init();
ANSEL = 0xFF; // PORTA ==> Analog
TRISA = 0xFF; // PORTA ==> input
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD cursor off
//Lcd_Out(1,1,"Initializing..."); // Show Text
/***************************/
while(PORTA.F4); //press button
Forward(255);
x_L_R=0;
x_THETA=0;
Motor_Init();
Change_Duty(255);
Motor_Brake();
Delay_ms(300);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Home Pos");
Init_Home();
//press button again
Forward(255); //MOVE
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Move,Arm low");
/****Smart auto-sensing & comparing system****/
x_THETA=ARM_MOTION_DOWN();//ARM LOWER DOWN
Lcd_Cmd(_LCD_CLEAR);
while(1)
{
ADCON0=0b11001001; //side sensor
ADCON0.GO=1;
while(ADCON0.GO);
x_S=(ADRESH*4)+(ADRESL/64);
if(x_THETA==0)
ARM_MOTION_DOWN_HOLD();
if((x_S<100) & (x_THETA==0))
{
x_THETA=1;
ARM_MOTION_DOWN_HOLD();
ADCON0=0b11000001; //distance sensor1
ADCON0.GO=1;
while(ADCON0.GO);
x_NHEIGHT= (ADRESH*4)+(ADRESL/64);
}
if((x_THETA==1) & (x_S<500))
ARM_MOTION_DOWN_HOLD();
if((x_THETA==1) & (x_S>500))
{
x_THETA=2;//reflection tape
ARM_MOTION_DOWN_HOLD();
}
if((x_THETA==2) & (x_S>200))
ARM_MOTION_DOWN_HOLD();
if((x_THETA==2) & (x_S<200))
{
x_THETA=3;
ARM_MOTION_DOWN_HOLD();
}
if((x_THETA==3) & (x_S<500))
ARM_MOTION_DOWN_HOLD();
TM3101 Mechanical Engineering Design
98
Faculty of Engineering
Department of Mechanical Engineering
if((x_THETA==3) & (x_S>500))
{
THETA_LEFT();
x_THETA=4;
}
if(x_THETA==4)
THETA_LEFT_HOLD_OPEN();
//Motor_Brake();
ADCON0=0b11010001; //left sensor
ADCON0.GO=1;
while(ADCON0.GO);
x_L= (ADRESH*4)+(ADRESL/64);
ADCON0=0b11010101; //right sensor
ADCON0.GO=1;
while(ADCON0.GO);
x_R= (ADRESH*4)+(ADRESL/64);
/****Line Tracking****/
PWM1_Set_Duty(255);
// Motor A
PWM2_Set_Duty(255);
// Motor B
if((x_L<200) & (x_R>700))
{
PWM1_Set_Duty(255);
// Motor A
PWM2_Set_Duty(0);
// Motor B
}
if((x_L>700) & (x_R>200))
{
PWM1_Set_Duty(0);
// Motor A
PWM2_Set_Duty(255);
// Motor B
}
/*********************/
if(x_L_R==5)
{
WordToStr(x_THETA,txt_THETA);
Lcd_Out(1,1,txt_THETA);
WordToStr(x_L_R,txt_L_R);
Lcd_Out(1,5,txt_L_R);
break;
}
if((x_L_R==0) & (x_L>700) & (x_R>700))
x_L_R=1;
if((x_L_R==1) & ((x_L<100) || (x_R<100)))
x_L_R=2;
if((x_L_R==2) & (x_L>700) & (x_R>700))
x_L_R=3;
if(x_L_R==3)// & (x_B<80))
{
Delay_ms(80);
Motor_Brake_New();
Motor_Stop();
x_L_R=5;
WordToStr(x_THETA,txt_THETA);
Lcd_Out(1,1,txt_THETA);
WordToStr(x_L_R,txt_L_R);
Lcd_Out(1,5,txt_L_R);
break;
}
WordToStr(x_THETA,txt_THETA);
Lcd_Out(1,1,txt_THETA);
WordToStr(x_L_R,txt_L_R);
Lcd_Out(1,5,txt_L_R);
}
TM3101 Mechanical Engineering Design
99
Faculty of Engineering
Department of Mechanical Engineering
while(1)
{
THETA_LEFT_HOLD_OPEN();
ADCON0=0b11001101; //behind sensor
ADCON0.GO=1;
while(ADCON0.GO);
x_B= (ADRESH*4)+(ADRESL/64);
if((x_L_R==4) || (x_L_R==5))//(x_L_R==5)
break;
if(x_B<80)
{
Motor_Brake_New();
Motor_Stop();
break;
}
}
x_DIFF=0;
x=0;
while(x_DIFF<200)
{
ADCON0=0b11000001; //distance sensor1
ADCON0.GO=1;
while(ADCON0.GO);
x_D1= (ADRESH*4)+(ADRESL/64);
ADCON0=0b11011001; //distance sensor2
ADCON0.GO=1;
while(ADCON0.GO);
x_D2= (ADRESH*4)+(ADRESL/64);
Lcd_Cmd(_LCD_CLEAR);
WordToStr(x_D1,txt_DIST);
Lcd_Out(1,9,txt_DIST);
x_DIFF=x_D1-x_NHEIGHT;
ADCON0=0b11001101; //behind sensor
ADCON0.GO=1;
while(ADCON0.GO);
x_B= (ADRESH*4)+(ADRESL/64);
if(x_D2>400)
break;
if((x_DIFF>200) && (x_B<100)) //& (x_D2>100))
break;
if(x_B<100) //& (x_D2>100)) //240
break;
if(x==0)
{
Forward(255);
x=1;
}
}
if(x==1)
{
Motor_Brake_New();
Motor_Stop();
}
//240
/*
THETA_LEFT_HOLD_OPEN();
ADCON0=0b11000001; //distance sensor1
ADCON0.GO=1;
while(ADCON0.GO);
x_D1= (ADRESH*4)+(ADRESL/64);
ADCON0=0b11011001; //distance sensor2
ADCON0.GO=1;
TM3101 Mechanical Engineering Design
100
Faculty of Engineering
Department of Mechanical Engineering
while(ADCON0.GO);
x_D2= (ADRESH*4)+(ADRESL/64);
Lcd_Cmd(_LCD_CLEAR);
WordToStr(x_D1,txt_DIST);
Lcd_Out(1,9,txt_DIST);
x_DIFF=x_D1-x_NHEIGHT;
if(x_DIFF>150) //& (x_D2>100)) //240
break;
}
*/
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Gripper Close");
k = GPR_CLOSE();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Arm Right");
//THETA_RIGHT();
THETA_RIGHT_noRC5(k);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Open, Done");
//Lcd_Out(2,1, "Task Completed!");
GPR_OPEN(x_NHEIGHT,k);
while(1);
/*
while(1)
{
PORTC.F5=1;
//THETA
PORTB.F4=1;
//ARM
PORTB.F5=1;
//GRIPPER
Delay_us(1000);
PORTC.F5=0;
Delay_us(200);
PORTB.F5=0;
Delay_us(700);
PORTB.F4=0;
Delay_us(2000);
}
while(PORTA.F4)
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(1500);
PORTB.F5=0;
PORTB.F4=0;
Delay_us(550);
PORTC.F5=0;
Delay_ms(20);
}
ARM_MOTION_DOWN();
while(1)
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(1200);
PORTB.F5=0;
Delay_us(300);
PORTC.F5=0;
Delay_us(300);
PORTB.F4=0;
TM3101 Mechanical Engineering Design
101
Faculty of Engineering
Department of Mechanical Engineering
Delay_ms(20);
}
*/
/*
while(PORTA.F4) //arm
{
//PORTC.F5=1;
PORTB.F4=1;
//PORTB.F5=1;
//Delay_us(600);
//600
//PORTB.F5=0;
Delay_us(1500);
//600
PORTB.F4=0;
//Delay_us(2050);
//400
//PORTC.F5=0;
Delay_ms(20);
//400us
}
while(PORTA.F4)//theta
{
PORTC.F5=1;
//PORTB.F4=1;
//PORTB.F5=1;
//Delay_us(600);
//600
//PORTB.F5=0;
//Delay_us(600);
//600
//PORTB.F4=0;
Delay_us(2050);
//400
PORTC.F5=0;
Delay_ms(20);
//400us
}
//Delay_ms(1000);
//ARM_MOTION_UP(0,3);
//while(PORTA.F4)
while(1)
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(800);
PORTC.F5=0;
Delay_us(400);
PORTB.F4=0;
Delay_us(300);
PORTB.F5=0;
Delay_us(500);
//PICK_POS2_GPR();
// Delay_ms(1000);
// HOME_GPR();
//
Delay_ms(1000);
}
while(PORTA.F4);
Motor_Init();
Change_Duty(255);
Motor_Brake();
Forward(255);
//Delay_ms(3000);
//Motor_Brake();
//while(PORTA.F4);
//Delay_ms(1000);
102
Faculty of Engineering
Department of Mechanical Engineering
//ARM_MOTION_DOWN(4,0);
Motor_Brake();
while(PORTA.F4)
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(600);
PORTB.F5=0;
Delay_us(600);
PORTB.F4=0;
Delay_us(400);
PORTC.F5=0;
Delay_us(400);
}
//HOME_GPR();
*/
/*while(PORTA.F4)
{
PORTB.F5=1;
Delay_us(1300);
PORTB.F5=0;
Delay_us(700);
}
*/
/*
for(i=200;i>0;i--)
{
PORTB.F4=1;
PORTB.F5=1;
Delay_us(1200);
PORTB.F4=0;
Delay_us(300);
PORTB.F5=0;
Delay_us(500);
}
ARM_MOTION_UP(0,3);
while(PORTA.F4)
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(800);
PORTC.F5=0;
Delay_us(400);
PORTB.F4=0;
Delay_us(300);
PORTB.F5=0;
Delay_us(500);
//PICK_POS2_GPR();
// Delay_ms(1000);
// HOME_GPR();
//
Delay_ms(1000);
}
Delay_ms(1000);
PORTB.F5=0;
while(PORTA.F4)
{
PORTC.F5=1;
TM3101 Mechanical Engineering Design
103
Faculty of Engineering
Department of Mechanical Engineering
PORTB.F4=1;
//PORTB.F5=1;
Delay_us(800);
PORTC.F5=0;
Delay_us(400);
//PORTB.F5=0;
Delay_us(50);
PORTB.F4=0;
Delay_us(750);
}
while(PORTA.F4);
while(1)
{
// PICK_POS2();
Delay_ms(1000);
// PLACE_POS2();
Delay_ms(1000);
}
while(PORTA.F4)
{
Init_Home();
//HOME_THETA();
//PICK_POS1_ARM();
//HOME_GPR();
//Delay_ms(1000);
//PICK_POS1_GPR();
//PICK_POS1_THETA();
//Delay_ms(1000);
//for(j=1000;j>0;j--)
//PICK();
}
Delay_ms(1000);
//Forward(255);
while(PORTA.F4)
{
//Test();
}
//Motor_Stop();
Delay_ms(1000);
while(PORTA.F4);
while(1)
{
// PICK_POS1_ARM();
ADCON0=0b11001001;
ADCON0.GO=1;
while(ADCON0.GO);
x_S =(ADRESH*4)+(ADRESL/64);
if(x_S<600)
break;
}
//RA2
//PICK_POS2();
while((x_L<600 || x_R<600) && x_D<100)
TM3101 Mechanical Engineering Design
104
Faculty of Engineering
Department of Mechanical Engineering
{
// PICK_POS1_ARM();
ADCON0=0b11010001;
ADCON0.GO=1;
while(ADCON0.GO);
x_L =(ADRESH*4)+(ADRESL/64);
ADCON0=0b11010101;
ADCON0.GO=1;
while(ADCON0.GO);
x_R =(ADRESH*4)+(ADRESL/64);
ADCON0=0b11001101;
ADCON0.GO=1;
while(ADCON0.GO);
x_B =(ADRESH*4)+(ADRESL/64);
ADCON0=0b11000001;
ADCON0.GO=1;
while(ADCON0.GO);
x_D =(ADRESH*4)+(ADRESL/64);
}
//RA5
//RE0
//RA3
//RA0
Motor_Stop();
while(PORTA.F1);
while(PORTA.F4);
Delay_ms(1000);
Lcd_Init();
ANSEL = 0xFF; // PORTA ==> Analog
TRISA = 0xFF; // PORTA ==> input
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD cursor off
Lcd_Out(1,1,"SENSOR1 = "); // Show Text
ADCON0=0b11000001; // Select Analog1 RC_Mode and ADON
while(1)
{
ADCON0.GO=1;
while(ADCON0.GO);
x= (ADRESH*4)+(ADRESL/64);
WordToStr(x,data_);
Lcd_Out(1,10,data_);
Delay_ms(100);
}
*/
}
void SERVO_P_NEW(int k)
{
switch(k){
case 0:
break;
case 1:
Delay_us(25);
break;
case 2:
Delay_us(50);
break;
case 3:
Delay_us(75);
break;
case 4:
Delay_us(100);
break;
TM3101 Mechanical Engineering Design
105
Faculty of Engineering
Department of Mechanical Engineering
case 5:
Delay_us(125);
break;
case 6:
Delay_us(150);
break;
case 7:
Delay_us(175);
break;
case 8:
Delay_us(200);
break;
case 9:
Delay_us(225);
break;
case 10:
Delay_us(250);
break;
case 11:
Delay_us(275);
break;
case 12:
Delay_us(300);
break;
case 13:
Delay_us(325);
break;
case 14:
Delay_us(350);
break;
case 15:
Delay_us(375);
break;
case 16:
Delay_us(400);
break;
case 17:
Delay_us(425);
break;
case 18:
Delay_us(450);
break;
case 19:
Delay_us(475);
break;
case 20:
Delay_us(500);
break;
case 21:
Delay_us(525);
break;
case 22:
Delay_us(550);
break;
case 23:
Delay_us(575);
break;
case 24:
Delay_us(600);
break;
case 25:
TM3101 Mechanical Engineering Design
106
Faculty of Engineering
Department of Mechanical Engineering
case
case
case
case
case
case
case
case
case
case
case
case
case
case
case
case
case
case
case
case
Delay_us(625);
break;
26:
Delay_us(650);
break;
27:
Delay_us(675);
break;
28:
Delay_us(700);
break;
29:
Delay_us(725);
break;
30:
Delay_us(750);
break;
31:
Delay_us(775);
break;
32:
Delay_us(800);
break;
33:
Delay_us(825);
break;
34:
Delay_us(850);
break;
35:
Delay_us(875);
break;
36:
Delay_us(900);
break;
37:
Delay_us(925);
break;
38:
Delay_us(950);
break;
39:
Delay_us(975);
break;
40:
Delay_us(1000);
break;
41:
Delay_us(1025);
break;
42:
Delay_us(1050);
break;
43:
Delay_us(1075);
break;
44:
Delay_us(1100);
break;
45:
Delay_us(1125);
107
Faculty of Engineering
Department of Mechanical Engineering
break;
case 46:
Delay_us(1150);
break;
case 47:
Delay_us(1175);
break;
case 48:
Delay_us(1200);
break;
case 49:
Delay_us(1225);
break;
case 50:
Delay_us(1250);
break;
case 51:
Delay_us(1275);
break;
case 52:
Delay_us(1300);
break;
case 53:
Delay_us(1325);
break;
case 54:
Delay_us(1350);
break;
case 55:
Delay_us(1375);
break;
case 56:
Delay_us(1400);
break;
case 57:
Delay_us(1425);
break;
case 58:
Delay_us(1450);
break;
case 59:
Delay_us(1475);
break;
case 60:
Delay_us(1500);
break;
case 61:
Delay_us(1525);
break;
case 62:
Delay_us(1550);
break;
case 63:
Delay_us(1575);
break;
case 64:
Delay_us(1600);
break;
case 65:
Delay_us(1625);
break;
TM3101 Mechanical Engineering Design
108
Faculty of Engineering
Department of Mechanical Engineering
case 66:
Delay_us(1650);
break;
case 67:
Delay_us(1675);
break;
case 68:
Delay_us(1700);
break;
case 69:
Delay_us(1725);
break;
case 70:
Delay_us(1750);
break;
case 71:
Delay_us(1775);
break;
case 72:
Delay_us(1800);
break;
case 73:
Delay_us(1825);
break;
case 74:
Delay_us(1850);
break;
case 75:
Delay_us(1875);
break;
case 76:
Delay_us(1900);
break;
case 77:
Delay_us(1925);
break;
case 78:
Delay_us(1950);
break;
case 79:
Delay_us(1975);
break;
case 80:
Delay_us(2000);
break;
case 81:
Delay_us(2025);
break;
case 82:
Delay_us(2050);
break;
case 83:
Delay_us(2075);
break;
case 84:
Delay_us(2100);
break;
case 85:
Delay_us(2125);
break;
case 86:
TM3101 Mechanical Engineering Design
109
Faculty of Engineering
Department of Mechanical Engineering
Delay_us(2150);
break;
case 87:
Delay_us(2175);
break;
case 88:
Delay_us(2200);
break;
case 89:
Delay_us(2225);
break;
case 90:
Delay_us(2250);
break;
case 91:
Delay_us(2275);
break;
case 92:
Delay_us(2300);
break;
default :
break;
}
}
/*
void SERVO_P(int k)
{
switch(k){
case 0:
break;
case 1:
Delay_us(100);
break;
case 2:
Delay_us(200);
break;
case 3:
Delay_us(300);
break;
case 4:
Delay_us(400);
break;
case 5:
Delay_us(500);
break;
case 6:
Delay_us(600);
break;
case 7:
Delay_us(685);
break;
case 8:
Delay_us(785);
break;
case 9:
Delay_us(885);
break;
case 10:
Delay_us(985);
break;
case 11:
TM3101 Mechanical Engineering Design
110
Faculty of Engineering
Department of Mechanical Engineering
Delay_us(1080);
break;
case 12:
Delay_us(1180);
break;
case 13:
Delay_us(1280);
break;
case 14:
Delay_us(1370);
break;
case 15:
Delay_us(1470);
break;
case 16:
Delay_us(1570);
break;
case 17:
Delay_us(1670);
break;
case 18:
Delay_us(1770);
break;
case 19:
Delay_us(1870);
break;
case 20:
Delay_us(1960);
default :
break;
}
}
*/
/*
void Read_Adc()
{
//ADCON0=0b11000001; // Select Analog2 RC_Mode and ADON
ADCON0.GO=1; // Start Convert
while(ADCON0.GO); // Wait Until Convert Complete
Adc=(ADRESH*4)+(ADRESL/64); // 10 bit Data ==> Adc
}
void HOME_THETA()
{
for(i=5000;i>0;i--)
{
PORTC.F5=1;
Delay_us(1720);
PORTC.F5=0;
Delay_us(280);
}
}
//-90
void HOME_ARM()
{
for(i=200;i>0;i--)
{
PORTB.F4=1;
Delay_us(800);
//90
111
Faculty of Engineering
Department of Mechanical Engineering
PORTB.F4=0;
Delay_us(1200);
}
}
void HOME_GPR()
{
for(i=200;i>0;i--)
{
PORTB.F5=1;
Delay_us(600);
PORTB.F5=0;
Delay_us(1400);
}
}
//90
void PICK_POS1_THETA()
{
for(i=5000;i>0;i--)
{
PORTC.F5=1;
Delay_us(1000);
PORTC.F5=0;
Delay_us(1000);
}
}
//0
void PICK_POS1_ARM()
{
//for(i=200;i>0;i--)
//{
PORTB.F4=1;
Delay_us(800);
PORTB.F4=0;
Delay_us(1200);
//}
}
//30
void PICK_POS1_GPR()
{
for(i=200;i>0;i--)
{
PORTB.F5=1;
Delay_us(800);
PORTB.F5=0;
Delay_us(1200);
}
}
//45
void PICK_POS2_THETA()
{
for(i=200;i>0;i--)
{
PORTC.F5=1;
Delay_us(1500);
PORTC.F5=0;
Delay_us(500);
}
}
//0
void PICK_POS2_ARM()
//30
112
Faculty of Engineering
Department of Mechanical Engineering
{
for(i=200;i>0;i--)
{
PORTB.F4=1;
Delay_us(1650);
PORTB.F4=0;
Delay_us(250);
}
}
void PICK_POS2_GPR()
{
for(i=200;i>0;i--)
{
PORTB.F5=1;
Delay_us(1300);
PORTB.F5=0;
Delay_us(700);
}
}
*/
void Init_Home()
{
while(PORTA.F4)
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(1500);
PORTB.F5=0;
PORTB.F4=0;
Delay_us(550);
PORTC.F5=0;
Delay_ms(20);
}
}
/*
void HOME_HOLD()
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(1500);
PORTB.F5=0;
PORTB.F4=0;
Delay_us(550);
PORTC.F5=0;
Delay_ms(20);
}
//45
void Test()
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(1200);
PORTB.F4=0;
Delay_us(200);
PORTC.F5=0;
PORTB.F5=0;
//Delay_us(100);
TM3101 Mechanical Engineering Design
113
Faculty of Engineering
Department of Mechanical Engineering
//PORTB.F4=0;
Delay_us(600);
}
void PICK_POS1()
{
PICK_POS1_THETA();
PICK_POS1_ARM();
PICK_POS1_GPR();
}
//POS 0 DEG
//POS 30 DEG
//POS 45 DEG
void PICK_POS2()
{
for(i=200;i>0;i--)
{
PORTC.F5=1;
Delay_us(1650);
PORTC.F5=0;
Delay_us(350);
}
}
void PLACE_POS2()
{
for(i=200;i>0;i--)
{
PORTC.F5=1;
Delay_us(800);
PORTC.F5=0;
Delay_us(1200);
}
}
void PICK()
{
PORTB.F5=1;
Delay_us(1500);
PORTB.F5=0;
Delay_us(500);
}
void ARM_MOTION_DOWN_OLD(int l,int m)
{
int j;
int x_SERVO;
for(j=l;j>=m;j--)
{
for(x_SERVO=100;x_SERVO>0;x_SERVO--)
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(600);
PORTB.F5=0;
SERVO_P(6-j);
PORTB.F4=0;
SERVO_P(4+j);
PORTC.F5=0;
SERVO_P(4);
}
}
TM3101 Mechanical Engineering Design
114
Faculty of Engineering
Department of Mechanical Engineering
}
*/
int ARM_MOTION_DOWN()
{
int x_S,x_THETA=0;
int j;
int x_SERVO;
for(j=10;j>0;j--)
{
PORTC.F5=1;
//THETA
PORTB.F4=1;
//ARM
PORTB.F5=1;
//GRIPPER
Delay_us(700);
PORTB.F5=0;
Delay_us(350);
PORTC.F5=0;
Delay_us(450);
PORTB.F4=0;
Delay_ms(20);
}
for(j=17;j<41;j++)
{
for(x_SERVO=2;x_SERVO>0;x_SERVO--)
{
PORTC.F5=1;
//THETA
PORTB.F4=1;
//ARM
PORTB.F5=1;
//GRIPPER
Delay_us(700);
PORTB.F5=0;
Delay_us(350);
PORTC.F5=0;
SERVO_P_NEW(j);
PORTB.F4=0;
Delay_ms(20);
ADCON0=0b11001001; //side sensor
ADCON0.GO=1;
while(ADCON0.GO);
x_S=(ADRESH*4)+(ADRESL/64);
if((x_S<100) & (x_THETA==0))
{
x_THETA=1;
}
if((x_THETA==1) & (x_S<500));
if((x_THETA==1) & (x_S>500))
{
x_THETA=2;//reflection tape
}
if((x_THETA==2) & (x_S>200));
if((x_THETA==2) & (x_S<200))
{
x_THETA=3;
}
}
}
return x_THETA;
}
void ARM_MOTION_DOWN_HOLD()
{
PORTC.F5=1;
//THETA
PORTB.F4=1;
//ARM
TM3101 Mechanical Engineering Design
115
Faculty of Engineering
Department of Mechanical Engineering
PORTB.F5=1;
Delay_us(700);
PORTB.F5=0;
Delay_us(350);
PORTC.F5=0;
Delay_us(1100);
PORTB.F4=0;
Delay_ms(19);
Delay_us(950);
//GRIPPER
int THETA_LEFT_HOLD_HCLOSE()
{
int x;
int j;
int k=1;
int x_D2;
int m;
for(x=0;x<50;x++)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
Delay_us(700);
PORTB.F5=0;
Delay_us(900);
//qiming800
PORTC.F5=0;
Delay_us(550);
//qiming650
PORTB.F4=0;
Delay_ms(20);
}
/****Auto compensation sensing and gripping system****/
ADCON0=0b11011001; //distance sensor2
while(1)
{
ADCON0.GO=1;
while(ADCON0.GO);
x_D2= (ADRESH*4)+(ADRESL/64);
if(x_D2>=400)
break;
if(x_D2<400)
{
for(m=30;m>0;m--)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
Delay_us(700);
PORTB.F5=0;
SERVO_P_NEW(36-k);
//qiming32-k
PORTC.F5=0;
SERVO_P_NEW(18+k);
//qiming22+k
PORTB.F4=0;
Delay_ms(20);
}
}
k++;
}
if(k!=1);
TM3101 Mechanical Engineering Design
116
Faculty of Engineering
Department of Mechanical Engineering
{
for(x=28;x<55;x++)
{
for(j=2;j>0;j--)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
SERVO_P_NEW(x);
PORTB.F5=0;
SERVO_P_NEW(64-k-x);
//qiming60-k-x
PORTC.F5=0;
SERVO_P_NEW(18+k);
//qiming22+k
PORTB.F4=0;
Delay_ms(20);
}
}
//qimingk=60-k;
return k;
}
if(k==1)
//qiming k==1
{
for(x=28;x<55;x++)
{
for(j=5;j>0;j--)
//qiming 2
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
SERVO_P_NEW(x);
PORTB.F5=0;
SERVO_P_NEW(64-x);
//qiming 60-x
PORTC.F5=0;
Delay_us(450);
//qiming550
PORTB.F4=0;
Delay_ms(20);
}
}
}
}
void THETA_LEFT_HOLD_CLOSE(int k)
{
int m;
for(m=100;m>0;m--)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
SERVO_P_NEW(62-k); //qiming k
PORTC.F5=0;
SERVO_P_NEW(4+k);
//qiming 68-k
PORTB.F5=0;
Delay_us(350);
PORTB.F4=0;
Delay_ms(20);
}
}
void THETA_LEFT_HOLD_OPEN()
{
TM3101 Mechanical Engineering Design
117
Faculty of Engineering
Department of Mechanical Engineering
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
Delay_us(700);
PORTB.F5=0;
Delay_us(900);
PORTC.F5=0;
Delay_us(550);
PORTB.F4=0;
Delay_ms(20);
//qiming800
//qiming650
}
/*
void THETA_RIGHT()
{
int j;
int x_SERVO;
for(j=51;j>20;j--)
{
for(x_SERVO=1000;x_SERVO>0;x_SERVO--)
{
PORTC.F5=1;
PORTB.F5=1;
PORTB.F4=1;
SERVO_P_NEW(j);
PORTC.F5=0;
SERVO_P_NEW(55-j);
PORTB.F5=0;
Delay_us(500);
PORTB.F4=0;
Delay_us(2000);
}
}
for(j=21;j>14;j--)
{
for(x_SERVO=1000;x_SERVO>0;x_SERVO--)
{
PORTC.F5=1;
PORTB.F5=1;
PORTB.F4=1;
SERVO_P_NEW(j);
PORTC.F5=0;
SERVO_P_NEW(55-j);
PORTB.F5=0;
SERVO_P_NEW(32-j);
PORTB.F4=0;
Delay_us(2000);
}
}
}
*/
void THETA_RIGHT_noRC5(int k)
{
int j;
int x_SERVO;
int x_SERVO2;
x_SERVO2=58;
for(j=20;j>11;j=j--)
{
x_SERVO2=x_SERVO2-2;
for(x_SERVO=30;x_SERVO>0;x_SERVO--)
{
TM3101 Mechanical Engineering Design
//4
118
Faculty of Engineering
Department of Mechanical Engineering
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
SERVO_P_NEW(x_SERVO2+k);
PORTC.F5=0;
//PORTB.F5=0;
SERVO_P_NEW(62-x_SERVO2-k);
//PORTC.F5=0;
//Delay_us(1500);
PORTB.F5=0;
//Delay_us(50); QIMING
//Delay_us(500);
SERVO_P_NEW(j);
PORTB.F4=0;
Delay_ms(20);
}
}
x_SERVO2=40;
//32
for(j=16;j<22;j++)
{
if((j!=16) & (j!=21))
x_SERVO2=x_SERVO2-4;
if(j==21)
x_SERVO2=24;
for(x_SERVO=50;x_SERVO>0;x_SERVO--)
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
SERVO_P_NEW(x_SERVO2-k);
PORTC.F5=0;
//PORTB.F5=0;
SERVO_P_NEW(43-x_SERVO2+k);
Delay_us(400);
PORTB.F5=0;
//Delay_us(20);
//PORTC.F5=0;
SERVO_P_NEW(j);
PORTB.F4=0;
Delay_ms(20);
}
}
}
//+9
//qiming60 -x_SERVO2
//+9
//15
//qiming+k
//qiming-k
//+9
int GPR_CLOSE()
{
int x;
int k;
ADCON0=0b11000101; // Select Analog1 RC_Mode and ADON
k=THETA_LEFT_HOLD_HCLOSE();
// while(1)
//{
//for(x=10;x>0;x--)
//{
/*
PORTB.F4=1;
PORTB.F5=1;
Delay_us(1600);
PORTB.F5=0;
Delay_us(550);
TM3101 Mechanical Engineering Design
119
Faculty of Engineering
Department of Mechanical Engineering
PORTB.F4=0;
Delay_ms(20);
*/
//}
THETA_LEFT_HOLD_CLOSE(k);
/*
ADCON0.GO=1;
while(ADCON0.GO);
x= (ADRESH*4)+(ADRESL/64);
if(x<900)
break;
}
*/
/*
for(x=10;x>0;x--)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
Delay_us(100);
PORTC.F5=0;
Delay_us(100);
PORTB.F5=0;
//Delay_us(100);
//PORTC.F5=0;
Delay_us(550);
PORTB.F4=0;
Delay_ms(20);
}
*/
return k;
}
void GPR_OPEN(int x_NHEIGHT, int k)
{
int x;
if(k!=1)
{
for(x=30;x>0;x--)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
SERVO_P_NEW(25-k);//qiming-k
PORTC.F5=0;
SERVO_P_NEW(35+k);//qiming870
+k
PORTB.F5=0;
Delay_us(650);
PORTB.F4=0;
Delay_ms(20);
}
while(PORTA.F4)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
SERVO_P_NEW(25-k);//qiming-k 630
PORTC.F5=0;
SERVO_P_NEW(35+k);//qiming+k
PORTB.F5=0;
Delay_us(800);
TM3101 Mechanical Engineering Design
120
Faculty of Engineering
Department of Mechanical Engineering
PORTB.F4=0;
Delay_ms(20);
}
}
if(k==1)
{
for(x=30;x>0;x--)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
Delay_us(630);
PORTC.F5=0;
Delay_us(870);
PORTB.F5=0;
Delay_us(650);
PORTB.F4=0;
Delay_ms(20);
}
while(PORTA.F4)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
Delay_us(630);
PORTC.F5=0;
Delay_us(720);
PORTB.F5=0;
Delay_us(800);
PORTB.F4=0;
Delay_ms(20);
}
}
}
void GPR_OPEN_OLD(int x_NHEIGHT,int k) //QIMING
{
int x=1;
int x_D1;
int x_DIFF;
//int k=1;
int m;
/*
ADCON0=0b11000001; //distance sensor1
if(k!=1)
{
while(1)
{
ADCON0.GO=1;
while(ADCON0.GO);
x_D1= (ADRESH*4)+(ADRESL/64);
x_DIFF=x_D1-x_NHEIGHT;
if(x_DIFF>=250)
break;
if(x_DIFF<250)
{
for(m=30;m>0;m--)
{
PORTB.F4=1;
PORTC.F5=1;
TM3101 Mechanical Engineering Design
121
Faculty of Engineering
Department of Mechanical Engineering
PORTB.F5=1;
SERVO_P_NEW(25-k);
PORTC.F5=0;
SERVO_P_NEW(39+K);
PORTB.F5=0;
Delay_us(475);
PORTB.F4=0;
Delay_ms(20);
}
}
x++;
}
*/
/*
if(k<=23)
{
for(x=50;x>0;x--)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
SERVO_P_NEW(25-k);
PORTC.F5=0;
SERVO_P_NEW(33+K);
PORTB.F5=0;
Delay_us(500);
PORTB.F4=0;
Delay_ms(20);
}
while(PORTA.F4)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
SERVO_P_NEW(23-k);
PORTC.F5=0;
SERVO_P_NEW(28+K);
PORTB.F5=0;
Delay_us(550);
PORTB.F4=0;
Delay_ms(20);
}
}
if(k>23)
{
while(PORTA.F4)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
Delay_us(630);
PORTC.F5=0;
Delay_us(720);
PORTB.F5=0;
Delay_us(800);
PORTB.F4=0;
Delay_ms(20);
}
}
*/
//}
TM3101 Mechanical Engineering Design
122
Faculty of Engineering
Department of Mechanical Engineering
k=1;
if(k==1)
{
for(x=30;x>0;x--)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
Delay_us(630);
PORTC.F5=0;
Delay_us(870);
PORTB.F5=0;
Delay_us(650);
PORTB.F4=0;
Delay_ms(20);
}
while(PORTA.F4)
{
PORTB.F4=1;
PORTC.F5=1;
PORTB.F5=1;
Delay_us(630);
PORTC.F5=0;
Delay_us(720);
PORTB.F5=0;
Delay_us(800);
PORTB.F4=0;
Delay_ms(20);
}
}
}
/*
void ARM_MOTION_UP(int l,int m)
{
int j;
int x_SERVO;
for(j=l;j<m+1;j++)
{
for(x_SERVO=100;x_SERVO>0;x_SERVO--)
{
PORTC.F5=1;
PORTB.F4=1;
PORTB.F5=1;
Delay_us(800);
PORTC.F5=0;
SERVO_P(4-j);
PORTB.F4=0;
SERVO_P(3+j);
PORTB.F5=0;
SERVO_P(5);
}
}
for(j=m;j>=l;j--)
{
for(x_SERVO=100;x_SERVO>0;x_SERVO--)
{
PORTC.F5=1;
PORTB.F4=1;
TM3101 Mechanical Engineering Design
123
Faculty of Engineering
Department of Mechanical Engineering
PORTB.F5=1;
Delay_us(800);
PORTC.F5=0;
SERVO_P(4-j);
PORTB.F4=0;
SERVO_P(3+j);
PORTB.F5=0;
SERVO_P(5);
}
}
}
*/
/********** Stop Motor
************/
void Motor_Brake()
{
Pwm1_Start();
Pwm2_Start();
PORTB.F1 =1;
PORTB.F2 =1;
PORTD.F0 =1;
PORTD.F1 =1;
PORTC.F1 =1;
PORTC.F2 =1;
}
/************************************/
/********** Stop Motor
************/
void Motor_Brake_New()
{
//Pwm1_Start();
//Pwm2_Start();
PORTB.F1 =1;
PORTB.F2 =1;
PORTD.F0 =1;
PORTD.F1 =1;
PORTC.F1 =1;
PORTC.F2 =1;
}
/************************************/
/*
void SENSOR_TEST
{
Lcd_Init();
ANSEL = 0xFF; // PORTA ==> Analog
TRISA = 0xFF; // PORTA ==> input
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD cursor off
Lcd_Out(1,1,"SENSOR1 = "); // Show Text
ADCON0=0b11010001; // Select Analog1 RC_Mode and ADON
while(PORTA.F4)
{
ADCON0.GO=1;
while(ADCON0.GO);
x= (ADRESH*4)+(ADRESL/64);
WordToStr(x,data_);
Lcd_Out(1,10,data_);
Delay_ms(100);
}
Delay_ms(500);
ADCON0=0b11001101; // Select Analog1 RC_Mode and ADON
while(PORTA.F4)
{
TM3101 Mechanical Engineering Design
124
Faculty of Engineering
Department of Mechanical Engineering
ADCON0.GO=1;
while(ADCON0.GO);
x= (ADRESH*4)+(ADRESL/64);
WordToStr(x,data_);
Lcd_Out(1,10,data_);
Delay_ms(100);
}
Delay_ms(500);
ADCON0=0b11001001; // Select Analog1 RC_Mode and ADON
while(PORTA.F4)
{
ADCON0.GO=1;
while(ADCON0.GO);
x= (ADRESH*4)+(ADRESL/64);
WordToStr(x,data_);
Lcd_Out(1,10,data_);
Delay_ms(100);
}
Delay_ms(500);
ADCON0=0b11000001; // Select Analog1 RC_Mode and ADON
while(PORTA.F4)
{
ADCON0.GO=1;
while(ADCON0.GO);
x= (ADRESH*4)+(ADRESL/64);
WordToStr(x,data_);
Lcd_Out(1,10,data_);
Delay_ms(100);
}
Delay_ms(500);
}
*/
char motor_duty_= 127;
// Defalt PWM 50%
char motor_init_=0;
// Status initial
//
//
//
//
*** Motor A
PD0 ====>
PD1 ====>
PC2 ====>
*****
1A
1B
1E (PWM1)
//
//
//
//
*** Motor B
PB1 ====>
PB2 ====>
PC1 ====>
*****
2A
2B
2E (PWM2)
//****************************************************
//********** Initial Motor Function ******************
//****************************************************
void Motor_Init()
{
if (motor_init_==0)
// First time ?
{
motor_init_=1;
// Status
ANSELH.F0=0;
// RB1 ==> Digital IO
ANSELH.F2=0;
// RB2 ==> Digital IO
TRISB.F1=0;
// Motor B 2A
TRISB.F2=0;
// Motor B 2B
TRISD.F0=0;
// Motor A 1A
TRISD.F1=0;
// MOtor A 1B
Pwm1_Init(5000);
// Initail PWM 1E
Pwm2_Init(5000);
// Initail PWM 2E
TM3101 Mechanical Engineering Design
125
Faculty of Engineering
Department of Mechanical Engineering
}
}
//****************************************************
//****************************************************
//********** Control Duty Cycle *********************
//****************************************************
void Change_Duty(char speed)
{
if (speed != motor_duty_)
// Check Same old speed
{
motor_duty_=speed;
// Save for old speed
PWM1_Set_Duty(speed);
// Motor A
PWM2_Set_Duty(speed);
// Motor B
}
}
//****************************************************
/********** Motor A Forward ********/
void Motor_A_FWD()
{
Pwm1_Start();
PORTD.F0 =0;
PORTD.F1 =1;
}
/************************************/
/********** Motor B Forward ********/
void Motor_B_FWD()
{
Pwm2_Start();
PORTB.F1 =0;
PORTB.F2 =1;
}
/************************************/
/********** Motor A Backward *******
void Motor_A_BWD()
{
Pwm1_Start();
PORTD.F0 =1;
PORTD.F1 =0;
}
************************************/
/********** Motor B Backward *******
void Motor_B_BWD()
{
Pwm2_Start();
PORTB.F1 =1;
PORTB.F2 =0;
}
************************************/
/********** Go Forward
************/
void Forward(char speed)
{
Motor_Init();
Change_Duty(speed);
Motor_A_FWD();
Motor_B_FWD();
TM3101 Mechanical Engineering Design
126
Faculty of Engineering
Department of Mechanical Engineering
}
/************************************/
/********** Go Backward ************
void Backward(char speed)
{
Motor_Init();
Change_Duty(speed);
Motor_A_BWD();
Motor_B_BWD();
}
************************************/
/********** Spin Left
*************
void S_Right(char speed)
{
Motor_Init();
Change_Duty(speed);
Motor_A_FWD();
Motor_B_BWD();
}
************************************/
/********** Spin Right
************
void S_Left(char speed)
{
Motor_Init();
Change_Duty(speed);
Motor_A_BWD();
Motor_B_FWD();
}
************************************/
/*
void T_LEFT()
{
PWM1_Set_Duty(0);
// Motor A
PWM2_Set_Duty(255);
// Motor B
}
void T_RIGHT()
{
PWM1_Set_Duty(255);
// Motor A
PWM2_Set_Duty(0);
// Motor B
}
*/
/********** Motor A Off ************/
void Motor_A_Off()
{
Pwm1_Stop();
PORTD.F0 =0;
PORTD.F1 =0;
}
/************************************/
/********** Motor B Off ************/
void Motor_B_Off()
{
Pwm2_Stop();
PORTB.F1 =0;
PORTB.F2 =0;
}
/************************************/
TM3101 Mechanical Engineering Design
127
Faculty of Engineering
Department of Mechanical Engineering
128