-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanner.c
151 lines (127 loc) · 3.17 KB
/
planner.c
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Program skeleton for the course "Programming embedded systems"
*
* Lab 1: the elevator control system
*/
/**
* The planner module, which is responsible for consuming
* pin/key events, and for deciding where the elevator
* should go next
*/
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>
#include "global.h"
#include "planner.h"
#include "assert.h"
#include "position_tracker.h"
#define MOTOR_UPWARD (TIM3->CCR1)
#define MOTOR_DOWNWARD (TIM3->CCR2)
#define MOTOR_STOPPED (!MOTOR_UPWARD && !MOTOR_DOWNWARD)
#define DOORS_CLOSED GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8)
#define debounced 1252
#define STOP_PRESSED GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3)
static void plannerTask(void *params) {
int f1=0,f2=0,f3=0;
int dist, started=0,bypass=0;
PinEvent value;
portTickType xLastWakeTime;
int dir, spf=0;
// ...
for(;;){
xLastWakeTime = xTaskGetTickCount();
if(MOTOR_UPWARD)
dir=1;
else if (MOTOR_DOWNWARD)
dir=0;
dist=getCarPosition();
xQueueReceive( pinEventQueue, &( value ), 50 / portTICK_RATE_MS);
if(STOP_PRESSED)
{
printf("Stopped \n");
setCarMotorStopped(1);
spf=1;
}
else if(STOP_RELEASED && spf==1)
{
printf("Released \n");
spf=0;
setCarMotorStopped(0);
}
if (value==TO_FLOOR_1)
{
printf("Floor 1\n");
f1=1;
}
else if (value==TO_FLOOR_2)
{
printf("Floor 2\n");
f2=1;
}
else if (value==TO_FLOOR_3)
{
printf("Floor 3\n");
f3=1;
}
if(MOTOR_STOPPED){
if(dist >= -1 && dist <= 1 && DOORS_CLOSED && started==1) {
// reached floor 1
printf("arrived at floor 1\n");
f1=0;
bypass=0;
vTaskDelay(1000/portTICK_RATE_MS);
}
if (dist >= 399 && dist <= 401 && DOORS_CLOSED) {
// reached floor 2
printf("arrived at floor 2\n");
f2=0;
started=1;
bypass=0;
vTaskDelay(1000/portTICK_RATE_MS);
}
if (dist >= 799 && dist <= 801 && DOORS_CLOSED) {
// reached floor 3
printf("arrived at floor 3\n");
f3=0;
started=1;
bypass=0;
vTaskDelay(1000/portTICK_RATE_MS);
}
}
/*To ensure motor continues in its direction rather than changing directions after reaching a floor*/
if(dir==1){
if (f2==1 && MOTOR_STOPPED && DOORS_CLOSED && dist<400){
setCarTargetPosition(400);
bypass=1;
}
else if (f3==1 && MOTOR_STOPPED && DOORS_CLOSED){
setCarTargetPosition(800);
bypass=1;
}
}
else if(dir==0){
if(f2==1 && MOTOR_STOPPED && DOORS_CLOSED && dist>400){
setCarTargetPosition(400);
bypass=1;
}
else if (f1==1 && MOTOR_STOPPED && DOORS_CLOSED){
setCarTargetPosition(0);
bypass=1;
}
}
/*To go to a particuar floor to which the button was pressed*/
if (f1==1 && MOTOR_STOPPED && DOORS_CLOSED && bypass==0){
setCarTargetPosition(0);
}
else if (f2==1 && MOTOR_STOPPED && DOORS_CLOSED && bypass==0){
setCarTargetPosition(400);
}
else if (f3==1 && MOTOR_STOPPED && DOORS_CLOSED && bypass==0){
setCarTargetPosition(800);
}
vTaskDelayUntil(&xLastWakeTime, 10 / portTICK_RATE_MS);
}
}
void setupPlanner(unsigned portBASE_TYPE uxPriority) {
xTaskCreate(plannerTask, "planner", 100, NULL, uxPriority, NULL);
}