-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomtest.ini
119 lines (102 loc) · 2.71 KB
/
randomtest.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// randomTest(); simulateCarMotor();
SIGNAL void randomTest()
{
int pos;
int i, j;
int lastPulsePinValue;
int reached1, reached2, reached3;
int requested1, requested2, requested3; // 1 if a call has been made to this floor
int at_floor;
// Random variables
int randButton, initFeed, random, count, delay;
reached1 = 0;
reached2 = 0;
reached3 = 0;
requested1 = 0;
requested2 = 0;
requested3 = 0;
pos = 0;
count = 0;
at_floor = 0;
// Initially feed the random variable
initFeed = rand(0xAB21);
printf("initFeed: %d\n", initFeed);
random = rand(initFeed);
printf("randomTest() is running\n");
while(1) // run forever
{
// Get a random value at every iteration depending on counter
random = rand(count + random); // counter's number is arbitary (because we're starting with random delay)
delay = random % 5; // 0 to 5 sec (maximum) delay
// call from random floor
randButton = rand(count + random) % 3;
PORTC |= 1 << randButton;
// register that a call to this floor has been made
if(randButton == 0)
requested1 = 1;
else if(randButton == 1)
requested2 = 1;
else
requested3 = 1;
// close the doors
PORTC |= 1 << 8;
swatch(0.0025);
// reset the pin (button)
PORTC ^= 1 << randButton;
// delay for the generated amount of time
while(j < delay)
j++;
j = 0;
at_floor = 0;
// every second, check whether we are at a floor
if ((PORTC & (1 << 7)) && !TIM3_CCR1 && !TIM3_CCR2)
{
if (pos >= -1 && pos <= 1 && !reached1) {
// reached floor 1
printf("arrived at floor 1\n");
reached1 = 1;
if(!requested1)
printf("Unnecessary stop!\n");
requested1 = 0; // served
at_floor = 1;
}
if (pos >= 399 && pos <= 401 && !reached2) {
// reached floor 2
printf("arrived at floor 2\n");
reached2 = 1;
if(!requested2)
printf("Unnecessary stop!\n");
requested2 = 0; // served
at_floor = 1;
}
if (pos >= 799 && pos <= 801 && !reached3) {
// reached floor 3
printf("arrived at floor 3\n");
reached3 = 1;
if(!requested3)
printf("Unnecessary stop!\n");
requested3 = 0; // served
at_floor = 1;
}
}
if (TIM3_CCR1 || TIM3_CCR2) // if the elevator is moving, nothing has been reached
{
reached1 = 0;
reached2 = 0;
reached3 = 0;
}
// if we have stopped at a floor, we open the doors for 1s
if (at_floor)
PORTC ^= 1 << 8;
// wait 1s
for (i = 0; i < 400; ++i) {
if (lastPulsePinValue < (PORTC & (1 << 9)))
pos += TIM3_CCR1 ? 1 : -1;
lastPulsePinValue = PORTC & (1 << 9);
swatch(0.0025);
}
// close the doors
PORTC |= 1 << 8;
count++; // increment the counter. Used as rand() feed. Overflow of this counter is desired (more random values)
}
}