Professional Documents
Culture Documents
Final Design Report 2017.04.18
Final Design Report 2017.04.18
Final Design Report 2017.04.18
Faculty of Engineering
MECH485
Mechatronics
i|Page
9. Experience and Recommendations ........................................................................... 18
10. References .............................................................................................................. 19
Appendix A – List of Global Variables ......................................................................... i
Appendix B – System Level Diagram ........................................................................... i
Appendix C – Circuit Diagrams .................................................................................... i
List of Figures
Figure 1: Transportation Mechanism .................................................................................. 1
Figure 2: Gantt Chart .......................................................................................................... 2
Figure 3: Forward and Backward Step Functions............................................................... 4
Figure 4: Optical Reflective Control Flow ......................................................................... 5
Figure 5: ADC Interrupt Algorithm .................................................................................... 5
Figure 6: Part Classification................................................................................................ 6
Figure 7: Exit Sensor Interrupt ........................................................................................... 6
Figure 8: Setup Hardware Function .................................................................................... 7
Figure 9: PWM Setup Function .......................................................................................... 8
Figure 10: ADC Setup Function External Interrupts .......................................................... 8
Figure 11: Home Stepper Motor Function .......................................................................... 9
Figure 12: Linked List Data Structure ................................................................................ 9
Figure 13: Part Classification Algorithm .......................................................................... 10
Figure 14: Example From the DecisionMatrix() Function ............................................... 11
Figure 15: Pause Button Display While Loop .................................................................. 12
Figure 16: First if Statement in the Pause Button ISR ...................................................... 13
Figure 17: Second if Statement in the Pause Button ISR ................................................. 13
Figure 18: Calibration Spreadsheet Example ................................................................... 15
Figure 19. Angular Acceleration, Speed, and Position [1] ............................................... 16
Figure 20. Speed Profile vs. Stepper Motor Pulses/Speed................................................ 16
Figure 21. State Machine for Timer Interrupt ................................................................... 17
List of Tables
Table 1: Two-phase Full Step Commands .......................................................................... 3
Table 2: Data Direction Register Initialization ................................................................... 7
Table 3. Scaling of Standard SI Values ............................................................................ 17
Table 4: Progressive Stepper Adjustment ......................................................................... 18
Table 5: List of Global Variables......................................................................................... i
ii | P a g e
1. Introduction
The sorting machine inspects 4 different materials and places them into their
characteristic container. The parts are inspected with an array of sensors which are
positioned on a conveyer belt driven by a DC motor. Once a part is identified a stepper
motor turns a container to the respective position to catch and sort the part. The system is
shown below in Figure 1.
To classify each piece, the inspection system uses three optical beam sensors, one
inductive sensor, one reflective sensor, and a ferromagnetic sensor. The three optical
beam sensors are used to notify of a parts location which triggers a specific sorting event.
The inductive sensor can be used to differentiate a part between metal or plastic, the
reflective sensors reads an analog value which can be converted to a digital value to
classify all part materials (black, white, steel, or aluminum).
1|Page
Based on past group performances from sessions of this course the project team felt that
based on previous experience and existing course load the following objectives could be
achieved:
1. Zero Sorting Errors: We are aiming to complete the sorting task with no errors.
As a group, we are aiming for a quick sorting time however accuracy is of high
importance.
2. Sub 40 second sorting time: We were aiming to complete the sorting task in
under 40 seconds as a top score was potentially unattainable with our course load
and timeline. However, we are still going to strive for the fastest time that we can
attain.
1.3 Timeline
Creating a project timeline with a Gantt chart is essential to the completion of any
project. The Gantt chart below in Figure 2 was used to keep the team on task and meet
the final system implementation in preparation for a demonstration on April 10, 2017.
2. Firmware
2|Page
Atmel provides a delay timer within their standard library that implements a software
counter. However, in the final implementation of this system a 16-bit hardware timer was
implemented as it is more accurate and easily configured to fit specific needs.
To advance the stepper motors rotational position the 4 coils are pulsed individually one
at a time over and over. It is imperative that the stepper motor receive the correct pulses
to each coil to avoid double-stepping or miss-stepping. The stepper motor drive is
displayed below in Figure 3.
To ensure that no steps would be missed a variable was incremented for the number of
steps desired and the last step is always recorded as a global variable. The drive function
is show below in Figure 3.
3|Page
Figure 3: Forward and Backward Step Functions
1. Grab the vale from the global variable “lastStep” and increment or decrement it
by 1.
2. Check if the incremented/decremented step is 4. If it is, set counter variable to 0.
3. Send the respective Two-phase full step command from PORTA to the stepper
motor coils.
4. Assign the “lastStep” to the current step.
It should be noted that StepForward() and StepBackward() are only used to send a pulse
to the stepper coil and keep track of which step the motor is on. The command for the
position, speed, acceleration, and deceleration are controlled by SpeedControlMove()
with a timer interrupt.
2.3 DC Motor
To drive the parts through the sensors for classification to the sorting containers a DC
motor was used. The motor consisted of a rotor and a fixed magnetic field generated by
permanent magnets. To control the angular velocity of the motor PWM was
implemented. With a duty cycle of 50%, a top frequency of 0xFF and a bottom of 0x00,
8-bit speed adjustment was possible. Frequencies within these two bounds were explored
to get the fastest belt speed possible for the system without compromising sensor function
or the proper placement of parts.
4|Page
event fires it sets a 16-bit integer high to the ADC_result variable and starts an ADC
conversion as shown below in Figure 4.
Starting the ADC fires a designated interrupt within the microcontroller where the ADC
value can be obtained for part classification. Steel, aluminum, black, and white all return
different values therefore, to differentiate the parts, the ADC interrupt is fired until the
minimum value is identified.
In Figure 5 above, a high 16-bit global variable ADC_result is compared to the ADC
registers 10-bit value to find the minimum value. If ADC_result is greater than the value
within the register ADC_result is set to the ADC value. This process continues until the
minimum ADC value is found. It should also be noted that a new conversion is started if
the part triggers the optical beam sensor.
Once the lowest value of the ADC is obtained from the algorithm in Figure 5, it is then
assigned a “classificationValue” and added as a node to the list. The implementation is
shown below in Figure 6.
5|Page
Figure 6: Part Classification
The home position is set by rotating the stepper motor until a rising edge is caught and
the current position of the stepper motor is saved globally.
3. Circuit Diagrams
The circuit diagrams for the entire final design can be seen in Appendix C. The diagrams
were created in AUTODESK Eagle Version 8.0 and were regularly referenced to ensure
proper integration of components.
4. System Implementation
This section outlines the algorithms used in this project to meet the project specifications.
A full system implementation can be found in Appendix B.
6|Page
Figure 8: Setup Hardware Function
7|Page
Figure 9: PWM Setup Function
During the time that the ferromagnetic sensor input signal is being polled a global
variable is set to 1 to tell timer3’s interrupt to remain in setup mode. This condition calls
stepper forward and pulses the motor to turn clockwise. The implementation is show
below in Figure 11.
8|Page
Figure 11: Home Stepper Motor Function
9|Page
4.2 Sorting (Decision Matrix)
The chief objective of this project was to sort ingots composed of 4 materials into their
respective categories. To achieve this goal the ingots had to be classified based on the
sensor readings. A simple block of “if” statements classified the pieces based on the
readings of the reflective sensor.
The “classificationValue” seen above was a temporary variable used to transfer the piece
to the RelectiveValue variable stored in the linked list element.
Once a piece reached the exit sensor a decision needed to be made about the direction and
duration of movement implemented with the stepper motor. The first step was to dequeue
the list and pass the information to the DecisionMatrix() function. The DecisionMatrix()
function used a series of “if” statements that read which category this next piece belonged
to. Once the appropriate condition was met and the program flow entered an “if”
statement, a “switch” statement was used in conjunction with a global variable that stored
the current position of the stepper motor, to determine the necessary movement for the
stepper. Each case of the “switch” statements contained different instructions for the
stepper. An example of one of these “if” and “switch” statement blocks is shown below.
10 | P a g e
Figure 14: Example From the DecisionMatrix() Function
11 | P a g e
Figure 15: Pause Button Display While Loop
Once the button was pressed again the system was supposed to restart from where it had
left off. Looking at previous demos and what other groups were doing most of the
systems would need to complete their display loop before reacting to the second button
push and would thus the system restart would be delayed. It was decided that the system
pause button should take effect immediately to restart the system as soon as the button
was pressed for the second time; however, this posed several challenges. For starters,
how could we break out of an infinite while loop by pressing a button and still turn off
the display upon exiting the loop?
The solution came in the form of the careful manipulation of a flag and three of the
registers involved with running external interrupts. The pause button flag was initialized
as 0 but upon the first button push it would be assigned to 1 after the program flow had
entered an “if” statement checking its value. After the flag’s value was changed it would
enter the infinite while loop. Upon the second button push, when the interrupt service
routine was restarted, the program flow would skip the first “if” statement and enter a
second “if” statement executing the code to leave the interrupt service routine.
In order for an external interrupt to function 4 registers are used. The SREG registers
contain the global interrupt enable bit which must be set. The “External Interrupt Mask
Register” (EIMSK) enables specific interrupts. The “External Interrupt Control Register
A/B” (EICRA/B) sets the mode of the interrupt (falling edge or rising edge). Finally, the
“External Interrupt Flag Register” (EIFR) contains the bits that change in accordance
with the external stimulus and tell the CPU something has happened. Typically when an
external interrupt is fired the global interrupt enable bit is flipped disabling all of the
other external interrupts. Including the “ISR_NOBLOCK” key word in the interrupt
12 | P a g e
service routine resets the global interrupt enable bit as soon as possible in the interrupt
service routine, meaning that other interrupts can now fire. The “ISR_NOBLOCK” is a
dangerous tool to use and only appropriate for specific cases; it was felt that this was one
such situation and that practice with this tool (and its multiple pitfalls) would provide
excellent learning outcomes. Having the other external interrupts enabled during the
system pause interrupt was a double edged sword. Allowing external interrupts to fire
meant that the system pause could interrupt itself but meant that the sensors would still
fire. To prevent the other interrupts from firing but still enabling the pause button
interrupt to execute required manipulation of the EIMSK register as seen in the following
screen grab.
Upon the pause button being hit for the first time the program flow would enter this “if”
statement and disable the interrupts. Unfortunately, since the global interrupt enable bit
was reset the external interrupt flags would still be triggered. While this may not seem
like an issue at first it meant that as soon as the interrupts were re-enabled, they would
fire. To overcome this, the bits had to be set to 1 before re-enabling the interrupts (The
bits work backwards in the EIFR).
After the pause button flag was set and the interrupts re-enabled the StateMachine()
function was called. The StateMachine() function contained the infinite for loop that the
program sat in while waiting for something to happen. This structure was implemented so
that the ISR of the second button push would not return to the first one upon completion.
It’s suspected that this could lead to a memory overflow or other issues with the stack if
the system was left to run for long enough. However, for the purposes of the testing this
would not be an issue and there was insufficient time to consider manually manipulating
the stack with assembly to correct this potential problem.
13 | P a g e
4.4 System ramp down
The system ramp down function was similar to the system pause button. The major
differences between the two were that the system ramp down used a timer interrupt but
did not use the “ISR_NOBLOCK” command. When the system ramp down button was
pressed the timer interrupt would be activated. The timer interrupt was set up such that it
would only shut down the system after 4 seconds. The delay in action was crucial as it
gave the system time to sort two more pieces. Once the allotted time was up the system
shut all of the sensors and motors off; before using the same while loop as the system
pause, to output a display on the LEDs.
To avoid system failure during testing, the stepper motor had to be slowed down
significantly to handle to load on the sorting tray. Additionally, near the testing date it
was identified that the exit sensor was double firing due to hardware bouncing and was
mitigated with a capacitor.
Sorting 48 parts within 45 seconds with one error did fall short of the team’s initial goal.
However, the system has the potential to operate at a much higher speed as each
component of the system was implemented with advanced control techniques. The slower
time can be attributed directly to calibration issues when testing along with last minute
adjustments to the stepper delay that needed to be made in order to complete the demo.
This 10-bit number was then converted into a decimal and logged on an excel sheet
where the minimum, maximum, and average are calculated as shown below in Figure 18.
14 | P a g e
Figure 18: Calibration Spreadsheet Example
The calibration procedure was completed multiple times prior to the demo to achieve the
most stable ADC values. One major complication that was identified just prior to testing
is that the values of changed when the mini USB connection was removed from the
controller. According to the lab TA about 50% of the microcontrollers change the base
line values wen the USB cable is plugged in. This was detrimental to the test as parts
were classified incorrectly because the system was calibrated with the USB cable plugged
and then tested with it unplugged.
To resolve the issue the TA allowed the team to have the USB cable plugged in during
the testing so that the reference values matched the firmware flashed onto the controller.
15 | P a g e
8.1 Timer Interrupt Driven Stepper Motor
The stepper motor control is based off of Atmel’s linear speed control of a stepper motor
[1]. The document describes how to implement an exact linear speed controller for
angular acceleration and deceleration. The desired speed profile of the motors is shown
below in Figure 19.
The desired speed sloped is achieved be exponential decreasing or increasing the delay
between pulsing the stepper motor. This is displayed conceptually below in Figure 20.
The firmware is implemented with a timer that is pre-scaled based on the frequency of
the microcontroller clock speed. In this case, the AT90USB1287 runs at 1 MHz and a
pre-scaler of 64 results in a timer reset of 15625 Hz. The system requires:
16 | P a g e
1. The minimum timer length.
2. Step delay.
3. Maximum number of steps to achieve desired speed.
4. The number of steps before deceleration starts.
If the maximum speed is greater than the acceleration limit of the stepper motor then the
deceleration value is calculated and the stepper will decelerate to keep a smooth speed
profile. The acceleration/deceleration state diagram is shown below in Figure 21.
The default state is STOP and sits in this state if the stepper is not moving. As soon as the
step amount, acceleration, deceleration, and speed are given to the stepper control
function a 16 bit timer is turned on, the system constants are calculated, and the largest
value the timer can count to is reset. It should be noted that the acceleration, deceleration,
and speed are all scaled by a factor of 100 to increase the resolution and computing
speed. This relation is tabulated below in Table 3. Scaling of Standard SI Values.
17 | P a g e
count the number of part sorted and then the acceleration, deceleration, and speed are set
accordingly as shown below in Table 1.
With a total of 3 incremented step changes of velocity across the entire sorting the fasted
possible stepper movement can be achieved. However, these values are not the optimal
values for the stepper speed and could be tuned to get the fast stepper speed. The team
ran out of time and had to reduce the stepper motor parameter to ensure the demo could
be completed.
When concerned with future development of this system, it is recommended that more
time be spent optimizing the stepper motor acceleration and speeds for the progressive
stepper adjustment. This is suggested because the implementation for a very fast sorting
machine is there but the time for optimization was overlooked which was detrimental to
the system performance. The second recommendation to improve the sorting speed would
be to move the stepper motor to a parts respective container before it reached the exit
sensor so that no delay would exist when sorting parts.
Overall, the team was thoroughly impressed with the amount of knowledge and
experience that was obtained implementing this system and each members interest within
the field of mechatronics has definitely increased.
18 | P a g e
10. References
[1] Atmel Corporation, "AVR446: Linear speed control of stepper motor," pp. 1-
15, 2006.
19 | P a g e
Appendix A – List of Global Variables
Table 5: List of Global Variables
i|Page
Appendix B – System Level Diagram
System pause
No
Interrupt 2 fires Begins ADC conversion
Move stepper
A value is read from the
object and fed through a Piece leaves the
switch statement to second light sensor
determine piece Return to previous function
or interrupt
Yes
Stepper moves from
current position to List already Enqueue is called, new link
new created? with conversion is created
No
Current position is stored in Return to main
a global variable
A new list is created
Return to main
i|Page
Appendix C – Circuit Diagrams
Sheet Description
1 Shows the port configuration and pin out
for the AT90USB1287
i|Page
1|Page
2|Page