Welkom op deze website

Hier vind je alle informatie over het mechatronica project van Kristof Hubin en Evert Vanderhaegen.

Overview & Source Code

In the following, the program code used to control the elevator will be explained. Flowcharts are often used to graphically present a part of the code and make it more clear. The program can in fact be divided in three large blocks : the IDLE, MOVE and STOP blocks. These will be separately treated, as well as the algorithm used to control the elevator stops and it's speed.

The program starts with a reset procedure which has the following tasks:

·        set up the analog to digital converter built in the microcontroller

·        set up the PWM mode built in the microcontroller

·        initialise values and registers and defining constants


The three main blocks will now be discussed, but first an overview is given of some important variables.

Source Code

Back up

Defining Variables

In the course of the following, some variables will be used to explain the procedures and program code. Some of the most important ones, are explained here in order to easily comprehend the code.  
  •  ·        a FLAG – register was defined. This aided us in determining which floors were requested and which have been serviced already. The first seven bits of the register simply correspond with the seven different buttons in our installation. Inside the elevator: 1 , 2 and 3. Outside the elevator: 1UP, 2DOWN, 2UP and 3DOWN. Bit 6 for example corresponds to the button 2UP. Every time a button is pressed, the proper flag is set if it has not been set already. Each time a floor is serviced, the proper flags are cleared. Depending on what flags are cleared and set, the DetermineRequestedLevel procedure indicates the next stop.
  •  ·        The DIRECTION register is just used for it's first bit, to indicate whether the lift is moving upwards (bit set) or downwards (bit cleared).
  •  ·        The TRESHOLD register contains a binary value used to determine if the elevator is close enough to the requested level, to eliminate effects of noise of the potentiometer and incorporate the effect of inertia of the cage and motor.
  •  ·        DISTANCE register is used to store the difference in height between the current position of the motor and the requested position. It is used to tell the motor to accelerate, brake or maintain it's maximum speed.
  •  ·        the four different DUTYCONSTANT – registers contain a experimentally determined value which determine how fast the lift accelartes and brakes in upward and in downward motion.
  • the three first bits of the POSITION register are used to store the area in which the elevator is situated, as determined in the DetermineArea routine. 001, 010 and 100 correspond to area 1,2 and 3 respectively.
  • the RX – register is used to store the serial data that is sent by the shift register to the microcontroller.
The other variables are not hard to understand and there meaning or use is illustrated by the code.

Back up

Idle Block

During the idle procedure, the lift is stationary and is waiting for new requests. The "Flag"-register is regularly updated using the CheckButtons routine in order to detect a new request. Once a flag has been set, the idle procedure is left and the elevator needs to start moving. A flowchart of the idle block is shown below.
During the idle loop, and during the other two large blocks as well, the CheckButtons procedure is frequently invoked. This procedure allows to read the serial data sent by the shift register, determine if there are new requests, and transfer this information to the "Flag"-register. The program is graphically pictured below. The serial data is read, and by rotating the "RX" register and sending clock pulses, transferred into a 7 bit binary value, indicating which flags are set and cleared. This is shown is the flowchart below.

Back up

Move Block

The second large block in our program concerns the moving of the elevator. Here the direction and speed of the lift are decided on. It's main tasks are: 
  •  ·        check for new requests
  •  ·        determine the requested level
  •  ·        control the direction of motion
  •  ·        control the speed of the elevator
  •  ·        decide when to stop the lift
 First of all, the state of the buttons are checked, in order to continuously update the "Flag"-register. Next, the analogue to digital conversion is started. Once finished, the value of the current height of the lift is stored. Most importantly, the requested level now needs to be determined. An algorithm was used based on the direction the lift is moving and on it's current position. A more complete explanation is given in the chapter on the "Elevator Algorithm". The requested level needs then to be displayed. This is plainly done by controlling three LED's.
Now that the elevator knows where to go, the direction and speed of the elevator need to be set. First, the current level is compared with the requested level to see if the elevator has reached the floor. To avoid the influence of noise on the potentiometer reading and the effect of the inertia of lift and motor, a certain region around each floor is defined where the lift is "close enough". The "Threshold"-register is used for this purpose. If the lift has indeed approached the floor close enough, the stopping-procedure is invoked. Otherwise the lift will either move down or move up, depending on whether the requested level is higher than the current level or not.
The MoveUp and MoveDown procedures have important tasks. The motor direction is controlled by sending the correct combination of digital signals to the motor driver chip L293. More information can be found in the chapter on the driver chip in "Electronics". Either we send a 1 and 0 or a 0 and 1 to the L293, controlling the sense of the motor and lift. Next the AdjustAscendingSpeed or AdjustDescendingSpeed routines are invoked, in order to control the speed of the lift depending on how far it is situated from the destination floor. This part of the code is discussed in more detail in the chapter "Variable Speed". Finally, the "Direction"-bit is set or cleared, which is used in the elevator algorithm to determine the sense of motion.
 After establishing the right speed and sense, the program starts it's procedure to control the movement all over.
 A summarized flowchart is represented below.

Back up

Stop Block

The third major programming block is the STOP program. It is invoked when the elevator has reached it's destination floor. It's main tasks are: 
  •  ·        halt the elevator
  •  ·        clear the appropriate flags
  •  ·   wait a certain time at the floor before moving on if necessary
 Immediately,  the duty cycle of the PWM signal sent to the motor is set zero, to stop the elevator. Next, the ClearFlags routine is called, which clears certain flags depending on what floor is being serviced. A TimeOut procedure is now invoked. This ensures that the lift waits at the floor for a few seconds, to enable people to safely get in and out of the car. In the meantime, however, CheckButtons is regularly called, in order to prevent missing a new request while waiting at the floor. After this time has passed, CheckFloor1, CheckFloor2 or CheckFloor3 is called, depending on the floor. These routines check if there are no more flags set for the current floor, for example somebody who keeps pressing the 1UP button while the lift is on floor 1. If indeed this is the case, the stopping procedure is started over and another timeout is invoked. When no longer the elevator is halted on the current floor, the stopping procedure is terminated and the program returns to idle. Here it will continue to move again if there are still floors to be serviced. A flowchart representing briefly this program block is presented here.

Back up

Elevator Algorithm

To determine the requested level, an algorithm was developed to decide where the lift needs to stop. This elevator algorithm is commonly used in present lift installations and is based on two basic principles:

         ·        Continue to travel in the current direction while there are still remaining requests in that same direction
        ·        If there are no further requests in that direction, then stop and become idle, or change direction if there are requests in the opposite direction
As an example imagine someone entering the elevator at the first of three floors, and pressing on the button for the first floor. The elevator departs, heading for the third floor. Now presume that somebody on the second floor requests to travel up. Then the lift will halt at the second floor first, before continuing to the third. On the other hand, if the customer at the second floor requests to go down, the elevator will not stop but continue its upward sweep. After servicing the third floor, it travels to the second again. 
The know which floor has priority, all the elevator needs to know is it's current position and its direction of motion. Knowing these two variables, unambiguously determines in which order demands need to be handled.
In the subroutine DetermineArea , the current position is compared with the levels of the different floors, in order to determine where the lift is. Three areas are defined : the second floor is considered as an area on its own, the area between the second and the third, and between the second and the first.

The direction is simply determined by the motor voltage sign. In the MoveDown and MoveUp procedures, the "Direction" - bit is respectively cleared or set.
Knowing the direction and position of the elevator, the DetermineRequestedLevel routine is invoked. Six possible combinations are possible (three floors, two directions) and for each possibility the different floors were listed in decreasing priority. The routine then checks if there are floors requested, looking at the floors in this determined order. As soon as it encounters a request, the routine is leaved and the requested level is stored. As said before, whether or not a floor is requested is storedin the "Flag" -register.
The following flowchart makes this clear. The flowchart has been divided into three parts, depending on the area, to keep it neat.

Some examples to clarify. Suppose the elevator is one floor1 and somebody requests on floor2 presses the 2UP button. Yet when he steps into the lift, he changes his mind and decides to go to the base floor. How will our algorithm handle this situation ? After pressing 2UP, our elevator will respond and travel to floor 2. On arrival, flags 2 and 2UP are cleared. Being in an upward motion, if in the meantime a customer on floor 3 demands the lift, the lift will first go upwards before returning to floor 1 as the first customer asked. 
Note that in some branches, the direction bit is adjusted, indicating that the sense in which the algorithm is looking for requests is reversed. This proved to be helpful later on in the program in special situations. 
For instance, consider the case in the previous situation, but now the customer pushes the 2DOWN button. The lift will mount, but on arrival flag 2 DOWN will not be cleared, as programmed in the ClearFlags register. Only 2 and 2UP might be cleared, but they weren’t set. The stop block is left and the programs returns to the idle state. Yet, it sees that there is still a flag set. Thus the requested level is still determined, this time branch 3 is started and now the program will see that flag 2 DOWN is still set and it will change the direction bit. Stopping procedure is left, idle invoked, yet a flag is still set. Now branch 4 of the elevator algorithm is read, and now the lift sees that flag 1 is set and will travel downwards.
As you might notice, at the end of each branch of the algorithm, there is one more line containing the coordinates of the floor itself. This is only used as a certain safety that the program will always leave the procedure. If the flags are properly set and the area determined right, the last line will never be read.
This algorithm enables the elevator to determine where to stop first and essentially provides the lift with some brains. It proved to be robust and handle all requests and special situations properly.

Back up

Variable Speed

In order to limit the g-forces to which passengers are exposed during the start and arrival of the elevator at a floor, a procedure was included to be able to alter the speed of the elevator. The idea was the following: accelerate the elevator initially at a predetermined rate, then continue to travel at maximum speed once reached, and slow down once the floor is almost reached.
The speed of the elevator is mainly dependant on the voltage at the motor's stator terminals, the weight of the elevator the direction of motion. The second factor is not controlled by our installation, the first is an easy modified parameter. Thus altering the duty cycle allows us to accelerate and decelerate the elevator. Four constants were defined in order to adjust the duty cycle. Two for each direction, in order to take into account the fact that a same duty cycle causes the elevator to descend faster than to ascend. Two per direction, in order to allow the freedom to control the acceleration separately from the deceleration. Consequently, the constants DutyConstant1U and DutyConstant2U (for acceleration and deceleration) in the upward sweep, and DutyConstant1D and DutyConstant2D, were defined and experimentally adjusted.
Both AdjustAscendingSpeed and AdjustDescendingSpeed can be simplified and represent as follows in the flowchart. The increase and decrease duty cycle blocks represent the changing of the duty cycle value by one of the four constants described above.

 As shown, to control the ascending speed, the duty cycle is augmented initially until the maximum is reached. As soon as the elevator approaches the destination close enough, prescribed by the value of register "DistanceMin", the duty cycle is gradually reduced. A minimum is also set, in order to prevent that the elevator no longer moves because the voltage at the stator terminals is too low.
This procedure allows the speed of the elevator to be variable and to safely depart from and arrive at a floor.

Back up

Emergency Procedure

In order to handle unexpected situations and prevent damage to the installation, two sensors were placed at the bottom and top of the lift. If, for any odd reason, the lift doesn't stop on the bottom or top floor and hits the ceiling or bottom, the sensors detect this and send this information to the controller. Here, it is handled as an interrupt: the normal course of the program is immediately interrupted and a special procedure is invoked.
 Initially, the duty cycle is set to zero in order to halt the lift. Then, it is detected if the ceiling or bottom of the lift shaft has been reached. Depending on this, the lift will be asked to start moving up or down. The flags are all cleared but one, namely the flag for floor 1 or 3 , again depending on where the interrupt came from. Afterwards, the normal program is once again resumed. All the other flags have been cleared now and the elevator will travel to the closest floor (1 or 3) and will wait there, ready for normal use once again.
 A flowchart is presented.

Back up